Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a Dialog with a search box?

I have a main activity with a list and a method that receives a keyword to make a search in the database.

I want to implement an AlertDialog with a search box that should appear on the main activity when I click a button through an action listener in this form

OnClickListener searchListener = new OnClickListener() {

    public void onClick(View v) {
        // ...
    }
};

This dialog should have a simply EditText box and two buttons (Search and Cancel), and have to pass the String picked by the EditText to a search method in the form:

public void searchWord(String keyword){
    // ....search in DB
    // ...updateListGUIWithNewValues()
}

This method is in the main activity to get the new list values so the activity can update the GUI list.

I have already implemented the search method and the main activity but I don't know the right way to implement this kind of dialog.

like image 301
AndreaF Avatar asked Feb 28 '13 19:02

AndreaF


2 Answers

Here's my alert dialog code, which features an edit text. I call it within a button click listener, so it is not a separate method. I found it somewhere on this site last week, but I can't seem to find it so i can cite it.

AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setTitle("Manual Item Search");
alert.setMessage("Input Search Query");
// Set 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 result = input.getText().toString();
        //do what you want with your result
        }
    });
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
         // Canceled.
        }
    });
alert.show();
like image 83
Mike Avatar answered Sep 21 '22 11:09

Mike


Hi try this tutorial to creating a Search dialog using alert dialog Click here

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

  alert.setTitle("Search Box");
  alert.setMessage("Message");

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

 alert.setPositiveButton("Search", new DialogInterface.OnClickListener() {
 public void onClick(DialogInterface dialog, int whichButton) {
  String value = input.getText();
 // Do something with value!
   }
  });
  alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface dialog, int whichButton) {
  // Canceled.
  }
 });

  alert.show();
like image 35
androidgeek Avatar answered Sep 22 '22 11:09

androidgeek