Monday, April 14, 2014

The RadioButton Control

RadioButton

The easiest and common method of taking user input in android is RadioButton. “android.widget.RadioButton class is used to render radio button and these RadioButton are generally grouped "android.widget.RadioGroup". Whenever one RadioButton is selected by the user the other RadioButton is automatically disabled.


  •  In Layout create a .xml and paste this code. In layout we have created three Buttons within Radio group. So whenever we press the first RadioButton red color and a message will be displayed in the TextBox and the same thing will happen for other Buttons also.
  

1:  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
2:    xmlns:tools="http://schemas.android.com/tools"  
3:    android:layout_width="match_parent"  
4:    android:layout_height="match_parent"  
5:    android:paddingBottom="@dimen/activity_vertical_margin"  
6:    android:paddingLeft="@dimen/activity_horizontal_margin"  
7:    android:paddingRight="@dimen/activity_horizontal_margin"  
8:    android:paddingTop="@dimen/activity_vertical_margin"  
9:    android:background="#dfbfc0"  
10:    tools:context=".MainActivity" >  
11:    <RadioGroup  
12:      android:id="@+id/radiogroup"  
13:      android:layout_width="wrap_content"  
14:      android:layout_height="wrap_content" >  
15:    <RadioButton  
16:      android:id="@+id/radioButton2"  
17:      android:layout_width="wrap_content"  
18:      android:layout_height="wrap_content"  
19:      android:layout_below="@+id/radioButton1"  
20:      android:layout_centerHorizontal="true"  
21:      android:textColor="#037e00"  
22:      android:text="greenbutton"   
23:      android:onClick="onRadioButtonClicked"/>  
24:    <RadioButton  
25:      android:id="@+id/radioButton3"  
26:      android:layout_width="wrap_content"  
27:      android:layout_height="wrap_content"  
28:      android:layout_marginLeft="36dp"  
29:      android:layout_marginTop="15dp"  
30:      android:onClick="onRadioButtonClicked"  
31:      android:text="Blue button"  
32:      android:textColor="#00037e" />  
33:    <RadioButton  
34:      android:id="@+id/radioButton1"  
35:      android:layout_width="wrap_content"  
36:      android:layout_height="wrap_content"  
37:      android:layout_marginRight="22dp"  
38:      android:layout_marginTop="18dp"  
39:      android:onClick="onRadioButtonClicked"  
40:      android:text="Red button"  
41:      android:textColor="#7e0004" />  
42:    </RadioGroup>  
43:    <TextView  
44:      android:id="@+id/textView1"  
45:      android:layout_width="wrap_content"  
46:      android:layout_height="wrap_content"  
47:      android:layout_centerInParent="true"  
48:      android:layout_marginTop="142dp"  
49:      android:text="text"  
50:      android:textAppearance="?android:attr/textAppearanceLarge" />  
51:  </RelativeLayout>  


 

  • Now In the scr directory paste the java code.
 
1:  public class MainActivity extends Activity {  
2:   TextView tv;  
3:   RadioGroup rg;  
4:   RadioButton rb1, rb2, rb3;  
5:    @Override  
6:    protected void onCreate(Bundle savedInstanceState) {  
7:      super.onCreate(savedInstanceState);  
8:      setContentView(R.layout.activity_main);  
9:      tv = (TextView)findViewById(R.id.textView1);  
10:      }  
11:      public void onRadioButtonClicked (View view) {  
12:        // Is the button now checked?  
13:        boolean checked = ((RadioButton) view).isChecked();  
14:        // Check which radio button was clicked  
15:        switch(view.getId()) {  
16:   // If first RadioButton is clicked then display text "Red color" and set Background color  
17:        // to Red  
18:          case R.id.radioButton1:  
19:            if (checked)  
20:              // RED BUTTON  
21:              tv.setText("RED COLOR");  
22:              tv.setBackgroundColor(Color.parseColor("#7e0004"));    
23:            break;  
24:   // If second RadioButton is clicked then display text "Green color" and set Background  
25:        //color toGreen  
26:          case R.id.radioButton2:  
27:            if (checked)  
28:              // Green Button  
29:              tv.setText("Green COLOR");  
30:              tv.setBackgroundColor(Color.parseColor("#037e00"));  
31:            break;  
32:  // If Third RadioButton is clicked then display text "Blue color" and set Background color  
33:        // to Blue  
34:          case R.id.radioButton3:  
35:            if (checked)  
36:              // Blue Button  
37:              tv.setText("Blue COLOR");  
38:              tv.setBackgroundColor(Color.parseColor("#00037e"));  
39:            break;  
40:        }  
41:      }  
42:      }  


The Link for the code - https://www.dropbox.com/s/hml9o7v894ge364/radiobutton.zip


In The Next post we will discuss about the different layouts.

No comments:

Post a Comment