How to set the height of the action bar programmatically

Imanol picture Imanol · Nov 29, 2013 · Viewed 8.4k times · Source

I'm implementing a custom view inside the Android action bar. The problem is that under some conditions I need to double the height of the action bar to allow the view to be displayed completely. I can set the height using a custom theme, but this height is static.

Is is possible to change the action bar height programmatically?

Answer

RyanCheu picture RyanCheu · Jun 27, 2015

You can do this using reflection (possibly not a good idea):

    Window window = getWindow();
    View v = window.getDecorView();
    int actionBarId = getResources().getIdentifier("action_bar", "id", "android");
    ViewGroup actionBarView = (ViewGroup) v.findViewById(actionBarId);
    try {
        Field f = actionBarView.getClass().getSuperclass().getDeclaredField("mContentHeight");
        f.setAccessible(true);
        f.set(actionBarView, 50);
    } catch (NoSuchFieldException e) {

    } catch (IllegalAccessException e) {

    }