简体   繁体   中英

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 .

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">

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"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM