Tuesday, 12 February 2013

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

No comments:

Post a Comment