Question

I want to save my current text size by SharedPreferences, and set it back when screen orientation is changed.

I use this code, but when I change the screen orientation it makes the textSize bigger...

package com.example.zz;

import android.app.ActionBar;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends Activity {
    TextView text;
    float size;
    ActionBar actionBar;
    private final String TEXT_SIZE = "textsize";
    final String MyPref = "preference";
    SharedPreferences settings;
    Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text = (TextView) findViewById(R.id.textView1);

        settings = this.getSharedPreferences(MyPref, 0);
        text.setTextSize(settings.getFloat(TEXT_SIZE, 0));
        editor = settings.edit();
    }

    public void sizeUp(MenuItem item) {
        size = text.getTextSize();
        text.setTextSize(TypedValue.COMPLEX_UNIT_PX, (float) (size + 5));

    }

    public void sizeDown(MenuItem item) {
        size = text.getTextSize();
        text.setTextSize(TypedValue.COMPLEX_UNIT_PX, (float) (size - 5));

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.actionbar, menu);
        return true;
    }

    @Override
    protected void onStop() {
        super.onStop();

        SharedPreferences.Editor editor = settings.edit();
        editor.putFloat(TEXT_SIZE, text.getTextSize());

        editor.commit();
    }

}

Thanks.

Was it helpful?

Solution

Method getTextSize() returns text size in pixels, but setTextSize() treats the size in sp units. That's why your text looks bigger. You should scale the size before using in setTextSize(). You can do like this:

SharedPreferences.Editor editor = settings.edit();
editor.putFloat(TEXT_SIZE, text.getTextSize()/getResources().getDisplayMetrics().scaledDensity); // put text size in sp
editor.commit();

OTHER TIPS

I did't find onConfigure chnage method in this code,

You have to implement this method, and changes text size in this method.

    @Override
public void onConfigurationChanged(Configuration newConfig) {
    // TODO Auto-generated method stub
    super.onConfigurationChanged(newConfig);
    }

Try this:

@Override
    protected void onStop() {
        super.onStop();

        SharedPreferences settings = getSharedPreferences(MyPref, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putFloat(TEXT_SIZE, text.getTextSize());

        editor.commit();
    }

Try this :

  @Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    if (picUri != null) {
    // here you store string    
        outState.putString("cameraImageUri", picUri.toString());


    }


}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    if (savedInstanceState.containsKey("cameraImageUri")) {
           // here you get string again
        picUri = Uri.parse(savedInstanceState.getString("cameraImageUri"));
    }


}

Instead of doing this by code, you can use styles for this.

First, set the style of your TextView in styles.xml with nthe desired text size for portrait mode. For example:

<style name="MyTextView">
   <item name="android:textSize">18sp</item>
</style>

Then, create a folder values-land in the res folder and put a styles.xml file in this folder and create a style with the same name but a different value for text size:

<style name="MyTextView">
   <item name="android:textSize">24sp</item>
</style>

Finally, apply this style to your TextView in your layout and the text size will automaticall change according to the phones orientation. Extra benifit is that it's very easy to maintain/change the look of your views this way.

Edit: changed textsize in dp to sp

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top