Showing posts with label TextView. Show all posts
Showing posts with label TextView. Show all posts

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

Print the Text using TextView

Purpose of TextView:
                                 Normally TextView is used to display some static message. Its Similarly like Label in Java  TextView is like notice board. we can display our message to user via TextView.



We can print text from XML or Java Code. Here we are going to see both methods..
This example is based on GingerBread Version.


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

    <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="38dp"
        android:text="This Text From XML"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <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="40dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>



TextViewExample.java:


package com.example.textviewexample;

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

public class TextViewExample 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("This Text From Java Code");
        
    }
   
}







Thanks for Visiting
 --
Regards with

R. Partha Sarathy