RadioButton
This Android UI component is used to select single selection among multiple choices. Example for RadioButton is given as below. in that we are able to select only either Male/Female.
Step1> create a project with the name of RadioButtonEx
Step2> Give Class name as MainActivity.java , and Give layout xml name as activity.xml
Step 3> write the below code in activity.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" android:background="#FFFFFF">
<TableRow android:layout_height="0dp"
android:layout_weight="0.15"
android:layout_width="match_parent">
<TextView android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="0.5"
android:text="Gender"
android:textSize="30dp" android:gravity="center|bottom"
android:textColor="#000000"/>
</TableRow>
<TableRow
android:layout_height="0dp"
android:layout_weight="0.6"
android:layout_width="match_parent">
<RadioButton
android:id="@+id/rdMale"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="0.5"
android:text="Male"
android:textSize="25sp"
android:textColor="#000000"
/>
<RadioButton
android:id="@+id/rdFemale"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="0.5"
android:text="Female"
android:textSize="25sp"
android:textColor="#000000"
/>
</TableRow>
</TableLayout>
Step4> Write the below code MainActivity.java
package com.example.spinnerex;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
final RadioButton rdMale=(RadioButton)findViewById(R.id.rdMale);
final RadioButton rdFemale=(RadioButton)findViewById(R.id.rdFemale);
// Click event for RadioButton is given below.
rdMale.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
rdFemale.setChecked(false);
}
});
rdFemale.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
rdMale.setChecked(false);
}
});
}
}
OutPut:
This Android UI component is used to select single selection among multiple choices. Example for RadioButton is given as below. in that we are able to select only either Male/Female.
Step1> create a project with the name of RadioButtonEx
Step2> Give Class name as MainActivity.java , and Give layout xml name as activity.xml
Step 3> write the below code in activity.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" android:background="#FFFFFF">
<TableRow android:layout_height="0dp"
android:layout_weight="0.15"
android:layout_width="match_parent">
<TextView android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="0.5"
android:text="Gender"
android:textSize="30dp" android:gravity="center|bottom"
android:textColor="#000000"/>
</TableRow>
<TableRow
android:layout_height="0dp"
android:layout_weight="0.6"
android:layout_width="match_parent">
<RadioButton
android:id="@+id/rdMale"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="0.5"
android:text="Male"
android:textSize="25sp"
android:textColor="#000000"
/>
<RadioButton
android:id="@+id/rdFemale"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="0.5"
android:text="Female"
android:textSize="25sp"
android:textColor="#000000"
/>
</TableRow>
</TableLayout>
Step4> Write the below code MainActivity.java
package com.example.spinnerex;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
final RadioButton rdMale=(RadioButton)findViewById(R.id.rdMale);
final RadioButton rdFemale=(RadioButton)findViewById(R.id.rdFemale);
// Click event for RadioButton is given below.
rdMale.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
rdFemale.setChecked(false);
}
});
rdFemale.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
rdMale.setChecked(false);
}
});
}
}
OutPut:


No comments:
Post a Comment