-1

in my xml file i have this button :

<Button
android:id="@+id/button_8"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/Bf"
android:background="@drawable/button_purple" 
android:layout_weight="1"
android:textColor="#ffffff"
android:onClick="action"            
/>

And in my activity i have this method for the button :

public void action (View v)
{
    s = "m";
    changeCouleur("blue");
    v.setPressed(true);
}

When i pressed the button it's working but the button don't stay pressed.

I don't use an image this is what i use for the color :

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <shape>
            <solid
                android:color="#449def" />
            <stroke
                android:width="1dp"
                android:color="#2f6699" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
    <item android:state_focused="true" >
        <shape>
            <solid
                android:color="#449def" />
            <stroke
                android:width="1dp"
                android:color="#2f6699" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
    <item>
        <shape>
            <gradient
                android:startColor="#449def"
                android:endColor="#2f6699"
                android:angle="270" />
            <stroke
                android:width="1dp"
                android:color="#2f6699" />
            <corners
                android:radius="4dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
</selector>

I tried toggle button but that doesn't fit with what i trying to do. Thanks in advance if you noticed something wrong.

user1527152
  • 946
  • 1
  • 13
  • 37
  • You could try this: http://stackoverflow.com/questions/4747311/how-can-i-keep-one-button-as-pressed-after-click-on-it – Michael Jul 28 '12 at 09:06

1 Answers1

0

maybe you can change the button to an image then you can create animation when the button pressed

this is an example

Bitmap source0 = BitmapFactory.decodeResource(getResources(), R.drawable.top);
Bitmap source1 = BitmapFactory.decodeResource(getResources(), R.drawable.top_a);
Bitmap source2 = BitmapFactory.decodeResource(getResources(), R.drawable.top_b);
Bitmap source3 = BitmapFactory.decodeResource(getResources(), R.drawable.top_c);
v.startAnimation(AnimationUtils.loadAnimation(mainContext, R.anim.image_click));
toptop.setImageBitmap(source0);
top_a.setImageBitmap(processingBitmap_Brightness(source1));
top_b.setImageBitmap(source2);
top_c.setImageBitmap(source3);

place this code on a ontouch event then v.startanimation is for animation when the image touched then we set the selected imageview to be highlighted with set the brightness brighter than the other image

this is the anim.image_click code

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

<alpha
android:fromAlpha = "1.0"
android:toAlpha = "0.5"
android:duration = "300">
</alpha>
<scale
android:fromXScale = "1"
android:toXScale = "0.9" 
android:fromYScale = "1"
android:toYScale = "0.9" 
android:pivotX="50%"
android:pivotY="50%" 
android:duration = "50">
</scale>
</set>

and this is is the procssingBitmap_Brightness (for processing the image brightness)

private Bitmap processingBitmap_Brightness(Bitmap src){
            Bitmap dest = Bitmap.createBitmap(
              src.getWidth(), src.getHeight(), src.getConfig());

            for(int x = 0; x < src.getWidth(); x++){
             for(int y = 0; y < src.getHeight(); y++){
              int pixelColor = src.getPixel(x, y);
              int pixelAlpha = Color.alpha(pixelColor);

              int pixelRed = Color.red(pixelColor) + brightnessValue;
              int pixelGreen = Color.green(pixelColor) + brightnessValue;
              int pixelBlue = Color.blue(pixelColor) + brightnessValue;

              if(pixelRed > 255){
               pixelRed = 255;
              }else if(pixelRed < 0){
               pixelRed = 0;
              }

              if(pixelGreen > 255){
               pixelGreen = 255;
              }else if(pixelGreen < 0){
               pixelGreen = 0;
              }

              if(pixelBlue > 255){
               pixelBlue = 255;
              }else if(pixelBlue < 0){
               pixelBlue = 0;
              }

              int newPixel = Color.argb(
                pixelAlpha, pixelRed, pixelGreen, pixelBlue);

              dest.setPixel(x, y, newPixel);

             }
            }
            return dest;
           }
Niko Adrianus Yuwono
  • 11,012
  • 8
  • 42
  • 64
  • Thanks to tried to help me but this solution don't match. I think i will do for each color another button for exemple "button_purple_pressed" and the normal state of this one will be the state pressed of "button_purple". – user1527152 Jul 28 '12 at 10:49