How to add a return button to the previous activation, instead of using the system button? The example in the figure below
- getSupportActionBar (). setDisplayHomeAsUpEnabled (true); getSupportActionBar (). setDisplayShowHomeEnabled (true); - Chaynik
- instead of the system one, hmm, only if you create a button and activate startActivity inside onClick, or onBackPressed () - iFr0z
- not so much in return, how much so as not to press the system backward, but to use this new button - BuzZer
|
2 answers
not so much in return, how much not to press the system backward, but to use this new button
In general, your picture shows the following: Toolbar is a good replacement for the good old ActionBar.
*toolbar.xml* <?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/tool_bar" android:layout_width="match_parent" android:layout_height="wrap_content" android:minHeight="@dimen/triple_height_toolbar" android:background="#2196F3" android:elevation="4dp" app:theme="@style/ToolbarColoredBackArrow" app:contentInsetEnd="0dp" app:contentInsetStart="0dp" > </android.support.v7.widget.Toolbar> To work with the toolbar:
import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; public class AndroidToolbarExample extends AppCompatActivity { Toolbar toolbar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); toolbar = (Toolbar) findViewById(R.id.tool_bar); setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setDisplayShowTitleEnabled(false); toolbar.setTitle(""); toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v){ onBackPressed();// возврат на предыдущий activity } }); } } Ps toolbar works the same everywhere, starting with android 2+
- oneIt turned out to use the code after some changes: 1. In Android Studio 2, the project was created on Basic Activity. 2. Then edit the following - // onBackPressed (); // return to the previous activity Toast.makeText (MainActivity.this, "Backward button pressed in the Toolbar", Toast.LENGTH_SHORT) .show (); as in MainActivity it is the exit from the program - BuzZer
|
It's not entirely clear what exactly you want to do. If you replace the icon, do this:
Drawable drawable = ContextCompat.getDrawable(context, R.drawable.your_drawable_icon); Toolbar toolbar = (Toolbar) v.findViewById(R.id.toolbar); setSupportActionBar(toolbar); toolbar.setNavigationIcon(drawable); If you want to click on the previous activation, do this:
toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v){ onBackPressed(); } }); |
