10

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Imanol
  • 4,824
  • 2
  • 28
  • 35
  • It seems that it's not possible and that also goes against the Android Style Guide. Alternatives are: Create a custom view or ask the designer to use a standard android pattern. – Imanol Jan 31 '14 at 09:49
  • check this http://developer.android.com/reference/android/app/ActionBar.LayoutParams.html. I also have same problem, In AppCompact Custom Actionbar in 2.X actionbar is fullscreen. This might help you. – Dharmik Aug 15 '14 at 06:47

2 Answers2

4

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) {

    }
RyanCheu
  • 3,522
  • 5
  • 38
  • 47
  • 1
    There's no need anymore to do this since with lolillop you can use the new toolbar api and set the height http://stackoverflow.com/questions/26491689/how-do-i-declare-an-extended-height-toolbar-action-bar-on-android-lollipop – Imanol Jun 27 '15 at 18:51
  • Could not work: DeclaredField "mContentHeight" seems no longer available – 1111161171159459134 Apr 20 '17 at 15:34
2

RyanCheu's answer worked for me, but I found that if I selected text in an activity, the text selection toolbar appeared at the default action bar size, which resized the activity, which removed the text selection, so it was impossible to use the Copy and Select All functions. I ended up going back to using themes to set the action bar height, and changing themes programmatically using setTheme() and recreate(). Example code is here:

https://stackoverflow.com/a/37974527/462162

Community
  • 1
  • 1
arlomedia
  • 8,534
  • 5
  • 60
  • 108