0

I want to change the image of the button when clicked on particuar button and deselet all the other one please help.

This is my code

public void onClick(View view) {
    Object tag = ((Button) view).getTag();

    System.out.println("value o ftagagdsgsdg:::" +tag.toString());
    String strtag = tag.toString();
    System.out.println("value of strtag:::" +strtag);

    enablebutton = Integer.valueOf(tag.toString());

    System.out.println("value of enable buttons variable::" +enablebutton);
    DeselectButtons();

    ((Button)view).setEnabled(true);
    for(int a = 0;a<adapt_obj.city_id_array.length;a++){
        System.out.println("ddsgdgsdg::"+adapt_obj.city_code_array[a]);
        System.out.println("value o ftagagdsgsdg:inside for loop::" +tag.toString());

        if(tag.toString() == adapt_obj.city_code_array[a]){
            ((Button)view).setFocusable(true);
            //clicked = false;

            // Calling process to fetch the data of city 
            selectedcityidclicked(tag.toString(), view);
            ((Button)view).setSelected(true);

            /*Drawable dr = getResources().getDrawable(R.drawable.location_btn_active_1);// active
            Bitmap bitmap = ((BitmapDrawable) dr).getBitmap();
            // Scale it to 50 x 50
            // Set your new, scaled drawable "d"
            ((Button)view).setBackgroundDrawable(new BitmapDrawable(Bitmap.createScaledBitmap(bitmap, 10, 10, true)));*/
           // ((Button)view).invalidate();
        }
        else if(tag.toString() != adapt_obj.city_code_array[a]){

            /*((Button)view).setFocusable(true);
            selectedcityidclicked(tag.toString(), view);
            Drawable dr2 = getResources().getDrawable(R.drawable.location_btn_active_1);// inactive
            Bitmap bitmap2 = ((BitmapDrawable) dr2).getBitmap();
            // Scale it to 50 x 50
            Drawable d2 = new BitmapDrawable(Bitmap.createScaledBitmap(bitmap2, 10, 10, true));
            // Set your new, scaled drawable "d"
            ((Button)view).setBackgroundDrawable(d2);
            ((Button)view).invalidate();
            clicked = false;*/
        }
    }

    /*else if(tag.toString() != adapt_obj.city_code_array[a]){
         ((Button)view).setFocusable(true);
        Drawable dr2 = getResources().getDrawable(R.drawable.location_btn_inactive_1);//instead of inactive
        Bitmap bitmap2 = ((BitmapDrawable) dr2).getBitmap();
        // Scale it to 50 x 50
        Drawable d2 = new BitmapDrawable(Bitmap.createScaledBitmap(bitmap2, 10, 10, true));
        // Set your new, scaled drawable "d"
        ((Button)view).setBackgroundDrawable(d2);
        ((Button)view).invalidate();
    }
    else {
        Drawable dr2 = getResources().getDrawable(R.drawable.location_btn_active_1);//white
        Bitmap bitmap2 = ((BitmapDrawable) dr2).getBitmap();
        // Scale it to 50 x 50
        Drawable d2 = new BitmapDrawable(Bitmap.createScaledBitmap(bitmap2, 10, 10, true));
        // Set your new, scaled drawable "d"
        ((Button)view).setBackgroundDrawable(d2);
        ((Button)view).invalidate();
    }*/

    /*((Button) view).setText("*");
    //selectedcityidclicked(tag.toString());
    ((Button) view).setEnabled(false);*/
}
private void DeselectButtons() {
    // TODO Auto-generated method stub
    for(int x=0; x<adapt_obj.city_code_array.length;x++){ 
        System.out.println("value of enable tag::" +enablebutton);
        System.out.println("valuie dofdsf xxx:::" +x);
        if (enablebutton!= x)
            //What should i do here as i had to take the un clicked button as set selected as false //
            // ((Button)view).setSelected(false);
            //this.findViewById(i).setSelected(false);
            //this.findViewById(i).setSelected(false);
    }
}
private void selectedcityidclicked(String cityval, View view) {
     cityidvalue = "&city_code="+cityval;
     System.out.println("value of cityidvalue::" +cityidvalue);

     new MyAsyncTask(view).execute();
}
9
  • you need to create an selector xml for your buttons Commented Apr 18, 2012 at 9:24
  • its a dynamic button whose number of values comes from the api and i am displaying it in the screen it works all fine just the problem is of changing image runtime Commented Apr 18, 2012 at 9:27
  • yes thats why i am saying use selector xml just create it and then use as button background Commented Apr 18, 2012 at 9:28
  • can u please help me by showing in my code please help Commented Apr 18, 2012 at 9:33
  • stackoverflow.com/questions/2059454/… please see this link its just exactly same but in deselectbuttons method after if (enablebutton!= x) what should i do ? Commented Apr 18, 2012 at 9:36

2 Answers 2

1

You can use this selector as drawable of your button that will change its drawable when you click it or make it selected.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" android:drawable="@drawable/home_hover"></item>
    <item android:state_selected="true" android:drawable="@drawable/home_selected"></item>
    <item android:drawable="@drawable/home"></item>
</selector>

For making other buttons unselected you need to loop them for e.g. put all your buttons in Linear or Relative layout than after fetching reference of that relative layout loop through its children and see for their tags. If they equal tag of buttons you wanna unselect than call setSelected(false) to that button.

private void DeselectButtons() {
    LinearLayout layout = findViewById(R.id.parent_ofbuttons);
    for(int i=0; i<layout.getChildCount(); i++) {
         Button btn = (Button)layout.getChildAt(i);  
         if(btn.getTag().toString().equals("100")) {
              btn.setSelected(false);
         }
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

stackoverflow.com/questions/2059454/… please see this link its just exactly same but in deselectbuttons method after if (enablebutton!= x) what should i do ?
just set tags of all of your buttons to 100 by setTag method of button
yes that's true but see my code i dont want to again make the linearlayout and deselect the button but just deselect that button which is not select from number of buttons please help i m struggling since last 2 days
These buttons must be added inside some parent layout. Just loop through that parent layout and see for tag of buttons.
0

create an selector xml which is normally an xml

there in items set your images for android:state_pressedpressed="true" and android:state_pressed="false"

my_button_background.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" android:drawable="@drawable/button_pressed_image"></item>
    <item android:state_pressed="false" android:drawable="@drawable/button_normal_image"></item>
</selector>

then save this file in your drawable then set your button background to this file

as myButton.setBackgroundResourceId(R.drawable.my_button_background);

-----------------------------

edited

button1.setOnClickListener(this);
button2.setOnClickListener(this);

now in your override onCLick

@Override
OnClick(View v)
{
   if(v.getId() == R.id.button1_id)
   {
      button1.setBackgroundResource(R.drawable.anyImage1);
      button2.setBackgroundResource(R.drawable.anyImage2);
   }
   else
   {
 if(v.getId() == R.id.button1_id)
   {
      button1.setBackgroundResource(R.drawable.anyImage2);
      button2.setBackgroundResource(R.drawable.anyImage1);
   }
   }
}

dont forget to declare button1 and button2 member variables

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.