public void addItem(Item item) { int size = icon + padding + padding; ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, size); TextView row = new TextView(getContext()); row.setId(item.getId()); row.setLayoutParams(params); row.setMaxLines(1); row.setEllipsize(TextUtils.TruncateAt.END); row.setGravity(Gravity.CENTER_VERTICAL); row.setText(item.getTitle()); row.setTypeface(Typeface.DEFAULT_BOLD); row.setOnClickListener(this); row.setTextColor(BottomUtils.colorStateListText(getContext())); TypedValue typedValue = new TypedValue(); getContext() .getTheme() .resolveAttribute(android.R.attr.selectableItemBackground, typedValue, true); row.setBackgroundResource(typedValue.resourceId); if (item.getIcon() != null) { row.setCompoundDrawablesWithIntrinsicBounds(icon(item.getIcon()), null, null, null); row.setCompoundDrawablePadding(padding); row.setPadding(padding, padding, padding, padding); } else { row.setPadding(size, padding, padding, padding); } container.addView(row); }
/** * @param drawable Drawable from menu item * @return Drawable resized 32dp x 32dp and colored with color textColorSecondary */ private Drawable icon(Drawable drawable) { if (drawable != null) { Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap(); Drawable resizeIcon = new BitmapDrawable( getContext().getResources(), Bitmap.createScaledBitmap(bitmap, icon, icon, true)); Drawable.ConstantState state = resizeIcon.getConstantState(); resizeIcon = DrawableCompat.wrap(state == null ? resizeIcon : state.newDrawable()).mutate(); DrawableCompat.setTintList(resizeIcon, BottomUtils.colorStateListIcon(getContext())); return resizeIcon; } return null; }