RadioButton – TextChange – ChangeTypeface in Android

Example of RadioButton – TextChange – ChangeTypeface

MainCLASS

package com.example.malik.test;

import android.graphics.Typeface;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

private EditText getInput;
private TextView textOut;
private Button ok;
private RadioGroup Gunit, Sunit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Gunit = (RadioGroup) findViewById(R.id.rgGravity);
Sunit = (RadioGroup) findViewById(R.id.rgStyle);
Gunit.setOnCheckedChangeListener(this);
Sunit.setOnCheckedChangeListener(this);
getInput = (EditText) findViewById(R.id.etInput);
textOut = (TextView) findViewById(R.id.tvGetInput);
ok = (Button) findViewById(R.id.bOK);

ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textOut.setText(getInput.getText());
getInput.setText(“”);
}
});
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.rbLeft:
textOut.setGravity(Gravity.LEFT);
break;

case R.id.rbCenter:
textOut.setGravity(Gravity.CENTER);
break;

case R.id.rbRight:
textOut.setGravity(Gravity.RIGHT);
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.rbItalic:
textOut.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC), Typeface.ITALIC);
break;
}
}
}

 

MainXML

<?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”
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=”com.example.malik.test.MainActivity”>
<EditText
android:id=”@+id/etInput”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”” />

<LinearLayout
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:orientation=”horizontal”>

<TextView
android:id=”@+id/textView”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Gravity”
android:layout_weight=”1″
android:gravity=”center”
/>

<TextView
android:id=”@+id/textView2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Style”
android:layout_weight=”1″
android:gravity=”center” />
</LinearLayout>

<LinearLayout
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:orientation=”horizontal”>

<RadioGroup
android:id=”@+id/rgGravity”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:orientation=”vertical”
android:layout_weight=”50″
>

<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>

<RadioGroup
android:id=”@+id/rgStyle”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:orientation=”vertical”
android:layout_weight=”50″>

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

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

<RadioButton
android:id=”@+id/rbItalic”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Italic” />
</RadioGroup>
</LinearLayout>
<TextView
android:id=”@+id/tvGetInput”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Enter something and click ok”
android:gravity=”center”
android:textSize=”25dp”
/>

<Button
android:id=”@+id/bOK”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:text=”OK” />

</LinearLayout>

 

Output :

pic

 

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.