Skip to content

Instantly share code, notes, and snippets.

@ookami-kb
Created April 7, 2015 20:17
Show Gist options
  • Save ookami-kb/acab8aad4ed26ba0fff5 to your computer and use it in GitHub Desktop.
Save ookami-kb/acab8aad4ed26ba0fff5 to your computer and use it in GitHub Desktop.
DrawerListView with width by Material design guidelines
public class DrawerListView(context: Context, attrs: AttributeSet) : ListView(context, attrs) {
val widthCalculated : Int
init {
val displayMetrics = getContext().getResources().getDisplayMetrics()
val maxWidth = Math.round(400 * displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT)
val wm = getContext().getSystemService(Context.WINDOW_SERVICE) as WindowManager
val size = Point()
wm.getDefaultDisplay().getSize(size)
val windowWidth = size.x
val styledAttrs = getContext().getTheme().obtainStyledAttributes(intArray(android.R.attr.actionBarSize))
val abSize = styledAttrs.getDimension(0, 0f)
styledAttrs.recycle()
widthCalculated = Math.min((windowWidth - abSize).toInt(), maxWidth)
}
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
super.onMeasure(View.MeasureSpec.makeMeasureSpec(widthCalculated, View.MeasureSpec.EXACTLY), heightMeasureSpec)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment