Create Android Application:
- File >> New >> Android Application
- Enter Project name: RadioGroupApp
- Package: com.ambilpursunil.newapp
- Keep other default selections, click Next until you reach Finish

1.Simply Copy and Past the Code which is display below in the MainActivity.java
package com.ambilpursunil.newapp;
import android.app.Activity;
import android.os.Bundle;
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, vibrate, slient;
private Button chooseBtn;
TextView tv;
@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) {
if (checkedId == R.id.slient)
Toast.makeText(getApplicationContext(), "Slient
Mode Is Activity", Toast.LENGTH_SHORT).show();
}
if (checkedId == R.id.sound) {
Toast.makeText(getApplicationContext(), "Sound
Mode Is Activity", Toast.LENGTH_SHORT).show();
}
if (checkedId == R.id.vibrate) {
Toast.makeText(getApplicationContext(),"Vibrate
Mode Is Activity", Toast.LENGTH_SHORT).show();
}
}
});
sound =
(RadioButton) findViewById(R.id.sound);
slient =
(RadioButton) findViewById(R.id.slient);
vibrate =
(RadioButton) findViewById(R.id.vibrate);
tv =
(TextView) findViewById(R.id.ChooseText);
chooseBtn =
(Button) findViewById(R.id.chooseBtn);
chooseBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int selectedID = radioGroup.getCheckedRadioButtonId();
if (selectedID == sound.getId())
{
tv.setText("you
choose sound");
}
if (selectedID == slient.getId())
{
tv.setText("you
choose slient");
}
if (selectedID == vibrate.getId())
{
tv.setText("you
choose vibrate");
}
}
});
}
}
<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:id="@+id/ChooseText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="2dp"
android:text="@string/ChooseText"
android:layout_marginTop="16dp"/>
<RadioGroup
android:id="@+id/myRadioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/ChooseText"
android:checkedButton="@+id/sound"
android:background="#E0E0E0">
<RadioButton
android:id="@+id/sound"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sound" />
<RadioButton
android:id="@+id/vibrate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/vibrate"/>
<RadioButton
android:id="@+id/slient"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/slient" />
</RadioGroup>
<Button
android:id="@+id/chooseBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/myRadioGroup"
android:layout_marginTop="10dp"
android:text="@string/button" />
</RelativeLayout>
3.Copy and past the code for manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ambilpursunil.newapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
When our application launch you will see a group of radio buttons once you click on any of the radio button then you get a Toast message.
Please Send Your Comments To ambilpura.sunil@gmail.com
Stay Tune For Next Tutorial... Use Font and colors For TextView Text In Android:
No comments:
Post a Comment