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.
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();
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With