Skip to content

Instantly share code, notes, and snippets.

@bgorkowy
Created June 24, 2015 14:21
Show Gist options
  • Save bgorkowy/ce7c39479bb272f8133f to your computer and use it in GitHub Desktop.
Save bgorkowy/ce7c39479bb272f8133f to your computer and use it in GitHub Desktop.
public class NavigationDrawerLayout extends ScrimInsetsLinearLayout {
private int drawerMaxWidth = -1;
public NavigationDrawerLayout(Context context) {
super(context);
}
public NavigationDrawerLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NavigationDrawerLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onSizeChanged(int w, int h, int oldW, int oldH) {
super.onSizeChanged(w, h, oldW, oldH);
if (w != oldW) {
if (drawerMaxWidth <= 0) {
if (getLayoutParams().width != ViewGroup.LayoutParams.MATCH_PARENT && getLayoutParams().width != ViewGroup.LayoutParams.WRAP_CONTENT) {
setDrawerMaxWidth(getLayoutParams().width);
} else {
resetDrawerMaxWidth();
}
}
updateDrawerWidth();
}
}
private void updateDrawerWidth() {
int viewportWidth = getContext().getResources().getDisplayMetrics().widthPixels;
int viewportHeight = getContext().getResources().getDisplayMetrics().heightPixels;
//Minus the width of the vertical nav bar
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
int navigationBarWidthResId = getResources().getIdentifier("navigation_bar_width", "dimen", "android");
if (navigationBarWidthResId > 0) {
viewportWidth -= getResources().getDimensionPixelSize(navigationBarWidthResId);
}
}
int viewportMin = Math.min(viewportWidth, viewportHeight);
//App bar size
TypedValue typedValue = new TypedValue();
getContext().getTheme().resolveAttribute(R.attr.actionBarSize, typedValue, true);
int actionBarSize = TypedValue.complexToDimensionPixelSize(typedValue.data, getResources().getDisplayMetrics());
int width = viewportMin - actionBarSize;
getLayoutParams().width = Math.min(width, drawerMaxWidth);
}
/**
* Sets the max drawer width
*
* @param drawerMaxWidth Max drawer width to set
*/
public void setDrawerMaxWidth(int drawerMaxWidth) {
this.drawerMaxWidth = drawerMaxWidth;
updateDrawerWidth();
}
/**
* Resets the max drawer width
*/
public void resetDrawerMaxWidth() {
this.drawerMaxWidth = getResources().getDimensionPixelSize(R.dimen.navigation_drawer_max_width);
updateDrawerWidth();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment