android radio group example
in this Tutorial, today Learn android radio group example follow the example android radio group example.
1.Open Android Studio And Click New Project:
2.Click Empty Activity and click Next:
3.Rename Application Name and package Name after Click Finsih.
1.Radio_Activity.Java:
package com.akash.myapplication.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;
import com.akash.myapplication.R;
public class Radio_Activity 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_radio_);
textout = (TextView) findViewById(R.id.tvChange);
textin = (EditText) findViewById(R.id.edittext1);
styleG = (RadioGroup) findViewById(R.id.rg11);
styleG.setOnCheckedChangeListener((OnCheckedChangeListener) this);
gravityG = (RadioGroup) findViewById(R.id.rg21);
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.rba:
textout.setGravity(Gravity.LEFT);
break;
case R.id.rbb:
textout.setGravity(Gravity.RIGHT);
break;
case R.id.rbc:
textout.setGravity(Gravity.CENTER);
break;
case R.id.rbd:
textout.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL),
Typeface.NORMAL);
break;
case R.id.rbe:
textout.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD),
Typeface.BOLD);
break;
case R.id.rbf:
textout.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC),
Typeface.ITALIC);
break;
default:
break;
}
}
}
2.activity_radio_.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">
<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="Text Style"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/tvchange"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_horizontal"
android:paddingRight="40px"
android:text="Text Change"
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/rg11"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<RadioButton
android:id="@+id/rba"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A" />
<RadioButton
android:id="@+id/rbb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B" />
<RadioButton
android:id="@+id/rbc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C" />
</RadioGroup>
<RadioGroup
android:id="@+id/rg21"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:paddingLeft="70px" >
<RadioButton
android:id="@+id/rbd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="D" />
<RadioButton
android:id="@+id/rbe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="E" />
<RadioButton
android:id="@+id/rbf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="F" />
</RadioGroup>
</LinearLayout>
<TextView
android:id="@+id/tvChange"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="40px"
android:text="Press Button"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/bGenerate"
android:layout_width="308dp"
android:layout_height="wrap_content"
android:text="Click" />
</LinearLayout>
3.AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.akash.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".radiogroup.Radio_Activity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
4.strings.xml:
<resources>
<string name="app_name">android radiogroup example</string>
</resources>
5.colors.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#6200EE</color>
<color name="colorPrimaryDark">#3700B3</color>
<color name="colorAccent">#03DAC5</color>
</resources>
No comments:
Post a Comment