1

Currently, the ImageButton have set android:background="@drawable/mem_btn",

mem_btn.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" >         
        <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
            <stroke android:width="0dp" android:color="@color/black" />
            <solid android:color="@color/pressed"/>
            <padding android:left="5dp" android:top="5dp" 
                android:right="5dp" android:bottom="5dp" /> 
            <corners android:radius="14dp" /> 
        </shape>    
    </item>

    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
            <stroke android:width="0dp" android:color="@color/black" />
            <solid android:color="#3366CC"/>
            <padding android:left="5dp" android:top="5dp" 
                android:right="5dp" android:bottom="2dp" /> 
            <corners android:radius="14dp" /> 
        </shape>
    </item>

</selector>

The above works perfectly.

Question:

I would like to let the user change the background color: i.e. other colors upon users' choice, yet with the pressed_color, radius remain unchanged.

In this case, how to set the information in the xml programmatically such that the color for the non-pressed state is a variable?

Thanks!

pearmak
  • 4,979
  • 15
  • 64
  • 122
  • I think http://stackoverflow.com/questions/5940825/android-change-shape-color-in-runtime will help you. – Ranjit Aug 03 '14 at 14:43
  • thanks for your reference. I have tried that, it works for the non-pressed state, but when pressed, there was still no pressed state color...do you have any idea how could that be achieved? – pearmak Aug 03 '14 at 14:58

2 Answers2

2

Thanks Ranjit Pati for the reference, such that I can further researched on right track and found out StateListDrawable, and the following works perfectly:

public void set_buttons(int t, int color_idd) 
{
    ShapeDrawable activeDrawable = new ShapeDrawable();
    ShapeDrawable inactiveDrawable = new ShapeDrawable();

    // The corners are ordered top-left, top-right, bottom-right, bottom-left. // For each corner, the array contains 2 values, [X_radius, Y_radius]

    float[] radii = new float[8];
    for (int i = 0; i <= 7; i++) 
    {
        radii[i] =  (int) getResources().getDimension(R.dimen.footer_corners);  
    }
    inactiveDrawable.setShape(new RoundRectShape(radii, null, null));
    inactiveDrawable.getPaint().setColor(color_idd);

    activeDrawable.setShape(new RoundRectShape(radii, null, null));
    activeDrawable.getPaint().setColor( (Color.parseColor ("#008B00")));

    StateListDrawable states = new StateListDrawable();
    states.addState(new int[] {-android.R.attr.state_enabled}, inactiveDrawable);
    states.addState(new int[] {android.R.attr.state_pressed}, activeDrawable);
    btns[t].setBackground(states);
}
pearmak
  • 4,979
  • 15
  • 64
  • 122
0

if I got your main problem point correctly, this custom button solve your problem.

public class customButton extends Button{
        int dColor, pColor;
        public customButton(Context context, int defaultColor, final int pressedColor) {
            super(context);
            dColor = defaultColor;
            pColor = pressedColor;
            this.setBackgroundDrawable(getShape(defaultColor));

            this.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    v.setBackgroundDrawable(getShape(pressedColor));

                }
            });
        }

        //A method that return a single color shape with radius corner
        private Drawable getShape(int color){
            GradientDrawable gradientDrawable = new GradientDrawable(GradientDrawable.Orientation.TL_BR, new int[] { color,
                    color, color}); 
            gradientDrawable.setGradientType(GradientDrawable.RECTANGLE);
            float[] f = {1,1,1,1,1,1,1,1}; //set the radius as you like
            gradientDrawable.setCornerRadii(f);

            return gradientDrawable;
        }

        //A method that change the default and pressed color of the button
        public void changeColor(int defaultColor, int pressedColor){
            this.setBackgroundDrawable(getShape(defaultColor));
            pColor = pressedColor;
        }

    }
Babak-Na
  • 104
  • 1
  • 1
  • 8