2

I am working on an android project, at this point, I want to have this 'fab' in such a way that onclick, it brings a pop-up window with a text input. and two buttons. But it brings an error which I have tried to fix and lastly I thought I can find help here.

This line has an error:

AlertDialog.Builder builder = new AlertDialog.Builder(this); with this one too: final EditText input = new EditText(this);

 private String m_Text = "";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("QuickSearch:");
            //input initial
            final EditText input = new EditText(this);
            //
            input.setInputType(InputType.TYPE_CLASS_TEXT );
            builder.setView(input);
            // Set of  buttons to be displayed.
            builder.setPositiveButton("Go", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    m_Text = input.getText().toString();
                }
            });
            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });

            builder.show();


            //Search action here.
            //Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                  //  .setAction("Action", null).show();
        }
    });
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
KE Keronei
  • 323
  • 2
  • 8

2 Answers2

0

you are creating new Alertdialog instance in each time button click.. without removing the view

try this:

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


  fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            builder.setTitle("QuickSearch:");
            //input initial
            final EditText input = new EditText(MainActivity.this);
            //
            input.setInputType(InputType.TYPE_CLASS_TEXT );
            builder.setView(input);
            // Set of  buttons to be displayed.
            builder.setPositiveButton("Go", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    m_Text = input.getText().toString();
                }
            });
            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    alert.dismiss();
                }
            });

            alert.show();


            //Search action here.
            //Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                  //  .setAction("Action", null).show();
        }
    });
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
0

In your code this is referring to that onClick not the context of that Activity.

Code should be like this

AlertDialog.Builder builder = new AlertDialog.Builder(YourActivityName.this);
final EditText input = new EditText(YourActivityName.this);
Community
  • 1
  • 1
Kaushik
  • 6,150
  • 5
  • 39
  • 54