1

XML

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/black">
    <TextView
        android:id="@+id/name"
        android:textColor="@color/white"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"/>
    </android.support.v7.widget.Toolbar>

</android.support.design.widget.AppBarLayout>

Activity

OnCreate

getActionBar().setDisplayHomeAsUpEnabled(true);

onOptionsItemSelected

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        // Respond to the action bar's Up/Home button
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
    }
    return super.onOptionsItemSelected(item);
}

Getting a null pointer exception on the line getActionBar().setDisplayHomeAsUpEnabled(true);

  • have you already looked up "how to set back button on toolbar?" – Zun Jun 22 '18 at 11:38
  • i had been googling errors for the past 1 hour and tried so many methods... but no luck... im not sure where i have gone wrong –  Jun 22 '18 at 11:39
  • let me ask the same question again, have you already looked up "how to set back button on toolbar?" – Zun Jun 22 '18 at 11:39
  • yes i have..... –  Jun 22 '18 at 11:42
  • I don't think so cause you're missing code. – Zun Jun 22 '18 at 11:42
  • I can tell you what you're missing but you'll never learn this way – Zun Jun 22 '18 at 11:43
  • what am i missing? idk dude i had tried so many stuff so i might have missed something –  Jun 22 '18 at 11:43
  • 1
    I'm not a professional developer, but when I googeled this `set toolbar android` I found this page (https://developer.android.com/training/appbar/setting-up which is the first result) which PERFECTLY eplains what you are missing!! – Zun Jun 22 '18 at 11:50
  • I looked up to googles oficial page too... i tried that method but the back button only directs me back to the parent activity which i set in manifest... i have an 2 activities which both lead to the same activity and when i press back it used to lead me to the activity i set in manifest and not the activity through which i got there... –  Jun 22 '18 at 11:53
  • so you're saying you have activity A, B and C. A opens B and B opens C (A>B>C). You're saying C leads back to A whenever you click back? – Zun Jun 22 '18 at 11:57
  • No i meant,,,, a,b,c.... a opens b... c also opens b.... so from b if i click back it dsnt go back to the activity i came from but goes back to the activity defined in manifest –  Jun 22 '18 at 12:15

3 Answers3

3

Try this

Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
0

You should link your Toolbar to the actionBar:

private void initActionBar() {
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null)
        actionBar.setDisplayHomeAsUpEnabled(true);
}
SamyB
  • 235
  • 1
  • 9
0

try this way to get back icon in your toolbar

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/black">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorAccent"
            android:paddingLeft="15dp"
            android:paddingRight="15dp">

            <android.support.v7.widget.AppCompatImageView
                android:id="@+id/imgBack"
                android:layout_width="@dimen/_35sdp"
                android:layout_height="@dimen/_35sdp"
                android:layout_centerVertical="true"
                android:foreground="?android:attr/selectableItemBackground"
                android:padding="@dimen/_5sdp"
                android:src="@drawable/ic_arrow_back_black" />

            <TextView
                android:id="@+id/name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/white"
                android:textSize="20dp" />
        </RelativeLayout>
    </android.support.v7.widget.Toolbar>
Milan Pansuriya
  • 2,521
  • 1
  • 19
  • 33