How to implement a dialogue with a search field?

I have a main action with a list and a method that gets the keyword to search the database.

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

 OnClickListener searchListener = new OnClickListener() { public void onClick(View v) { // ... } }; 

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

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

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

I have already implemented the search method and the main action, but I do not know how to properly implement this dialog.

+4
source share
3 answers

Here is my warning dialog box code in which there is edit text. I call it in the button listener, so this is not a separate method. I found it somewhere on this site last week, but I cannot find it, so I can bring 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(); 
+3
source

Hi, this tutorial is to create a search dialog using a warning dialog box. 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(); 
+2
source
Dialogue

XML

create this xml in 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> 

Dialog Class

This class is for handling dialogue

 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); } }); } } 

Your activity

this is your main action, call the dialog class when the button is clicked:

 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 CHECK

+2
source

All Articles