Android studio RadioGroup Example
In this Tutorial lean Android studio RadioGroup Example Follow This Tutorial
1.MainActivity.java:
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;
}
}
}
2.activity_main.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>
No comments:
Post a Comment