May 19, 2011

Change TextSize in run-time

Change TextSize in run-time

package com.AndroidTextSize;

import android.app.Activity;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class AndroidTextSize extends Activity {

String[] optUnit = {"COMPLEX_UNIT_DIP",
"COMPLEX_UNIT_IN",
"COMPLEX_UNIT_MM",
"COMPLEX_UNIT_PT",
"COMPLEX_UNIT_PX",
"COMPLEX_UNIT_SP"};

int[] valueUnit = { TypedValue.COMPLEX_UNIT_DIP,
TypedValue.COMPLEX_UNIT_IN,
TypedValue.COMPLEX_UNIT_MM,
TypedValue.COMPLEX_UNIT_PT,
TypedValue.COMPLEX_UNIT_PX,
TypedValue.COMPLEX_UNIT_SP};

String[] optSize ={"0.05", "0.1", "0.25", "0.5", "1", "4", "8", "10", "14", "16", "20", "30"};
float[] valueSize = {0.05f, 0.1f, 0.25f, 0.5f, 1f, 4f, 8f, 10f, 14f, 16f, 20f, 30f};

Spinner selUnit, selSize;
TextView textOut;

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

selUnit = (Spinner)findViewById(R.id.selunit);
selSize = (Spinner)findViewById(R.id.selsize);
textOut = (TextView)findViewById(R.id.textout);

ArrayAdapter<String> adapterUnit = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, optUnit);
adapterUnit.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
selUnit.setAdapter(adapterUnit);
selUnit.setOnItemSelectedListener(selUnitOnItemSelectedListener);

ArrayAdapter<String> adapterSize = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, optSize);
adapterSize.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
selSize.setAdapter(adapterSize);
selSize.setOnItemSelectedListener(selSizeOnItemSelectedListener);
}

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

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

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

}};

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

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

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

}};

private void updateTextSize(){
int unit = valueUnit[selUnit.getSelectedItemPosition()];
String strUnit = optUnit[selUnit.getSelectedItemPosition()];
float size = valueSize[selSize.getSelectedItemPosition()];
String strSize = optSize[selSize.getSelectedItemPosition()];

textOut.setTextSize(unit, size);
textOut.setText(strUnit + " : " + strSize);
}
}


<?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/selunit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Spinner
android:id="@+id/selsize"
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

Infolinks In Text Ads