0% found this document useful (0 votes)
9 views6 pages

Ex 12

The document contains Java code for an Android application that utilizes RadioButtons and RadioGroups to allow users to select options. It includes two main activities: one that shows a toast message with the selected option when a button is clicked, and another that updates a TextView based on the selected RadioButton. Additionally, XML layout files define the user interface elements for the application.

Uploaded by

Ravi Pardhi
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)
9 views6 pages

Ex 12

The document contains Java code for an Android application that utilizes RadioButtons and RadioGroups to allow users to select options. It includes two main activities: one that shows a toast message with the selected option when a button is clicked, and another that updates a TextView based on the selected RadioButton. Additionally, XML layout files define the user interface elements for the application.

Uploaded by

Ravi Pardhi
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/ 6

package com.example.

helloworld;

import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


Button button;
RadioButton genderradioButton;
RadioGroup radioGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup=(RadioGroup)findViewById(R.id.radioGroup);
}
public void onclickbuttonMethod(View v){
int selectedId = radioGroup.getCheckedRadioButtonId();
genderradioButton = (RadioButton) findViewById(selectedId);
if(selectedId==-1){
Toast.makeText(MainActivity.this,"Nothing selected",
Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(MainActivity.this,genderradioButton.getText(),
Toast.LENGTH_SHORT).show();
}

}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>

<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="center_horizontal"
android:textSize="22dp"
android:text="Single Radio Buttons" />

<!-- Default RadioButtons -->

<RadioButton
android:id="@+id/radioButton1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Radio Button 1"
android:layout_marginTop="20dp"

android:textSize="20dp" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Radio Button 2"
android:layout_marginTop="10dp"

android:textSize="20dp" />

<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_marginTop="20dp"
android:background="#B8B894" />

<TextView
android:id="@+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="center_horizontal"
android:textSize="22dp"
android:text="Radio button inside RadioGroup" />

<!-- Customized RadioButtons -->

<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioGroup">

<RadioButton
android:id="@+id/radioMale"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" Male"
android:layout_marginTop="10dp"
android:checked="false"
android:textSize="20dp" />

<RadioButton
android:id="@+id/radioFemale"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" Female"
android:layout_marginTop="20dp"
android:checked="false"

android:textSize="20dp" />
</RadioGroup>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Selected"
android:id="@+id/button"
android:onClick="onclickbuttonMethod"
android:layout_gravity="center_horizontal" />

</LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:id="@+id/text"
android:text="@string/ChoiceText" />

<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text"
android:id="@+id/myRadioGroup"
android:background="#abf234"
android:checkedButton="@+id/sound" >

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/sound"
android:text="@string/Sound" />

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/vibration"
android:text="@string/Vibration" />

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/silent"
android:text="@string/Silent" />

</RadioGroup>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/myRadioGroup"
android:layout_marginTop="10dp"
android:id="@+id/chooseBtn"
android:text="@string/Choose" />

</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">RadioGroupExample</string>
<string name="action_settings">Settings</string>
<string name="ChoiceText">Choose one of the radio buttons below</string>
<string name="Sound">Sound</string>
<string name="Vibration">Vibration</string>
<string name="Silent">Silent</string>
<string name="Choose">Choose</string>

</resources>

package com.javacodegeeks.android.radiogroupexample;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

private RadioGroup radioGroup;


private RadioButton sound, vibration, silent;
private Button button;
private TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

radioGroup = (RadioGroup) findViewById(R.id.myRadioGroup);

radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// find which radio button is selected
if(checkedId == R.id.silent) {
Toast.makeText(getApplicationContext(), "choice: Silent",
Toast.LENGTH_SHORT).show();
} else if(checkedId == R.id.sound) {
Toast.makeText(getApplicationContext(), "choice: Sound",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "choice:
Vibration",
Toast.LENGTH_SHORT).show();
}
}

});

sound = (RadioButton) findViewById(R.id.sound);


vibration = (RadioButton) findViewById(R.id.vibration);
silent = (RadioButton) findViewById(R.id.silent);
textView = (TextView) findViewById(R.id.text);

button = (Button)findViewById(R.id.chooseBtn);
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
int selectedId = radioGroup.getCheckedRadioButtonId();

// find which radioButton is checked by id


if(selectedId == sound.getId()) {
textView.setText("You chose 'Sound' option");
} else if(selectedId == vibration.getId()) {
textView.setText("You chose 'Vibration' option");
} else {
textView.setText("You chose 'Silent' option");
}
}
});
}

You might also like