Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seek bar changing text size

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();
    }   
}
like image 780
BasILyoS Avatar asked Dec 26 '22 21:12

BasILyoS


1 Answers

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);
like image 70
moh.sukhni Avatar answered Dec 28 '22 12:12

moh.sukhni