postheadericon How to set text style for TextView using Typeface in Android





MainActivity.java

package com.example.radiobuttondemo;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Html;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final RadioGroup rg=(RadioGroup) findViewById(R.id.radioGroup1);
        rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
final TextView tv=(TextView) findViewById(R.id.textView1);
tv.setText("www.BipinRupadiya.com");
if(R.id.rdoNormal==arg0.getCheckedRadioButtonId())
{
tv.setTypeface(tv.getTypeface(), Typeface.NORMAL);
}
if(R.id.rdoBold==arg0.getCheckedRadioButtonId())
{
tv.setTypeface(tv.getTypeface(), Typeface.BOLD);
}
if(R.id.rdoItalic==arg0.getCheckedRadioButtonId())
{
tv.setTypeface(tv.getTypeface(), Typeface.ITALIC);
}
if(R.id.rdoUnderLine==arg0.getCheckedRadioButtonId())
{
String myHtml="<u>"+tv.getText().toString()+"</u>";
tv.setText(Html.fromHtml(myHtml));
}
}
} );
    }
   
}



activity_main.xml

<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"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="28dp"
        android:text="www.BipinRupadiya.com" />

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="52dp" >

         <RadioButton
            android:id="@+id/rdoNormal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="Normal" />
       
        <RadioButton
            android:id="@+id/rdoBold"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"          
            android:text="Bold" />

        <RadioButton
            android:id="@+id/rdoItalic"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Italic" />

        <RadioButton
            android:id="@+id/rdoUnderLine"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="UnderLine" />

    </RadioGroup>

</RelativeLayout>






0 comments:

Follow by Email

Total Pageviews

© BipinRupadiya.com. Powered by Blogger.