Android Textview Programmatically
Android Textview Programmatically : In my previous post, I explained basics of Android Textview Control. In this post, I’ll explain how you can set properties of a Android textview control programmatically.
In your layout file, make a Textview as below :
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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="#fff" android:orientation="vertical" > <TextView android:id="@+id/txtView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="2dp" android:padding="5dp" /> </LinearLayout>
Here, my Textview control Id is txtView1. In the code file, we can use this Id to set its property as below :
txtView1.setText("Hello, I'm a simple Android Textview control."); txtView1.setTextSize(16); txtView1.setTextColor(Color.parseColor("#ff0000"));
Here’s my full java file :
TextActivity.java :
package parallelcodes.simplewidget; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.widget.TextView; public class TextActivity extends Activity { TextView txtView1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); txtView1 = (TextView) findViewById(R.id.txtView1); txtView1.setText("Hello, I'm a simple Android Textview control."); txtView1.setTextSize(16); txtView1.setTextColor(Color.parseColor("#ff0000")); } }
This is output of above code :
Please also see : Android Beginners Tutorial.