This example will show you how to create a simple android TextView, set the text to bold, center it, change the font color and attach an OnClickListener event.
Start by adding the main activity logic.
File: MainActivity.java
package com.ahotbrew.textview; import android.graphics.Color; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.TextView; public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void changeTextColor (View view) { TextView textView = (TextView) view.findViewById(R.id.textview_onclick); textView.setTextColor(android.graphics.Color.RED); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
File: activity_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" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context="com.ahotbrew.textview.MainActivity"> <TextView android:text="@string/textview_simple" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textview_simple" android:layout_marginBottom="30dp" android:textSize="25dp"/> <TextView android:text="@string/textview_bold" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textview_bold" android:layout_below="@+id/textview_simple" android:layout_marginBottom="30dp" android:textSize="25dp" android:textStyle="bold"/> <TextView android:text="@string/textview_center" android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/textview_center" android:layout_below="@+id/textview_bold" android:layout_marginBottom="30dp" android:textSize="25dp" android:gravity="center"/> <TextView android:text="@string/textview_onclick" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textview_onclick" android:layout_below="@+id/textview_center" android:textSize="25dp" android:onClick="changeTextColor" android:clickable="true"/> </RelativeLayout>
File: strings.xml
<resources> <string name="app_name">TextView</string> <string name="title_activity_main">Ahotbrew.com TextView</string> <string name="textview_simple">Simple TextView</string> <string name="textview_bold">Bold TextView</string> <string name="textview_center">Centered TextView</string> <string name="textview_onclick">OnClick TextView</string> <string name="action_settings">Settings</string> </resources>
Android TextView Color
Here you can see how we change the android TextView color programmatically to “Red”.
public void changeTextColor (View view) { TextView textView = (TextView) view.findViewById(R.id.textview_onclick); textView.setTextColor(android.graphics.Color.RED); }
Simple Android Label
This is a simple textview label.
<TextView android:text="@string/textview_simple" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textview_simple" android:layout_marginBottom="30dp" android:textSize="25dp"/>
Android TextView Bold
You can set the font to be bold with this extra line: android:textStyle=”bold”.
<TextView android:text="@string/textview_bold" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textview_bold" android:layout_below="@+id/textview_simple" android:layout_marginBottom="30dp" android:textSize="25dp" android:textStyle="bold"/>
Android TextView Center
In order to center the text, set the layout_width to “fill_parent” and the gravity to “center”.
<TextView android:text="@string/textview_center" android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/textview_center" android:layout_below="@+id/textview_bold" android:layout_marginBottom="30dp" android:textSize="25dp" android:gravity="center"/>
Android TextView OnClick Listener
Make sure clickable is set to “true” and the OnClick property has a valid method name. You can find the method in the MainActivity file line 20.
<TextView android:text="@string/textview_onclick" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textview_onclick" android:layout_below="@+id/textview_center" android:textSize="25dp" android:onClick="changeTextColor" android:clickable="true"/>
Sit back, take a sip from your hot brew and run your project.
Don’t forget to join or newsletter and get free android tutorials to enjoy with your hot brew.