How To Put The Title Of The Activity On The Right Of The Toolbar
Solution 1:
You should have a toolbar in your xml layout, so your xml file should look like this :
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns: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:layout_gravity="right"android:orientation="vertical"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"app:layout_behavior="@string/appbar_scrolling_view_behavior"tools:context="com.example.hsports.practicesession.HomePage"tools:showIn="@layout/activity_home_page"><android.support.design.widget.AppBarLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:theme="@style/AppTheme.AppBarOverlay"><android.support.v7.widget.Toolbarandroid:id="@+id/toolbar"android:layout_width="match_parent"android:layout_height="?attr/actionBarSize"android:background="?attr/colorPrimary"app:popupTheme="@style/AppTheme.PopupOverlay" /></android.support.design.widget.AppBarLayout><TextViewandroid:id="@+id/displayUserEmail"android:layout_width="match_parent"android:layout_height="wrap_content" /><TextViewandroid:id="@+id/displayUserPassword"android:layout_width="match_parent"android:layout_height="wrap_content" /></LinearLayout>
and then you need to set the supportActionBar
in the onCreate
method like this :
Toolbartoolbar= (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
after this you can set the title :
getSupportActionBar().setTitle("Welcome"+" "+getUserEmail);
but in your case you're trying to set the title before you've set the supportActionBar
, so you're getting NullPointerException
.
Also, R.id.toolbar
is not in your current xml layout, so again it'll throw a NullPointerException
, so change your xml file to add a Toolbar
as suggested above.
UPDATE
To have the text on right :
You need to add your own TextView
inside the Toolbar
and then there's no need to set toolbar title like this :
getSupportActionBar().setTitle("Welcome"+" "+getUserEmail);
Change your activity_home_page.xml
code to this :
<?xml version="1.0" encoding="utf-8"?><android.support.design.widget.CoordinatorLayoutxmlns: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:fitsSystemWindows="true"tools:context="com.example.hsports.practicesession.HomePage"><android.support.design.widget.AppBarLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:theme="@style/AppTheme.AppBarOverlay"><android.support.v7.widget.Toolbarandroid:id="@+id/toolbar"android:layout_width="match_parent"android:layout_height="?attr/actionBarSize"android:background="?attr/colorPrimary"app:popupTheme="@style/AppTheme.PopupOverlay" ><RelativeLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"><TextViewandroid:id="@+id/welcomeText"android:layout_marginRight="30dp"android:layout_alignParentRight="true"android:layout_width="wrap_content"android:layout_height="wrap_content" /></RelativeLayout></android.support.v7.widget.Toolbar></android.support.design.widget.AppBarLayout><includelayout="@layout/content_home_page" /><android.support.design.widget.FloatingActionButtonandroid:id="@+id/fab"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="bottom|end"android:layout_margin="@dimen/fab_margin"android:src="@android:drawable/ic_dialog_email" /></android.support.design.widget.CoordinatorLayout>
then in your Activity
do this :
Toolbartoolbar= (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TextViewwelcomeTextView= (TextView) toolbar.findViewById(R.id.welcomeText);
welcomeTextView.setText("Welcome " + getUserEmail);
OUTPUT :
Solution 2:
The null object reference is because you are setting the title of the toolbar before you set the toolbar itself. This can be fixed by:
//REMOVED
TextView displayUserPassword=(TextView)findViewById(R.id.displayUserPassword);
displayUserEmail.setText(getUserEmail);
displayUserPassword.setText(getUserPassword);
Toolbartoolbar= (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
this.getSupportActionBar().setTitle("Welcome"+" "+getUserEmail);
Solution 3:
Your
this.getSupportActionBar().setTitle("Welcome"+" "+getUserEmail);
is trying to set the title of an action bar that doesn't exist.
Your app theme may not have an action bar or you are not referencing it properly.
Post a Comment for "How To Put The Title Of The Activity On The Right Of The Toolbar"