'How to make the back button in toolbar to the right side of the toolbar?
Is it possible to make the back/up button in toolbar which we generally do using getSupportActionBar().setDisplayHomeAsUpEnabled(true) to the right hand side of the toolbar.
Solution 1:[1]
You need to initialize your Toolbar and setup your back button:
androidx.appcompat.widget.Toolbar toolbar = view.findViewById(R.id."Your Toolbar ID");
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);
((AppCompatActivity) getActivity()).getSupportActionBar().setHomeAsUpIndicator(R.drawable."Your icon for back button ");
toolbar.setTitle("");
toolbar.setSubtitle("");
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getActivity().onBackPressed();
}
});
then set android:layoutDirection="rtl" in your toolbar , like this:
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar_search_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/light_gray"
app:titleTextColor="@android:color/white"
android:layoutDirection="rtl">
Solution 2:[2]
Please try this inside Toolbar layout,
<ImageView
android:id="@+id/addaddresstickbt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/done_add_adrs"
android:layout_gravity="right"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"/>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Daniel Mohajer |
| Solution 2 | Subin Babu |
