Question

I want the user changes the text of a button many times he wants. To do that, he makes a long click in that button. This is the code:

@Override
public void onCreate(Bundle savedInstanceState) {

//blah blah

    final AlertDialog.Builder alert = new AlertDialog.Builder(this);

    alert.setMessage("Nueva Categoria:");

    // Seting an EditText view to get user input 
    final EditText input = new EditText(this);
    alert.setView(input);

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        Button esteBoton = (Button) findViewById(R.id.button1);
        String newCateg = input.getText().toString();
        esteBoton.setText(newCateg);
      }
    });       


    Button button = (Button) findViewById(R.id.button1);
    button.setOnLongClickListener(new View.OnLongClickListener() {
        public boolean onLongClick(View v) {
            alert.show();               
            return true;
        }
    });
}

Ok. When I run this code in device simulator of Eclipse, there's no problem if it is the first time I enter text for the button 1 in the Alert Dialog, but the application crashes if I attempt to enter code for the second time. I am not expert in Java but I think this is due by the "final" attribute for "input", I can't change its value after once determined. How can I fix it ? The code is simple and I want to keep it in that way.

Was it helpful?

Solution

Try to remove the builder part in the onCreate and move it to the onLongClickListener

Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.symptoms);
    button = (Button) findViewById(R.id.btDone);

    // final Dialog alert = builder.create();

    button.setOnLongClickListener(new View.OnLongClickListener() {
        public boolean onLongClick(View v) {

            // Declare your builder here - 
            final AlertDialog.Builder builder = new AlertDialog.Builder(
                    YOURACTIVITY.this);
            builder.setMessage("Nueva Categoria:");
            // Seting an EditText view to get user input
            final EditText input = new EditText(YOURACTIVITY.this);
            builder.setView(input);
            builder.setPositiveButton("Ok",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int whichButton) {
                            String newCateg = input.getText().toString();
                            button.setText(newCateg);
                        }
                    });

            builder.show();
            return true;
        }
    });
}

try this and see if this works.

OTHER TIPS

Try out below code:

 public class MainActivity extends Activity {
           Button button;
           Context context;
     @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    context = MainActivity.this;
    setContentView(R.layout.activity_main);
    button = (Button) findViewById(R.id.button1);
    button.setOnLongClickListener(new View.OnLongClickListener() {
        public boolean onLongClick(View v) {
            final AlertDialog.Builder alert = new AlertDialog.Builder(
                    context);
            alert.setMessage("Nueva Categoria:");
            // Seting an EditText view to get user input
            final EditText input = new EditText(context);
            alert.setView(input);
            alert.setPositiveButton("Ok",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int whichButton) {
                            String newCateg = input.getText().toString();
                            button.setText(newCateg);
                        }
                    });
            AlertDialog build = alert.create();
            build.show();
            return true;
        }
    });
 }
  }

Define your dialog inside onLongClickListener of the button. Check out the code , its working awesome now.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top