Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the height of the action bar programmatically

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?

like image 705
Imanol Avatar asked Nov 29 '13 11:11

Imanol


People also ask

How do I change the size of the action bar?

Under your in-game Video Options, check the "Use UI Scale" option and adjust it to the right accordingly. That will increase the UI size (including your action bars).

How do I increase the height of my toolbar?

You can set the height here android:minHeight="? attr/actionBarSize" in your XML. Use height and not minHeight.

What is action bar size in Android?

We can see that we are able to fetch the ActionBar height, which is equal to 147 Pixels.

How do I add settings to action bar?

All action buttons and other items available in the action overflow are defined in an XML menu resource. To add actions to the action bar, create a new XML file in your project's res/menu/ directory. The app:showAsAction attribute specifies whether the action should be shown as a button on the app bar.


2 Answers

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

    }
like image 144
RyanCheu Avatar answered Oct 06 '22 11:10

RyanCheu


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

like image 28
arlomedia Avatar answered Oct 06 '22 11:10

arlomedia