011 Java Android Program to Demonstrate Radio Group Application
Posted by Superadmin on December 04 2015 03:09:21
Here is source code of the Program to demonstrate a Radio Group in an Application. The program is successfully compiled and run on a Windows system using Eclipse Ide. The program output is also shown below.


package com.example.creating_radiogroup;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.renderscript.Type;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity implements OnCheckedChangeListener {

TextView textout;
EditText textin;
RadioGroup styleG, gravityG;
Button gen;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textout = (TextView) findViewById(R.id.tvChange);
textin = (EditText) findViewById(R.id.edittext1);
styleG = (RadioGroup) findViewById(R.id.rg1);
styleG.setOnCheckedChangeListener((OnCheckedChangeListener) this);
gravityG = (RadioGroup) findViewById(R.id.rg2);
gravityG.setOnCheckedChangeListener((OnCheckedChangeListener) this);// set us down @
// oncheckedChanged function
gen = (Button) findViewById(R.id.bGenerate);
gen.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
textout.setText(textin.getText().toString());
}
});

}

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch (checkedId) {
case R.id.rbLeft:
textout.setGravity(Gravity.LEFT);
break;
case R.id.rbRight:
textout.setGravity(Gravity.RIGHT);
break;
case R.id.rbCenter:
textout.setGravity(Gravity.CENTER);
break;
case R.id.rbNormal:
textout.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL),
Typeface.NORMAL);
break;
case R.id.rbBold:
textout.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD),
Typeface.BOLD);
break;
case R.id.rbitalics:
textout.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC),
Typeface.ITALIC);
break;

default:
break;
}
}
}

Xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/background1">

<EditText
android:id="@+id/edittext1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" " />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="40px"
android:paddingTop="40px"
android:weightSum="2" >

<!-- need to look at weight sum and layout_weight -->

<TextView
android:id="@+id/tvStyle"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:paddingLeft="50px"
android:text="Style"
android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
android:id="@+id/tvGravity"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_horizontal"
android:paddingRight="40px"
android:text="Gravity"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="40px"
android:weightSum="2" >

<RadioGroup
android:id="@+id/rg1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >

<RadioButton
android:id="@+id/rbNormal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Normal" />

<RadioButton
android:id="@+id/rbitalics"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Italics" />

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

<RadioGroup
android:id="@+id/rg2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:paddingLeft="70px" >

<RadioButton
android:id="@+id/rbLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Left" />

<RadioButton
android:id="@+id/rbCenter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Center" />

<RadioButton
android:id="@+id/rbRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Right" />
</RadioGroup>
</LinearLayout>

<TextView
android:id="@+id/tvChange"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="40px"
android:text="Type here text and press button"
android:textAppearance="?android:attr/textAppearanceLarge" />

<Button
android:id="@+id/bGenerate"
android:layout_width="308dp"
android:layout_height="wrap_content"
android:text="Generate" />

</LinearLayout>
AndroidManifest


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.creating_radiogroup"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.creating_radiogroup.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>