May 19, 2011

Change typeface and style programmatically using Java Code

To change typeface and style of TextView, use the code:

textview.setTypeface(typeface, style);

where typeface can be:
  • Typeface.DEFAULT
  • Typeface.DEFAULT_BOLD
  • Typeface.MONOSPACE
  • Typeface.SANS_SERIF
  • Typeface.SERIF


style can be:
  • Typeface.BOLD
  • Typeface.BOLD_ITALIC
  • Typeface.ITALIC
  • Typeface.NORMAL


Change typeface and style programmatically using Java Code

package com.AndroidTypeface;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class AndroidTypeface extends Activity {

String[] optTypeface = {"DEFAULT",
"DEFAULT_BOLD",
"MONOSPACE",
"SANS_SERIF",
"SERIF"};

Typeface[] tfTypeface = { Typeface.DEFAULT,
Typeface.DEFAULT_BOLD,
Typeface.MONOSPACE,
Typeface.SANS_SERIF,
Typeface.SERIF};

String[] optStyle ={"BOLD",
"BOLD_ITALIC",
"ITALIC",
"NORMAL"};
int[] intStyle = { Typeface.BOLD,
Typeface.BOLD_ITALIC,
Typeface.ITALIC,
Typeface.NORMAL};

Spinner selTypeface, selStyle;
TextView textOut;

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

selTypeface = (Spinner)findViewById(R.id.seltypeface);
selStyle = (Spinner)findViewById(R.id.selstyle);
textOut = (TextView)findViewById(R.id.textout);

ArrayAdapter<String> adapterTypeface = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, optTypeface);
adapterTypeface.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
selTypeface.setAdapter(adapterTypeface);
selTypeface.setOnItemSelectedListener(selTypefaceOnItemSelectedListener);

ArrayAdapter<String> adapterStyle = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, optStyle);
adapterStyle.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
selStyle.setAdapter(adapterStyle);
selStyle.setOnItemSelectedListener(selStyleOnItemSelectedListener);
}

private Spinner.OnItemSelectedListener selTypefaceOnItemSelectedListener
= new Spinner.OnItemSelectedListener(){

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
updateFront();
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}};

private Spinner.OnItemSelectedListener selStyleOnItemSelectedListener
= new Spinner.OnItemSelectedListener(){

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
updateFront();
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}};

private void updateFront(){
Typeface typeface = tfTypeface[selTypeface.getSelectedItemPosition()];
String familyName = optTypeface[selTypeface.getSelectedItemPosition()];
int style = intStyle[selStyle.getSelectedItemPosition()];
String stringStyle = optStyle[selStyle.getSelectedItemPosition()];

textOut.setTypeface(typeface, style);
textOut.setText(familyName + " : " + stringStyle);
}
}


<?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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Spinner
android:id="@+id/seltypeface"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Spinner
android:id="@+id/selstyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/textout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

No comments:

Post a Comment

Post a Comment

Infolinks In Text Ads