Last active
August 29, 2015 14:21
-
-
Save gfpacheco/640dc558380fb9c7fa4d to your computer and use it in GitHub Desktop.
A Toolbar subclass meant to be used by Fragments inside a translucent Activity that adds padding to itself to avoid being under the status bar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.gfpacheco.views.widget; | |
import android.content.Context; | |
import android.os.Build; | |
import android.support.v7.widget.Toolbar; | |
import android.util.AttributeSet; | |
public class PaddedToolbar extends Toolbar { | |
static int sStatusBarHeight = -1; | |
public PaddedToolbar(Context context) { | |
super(context); | |
} | |
public PaddedToolbar(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public PaddedToolbar(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
} | |
@Override | |
protected void onLayout(boolean changed, int l, int t, int r, int b) { | |
setPadding(0, getStatusBarHeight(), 0, 0); | |
super.onLayout(changed, l, t, r, b); | |
} | |
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
int height = MeasureSpec.getSize(heightMeasureSpec); | |
super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(height + getStatusBarHeight(), MeasureSpec.EXACTLY)); | |
} | |
public int getStatusBarHeight() { | |
if (sStatusBarHeight == -1) { | |
sStatusBarHeight = 0; | |
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && resourceId > 0) { | |
sStatusBarHeight = getResources().getDimensionPixelSize(resourceId); | |
} | |
} | |
return sStatusBarHeight; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment