I want to add a clickable icon/button to the left of the title in the action bar . How to do it? Following is the code with which I have added search and setting icon to the action bar. They appear on the right. But i want to add a new icon to the left of title. How to do it? XML :
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context=".ui.home.activities.TransactionSearchActivity"> <item android:id="@+id/action_search" android:icon="@android:drawable/ic_menu_search" android:title="search" app:actionViewClass="android.support.v7.widget.SearchView" app:showAsAction="always" /> <item android:id="@+id/action_setting" android:title="categories" android:icon="@drawable/icon_settings_h" app:showAsAction="always" />
MAIN ACTIVITY
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main_menu, menu); return true; }
Recommended way
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_settings_24dp);// set drawable icon getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Handle icon click event
@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: Toast.makeText(this, "click..!!", Toast.LENGTH_SHORT).show(); return true; default: return super.onOptionsItemSelected(item); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With