-1

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 ;

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

    View.OnLongClickListener listener = new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            showPopup(v);
            return true;    //Nothing shows error, but this part where i tried to set a onLongClick listener. 
        }
    };

    btn1.setOnLongClickListener(listener);
    btn2.setOnLongClickListener(listener);
    btn3.setOnLongClickListener(listener);
    btn4.setOnLongClickListener(listener);

    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());
    }

}


private void showPopup(View v) {
    //here you have your button
    final Button currentButton = (Button)v;
    v.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            showPopup(v);
            return true;
        }
    });


    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();
}

}

Bernard
  • 21
  • 1
  • 9

2 Answers2

0

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

Community
  • 1
  • 1
Pier Giorgio Misley
  • 5,305
  • 4
  • 27
  • 66
  • @Bernard sorry but I don't understand what you are talking about. the edited answer solved the original problem? after we solve it we can talk about something else :) – Pier Giorgio Misley Sep 22 '16 at 13:32
  • @Bernard you can post on your answer (editing it) the photo or the code. there is a img icon on top side of the text area on SO – Pier Giorgio Misley Sep 22 '16 at 14:03
  • @Bernard sorry I misswrote before, I was meaning edit the question not the answer, my bad. delete your answer and add the code in the question :) Anyway the problem is the "sad name" because it already exists. call it "showPopup" or enyway you like instead of showDialog – Pier Giorgio Misley Sep 22 '16 at 14:20
  • @Bernard yes, you have to rename your method – Pier Giorgio Misley Sep 22 '16 at 14:44
  • @Bernard np, did you read my edited answer? check below "use a single onclicklistener" – Pier Giorgio Misley Sep 22 '16 at 14:54
  • I did have a look at that... i understand onclick is just a click, not a long one, if you see my edited Q you'll see i used it, that is all my code in my Q. I should create a longclick event where it identifies the button being longpressed and call showPopup? – Bernard Sep 22 '16 at 15:10
  • @Bernard if you wanna do it with longclick instead of click, look this SO answer: http://stackoverflow.com/a/13417824/4700782 – Pier Giorgio Misley Sep 22 '16 at 15:13
  • @Bernard guess so. an alternative might be waiting like 2 sec and check if the button is still clicked. if it is you go on. but this is a bit advanced, since you are a beginner is better if you look at simpler things and if you check a tutorial :) – Pier Giorgio Misley Sep 22 '16 at 15:29
  • @Bernard btw if the answer solved your problem, consider accepting it as the correct one so future people looking for the same problem can easily access it. any further question is still well accepted – Pier Giorgio Misley Sep 22 '16 at 15:31
  • would it be to much trouble to maybe show how to do it with a button? if so don't worry ill then just make 80 onLongClick listeners :P – Bernard Sep 22 '16 at 15:33
  • @Bernard without any compiler would be really long at the moment. try checking on google and SO, it's a really common topic, search for "implement button control". the link I posted above was the exact problem you are facing, so consider studying that code. Anyway you better get some deeper knowledge before implementing controls and doing advanced features. Check "TheNewBoston" channel on youtube, they have a really good and clear tutorial from 0 to 100 on Android development. I started with that – Pier Giorgio Misley Sep 22 '16 at 15:36
  • I edited it again... tried to do it like this... it shows no errors but app crashes. http://stackoverflow.com/questions/17452030/onlongclick-listener-for-group-of-buttons-in-android – Bernard Sep 22 '16 at 16:47
  • @Bernard you copied the question, not the answer.. remember to check answer instead of questions (questions obiouvsly won't work because it's a question for a bug report) – Pier Giorgio Misley Sep 23 '16 at 07:37
0

I also faced same problem as @Bernard. Then I did some research and stored all the ID's of buttons in an array. I just made some changes to the solution given by @Pier Giorgio Misley. Thanks a lot Misley for your help. I could solve this with your solution.

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

    buttons = new ArrayList<Button>(BUTTON_IDS.length);

    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());
    }

    for(int id : BUTTON_IDS) {
        Button button = (Button)findViewById(id);
        button.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                renameButton("",v);
                return true;
            }
        });

    }
}

private void renameButton(String str,View v) {
    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_text);

    edit_dialog.setText(str);
    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();
}

Create an array of button ID's above onCreate method,

     private List<Button> buttons;
private static final int[] BUTTON_IDS = {
        R.id.btn1,
        R.id.btn2,
        R.id.btn3,
        R.id.btn4,
};
Eric Aya
  • 69,473
  • 35
  • 181
  • 253