I want to make ImageView with specific GradientDrawable and make the src = "#drawable/myImage". When I try this code I get all things fine but the src image doesn't display:
leftIcon = (ImageView)rowView.findViewById(R.id.left_icon);
leftIcon.setImageResource(R.drawable.arrow333);
GradientDrawable gd=new GradientDrawable();
gd.setCornerRadii(getRandomFloatArray());
gd.setStroke(5, Color.WHITE);
gd.setColor(values[position].getColor());
GradientDrawable gd_press=new GradientDrawable();
gd_press.setCornerRadii(getRandomFloatArray());
gd_press.setStroke(5, Color.BLUE);
gd_press.setColor(values[position].getColor());
StateListDrawable states = new StateListDrawable();
states.addState(new int[] {android.R.attr.state_pressed}, gd_press );
states.addState(new int[] {android.R.attr.state_focused}, gd_press);
states.addState(new int[] { }, gd);
leftIcon.setImageResource(R.drawable.arrow333);
leftIcon.setImageDrawable(states);
And my xml file:
<ImageView
android:id="#+id/left_icon"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" />
That's probably because you are setting a drawable states just right after setting image resource. If you want both the Images visible than apply one as ImageView background and other as a source:
leftIcon.setBackgroundResource(R.drawable.arrow333);
leftIcon.setImageDrawable(states);
OR
leftIcon.setBackgroundResource(states);
leftIcon.setImageDrawable(R.drawable.arrow333);
Related
I am trying to draw some colors in the background of a view.
The colors are to be placed as linear gradients and in different directions.
So i did the following:
private Orientation[] orientations = new Orientation[]{Orientation.BL_TR, Orientation.BR_TL, Orientation.TL_BR, Orientation.TR_BL, Orientation.TOP_BOTTOM};
public void drawBackgroudGradient(RelativeLayout backgroundView, int[] colors){
if (colors != null){
if (colors.length > 1){
for (int i=0; i<colors.length; i++){
backgroundView.setBackgroundDrawable(getGradientDrawable(colors[i], orientations[i]));
}
}else{
//in case of only one color just set that color as background
backgroundView.setBackgroundColor(colors[0]);
}
}
}
private GradientDrawable getGradientDrawable(int color, Orientation orientation){
int[] colors = new int[] {color, Color.WHITE};
GradientDrawable drawable = new GradientDrawable(orientation, colors);
drawable.setAlpha(125);
return drawable;
}
I have each drawable going from the starting color to transparent so that all the gradients are visible.
But the problem is that all the colors are not showing up. Only the last color is drawn via the gradient drawable. the rest can not be seen.
Could someone please help me figure out how to mix and show all the colors?
Thanks.
Sunny
create LayerDrawable and mix them:
private LayerDrawable getGradientDrawable(int color, Orientation orientation){
int[] colors = new int[] {color, Color.WHITE};
GradientDrawable drawable1 = new GradientDrawable(orientation, colors);
drawable1.setAlpha(125);
GradientDrawable drawable2 = new GradientDrawable();
drawable2.setStroke(4, Color.parseColor("#FFFFFF"));
drawable2.setColor(Color.TRANSPARENT);
return new LayerDrawable(new Drawable[]{drawable1 , drawable2});
}
drawable2 placed on top of drawable1.
I want to create a selector drawable with #000000 for selected and #FFFFFF for unselected state.
How can I create a drawable programmatically?
Currently I am doing it as following:
StateListDrawable states = new StateListDrawable();
ColorDrawable cdPress = new ColorDrawable(0xFF0000);
ColorDrawable cdUnPress = new ColorDrawable(0x0101DF);
states.addState(new int[] { android.R.attr.state_selected}, cdPress);
states.addState(new int[] {-android.R.attr.state_selected}, cdUnPress);
view.setBackgroundDrawable(states);
view.setSelected(isSelected);
create stateListDrawable and pass to the view
StateListDrawable stateListDrawable=new StateListDrawable();
stateListDrawable.addState(new int[]{android.R.attr.state_pressed}getColorDrawable(Colorcode));
stateListDrawable.addState(new int[]{android.R.attr.state_focused},getColorDrawable(Colorcode));
...
mView.setBackground(stateListDrawable);
...
}
private static Drawable getColorDrawable(int colorCode) {
return new ColorDrawable(colorCode);
}
I am trying to create a user themeable app. I have linearlayouts which are clickable and I need to set the selector programatically when the layouts are added to the parent view. However the following code throws a NoSuchMetod exception when I set v.SetBackground().
Where am I going wrong?
LinearLayout d = (LinearLayout) inflater.inflate(R.layout.adapter_psudo_launcheritem, null);
ColorDrawable clrBase = new ColorDrawable();
clrBase.setColor(gCurrentTheme.Colours.Backgrounds.MenuItem);
ColorDrawable clrSelect = new ColorDrawable();
clrSelect.setColor(gCurrentTheme.Colours.Backgrounds.MenuItem_Select);
ColorDrawable clrPress = new ColorDrawable();
clrPress.setColor(gCurrentTheme.Colours.Backgrounds.MenuItem_Press);
StateListDrawable states = new StateListDrawable();
states.addState(new int[] {android.R.attr.state_enabled}, clrBase);
states.addState(new int[] {android.R.attr.state_selected}, clrSelect);
states.addState(new int[] {android.R.attr.state_pressed}, clrPress);
//This line causes a NoSuchMethod exception!
v.setBackground(states);
View.setBackgroud has been introduce in api level 16 and replaceB setBackgroundDrawable (it is deprecated since API 16) If the mobile where you are running the above code as an API level lower than 16 you have to use setBackgroundDrawable
The reason I need to do this programmatically is that the text color is downloaded and not pre defined in the xml. I read this
Replace selector images programmatically
I only need to know from
StateListDrawable states = new StateListDrawable();
states.addState(new int[] {android.R.attr.state_pressed},
getResources().getDrawable(R.drawable.pressed));
how turn into
states.addState(new int[] {android.R.attr.state_pressed},**theMethodImLookingFor**("#e3bb87"));
forget about getResources().getColor(R.color.anycolor) , the color is not defined in xml
You can use this:
states.addState(new int[] {android.R.attr.state_pressed},
new ColorDrawable(Color.parseColor("#e3bb87")));
I think you are looking for ColorDrawable
you can do something like this:
StateListDrawable states = new StateListDrawable();
int color = 0xff00ff00;
states.addState(new int[] {android.R.attr.state_pressed},
new ColorDrawable(color));
The method would be
new ColorDrawable(Color.parseColor("#e3bb87"))
this is xml code that I use to change the button images in my app, according to the state:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false"
android:drawable="#drawable/button_n" />
<item android:state_pressed="true"
android:drawable="#drawable/button_p" />
</selector>
How can I do this by code? I have try this:
StateListDrawable sl = new StateListDrawable();
sl.addState(new int[]{ android.R.attr.state_pressed}, R.drawable.gridcard_button_p);
but addState takes an int array as first argument and a Drawable Object as sedon one (not an int as in my example).
How can I use this method in the right way?
StateListDrawable states = new StateListDrawable();
states.addState(new int[] {android.R.attr.state_pressed},
getResources().getDrawable(R.drawable.pressed));
states.addState(new int[] {android.R.attr.state_focused},
getResources().getDrawable(R.drawable.focused));
states.addState(new int[] { },
getResources().getDrawable(R.drawable.normal));
//... like this you can do for remaining
Button.setImageDrawable(states);