Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Icon to the left of the title in the action bar in android

Tags:

android

menu

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;     } 
like image 691
sana baid Avatar asked Mar 10 '17 08:03

sana baid


1 Answers

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);      } } 
like image 178
Mayur Dabhi Avatar answered Sep 23 '22 19:09

Mayur Dabhi