Add home button on action bar

To add home button in action bar write some code in onCreate section.

Write first code for enable button in action bar.

Code

getActionBar().setDisplayHomeAsUpEnabled(true);

After that write code for set home button in action bar.

code

getActionBar().setHomeAsUpIndicator(R.drawable.ic_home_white);

Note :- ic_home_white is id of icon. can change image as you want.

After that write some more code for performe any action.

}
 @Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case android.R.id.home:
//write here some action
break;
}
return super.onOptionsItemSelected(item);

Mostly we create AppCompat application and in this type application the code given up don’t work to modify action bar. so what should you do for modify action bar in AppCompat type apps.

Here is solution. write some code before write action bar enable and set home button codes.

Code

android.support.v7.app.ActionBar ab = getSupportActionBar();

Now write this code. its same code given up but it’s now work in AppCompat apps.

ab.setDisplayHomeAsUpEnabled(true);
ab.setHomeAsUpIndicator(R.drawable.ic_home_white);

To modify action bar in sketchwere write same code in add source directly and also apply same condition.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.