Recipe Setting and Changing Text Attributes

This recipe changes the color of displayed text when a button is clicked. It can easily be extended to change the font size or style instead, as discussed at the end of this recipe.

The main layout is simply a TextView and Button arranged in a vertical LinearLayout, as shown in Listing 4.8.The text is identified as mod_text and displays the string changed_text defined in the strings.xml file, as shown in Listing 4.9.The button is identified as change and displays the string button_text from the strings XML file.

Listing 4.8 res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"

android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:text="@string/changed_text" android:textSize="48sp" android:id="@+id/mod_text" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:text="@string/button_text" android:textSize="48sp" android:id="@+id/change" android:layout_width="wrap_content" android:layout_height="wrap_content" />

</LinearLayout>

Listing 4.9 res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?> <resources>

<string name="app_name">ChangeFont</string> <string name="changed_text">Rainbow Connection</string> <string name="button_text">Press to change the font color</string> </resources>

The activity shown in Listing 4.10 utilizes the main.xml layout and identifies the TextView handle to the mod_text ID. Then the button's OnClickListener is overriden to set the text color described in Table 4.3.The possible color resources are defined in a global colors.xml file, as shown in Listing 4.11.As defined, the colors are red, green, and blue, but they are named functionally as the start, mid, and last. This provides an easy way to change the colors later without needing to change their handle names.

Listing 4.10 src/com/cookbook/change_font/ChangeFont.java package com.cookbook.change_font;

import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView;

public class ChangeFont extends Activity { TextView tv;

private int color_vals[]={R.color.start, R.color.mid, R.color.last};

int idx=0;

/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv = (TextView) findViewById(R.id.mod_text);

Button changeFont = (Button) findViewById(R.id.change); changeFont.setOnClickListener(new View.OnClickListener() { public void onClick(View view) {

tv.setTextColor(getResources().getColor(color_vals[idx])); idx = (idx+1)%3;

Listing 4.11 res/values/colors.xml

<?xml version="1.0" encoding="utf-8"?> <resources>

<color name="start">#f00</color> <color name="mid">#0f0</color> <color name="last">#00f</color> </resources>

This recipe can be modified to change the text size (or text style) easily. For example, the color_vals[] would change to size_vals[] and point to the R.dimen resources:

private int size_vals[]={R.dimen.small, R.dimen.medium, R.dimen.large}; tv.setTextSize(getResources().getDimension(size_vals[idx]));

Also, instead of the colors.xml file, the dimens.xml file would be used, as shown in Listing 4.12.

Listing 4.12 Example of Similar Usage for the dimens.xml File

<?xml version="1.0" encoding="utf-8"?> <resources>

<dimen name="small">12sp</dimen> <dimen name="medium">24sp</dimen> <dimen name="large">48sp</dimen> </resources>

To use this recipe to change the text string instead, the color_vals[] would change to text_vals[] and point to the R.string resources as follows:

private int text_vals[]={R.string.first_text,

R.string.second_text, R.string.third_text}; tv.setText(getBaseContext().getString(text_vals[idx]));

The strings.xml file would then be used, as shown in Listing 4.13. Listing 4.13 Example of Similar Usage for the strings.xml File

<?xml version="1.0" encoding="utf-8"?>

<resources>

<string

name=

'app name">ChangeFont</string>

<string

name=

'changed text">Rainbow Connection</string>

<string

name=

'button text">Press To Change the Font Color</string>

<string

name=

'first text">First</string>

<string

name=

second_text">Second</string>

<string

name=

third_text">Third</string>

</resources>

</resources>

Character Building Thought Power

Character Building Thought Power

Character-Building Thought Power by Ralph Waldo Trine. Ralph draws a distinct line between bad and good habits. In this book, every effort is made by the writer to explain what comprises good habits and why every one needs it early in life. It draws the conclusion that habits nurtured in early life concretize into impulses in future for the good or bad of the subject.

Get My Free Ebook


Post a comment