How to get ActionBar view?
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.
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();
}