ActionBar contain action buttons like back,home,menu and also set The application name in action bar. We can do different action from one action bar. it’s also give attractive look to application. In this post i explain some java code for managing your action bar like change colour, show and hide bar, set title etc.

Hide/Show Action Bar

Show action bar code below add your project this code to show action bar.

getActionBar().show();

Hide action bar code below add your project this code to hide action bar

getActionBar().hide();

Change color Action bar

This code used to change or set color of action bar.

ActionBar actionBar = getActionBar(); actionBar.setBackgroundDrawable(new android.graphics.drawable.ColorDrawable(Color.parseColor(“#191919”)));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    Window w = MainActivity.this.getWindow();
    w.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    w.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    w.setStatusBarColor(Color.parseColor(“#000000”));
}

Full screen/unfull screen Action Bar

This code for set action bar full screen

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

This code for exit from full screen action bar

getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

Back home action bar

getActionBar().setDisplayHomeAsUpEnabled(true);
/* */}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
int itemId = item.getItemId();
switch (itemId) {
case android.R.id.home:
finish();
break;
}
return true;

Title and subtitle in action bar

Set title of the application in action bar.

getActionBar().setTitle(“Title”);

Set subtitle below the title of the application.

getActionBar().setSubtitle(“Subtitle”);