Bernard Bernard - 2 months ago 24
Android Question

Let the user rename a button onlongclick event and save it

I have 80 buttons which the user must be able to rename with a long click event and save the new button text. When the user closes the app it must remain the same text and must NOT go back to default text. This is what i have for the first button. How will i do this so that it know which button is long pressed and to rename that. (This coding doesn't save it when i go out)

public class MainActivity extends AppCompatActivity {

private Button btn1 , btn2 , btn3 , btn4;
private EditText edt;
private String btnID;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn3 = (Button) findViewById(R.id.btn3);
btn4 = (Button) findViewById(R.id.btn4);

SharedPreferences prefs = getSharedPreferences("btnID", Context.MODE_PRIVATE);
Map<String,?> keys = prefs.getAll();
for(Map.Entry<String,?> entry : keys.entrySet()){
Button button = (Button) findViewById(Integer.parseInt(entry.getKey()));
button.setText(entry.getValue().toString());
}

btn1.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
showDialog(v.getId());
return true;
}
});


}

private void showDialog(View v) {
//here you have your button
final Button currentButton = (Button)v;

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Rename to?");
View view = LayoutInflater.from(this).inflate(R.layout.dialog_view, null);
final EditText edit_dialog = (EditText)view.findViewById(R.id.edit_dialog);

edit_dialog.setText(currentButton.getText().toString());
builder.setView(view);
builder.setPositiveButton("confirm", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
currentButton.setText(edit_dialog.getText().toString());
SharedPreferences prefs = getSharedPreferences("btnID", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(String.valueOf(currentButton.getId()), currentButton.getText().toString());
editor.apply();
}


});
builder.show();
}

Answer

You have to pass also the id of the button, for example:

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

    btn1 = (Button) findViewById(R.id.btn1);
    btn2 = (Button) findViewById(R.id.btn2);
    btn3 = (Button) findViewById(R.id.btn3);
    btn4 = (Button) findViewById(R.id.btn4);

    //HERE you load the button texts from SharedPrefs or SQLite and you set the text to the buttons from ids

    btn1.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
                showDialog("", v.getId());
            return true;
        }
    });


}

private void showDialog(String str, int btnId) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Rename to?");
    View view = LayoutInflater.from(this).inflate(R.layout.dialog_view, null);
    final EditText edit_dialog = (EditText) view.findViewById(R.id.edit_dialog);
    edit_dialog.setText(str);
    builder.setView(view);
    builder.setPositiveButton("confirm", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            btn1.setText(edit_dialog.getText().toString());
            //HERE you save the text and the relative id.
            //you can use both SharedPrefs or SQLite
        }
    });
    builder.show();
}

Check comments to see when to save/load datas

How to use a single onClickListener

You have to customize the onClick way to retrieve the id of the button clicked. You can save in sharedPreferences

private void showDialog(View v) {
    //here you have your button
    Button currentButton = (Button)v;

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Rename to?");
    View view = LayoutInflater.from(this).inflate(R.layout.dialog_view, null);
    final EditText edit_dialog = (EditText)view.findViewById(R.id.edit_dialog);

    edit_dialog.setText(currentButton.getText().toString());
    builder.setView(view);
    builder.setPositiveButton("confirm", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            currentButton.setText(edit_dialog.getText().toString());
            SharedPreferences prefs = this.getSharedPreferences("sharedPrefName", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = prefs.edit();
            editor.putString(String.valueOf(currentButton.getId), currentButton.getText().toString());
            editor.apply();
        }
    });
    builder.show();
}

and you can call this method using in xml the onclick:

<Button
  android:id="@+id\myId"
  android:layout-width="wrap-content"
  android:layout-height="wrap-content"
  android:onclick="showDialog"
  />

in this way you will be able to use a single method for each button.

PS:

You have to add in onCreatesomething for the inizialization of the buttons:

SharedPreferences prefs = this.getSharedPreferences("sharedPrefName", Context.MODE_PRIVATE);
Map<String,?> keys = prefs.getAll();
for(Map.Entry<String,?> entry : keys.entrySet()){
  Button button = findViewById(Integer.Parse(entry.getHey()));
  button.setText(entry.getValue().toString());
}

Sorry if it might has any error but I wrote it by hands so I had no compiler.

Hope this helps