0

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.

JoeCoolman
  • 512
  • 3
  • 8
  • 27

2 Answers2

1

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.

Anukool
  • 5,301
  • 8
  • 29
  • 41
  • @Grishu code is similar, thanks guys. When I try your suggestions, Eclipse spat this out in the Runtime: E/AndroidRuntime(531): FATAL EXCEPTION: main java.lang.NullPointerException at com.android.internal.app.AlertController$AlertParams.(AlertController.java‌​:742) at android.app.AlertDialog$Builder.(AlertDialog.java:273) at com.stufflist.ListStuff$1.onLongClick(ListStuff.java:82) at android.view.View.performLongClick(View.java:2503) at android.widget.TextView.performLongClick(TextView.java:7640) at android.view.View$CheckForLongPress.run(View.java:9056) – JoeCoolman Mar 05 '13 at 17:19
  • at android.os.Handler.handleCallback(Handler.java:587) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:123) at android.app.ActivityThread.main(ActivityThread.java:3683) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:507) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) at dalvik.system.NativeStart.main(Native Method) – JoeCoolman Mar 05 '13 at 17:20
  • Sorry guys, your suggestions were perfectly good. I did a mistake in my own code. Everything is right now ! Thanks. – JoeCoolman Mar 06 '13 at 03:15
  • have a look here if you want to format the edit text also : https://stackoverflow.com/a/9345820/3904109 – DragonFire Apr 24 '20 at 01:51
0

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.

GrIsHu
  • 29,068
  • 10
  • 64
  • 102