Tuesday 12 February 2013

Change Font type, Style, Size and Color in TextView

Changing the Font type, Style, Size in Textview can be done via both xml and Java code..
Here i will show both methods.




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=".ChangeTextViewPropertity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="24dp"
        android:layout_marginTop="38dp"
        android:text="Text From XML"
        android:textSize="40dp"
        android:textStyle="bold"
        android:typeface="serif"
        android:textColor="#0000CD"
        
         />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="158dp"
        android:text="TextView" />

</RelativeLayout>


ChangeTextViewPropertity.java:
package com.example.font;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.Menu;
import android.widget.TextView;

public class ChangeTextViewPropertity extends Activity {
 TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv=(TextView)findViewById(R.id.textView2);
        tv.setText("Text From Java Code");
        tv.setTypeface(Typeface.SANS_SERIF);
        tv.setTextSize(50);
        tv.setTextColor(Color.RED);
               
    }   
}




Thanks for Visiting
 -- 
Regards with

R. Partha Sarathy