0% found this document useful (0 votes)
11 views5 pages

Radio N Demo

The document provides an overview of the RadioButton control in Android, highlighting its functionality as a two-state button that allows only one option to be selected from a group. It includes details on attributes used in XML for defining RadioButtons and a sample layout with a RadioGroup containing multiple RadioButtons. Additionally, it features a MainActivity Java class that demonstrates how to handle user selections and display the chosen option using Toast messages.

Uploaded by

ff.ssg227
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views5 pages

Radio N Demo

The document provides an overview of the RadioButton control in Android, highlighting its functionality as a two-state button that allows only one option to be selected from a group. It includes details on attributes used in XML for defining RadioButtons and a sample layout with a RadioGroup containing multiple RadioButtons. Additionally, it features a MainActivity Java class that demonstrates how to handle user selections and display the chosen option using Toast messages.

Uploaded by

ff.ssg227
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Android RadioButton

 In android, Radio Button is a two-states button that can be either checked or


unchecked and it’s the same as CheckBox control, except that it will allow
only one option to select from the group of options.
 The user can press or click on the radio button to make it select. In
android, CheckBox control allow users to change the state of control either
Checked or Unchecked but the radio button cannot be unchecked once it is
checked.
 Generally, we can use RadioButton controls in an android application to
allow users to select only one option from the set of values.
 In android, we use radio buttons with in a RadioGroup to combine multiple
radio buttons into one group and it will make sure that users can select only
one option from the group of multiple options.

 By default, the android RadioButton will be in OFF (Unchecked) state. We


can change the default state of RadioButton by using android:checked
attribute.
 In case, if we want to change the state of RadioButton to ON (Checked),
then we need to set android:checked = “true” in our XML layout file.

Attribute Description

android:id It is used to uniquely identify the control

android:checked It is used to specify the current state of radio button

android:gravity It is used to specify how to align the text like left, right, center, top, etc.

android:text It is used to set the text for the radio button.

android:textColor It is used to change the color of text.

android:textSize It is used to specify the size of the text.

android:textStyle It is used to change the style (bold, italic, bolditalic) of text.

android:background It is used to set the background color for radio button control.

android:padding It is used to set the padding from left, right, top and bottom.
Attribute Description

android:onClick It’s the name of the method to invoke when the radio button clicked.

android:visibility It is used to control the visibility of control.

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/
android"
android:layout_width="match_parent" android:layout_height="matc
h_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:layout_marginLeft="100dp"
android:textSize="18dp"
android:text="Select Your Course"
android:textStyle="bold"
android:id="@+id/txtView"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/rdGroup"
android:layout_below="@+id/txtView">
<RadioButton
android:id="@+id/rdbJava"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginLeft="100dp"
android:text="Java"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:id="@+id/rdbPython"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginLeft="100dp"
android:text="Python"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:id="@+id/rdbAndroid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginLeft="100dp"
android:text="Android"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:id="@+id/rdbAngular"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginLeft="100dp"
android:text="AngularJS"
android:onClick="onRadioButtonClicked"/>
</RadioGroup>
<Button
android:id="@+id/getBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_below="@+id/rdGroup"
android:text="Get Course" />
</RelativeLayout>

MainActivity.java
package com.tutlane.radiobuttonexample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


RadioButton android, java, angular, python;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
android = (RadioButton)findViewById(R.id.rdbAndroid);
angular = (RadioButton)findViewById(R.id.rdbAngular);
java = (RadioButton)findViewById(R.id.rdbJava);
python = (RadioButton)findViewById(R.id.rdbPython);
Button btn = (Button)findViewById(R.id.getBtn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String result = "Selected Course: ";
result+= (android.isChecked())?"Android":
(angular.isChecked())?"AngularJS":(java.isChecked())?"Java":
(python.isChecked())?"Python":"";
Toast.makeText(getApplicationContext(), result,
Toast.LENGTH_SHORT).show();
}
});
}
public void onRadioButtonClicked(View view) {
boolean checked = ((RadioButton) view).isChecked();
String str="";
// Check which radio button was clicked
switch(view.getId()) {
case R.id.rdbAndroid:
if(checked)
str = "Android Selected";
break;
case R.id.rdbAngular:
if(checked)
str = "AngularJS Selected";
break;
case R.id.rdbJava:
if(checked)
str = "Java Selected";
break;
case R.id.rdbPython:
if(checked)
str = "Python Selected";
break;
}
Toast.makeText(getApplicationContext(), str,
Toast.LENGTH_SHORT).show();
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy