Reputation: 12375
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.
Upvotes: 5
Views: 14268
Reputation: 28799
build this xml in your layout
folder
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2" >
<Button
android:id="@+id/search"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Search" />
<Button
android:id="@+id/cancel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel" />
</LinearLayout>
</LinearLayout>
This class to handle the dialog
class dialog extends Dialog {
private StackQuestionActivity activity;
private Button search, cancel;
private EditText text;
private dialog thisDialog;
public dialog(StackQuestionActivity context) {
super(context);
// TODO Auto-generated constructor stub
this.activity = context;
this.thisDialog = this;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.dialoge);
getWindow().setLayout(android.view.ViewGroup.LayoutParams.FILL_PARENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
initalize();
}
private void initalize() {
// TODO Auto-generated method stub
text = (EditText) findViewById(R.id.text);
search = (Button) findViewById(R.id.search);
cancel = (Button) findViewById(R.id.cancel);
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
thisDialog.cancel();
}
});
search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String textString = text.getText().toString();
activity.searchWord(textString);
}
});
}
}
this is your main activity , call the dialog class when the button is pressed:
package com.example.question;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class StackQuestionActivity extends Activity {
/** Called when the activity is first created. */
private Button press;
private StackQuestionActivity activity;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.activity = this;
setContentView(R.layout.main);
press = (Button) findViewById(R.id.button);
press.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog yourDialog = new dialog(activity);
yourDialog.show();
}
});
}
public void searchWord(String textString) {
// TODO Auto-generated method stub
}
}
this is the result
Upvotes: 2
Reputation: 3480
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();
Upvotes: 2
Reputation: 413
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();
Upvotes: 6