-1

by seekbar I resized textview, is it possible to save resized textview with sharedpreferences, and when reopen activity show resized textview?

MainActivity.java

public class MainActivity extends AppCompatActivity {

TextView txt;
SeekBar skb;
Button btn;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    //SharedPreferences ??





    txt = (TextView) findViewById(R.id.txt);

    skb = (SeekBar) findViewById(R.id.skb);
    skb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            android.view.ViewGroup.LayoutParams layoutParams = txt.getLayoutParams();
            layoutParams.height = progress;
            layoutParams.width = progress;
            txt.setLayoutParams(layoutParams);

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });


    btn = (Button) findViewById(R.id.btn);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            //?????

        }
    });
}

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.gela.myapplication111.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:background="@color/colorAccent"
        android:id="@+id/txt"
        android:gravity="center"
        android:textStyle="bold" />

    <SeekBar
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:id="@+id/skb"
        android:layout_marginBottom="64dp"
        android:max="250"
        android:indeterminate="false"
        android:layout_above="@+id/btn"
        android:layout_centerHorizontal="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="SAVE"
        android:id="@+id/btn"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="23dp" />
</RelativeLayout>
  • have you googled about sharedPreferences? It´s one of the easiest stuff in android... – Opiatefuchs Nov 29 '16 at 19:17
  • 1
    [here you have a lot of examples](http://stackoverflow.com/questions/3851560/how-to-use-sharedpreferences). oh and [here too](http://stackoverflow.com/questions/23024831/android-shared-preferences-example). have you even [googled antything about](https://www.google.pl/search?q=shared+preferences+stackoverflow)? duplicate – snachmsm Nov 29 '16 at 19:17

2 Answers2

1

Simply save the new height and width

        preferenceEditor = sharedpreferences.edit();
    preferenceEditor.putString("height",progress);
    preferenceEditor.putString("widht",progress);
    preferenceEditor.commit();

while reading ... in onCreate()

       int width = sharedpreferences.getInt("width",10);
    int height = sharedpreferences.getInt("height",10);
Muhammad Jamal
  • 442
  • 4
  • 21
1

here you have some example

for storing value:

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        android.view.ViewGroup.LayoutParams layoutParams = txt.getLayoutParams();
        layoutParams.height = progress;
        layoutParams.width = progress;
        txt.setLayoutParams(layoutParams);
        SharedPreferences preferences = 
            PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = sharedpreferences.edit()
        editor.putInt("progress", progress).commit();
    }

for retrieving and setting:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    txt = (TextView) findViewById(R.id.txt);

    SharedPreferences preferences = 
            PreferenceManager.getDefaultSharedPreferences(context);
    int progress = preferences.getInt("progress");

    android.view.ViewGroup.LayoutParams layoutParams = txt.getLayoutParams();
    layoutParams.height = progress;
    layoutParams.width = progress;
    txt.setLayoutParams(layoutParams);

        ...
snachmsm
  • 17,866
  • 3
  • 32
  • 74