0

I'm trying to develop code where user selection stays saved even when the app is closed and reopened. I have read that for small volumes of data sharedpreferences are a perfect idea. I can get my head around as i have got a menu with pop up to choose "level of club" (int). How can i add sharedpreferences to my button that already changes state of ImageViev but won't save it after app is closed. Any help is much appreaciated.

//Java

   public class MainActivity extends AppCompatActivity  implements   
PopupMenu.OnMenuItemClickListener  {                       
  ImageView img;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    Button btn = (Button) findViewById(R.id.btnShow);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            PopupMenu popup = new PopupMenu(MainActivity.this, v, Gravity.CENTER
            );
            popup.setOnMenuItemClickListener(MainActivity.this);
            popup.inflate(R.menu.popup_menu);
            popup.show();
        }
    });

    img = (ImageView) findViewById(R.id.imageView1);









}
@Override
public boolean onMenuItemClick(MenuItem item) {
    Toast.makeText(this, "Level: " +item.getTitle(), Toast.LENGTH_SHORT).show();
    switch (item.getItemId()) {
        case R.id.Level_One:
            img.setImageResource(R.drawable.home2);
            return true;
        case R.id.Level_Two:
            img.setImageResource(R.drawable.home3);
            return true;

            // do your code


        case R.id.print_item:
            // do your code
            return true;
        case R.id.share_item:
            // do your code
            return true;
        case R.id.bookmark_item:
            // do your code
            return true;
        default:
            return false;
    }
}
}


  //xml
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

   <ImageView
    android:id="@+id/imageView1"
    android:layout_width="60dp"
    android:layout_height="60dp"
  android:layout_marginTop="20dp"
    android:layout_marginStart="20dp"

    android:src="@drawable/home1" />


  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btnShow"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:background="@drawable/home2"

        android:layout_marginTop="400dp"
        android:layout_marginStart="20dp"/>
    </LinearLayout>

   </RelativeLayout>

  //popup_menu.xml

 <item
    android:title="Level 1" />
<item android:id="@+id/Level_One"
    android:title="Level 2" />
<item android:id="@+id/Level_Two"
    android:title="Level 3" />
<item android:id="@+id/print_item"
    android:title="Level 4" />
  <item android:id="@+id/share_item"
    android:title="Level 5" />
  <item android:id="@+id/bookmark_item"
    android:title="Level 6" />
  </menu>
a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
Jacob Nero
  • 1
  • 1
  • 2
  • 2
    Look at this example: https://developer.android.com/training/data-storage/shared-preferences#WriteSharedPreference. – codebod Feb 16 '21 at 22:18
  • Check this link hopefully you will get solution https://stackoverflow.com/questions/23024831/android-shared-preferences-for-creating-one-time-activity-example – Muhammad Haroon Feb 17 '21 at 06:52

0 Answers0