List of usage examples for android.graphics.drawable StateListDrawable addState
public void addState(int[] stateSet, Drawable drawable)
From source file:Main.java
/** * set btn selector with corner drawable for special position *//*ww w. ja v a 2 s. co m*/ public static StateListDrawable btnSelector(float radius, int normalColor, int pressColor, int postion) { StateListDrawable bg = new StateListDrawable(); Drawable normal = null; Drawable pressed = null; if (postion == 0) {// left btn normal = cornerDrawable(normalColor, new float[] { 0, 0, 0, 0, 0, 0, radius, radius }); pressed = cornerDrawable(pressColor, new float[] { 0, 0, 0, 0, 0, 0, radius, radius }); } else if (postion == 1) {// right btn normal = cornerDrawable(normalColor, new float[] { 0, 0, 0, 0, radius, radius, 0, 0 }); pressed = cornerDrawable(pressColor, new float[] { 0, 0, 0, 0, radius, radius, 0, 0 }); } else if (postion == -1) {// only one btn normal = cornerDrawable(normalColor, new float[] { 0, 0, 0, 0, radius, radius, radius, radius }); pressed = cornerDrawable(pressColor, new float[] { 0, 0, 0, 0, radius, radius, radius, radius }); } else if (postion == -2) {// for material dialog normal = cornerDrawable(normalColor, radius); pressed = cornerDrawable(pressColor, radius); } bg.addState(new int[] { -android.R.attr.state_pressed }, normal); bg.addState(new int[] { android.R.attr.state_pressed }, pressed); return bg; }
From source file:me.trashout.ui.SelectableImageButton.java
/** * Change background selected color/*from w ww .j a v a 2 s . c om*/ * @param color */ public void setBackgroundSelectedColor(int color) { LayerDrawable layerDrawable = (LayerDrawable) ContextCompat.getDrawable(getContext(), R.drawable.background_trash_circle_button_selected); GradientDrawable shape = (GradientDrawable) layerDrawable .findDrawableByLayerId(R.id.selected_background_shape).mutate(); shape.setColor(color); StateListDrawable states = new StateListDrawable(); states.addState(new int[] { android.R.attr.state_pressed }, shape); states.addState(new int[] { android.R.attr.state_selected }, shape); states.addState(new int[] {}, ContextCompat.getDrawable(getContext(), R.drawable.background_trash_circle_button)); setBackground(states); invalidate(); }
From source file:eu.geopaparazzi.library.forms.FormListFragment.java
@Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); List<String> fragmentTitles = mFragmentListSupporter.getListTitles(); ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_activated_1, fragmentTitles.toArray(new String[fragmentTitles.size()])); int color = Compat.getColor(getActivity(), R.color.formcolor); int[] colors = { 0, color, 0 }; ListView listView = getListView(); listView.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors)); listView.setDividerHeight(2);//from w ww. j a va 2 s. c o m listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); listView.setFocusableInTouchMode(true); StateListDrawable selector = new StateListDrawable(); selector.addState(new int[] { -android.R.attr.state_pressed }, new ColorDrawable(Compat.getColor(getContext(), R.color.main_selection))); listView.setSelector(selector); setListAdapter(adapter); if (fragmentTitles.size() > 0) listView.setItemChecked(0, true); }
From source file:com.auth0.android.lock.views.ActionButton.java
private Drawable generateStateBackground(Theme lockTheme) { int normalColor = lockTheme.getPrimaryColor(getContext()); int pressedColor = lockTheme.getDarkPrimaryColor(getContext()); int disabledColor = ContextCompat.getColor(getContext(), R.color.com_auth0_lock_submit_disabled); if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { return new RippleDrawable(ColorStateList.valueOf(pressedColor), new ColorDrawable(normalColor), null); } else {/*from w w w .j a v a2 s .c o m*/ StateListDrawable states = new StateListDrawable(); states.addState(new int[] { android.R.attr.state_enabled, android.R.attr.state_pressed }, new ColorDrawable(pressedColor)); states.addState(new int[] { android.R.attr.state_enabled }, new ColorDrawable(normalColor)); states.addState(new int[] {}, new ColorDrawable(disabledColor)); return states; } }
From source file:com.gudong.appkit.ui.fragment.ColorChooseDialog.java
private Drawable createSelector(int color) { ShapeDrawable coloredCircle = new ShapeDrawable(new OvalShape()); coloredCircle.getPaint().setColor(color); ShapeDrawable darkerCircle = new ShapeDrawable(new OvalShape()); darkerCircle.getPaint().setColor(shiftColor(color)); StateListDrawable stateListDrawable = new StateListDrawable(); stateListDrawable.addState(new int[] { -android.R.attr.state_pressed }, coloredCircle); stateListDrawable.addState(new int[] { android.R.attr.state_pressed }, darkerCircle); return stateListDrawable; }
From source file:cn.finalteam.galleryfinal.widget.FloatingActionButton.java
private StateListDrawable createFillDrawable(RectF circleRect) { StateListDrawable drawable = new StateListDrawable(); drawable.addState(new int[] { android.R.attr.state_pressed }, createCircleDrawable(circleRect, mColorPressed)); drawable.addState(new int[] {}, createCircleDrawable(circleRect, mColorNormal)); return drawable; }
From source file:com.nickandjerry.dynamiclayoutinflator.DynamicLayoutInflator.java
@SuppressLint("NewApi") private static void applyAttributes(View view, Map<String, String> attrs, ViewGroup parent) { if (viewRunnables == null) createViewRunnables();//ww w. j a v a 2 s . com ViewGroup.LayoutParams layoutParams = view.getLayoutParams(); int layoutRule; int marginLeft = 0, marginRight = 0, marginTop = 0, marginBottom = 0, paddingLeft = 0, paddingRight = 0, paddingTop = 0, paddingBottom = 0; boolean hasCornerRadius = false, hasCornerRadii = false; for (Map.Entry<String, String> entry : attrs.entrySet()) { String attr = entry.getKey(); if (viewRunnables.containsKey(attr)) { viewRunnables.get(attr).apply(view, entry.getValue(), parent, attrs); continue; } if (attr.startsWith("cornerRadius")) { hasCornerRadius = true; hasCornerRadii = !attr.equals("cornerRadius"); continue; } layoutRule = NO_LAYOUT_RULE; boolean layoutTarget = false; switch (attr) { case "id": String idValue = parseId(entry.getValue()); if (parent != null) { DynamicLayoutInfo info = getDynamicLayoutInfo(parent); int newId = highestIdNumberUsed++; view.setId(newId); info.nameToIdNumber.put(idValue, newId); } break; case "width": case "layout_width": switch (entry.getValue()) { case "wrap_content": layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT; break; case "fill_parent": case "match_parent": layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT; break; default: layoutParams.width = DimensionConverter.stringToDimensionPixelSize(entry.getValue(), view.getResources().getDisplayMetrics(), parent, true); break; } break; case "height": case "layout_height": switch (entry.getValue()) { case "wrap_content": layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT; break; case "fill_parent": case "match_parent": layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT; break; default: layoutParams.height = DimensionConverter.stringToDimensionPixelSize(entry.getValue(), view.getResources().getDisplayMetrics(), parent, false); break; } break; case "layout_gravity": if (parent != null && parent instanceof LinearLayout) { ((LinearLayout.LayoutParams) layoutParams).gravity = parseGravity(entry.getValue()); } else if (parent != null && parent instanceof FrameLayout) { ((FrameLayout.LayoutParams) layoutParams).gravity = parseGravity(entry.getValue()); } break; case "layout_weight": if (parent != null && parent instanceof LinearLayout) { ((LinearLayout.LayoutParams) layoutParams).weight = Float.parseFloat(entry.getValue()); } break; case "layout_below": layoutRule = RelativeLayout.BELOW; layoutTarget = true; break; case "layout_above": layoutRule = RelativeLayout.ABOVE; layoutTarget = true; break; case "layout_toLeftOf": layoutRule = RelativeLayout.LEFT_OF; layoutTarget = true; break; case "layout_toRightOf": layoutRule = RelativeLayout.RIGHT_OF; layoutTarget = true; break; case "layout_alignBottom": layoutRule = RelativeLayout.ALIGN_BOTTOM; layoutTarget = true; break; case "layout_alignTop": layoutRule = RelativeLayout.ALIGN_TOP; layoutTarget = true; break; case "layout_alignLeft": case "layout_alignStart": layoutRule = RelativeLayout.ALIGN_LEFT; layoutTarget = true; break; case "layout_alignRight": case "layout_alignEnd": layoutRule = RelativeLayout.ALIGN_RIGHT; layoutTarget = true; break; case "layout_alignParentBottom": layoutRule = RelativeLayout.ALIGN_PARENT_BOTTOM; break; case "layout_alignParentTop": layoutRule = RelativeLayout.ALIGN_PARENT_TOP; break; case "layout_alignParentLeft": case "layout_alignParentStart": layoutRule = RelativeLayout.ALIGN_PARENT_LEFT; break; case "layout_alignParentRight": case "layout_alignParentEnd": layoutRule = RelativeLayout.ALIGN_PARENT_RIGHT; break; case "layout_centerHorizontal": layoutRule = RelativeLayout.CENTER_HORIZONTAL; break; case "layout_centerVertical": layoutRule = RelativeLayout.CENTER_VERTICAL; break; case "layout_centerInParent": layoutRule = RelativeLayout.CENTER_IN_PARENT; break; case "layout_margin": marginLeft = marginRight = marginTop = marginBottom = DimensionConverter .stringToDimensionPixelSize(entry.getValue(), view.getResources().getDisplayMetrics()); break; case "layout_marginLeft": marginLeft = DimensionConverter.stringToDimensionPixelSize(entry.getValue(), view.getResources().getDisplayMetrics(), parent, true); break; case "layout_marginTop": marginTop = DimensionConverter.stringToDimensionPixelSize(entry.getValue(), view.getResources().getDisplayMetrics(), parent, false); break; case "layout_marginRight": marginRight = DimensionConverter.stringToDimensionPixelSize(entry.getValue(), view.getResources().getDisplayMetrics(), parent, true); break; case "layout_marginBottom": marginBottom = DimensionConverter.stringToDimensionPixelSize(entry.getValue(), view.getResources().getDisplayMetrics(), parent, false); break; case "padding": paddingBottom = paddingLeft = paddingRight = paddingTop = DimensionConverter .stringToDimensionPixelSize(entry.getValue(), view.getResources().getDisplayMetrics()); break; case "paddingLeft": paddingLeft = DimensionConverter.stringToDimensionPixelSize(entry.getValue(), view.getResources().getDisplayMetrics()); break; case "paddingTop": paddingTop = DimensionConverter.stringToDimensionPixelSize(entry.getValue(), view.getResources().getDisplayMetrics()); break; case "paddingRight": paddingRight = DimensionConverter.stringToDimensionPixelSize(entry.getValue(), view.getResources().getDisplayMetrics()); break; case "paddingBottom": paddingBottom = DimensionConverter.stringToDimensionPixelSize(entry.getValue(), view.getResources().getDisplayMetrics()); break; } if (layoutRule != NO_LAYOUT_RULE && parent instanceof RelativeLayout) { if (layoutTarget) { int anchor = idNumFromIdString(parent, parseId(entry.getValue())); ((RelativeLayout.LayoutParams) layoutParams).addRule(layoutRule, anchor); } else if (entry.getValue().equals("true")) { ((RelativeLayout.LayoutParams) layoutParams).addRule(layoutRule); } } } // TODO: this is a giant mess; come up with a simpler way of deciding what to draw for the background if (attrs.containsKey("background") || attrs.containsKey("borderColor")) { String bgValue = attrs.containsKey("background") ? attrs.get("background") : null; if (bgValue != null && bgValue.startsWith("@drawable/")) { view.setBackground(getDrawableByName(view, bgValue)); } else if (bgValue == null || bgValue.startsWith("#") || bgValue.startsWith("@color")) { if (view instanceof Button || attrs.containsKey("pressedColor")) { int bgColor = parseColor(view, bgValue == null ? "#00000000" : bgValue); int pressedColor; if (attrs.containsKey("pressedColor")) { pressedColor = parseColor(view, attrs.get("pressedColor")); } else { pressedColor = adjustBrightness(bgColor, 0.9f); } GradientDrawable gd = new GradientDrawable(); gd.setColor(bgColor); GradientDrawable pressedGd = new GradientDrawable(); pressedGd.setColor(pressedColor); if (hasCornerRadii) { float radii[] = new float[8]; for (int i = 0; i < CORNERS.length; i++) { String corner = CORNERS[i]; if (attrs.containsKey("cornerRadius" + corner)) { radii[i * 2] = radii[i * 2 + 1] = DimensionConverter.stringToDimension( attrs.get("cornerRadius" + corner), view.getResources().getDisplayMetrics()); } gd.setCornerRadii(radii); pressedGd.setCornerRadii(radii); } } else if (hasCornerRadius) { float cornerRadius = DimensionConverter.stringToDimension(attrs.get("cornerRadius"), view.getResources().getDisplayMetrics()); gd.setCornerRadius(cornerRadius); pressedGd.setCornerRadius(cornerRadius); } if (attrs.containsKey("borderColor")) { String borderWidth = "1dp"; if (attrs.containsKey("borderWidth")) { borderWidth = attrs.get("borderWidth"); } int borderWidthPx = DimensionConverter.stringToDimensionPixelSize(borderWidth, view.getResources().getDisplayMetrics()); gd.setStroke(borderWidthPx, parseColor(view, attrs.get("borderColor"))); pressedGd.setStroke(borderWidthPx, parseColor(view, attrs.get("borderColor"))); } StateListDrawable selector = new StateListDrawable(); selector.addState(new int[] { android.R.attr.state_pressed }, pressedGd); selector.addState(new int[] {}, gd); view.setBackground(selector); getDynamicLayoutInfo(view).bgDrawable = gd; } else if (hasCornerRadius || attrs.containsKey("borderColor")) { GradientDrawable gd = new GradientDrawable(); int bgColor = parseColor(view, bgValue == null ? "#00000000" : bgValue); gd.setColor(bgColor); if (hasCornerRadii) { float radii[] = new float[8]; for (int i = 0; i < CORNERS.length; i++) { String corner = CORNERS[i]; if (attrs.containsKey("cornerRadius" + corner)) { radii[i * 2] = radii[i * 2 + 1] = DimensionConverter.stringToDimension( attrs.get("cornerRadius" + corner), view.getResources().getDisplayMetrics()); } gd.setCornerRadii(radii); } } else if (hasCornerRadius) { float cornerRadius = DimensionConverter.stringToDimension(attrs.get("cornerRadius"), view.getResources().getDisplayMetrics()); gd.setCornerRadius(cornerRadius); } if (attrs.containsKey("borderColor")) { String borderWidth = "1dp"; if (attrs.containsKey("borderWidth")) { borderWidth = attrs.get("borderWidth"); } gd.setStroke( DimensionConverter.stringToDimensionPixelSize(borderWidth, view.getResources().getDisplayMetrics()), parseColor(view, attrs.get("borderColor"))); } view.setBackground(gd); getDynamicLayoutInfo(view).bgDrawable = gd; } else { view.setBackgroundColor(parseColor(view, bgValue)); } } } if (layoutParams instanceof ViewGroup.MarginLayoutParams) { ((ViewGroup.MarginLayoutParams) layoutParams).setMargins(marginLeft, marginTop, marginRight, marginBottom); } view.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom); view.setLayoutParams(layoutParams); }
From source file:android.support.design.internal.NavigationMenuItemView.java
private StateListDrawable createDefaultBackground() { TypedValue value = new TypedValue(); if (getContext().getTheme().resolveAttribute(android.support.v7.appcompat.R.attr.colorControlHighlight, value, true)) {/*from www . java 2 s.c o m*/ StateListDrawable drawable = new StateListDrawable(); drawable.addState(CHECKED_STATE_SET, new ColorDrawable(value.data)); drawable.addState(EMPTY_STATE_SET, new ColorDrawable(Color.TRANSPARENT)); return drawable; } return null; }
From source file:net.yanzm.mth.MaterialTabHost.java
/** * add new tab with specified view//from w w w . ja v a 2 s.co m * * @param view tab view */ public void addTab(View view) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { view.setBackgroundResource(R.drawable.mth_tab_widget_background_ripple); } else { // create background using colorControlActivated StateListDrawable d = new StateListDrawable(); d.addState(new int[] { android.R.attr.state_pressed }, new ColorDrawable(colorControlActivated)); d.setAlpha(180); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { view.setBackground(d); } else { view.setBackgroundDrawable(d); } } int tabId = tabWidget.getTabCount(); addTab(newTabSpec(String.valueOf(tabId)).setIndicator(view).setContent(android.R.id.tabcontent)); }
From source file:net.yanzm.mth.MaterialTabHost.java
/** * add new tab with title text/*from w ww . jav a2 s. c o m*/ * * @param title title text */ public void addTab(CharSequence title) { int layoutId = getLayoutId(type); TextView tv = (TextView) inflater.inflate(layoutId, tabWidget, false); tv.setText(title); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { tv.setBackgroundResource(R.drawable.mth_tab_widget_background_ripple); } else { // create background using colorControlActivated StateListDrawable d = new StateListDrawable(); d.addState(new int[] { android.R.attr.state_pressed }, new ColorDrawable(colorControlActivated)); d.setAlpha(180); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { tv.setBackground(d); } else { tv.setBackgroundDrawable(d); } } int tabId = tabWidget.getTabCount(); addTab(newTabSpec(String.valueOf(tabId)).setIndicator(tv).setContent(android.R.id.tabcontent)); }