Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get ActionBar view?

I'm using the Showcase library to explain my application feature to the user. In some point I need to dim the whole ActionBar to present another feature to the user.

For that I'm using the setAlpha(float num) of the View class. And so for doing that I need to get the actual view instance of my ActionBar

By the way, I'm using the support-7-appcompat library that gives ActionBar support for older systems.

Update

In the meantime I found this option, if you configure a custom view and add it to you ActionBar using:

getSupportActionBar().setCustomView(v); 

Then to get the View of the ActionBar you could do:

(View) activity.getSupportActionBar().getCustomView().getParent().getParent() 

Is there a simpler or easier way to do that?

like image 498
Emil Adz Avatar asked Nov 16 '13 20:11

Emil Adz


People also ask

Where is ActionBar in Android?

Android ActionBar is a menu bar that runs across the top of the activity screen in android. Android ActionBar can contain menu items which become visible when the user clicks the “menu” button.

How do I get the action bar?

You may otherwise add the action bar by calling requestFeature(FEATURE_ACTION_BAR) or by declaring it in a custom theme with the windowActionBar property. Beginning with Android L (API level 21), the action bar may be represented by any Toolbar widget within the application layout.

Which view can be used to customize action bar?

Custom Action Bar Layout The view layout consists of two ImageButtons that represent forward and back image buttons and a TextView in the center.


2 Answers

Yep. You can actually get the view by using this function:

public View getActionBarView() {     Window window = getWindow();     View v = window.getDecorView();     int resId = getResources().getIdentifier("action_bar_container", "id", "android");     return v.findViewById(resId); } 

Pretty much the way this works is that the actionbar container uses the id android.R.id.action_bar_container, but this id is not public. Therefore we use getIdentifier() to retrieve this id and then the rest is simple.

like image 84
idunnololz Avatar answered Oct 01 '22 16:10

idunnololz


I think this solution is more complete, handling both normal Activity and ActionBarActivity.

It also handles the case that the actionbar was set using a toolbar, but you need to implement it in the activity you've created:

public static View getActionBarView(final Activity activity) {     if (activity instanceof IToolbarHolder)         return ((IToolbarHolder) activity).getToolbar();     final String packageName = activity instanceof ActionBarActivity ? activity.getPackageName() : "android";     final int resId = activity.getResources().getIdentifier("action_bar_container", "id", packageName);     final View view = activity.findViewById(resId);     return view; }  public interface IToolbarHolder {     public android.support.v7.widget.Toolbar getToolbar(); } 
like image 42
android developer Avatar answered Oct 01 '22 17:10

android developer