Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change AppBarLayout height programmatically in Android

I'm trying to implement Flexible Space with image pattern, using this tutorial.

Everything works fine.

Notice the height definition of the AppBarLayout which is 192dp.

I'd like to make the height 1/3 of the screen instead, to match this google example for the pattern here.

Here's the code in the activity's onCreate (the layout xml is exactly the same as in the tutorial):

AppBarLayout appbar = (AppBarLayout)findViewById(R.id.appbar);
float density = getResources().getDisplayMetrics().density;
float heightDp = getResources().getDisplayMetrics().heightPixels / density;
appbar.setLayoutParams(new CoordinatorLayout.LayoutParams(LayoutParams.MATCH_PARENT, Math.round(heightDp / 3)));

But for some reason, the result is not what I'm expecting. I can't see the app bar at all with this code. (without the code, the height shows as expected but it's from XML and can't be set dynamically).

like image 393
Jjang Avatar asked Jul 11 '15 22:07

Jjang


People also ask

What is AppBarLayout android?

AppBarLayout is a vertical LinearLayout which implements many of the features of material designs app bar concept, namely scrolling gestures. Children should provide their desired scrolling behavior through AppBarLayout. LayoutParams. setScrollFlags(int) and the associated layout xml attribute: app:layout_scrollFlags .

What is the standard app bar toolbar height?

The Toolbar (if not extended) has the height of: 56dp (default)


2 Answers

Do this instead:

    AppBarLayout appbar = (AppBarLayout) findViewById(R.id.appbar);
    float heightDp = getResources().getDisplayMetrics().heightPixels / 3;
    CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams)appbar.getLayoutParams();
    lp.height = (int)heightDp;

In your original code I think that you calculation for 1/3 of the screen was wrong, but you still should have seen something. It could be that the LayoutParams.MATCH_PARENT in the setLP() wasn't imported correctly. Always declare the view type first, i.e. CoordinatorLayout.LayoutParams just to make sure. Otherwise it can be easy to use a Framelayout.LayoutParams, for instance.

like image 127
Brett Nottingham Avatar answered Sep 23 '22 00:09

Brett Nottingham


Some methods for changing the AppBarLayout height programatically with dividing, percent or by weight of the screen height:

private AppBarLayout appbar;

/**
 * @return AppBarLayout
 */
@Nullable
protected AppBarLayout getAppBar() {
    if (appbar == null) appbar = (AppBarLayout) findViewById(R.id.appbar);
    return appbar;
}

/**
 * @param divide Set AppBar height to screen height divided by 2->5
 */
protected void setAppBarLayoutHeightOfScreenDivide(@IntRange(from = 2, to = 5) int divide) {
    setAppBarLayoutHeightOfScreenPercent(100 / divide);
}

/**
 * @param percent Set AppBar height to 20->50% of screen height
 */
protected void setAppBarLayoutHeightOfScreenPercent(@IntRange(from = 20, to = 50) int percent) {
    setAppBarLayoutHeightOfScreenWeight(percent / 100F);
}

/**
 * @param weight Set AppBar height to 0.2->0.5 weight of screen height
 */
protected void setAppBarLayoutHeightOfScreenWeight(@FloatRange(from = 0.2F, to = 0.5F) float weight) {
    if (getAppBar() != null) {
        ViewGroup.LayoutParams params = getAppBar().getLayoutParams();
        params.height = Math.round(getResources().getDisplayMetrics().heightPixels * weight);
        getAppBar().setLayoutParams(params);
    }
}

If you want to follow the material design guidelines the height should be equal to the default height plus content increment(s) https://www.google.com/design/spec/layout/structure.html#structure-app-bar

like image 26
TouchBoarder Avatar answered Sep 23 '22 00:09

TouchBoarder