I search to know how to display back button in a class which inherit from FragmentActivity,
public class GoogleMapsActivity extends FragmentActivity
I know there is the method onBackPressed() which can be override but i don't know how to display back button
@Override
public void onBackPressed() {
}
I tried this method : https://stackoverflow.com/a/43976262/7079226 but it doesn't work, Thanks for your answers
I have same problem with you. I already use ((AppCompatActivity)getApplicationContext()).setSupportActionBar(toolbar);
but always getting force close.
Then my solution is create my own toolbar in the layout
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/overflow_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
app:srcCompat="?attr/homeAsUpIndicator" />
<TextView
android:id="@+id/textToolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="@+id/overflow_back"
android:paddingLeft="32dp"
android:textSize="18sp"
android:textStyle="bold"
android:layout_toRightOf="@+id/overflow_back"
android:text="Toolbar Title"
android:textColor="@android:color/white" />
</RelativeLayout> </android.support.v7.widget.Toolbar>
overflow_back
for backbutton
, textToolbar
for title
.
In your activity:
imageBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onBackPressed();
}
});