I am working with a simple application on android where at the moment i net to create a toolbar so i created a custom toolbar like this:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:layout_gravity="center"
android:id="#+id/toolbar_title"
/>
</android.support.v7.widget.Toolbar>
then i included this custom layout on my activity:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:fitsSystemWindows="true"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.afcosta.inesctec.pt.android.FamilyLibrary">
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
app:srcCompat="#android:drawable/ic_menu_camera"
app:backgroundTint="#f1c40f"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginEnd="37dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginBottom="33dp" />
<android.support.v7.widget.RecyclerView
android:id="#+id/gallery"
android:layout_width="0dp"
android:layout_height="562dp"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="97dp"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<include
layout="#layout/custom_toolbar"
android:id="#+id/toolbar"
tools:layout_constraintTop_creator="1"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="#+id/textView3"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Familias"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toTopOf="#+id/gallery"
android:layout_marginStart="8dp"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="15dp"
app:layout_constraintLeft_toLeftOf="parent"
android:textSize="24dp"/>
when i try to setText on my activity for the TextView, i fails and i get this error: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.support.v7.widget.Toolbar.findViewById(int)' on a null object reference
here is my code on the activity:
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);;
setSupportActionBar(myToolbar);
TextView toolbarText = (TextView) myToolbar.findViewById(R.id.toolbar_title);
toolbarText.setText("FamÃlias");
Any tip?
Thank you very much!
If I'm not mistaken, you don't need to call findViewById() on your toolbar, you should just be able to use this: findViewById(R.id.toolbar_title)
Your code should end up looking like this:
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
TextView toolbarText = (TextView) findViewById(R.id.toolbar_title);
toolbarText.setText("FamÃlias");
You are giving two different id to toolbar layout
android:id="#+id/my_toolbar"
and in main layout while including you are giving
<include
layout="#layout/custom_toolbar"
android:id="#+id/toolbar" />
make these ids same
Related
I'm designing a quotes app and i m having so many quotes in one page (activity) so i want a widget toolbar and 4 share buttons after every textview, i did this through xml, i want to do this by coding in java activity so the coding length will reduce, please give me the right solution for this and please look out these pics and my code
Image
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
tools:context="technoapps4.shayari_vayari.second">
<ScrollView
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:layout_editor_absoluteY="0dp"
tools:layout_editor_absoluteX="8dp">
<TextView
android:id="#+id/textView"
android:onClick="tv"
android:text="#string/Funny1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:textColor="#fff"
android:textIsSelectable="true"
android:textSize="20sp"/>
<android.support.v7.widget.Toolbar
android:id="#+id/tb"
android:layout_width="match_parent"
android:layout_marginTop="05dp"
android:layout_height="match_parent"
android:background="#fff">
</android.support.v7.widget.Toolbar>
<Button
android:id="#+id/copy1"
android:onClick="copy1"
android:layout_marginLeft="08dp"
android:layout_marginTop="-50dp"
android:layout_width="56dp"
android:layout_height="46dp"
android:background="#drawable/copy" />
<Button
android:id="#+id/whatsapp1"
android:onClick="whatsapp1"
android:layout_marginLeft="90dp"
android:layout_marginTop="-48dp"
android:layout_width="56dp"
android:layout_height="46dp"
android:background="#drawable/whatsapp" />
<Button
android:id="#+id/fb1"
android:onClick="fb1"
android:layout_marginLeft="180dp"
android:layout_marginTop="-46dp"
android:layout_width="56dp"
android:layout_height="46dp"
android:background="#drawable/facebook" />
<Button
android:id="#+id/share1"
android:onClick="share1"
android:layout_marginLeft="275dp"
android:layout_marginTop="-46dp"
android:layout_width="56dp"
android:layout_height="46dp"
android:background="#drawable/share" />
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="#string/Funny2"
android:textColor="#fff"
android:textIsSelectable="true"
android:textSize="20sp" />
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_marginTop="05dp"
android:layout_height="match_parent"
android:background="#fff">
</android.support.v7.widget.Toolbar>
<Button
android:id="#+id/copy2"
android:onClick="copy2"
android:layout_marginLeft="08dp"
android:layout_marginTop="-50dp"
android:layout_width="56dp"
android:layout_height="46dp"
android:background="#drawable/copy" />
<Button
android:id="#+id/what2"
android:onClick="whatsapp2"
android:layout_marginLeft="90dp"
android:layout_marginTop="-48dp"
android:layout_width="56dp"
android:layout_height="46dp"
android:background="#drawable/whatsapp" />
<Button
android:id="#+id/fb2"
android:layout_marginLeft="180dp"
android:onClick="fb2"
android:layout_marginTop="-46dp"
android:layout_width="56dp"
android:layout_height="46dp"
android:background="#drawable/facebook" />
<Button
android:id="#+id/share2"
android:onClick="share2"
android:layout_marginLeft="275dp"
android:layout_marginTop="-46dp"
android:layout_width="56dp"
android:layout_height="46dp"
android:background="#drawable/share" />
</LinearLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
If you will have many Quotes to display, I suggest you use RecyclerView along with RecyclerView.Adapter. LinearLayout might slow your app dow.
But if your quotes are not so much, or you are really want to use LinearLayout. You can build a listitem_quote.xml contains your quote and 4 Buttons first.
And then use an empty LinearLayout in your activity.xml like this.
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/myLinearLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</ScrollView>
In your activity, inflate() for every quotes. And add them to your layout.
onCreate(...){
setContentView(R.layout.activity_main);
myLinearLayout = (LinearLayout) findViewById(R.id.myLinearLayout);
LayoutInflater layoutInflater = LayoutInflater.from(this);
for(...){ // for every quote
// create a view for quote contains buttons
View quoteView = layoutInflater.inflate(R.layout.listitem_quote, null);
TextView tvQuote = quoteView.findViewById(...);
tvQuote.setText(...);
Button btn1 = quoteView.findViewById(...);
btn1.setOnclickListener(...);
myLinearLayout.addView(quote);// add to Linearlayout
}
}
This will do the trick.
However I still suggest you to use RecyclerView instead.
You can find many examples on the internet.
It looks more complex but it's useful.
In my ProfileFragment.java I have a collapsing toolbar.
There is a problem when I scroll the toolbar; my profile image doesn't scroll entirely.
Does the collapsing toolbar need to be inside a Coordinator Layout?
I've tested with relative and I couldn't scroll.
This is a gif showing its behavior:
.
This is my XML code:
<android.support.design.widget.AppBarLayout
android:id="#+id/testeparabackground"
android:layout_width="match_parent"
android:layout_height="203dp"
android:background="?attr/actionBarDivider">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/backgroundcollapsedtoolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="exitUntilCollapsed|scroll">
<ImageView
android:id="#+id/imgFotoCapa"
android:layout_width="446dp"
android:layout_height="203dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:paddingLeft="-32dp"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="#drawable/banner_profile"
tools:layout_editor_absoluteY="1dp" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbarid"
android:layout_width="match_parent"
android:layout_height="110dp"
app:layout_collapseMode="pin"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<ImageView
android:id="#+id/picFotoPerfil3"
android:layout_width="117dp"
android:layout_height="103dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="109dp"
android:elevation="50dp"
app:layout_anchor="#+id/testeparabackground"
app:layout_anchorGravity="bottom|center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#android:color/black" />
</android.support.design.widget.CoordinatorLayout>
Add app:layout_behavior="#string/appbar_scrolling_view_behavior" inside your ImageView
Trying to set the ActionBar title in middle instead of the default left aligned position. To do so, based on other answers this is what I've done:
Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbarDownloads);
setSupportActionBar(myToolbar);
if(getSupportActionBar()!=null)
{
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.actionbar_layout);
}
actionbar_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Downloads"
android:layout_centerInParent="true"
android:textColor="#000000"
android:id="#+id/mytext"
android:textSize="18sp" />
</RelativeLayout>
However, this does not produce the intended result (ie this renders the text however still is left aligned), what's wrong with this approach and how to fix this?
try this use custom Toolbar
<android.support.v7.widget.Toolbar
android:id="#+id/ar_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="exitUntilCollapsed"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Center"
android:orientation="vertical">
<TextView
android:id="#+id/toolbar_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
now in java file
private Toolbar toolbar;
toolbar = (Toolbar) findViewById(R.id.ar_toolbar);
TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
mTitle.setText("Nilesh Rathod");
setSupportActionBar(toolbar);
Simple approach use android:layout_gravity="center" on your TextView and hide default title using below code after that initialize your TextView and set value
Inside your activity:
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
toolbar_title = (TextView) findViewById(R.id.toolbar_title);
toolbar_title.setText("My Custom center title");
Custom toolbar XML Code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:contentInsetEnd="0dp"
app:contentInsetLeft="0dp"
app:contentInsetRight="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp">
<TextView
android:id="#+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Toolbar Title" />
</android.support.v7.widget.Toolbar>
You need to use Toolbar in your xml file and you can use TextView inside Toolbar. You can add custom view into Toolbar. Once you done with xml file then you can define Toolbar into Activity class.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="58dp"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:titleTextColor="#ffffff">
<TextView
android:id="#+id/mytext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Downloads"
android:textColor="#000000"
android:textSize="18sp" />
</android.support.v7.widget.Toolbar>
//Rest of your code
</LinearLayout>
In Activity class you can define as
Toolbar toolbar;
toolbar = (Toolbar) findViewById(R.id.ar_toolbar);
toolbar.setTitle("");
You don't need a Toolbar for this. Just use a theme like Theme.AppCompat.Light.DarkActionBar with ActionBar in your activity and use the following code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.actionbar_layout);
}
//your code
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:minHeight="?attr/actionBarSize"
app:contentInsetStart="0dp">
<TextView
android:id="#+id/back_txt"
style="#android:style/TextAppearance.Medium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center"
android:text="Back" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Downloads"
android:layout_gravity="center|center_horizontal|center_vertical"
android:layout_marginTop="20dp"
android:layout_centerInParent="true"
android:textColor="#000000"
android:id="#+id/mytext"
android:textSize="18sp" />
<TextView
android:id="#+id/filter_btn"
style="#style/Base.TextAppearance.AppCompat.Medium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical|center_horizontal"
android:layout_margin="10dp"
android:drawablePadding="5dp"
android:gravity="center"
android:text="Back"
android:textColor="#android:color/black" />
</android.support.v7.widget.Toolbar>
</LinearLayout>
I have a strange issue that i cannot solve, i have a little spacing between my settings bar and the toolbar, watch here
my xml for the toolbar:
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
on activity i call it like this:
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
All Acitivity XML (put all my code in external file and include it)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:fitsSystemWindows="true"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.afcosta.inesctec.pt.android.FamilyLibrary"
android:paddingTop="6dp">
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
app:srcCompat="#android:drawable/ic_menu_camera"
app:backgroundTint="#f1c40f"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginEnd="37dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginBottom="33dp" />
<android.support.v7.widget.RecyclerView
android:id="#+id/gallery"
android:layout_width="0dp"
android:layout_height="562dp"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="97dp"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<include
layout="#layout/custom_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/toolbar"/>
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Familias"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toTopOf="#+id/gallery"
android:layout_marginStart="8dp"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="15dp"
app:layout_constraintLeft_toLeftOf="parent"
android:textSize="24dp"/>
</android.support.constraint.ConstraintLayout>
it appears, but that strange white space is annoying :/
You have to remove padding from ConstraintLayout:
android:paddingTop="6dp"
I am using a custom toolbar layout with a TextView in ToolBar
But when I run it
It hint that
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fish.sevenseconds/com.goldfish.sevenseconds.activities.Addmem}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.Toolbar.setTitle(java.lang.CharSequence)' on a null object reference
this is my Activity code
toolbar=(Toolbar) findViewById(R.id.square_toolbar);
toolbar.setTitle("");
This is my xml
<android.support.v7.widget.Toolbar
android:id="#+id/admem_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/khaki"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TextView
android:id="#+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#color/silver"
android:textSize="20sp" />
</android.support.v7.widget.Toolbar>
Cheeck the below code change the id to match the xml for findviewbyid for toolbar
toolbar=(Toolbar) findViewById(R.id.admem_toolbar);
toolbar.setTitle("");
XML
<android.support.v7.widget.Toolbar
android:id="#+id/admem_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/khaki"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TextView
android:id="#+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#color/silver"
android:textSize="20sp" />
</android.support.v7.widget.Toolbar>