-1

how to do the button with ON/Off by clicking it must varies and along with that it shows Green for ON else RED. this is possible without graphics or must need graphics? any one tried like this plz help me?

Dinesh Prajapati
  • 9,274
  • 5
  • 30
  • 47
Crishnan Kandada
  • 651
  • 2
  • 9
  • 24

3 Answers3

3

ToggleButton does the same. And here is tutorial which will tell how to use this.

Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
2

You can use ToggleButton

If you with to use Button instead, you can make field boolean m_isOn; in your class, and in OnClickListener check this field and set button color (e.g. with setColorFilter()) and text accordingly.

Edit

Small example, if you really wish to avoid ToggleButton and using drawables:

@Override
public void onClick(View v) {
    m_isOn ^= true;
    ((Button)v).getBackground().setColorFilter(m_isOn ? 0xFF00FF00 : 0xFFFF0000, PorterDuff.Mode.MULTIPLY);
    ((Button)v).setText(m_isOn ? "ON" : "OFF");
}
Vladimir
  • 9,683
  • 6
  • 36
  • 57
  • yep.......im getting error like Description Resource Path Location Type Conversion to Dalvik format failed with error 1 onOffbutton Unknown Android Packaging Problem – Crishnan Kandada Nov 25 '11 at 10:08
  • something must be wrong with your project properties or imports then. try googling error description, it seems to give couple of solutions – Vladimir Nov 25 '11 at 10:34
  • here im getting error like:: Conversion to Dalvik format failed with error 1 – Crishnan Kandada Nov 28 '11 at 07:05
0

use ImageButton instead of button, and selector as image. this XML file you can put into drawable directory

 <selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_pressed="true"
         android:drawable="@drawable/button_prev_active" /> 
   <item android:drawable="@drawable/button_prev_no_active" /> 

In layout do this:

 <ImageButton android:id="@+id/btnPrev" 
        android:scaleType="fitXY" 
        android:padding="0px"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:onClick="prevClickHandler"
        android:src="@drawable/button_prev_selector"/>
Maep
  • 833
  • 1
  • 10
  • 16