ActionBar management Java codes

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”);

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.