I have one spinner which has some font size 14dp,16dp etc. when i select value from spinner I want to change font size in text view for all activity of application at run time. IS this possible?
I refer several answer but it doesn't work for me..
I have one spinner which has some font size 14dp,16dp etc. when i select value from spinner I want to change font size in text view for all activity of application at run time. IS this possible?
I refer several answer but it doesn't work for me..
Try this code:
package com.packegename;
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);
}
}
above code is working for one activity and for single textview or other view .. use this code globally for whole application activity modify
create one class. TextFontSize.class implement class with serializable
change updateTextSize()
to.
public static void updateTextView(TextView theTextView, float theSize) {
theTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, theSize);
}
public static void updateEditText(EditText theEditText, float theSize) {
theEditText.setTextSize(TypedValue.COMPLEX_UNIT_DIP, theSize);
}
and any other Layout view just create method as above.
and use this function any activity of your application like
private void setAllFontSizeOf(float theSize) {
TextVFontsize.updateTextView(textview, theSize);
TextFontSize.updateEditText(Edittext,theSize);
}
and finally declare this method to particular activity.