Wednesday, December 2, 2015

Android TextView Set value | Android TextView Set Color | Android TextView Set Font Size programmatically

Simple TextView  Example code for beginners -


Your XML Layout-

<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"
android:background="@android:color/white"
tools:context="com.androidhub4you.textview.MainActivity" >

<TextView
android:id="@+id/textViewName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="my blog"
android:textColor="@android:color/black"
android:textSize="20dp" />

</RelativeLayout>


Your Activity-
package com.smr.textview;

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

public class MainActivity extends Activity {
private TextView textViewName;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get the reference of textView from your layout
textViewName = (TextView) findViewById(R.id.textViewName);
/**
* you can override xml values inside your java code too
*/
// setting the text
textViewName.setText("androidhub4you.com");
// setting the color
textViewName.setTextColor(Color.GREEN);
// setting the font size
textViewName.setTextSize(25.0f);

}

}

Post your comment for more query .

No comments:

Post a Comment