user2456977
user2456977

Reputation: 3964

Button won't change background on click

I have three Buttons. I want the button to change color on click, and then if I click on the button again it goes back to the default color.

Problem: If I click on the first one, it will change color. However if I click on the second one, it will not change color. But if I click on that second Button an additional time it will finally change color. Does anyone know why the second Button won't change color on the first click?

Note: After debugging I noticed that the is_clicked is set to 1 after I click on the first Button. So when I click on the second Button, it thinks it has already changed color and sets it back to default. Does anyone know how I can properly set the is_clicked flag? Or is there something else I should do?

 int is_clicked = 0;


 public void ButtonOnClick(View v) {

            switch (v.getId()) {
              case R.id.button1:
                  if (is_clicked == 1){
                      button_list.get(0).getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x00000000));;
                      is_clicked--;
                      break;
                  }
                  if (is_clicked == 0){
                      button_list.get(0).getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0xFF00FF00));
                      is_clicked++;
                      break;
                  }
                      break;
              case R.id.button2:
                  if (is_clicked == 1){
                      button_list.get(1).getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x00000000));;
                      is_clicked--;
                      break;
                  }
                  if (is_clicked == 0){
                      button_list.get(1).getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0xFF00FF00));
                      is_clicked++;
                      break;
                  }
                      break;
              case R.id.button3:
                  if (is_clicked == 1){
                      button_list.get(2).getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x00000000));;
                      is_clicked--;
                      break;
                  }
                  if (is_clicked == 0){
                      button_list.get(2).getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0xFF00FF00));
                      is_clicked++;
                      break;
                  }
                      break;
               }
           }

Upvotes: 0

Views: 631

Answers (2)

M. Reza Nasirloo
M. Reza Nasirloo

Reputation: 16614

Its just a simple button

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

    <item android:drawable="@drawable/normal" android:state_enabled="true" android:state_focused="true"/>
    <item android:drawable="@drawable/pressed" android:state_enabled="true" android:state_pressed="true"/>
    <item android:drawable="@android:color/white"/>

</selector>

Set this as your button's background

Edits: The last one is default, there are more state also, like selected and disable, try to play with them, you can make lots of functionality with selectors.

This one is a nice button. with stroke and disable state.

<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_pressed="true" >
    <shape>
      <solid
          android:color="#00aeef" />
      <stroke
          android:width="1dp"
          android:color="#0090bf" />
      <corners
          android:radius="6dp" />
      <padding
          android:left="10dp"
          android:top="10dp"
          android:right="10dp"
          android:bottom="10dp" />
    </shape>
<!--     #ef4444  #992f2f -->
</item>
  <item android:state_enabled="false">
      <shape>
      <solid
          android:color="#174574"/>
      <stroke
          android:width="1dp"
          android:color="#12375D" />
      <corners
          android:radius="6dp" />
      <padding
          android:left="10dp"
          android:top="10dp"
          android:right="10dp"
          android:bottom="10dp" />
    </shape>
  </item>
<!--   Default -->
  <item>
    <shape>
      <gradient
          android:startColor="#3399FF"
          android:endColor="#2E8AE6"
          android:angle="270" />
      <stroke
          android:width="1dp"
          android:color="#297CCF" />
      <corners
          android:radius="6dp" />
      <padding
          android:left="10dp"
          android:top="10dp"
          android:right="10dp"
          android:bottom="10dp" />
    </shape>
  </item>
</selector>

Upvotes: 1

Indra Kumar S
Indra Kumar S

Reputation: 2934

While I agree with Pedram.... As per your request, You use tags in xml.

Put this tag in each button

     android:tag="0"

In case

  String clicked= (String)btn1.getTag();
  If ("0".equals(clicked)) {
     //set a color for your button
      button1.setTag("1");
   } else {
    //set a color for your button
      button1.setTag("0");
    }

Upvotes: 1

Related Questions