I want to change the text size of an edit text using seek bar and every time I close the activity and reopen it the seek bar automatically increase the text size, this is the code:
package com.e_orthodoxy.orthodox_prayers;
public class WakeupActivity extends Activity {
private SharedPreferences prefs;
private SeekBar seekbar;
private EditText edittext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wakeup);
seekbar = (SeekBar) findViewById(R.id.seekBar1);
edittext = (EditText) findViewById(R.id.id_wakeup_txt);
prefs = getPreferences(MODE_PRIVATE);
float fs = prefs.getFloat("fontsize", 12);
seekbar.setProgress((int)fs);
edittext.setTextSize(seekbar.getProgress());
seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar){
prefs = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor ed = prefs.edit();
ed.putFloat("fontsize", edittext.getTextSize());
ed.commit();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar){
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser){
edittext.setTextSize(progress);
}
});
}
@Override
public void onBackPressed() {
Intent intent_wakeup_back = new Intent (WakeupActivity.this, DailyPrayersActivity.class);
startActivity(intent_wakeup_back);
finish();
}
}
replace setTextSize(float size) with setTextSize(int unit,float size) and use TypedValue.COMPLEX_UNIT_PX for the unit
float fs = prefs.getFloat("fontsize", 12);
seekbar.setProgress((int)fs);
//edittext.setTextSize(seekbar.getProgress());
edittext.setTextSize(TypedValue.COMPLEX_UNIT_PX,seekbar.getProgress());
and don't forget onProgressChanged:
//edittext.setTextSize(progress);
edittext.setTextSize(TypedValue.COMPLEX_UNIT_PX,progress);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With