Open top menu


Example-How to used RadioButton Control in android?


In this blog explain how to used RadioGroup in android. the Radiogroup is a radio button control, the control check which button is checked then previous button is automatic Uncheck with same group.


activity_main.xml
...................................................................................................................................................................
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
android:layout_width="fill_parent"    
android:layout_height="fill_parent"   
android:orientation="vertical"    
android:background="#ffffff"     >
   
 <ImageView            
android:layout_width="wrap_content"            
android:layout_height="wrap_content"           
 android:id="@+id/imageView"            
android:layout_gravity="center"            
android:src="@drawable/my_heder_img"></ImageView>

    
<RadioGroup        
android:layout_width="wrap_content"        
android:layout_height="wrap_content"        
android:layout_marginTop="40dp"        
android:id="@+id/control"       
android:layout_gravity="center_vertical">
<RadioButton        
android:layout_width="wrap_content"        
android:layout_height="wrap_content"        
android:text="Lion"       
 android:id="@+id/radioButton"        />
<RadioButton        
android:layout_width="wrap_content"       
 android:layout_height="wrap_content"       
 android:text="Tigger"        
android:id="@+id/radioButton2"         />
<RadioButton       
android:layout_width="wrap_content"        
android:layout_height="wrap_content"        
android:text="Leopard"        
android:id="@+id/radioButton3"         />
    
<RadioButton        
android:layout_width="wrap_content"        
android:layout_height="wrap_content"        
android:text="Zabra"        
android:id="@+id/radioButton4"        />

<RadioButton        
android:layout_width="wrap_content"        
android:layout_height="wrap_content"        
android:text="Bear"        
android:id="@+id/radioButton5"        />
</RadioGroup>

<Button        
android:layout_width="wrap_content"        
android:layout_height="wrap_content"        
android:text="Select Animal Name"        
android:id="@+id/button"        
android:layout_marginTop="30dp"        
android:layout_gravity="center"/>


</LinearLayout>



...................................................................................................................................................................


MainActivity.java
...................................................................................................................................................................
public class MainActivity extends Activity  {
    private RadioGroup radioSexGroup;
    private RadioButton radioSexButton;
    private Button btnDisplay;

    @Override    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_main);

        radioSexGroup=(RadioGroup)findViewById(R.id.control);

        btnDisplay=(Button)findViewById(R.id.button);

        btnDisplay.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                int selectedId=radioSexGroup.getCheckedRadioButtonId();
                radioSexButton=(RadioButton)findViewById(selectedId);
                Toast.makeText(MainActivity.this,radioSexButton.getText(),Toast.LENGTH_SHORT).show();
            }
        });

    }
}



...................................................................................................................................................................





0 comments