1

I want that button1,2,3 respond to a longclick, the user will be prompt to enter the text's button. Inside of onCreate I wrote:

Button botonEditable;
Context context;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    context = MainActivity.this;
    Resources r = getResources();
    String pName = getPackageName();

    for (int i=1;i<4;i++){
    botonEditable = (Button) findViewById(r.getIdentifier("button" + i, "id", pName));
    botonEditable.setOnLongClickListener(new View.OnLongClickListener() {
        public boolean onLongClick(View v) {
            final AlertDialog.Builder alert = new AlertDialog.Builder(context);
            alert.setMessage("Nueva Categoria:");
            final EditText input = new EditText(context);
            alert.setView(input);
            alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton  ) {
                    // Do something with value!
                    String newCateg = input.getText().toString();
                    botonEditable.setText(newCateg);
                }
            });

            alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    // Canceled.
                }
            });        
            alert.show();                   
            return true;
        }
    });
    }
 }

When this is tested, the 3 buttons responds accordingly to show an alert message, but when I enter text and click ok, the text is changed only in button3, no matter which button has been longclicked :( What It's wrong and how fix it in a simple way?

JoeCoolman
  • 512
  • 3
  • 8
  • 27
  • Out of scope : Why are you storing a strong reference to the activity's context ? I believe this is bad practice, remove the attribute **Context context** and use **this** when needed. – Leeeeeeelo Mar 11 '13 at 07:42

5 Answers5

1

Try this

button.setOnLongClickListener(new longClcik());


class longClcik implements OnLongClickListener {

    public boolean onLongClick(View v) {
        return false;

    }
}
DjHacktorReborn
  • 2,908
  • 2
  • 20
  • 29
1

Try this.. Replace the following code

 public void onClick(DialogInterface dialog, int whichButton  ) {
                    // Do something with value!
                    String newCateg = input.getText().toString();
                    botonEditable.setText(newCateg);
                }

with

 public void onClick(DialogInterface dialog, int whichButton  ) {
                    // Do something with value!
                    String newCateg = input.getText().toString();
                    ((Button)v).setText(newCateg);
                }
Praful Bhatnagar
  • 7,425
  • 2
  • 36
  • 44
1

You should use the View passed to the OnLongClickListener. This will give you the currently clicked view and update it's value so instead do this

botonEditable.setOnLongClickListener(new View.OnLongClickListener() {
    public boolean onLongClick(View v) {
        final AlertDialog.Builder alert = new AlertDialog.Builder(context);
        alert.setMessage("Nueva Categoria:");
        final EditText input = new EditText(context);
        alert.setView(input);
        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton  ) {
                // Do something with value!
                String newCateg = input.getText().toString();
                ((Button)v).setText(newCateg); //<-- see the botonEditable changed to v
            }
        });

        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                // Canceled.
            }
        });        
        alert.show();                   
        return true;
    }
});
the-ginger-geek
  • 7,041
  • 4
  • 27
  • 45
  • Doesn't work. I tried your suggestions guys, @Praful Bhatnagar, but Eclipse complains "Cannot refer to a non-final variable v inside an inner class defined in a different method". That's why I've defined botonEditable as a global variable, this is seen by all methods, am I correct?. I'am just an intuitive android developer. I know I must read more about Java. – JoeCoolman Mar 11 '13 at 18:22
0

I found the solution, in my code I just replaced the line

public boolean onLongClick(View v) {....

by this one

public boolean onLongClick(final View v) {....

plus your suggestions:

((Button)v).setText(newCateg.toUpperCase());

Now, everything is alright. Thankyou guys, your help left me at millimeters of the solution.
:)

JoeCoolman
  • 512
  • 3
  • 8
  • 27
0

I do it in this way:

  1. First u have to implement to your class View.OnLongClickListener
  2. Second overide method onLongClick

    @Override
    public boolean onLongClick(View v) {
    onCheckboxLongClicked(v);
    return true;
    }

  3. Create method onCheckBoxLongClicked which is void. Or u can implement ur swith inside onLongClick

    switch(view.getId()) {
    case R.id.cbDigits:
    Toast.makeText(getActivity(), "Message", Toast.LENGTH_SHORT).show();
    break;
    .
    .
    .
    }

Sorry but I still dont know how to format code to get indent...

Marurban
  • 1,575
  • 1
  • 9
  • 9
  • Thanks Maruban, I will take your suggestion in consideration for future works. Now I am dealing with Blender and VBA Excel, so several Android projects are now delayed but I will take them soon. – JoeCoolman Nov 04 '13 at 01:04