0

I set the Button Text trough editText and a long Button Click. Afterwards the editText is set to an emptyString again. How do i save the Button name in SharedPreferances (as a String i guess) and read it when i restart the app?

package com.example.myapplication;

import ...

public class MainActivity<button1> extends AppCompatActivity {
    Button button
    EditText editText1
   



    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = findViewById(R.id.button3);
        button.setOnClickListener(v -> opensecondone());
        button.setOnLongClickListener(v -> naming());
        editText1 = (EditText) findViewById(R.id.tv_textView);

    }



    public boolean naming() {
        button = this.findViewById(R.id.button3);
        button.setText(editText1.getText().toString());
        editText1.setText(" ");
        return true;
   }

    public void opensecondone() {
        Intent intent = new Intent(this, firmaeins.class);
        startActivity(intent);
   }
}
  • 1
    https://stackoverflow.com/questions/23024831/android-shared-preferences-for-creating-one-time-activity-example Check this – Anas Mehar Sep 21 '21 at 10:04

1 Answers1

0

first you store your name in naming function

  SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putString("yourMessageKey", message);
    editor.commit();

then you get the value inside your oncreate of activity :

SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
String message = sharedPref.getString("yourMessageKey", "");
Arvin Rezaei
  • 818
  • 10
  • 25