-1

---In android application ---

after a button clicked,its background color is changed to required. as clicked again,its color will restore original color.

How to implement it?

any response,thank you!

================================================================================== Update: From network,i find a method to achieve this aim. design a drawable color in xml like this:

<drawable name="button_checked">#ffff0000</drawable>  

in activity,use below code to get Drawable object:

Resources resource = getBaseContext().getResources();
checked_drawable = resource.getDrawable(R.drawable.button_checked);

in onClick function,according to a boolean variable:

 setBackgroundDrawable(checked_mDrawable)

to set button background.

ooper
  • 1
  • 2
  • possible duplicate of [Standard Android Button with a different color](http://stackoverflow.com/questions/1521640/standard-android-button-with-a-different-color) – fvrghl Jul 18 '13 at 03:00

3 Answers3

0

In your js file put

$('#button').toggleClass('bg');

in your css, have your regular button css style

#button { background: white; }

and then add

#button.bg { background: red; }

So when they click on the button it will turn background red, if they click on it again, it will turn back to white.

Use whatever color / url you want for backgrounds.

bob
  • 859
  • 1
  • 8
  • 16
0

You just need to create a state list drawable resource like below:

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

    <item android:drawable="@drawable/btn_login_pressed" android:state_pressed="true"/>
    <item android:drawable="@drawable/btn_login_normal" android:state_pressed="false"/>

</selector>

Updated: Maybe you want the effect as RadioButton, Here is an example:

    <RadioButton
       android:id="@+id/tab_communication"
       android:layout_width="wrap_content"
       android:layout_height="40dp"
       android:layout_weight="1"
       android:background="@null"
       android:button="@null"
       android:drawableTop="@drawable/category_communication"
       android:paddingBottom="5dp"
       android:paddingTop="5dp" />

drawable/category_communication.xml:

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

    <item android:drawable="@drawable/category_communication_checked" android:state_checked="true"/>
    <item android:drawable="@drawable/category_communication_normal" android:state_checked="false"/>

 </selector>

ToggleButton is another choice.

Ivan
  • 703
  • 5
  • 9
  • i just tested your method. But what it show is not i hope. what i want is as button clicked and up,its backgound color is changed to stay color A. as i click it again,its backround color change to restore original color. – ooper Jul 18 '13 at 03:20
  • I want to know how to implement this function in general button,not RadioButton. thks! – ooper Jul 18 '13 at 03:30
  • For general button, since it has no checkable attribute, I don't know the implementation. May you can follow outcast's answer. – Ivan Jul 18 '13 at 03:35
  • [ToggleButton]http://developer.android.com/reference/android/widget/ToggleButton.html is another choice – Ivan Jul 18 '13 at 03:36
  • Thank you for your suggestion! Use ToggleButton is also good idea! – ooper Jul 18 '13 at 05:22
0

You can do it in code dynamically(I don't know how to configure it in xml).

boolean flag=true;
Button button=(Button)findViewById(R.id.button);
oncreate()
{
    button.setBackgroundResource(R.drawable.first_time_click);
    button.setOnClickListener(new OnClickListener()
    {
    public void onClick(View v)
    {
        if (flag)
        {
            button.setBackgroundResource(R.drawable.odd_time_click);        
        }else
        {
           button.setBackgroundResource(R.drawable.even_time_click);
        }
        flag=!flag;
    }
    });
}
outcast
  • 548
  • 1
  • 6
  • 20