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?
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.
:)