How can I put an ActionBar with a backspace arrow in a MapsActivity that extends from FragmentActivity.
How can I put an ActionBar with a backspace arrow in a MapsActivity that extends from FragmentActivity.
If your Activity extends from AppCompatActivity
you can use:
public class MainActivity extends AppCompatActivity {
...
...
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
that results in:
In the event that your Activity
extends from FragmentActivity
:
public class MainActivity extends FragmentActivity {
The option is to add a Toolbar:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="0dp"
android:layout_height="56dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
tools:layout_editor_absoluteY="8dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment title" />
</android.support.v7.widget.Toolbar>
and assign the arrow:
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
toolbar.setNavigationIcon(R.drawable.ic_arrow_back);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getActivity().onBackPressed();
}
});
and get something similar to:
I recommend this site to get "material design" icons.