Search code examples
androidautocompletedialogandroid-dialogfragmentdialogfragment

Autocomplete textview in dialog fragment showing suggestions in android


I have a dialogfragment class like this:

public class dialogfrag extends DialogFragment
    {

        @Override

        public Dialog onCreateDialog(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            // Get the layout inflater
            LayoutInflater inflater1 = getActivity().getLayoutInflater();


            // Inflate and set the layout for the dialog
            // Pass null as the parent view because its going in the dialog layout
            builder.setView(inflater1.inflate(R.layout.from, null))
            // Add action buttons
                   .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                       @Override
                       public void onClick(DialogInterface dialog, int id) {
                           // sign in the user ...
                       }
                   })
                   .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {

                       }
                   });      
            return builder.create();


        }

    }

In its layout xml file from.xml it contains an autocomplete textview in it. This is code of the from.xml file

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">

<TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:text="From"
    android:gravity="center"
    android:background="#FFFFBB33"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<AutoCompleteTextView
    android:id="@+id/autoCompleteTextView1d"
    android:layout_width="fill_parent"
    android:layout_marginTop="15dp"
    android:layout_height="wrap_content"
    android:hint="From" >

    <requestFocus />
</AutoCompleteTextView>

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Selected Place"
    android:layout_marginTop="20dp"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:weightSum="2" >

</LinearLayout>

I'm using the following code to popup the dialog fragment upon button click. So I have given like this:

 Button from=(Button)rootView.findViewById(R.id.button4);


    from.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
             DialogFragment dialog=new dialogfrag();
                dialog.show(getFragmentManager(), "fromtag");


        }
    });

Now I need to create an object of autocomplete textview and it should show me suggestions. I have an arraylist which contains the data for the arrayadapter of autocomplete text view and I have coded like this (here actv1 is the object of the autocomplete textview):

     actv1.addTextChangedListener(new TextWatcher() {

             @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {

                 String filter = s.toString().toLowerCase();
                listItems = new ArrayList<String>();
                for (String listItem : loc) {
                    if (listItem.toLowerCase().contains(filter))
                    {
                        listItems.add(listItem);

                    }

                }
                if (listItems.size() < 1){

                Toast toast = Toast.makeText(getActivity().getApplicationContext(),
                        "No entries contain your search parameters",
                        Toast.LENGTH_SHORT);

                toast.show();

                }
                ArrayAdapter<String> adapt=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,listItems);
                actv1.setAdapter(adapt);
            //  actv1.setAdapter(new ArrayAdapter<String>(getActivity(),
                            //  android.R.layout.simple_list_item_1, listItems
                                //      .size()));
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                 ArrayAdapter<String> adapt=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,listItems);
                actv1.setAdapter(adapt);
            }
        });

But the problem is that I donot know where should I add this code properly so that when I click on the button, a dialog pops out and it contains autocompletetextview in it. While typing, it should show suggestions using the code which I have mentioned here. Can some one please help me out...


Solution

  • I think, you need to find control in your dialog custom view. I don't see find the control from view in your code. Hope for help.
    In your onCreateDialog

    LayoutInflater inflater1 = getActivity().getLayoutInflater();
    
    View view = inflater1 .inflate(R.layout.from, null);
    AutoCompleteTextView actv1  = (AutoCompleteTextView) view.findViewById(R.id.autoCompleteTextView1d);
    actv1.addTextChangedListener(new TextWatcher() {
    
    @Override
    public void onTextChanged(CharSequence s, int start, int before,
                        int count) {
             Your code .........
    }
    }
       builder.setView(view);
    
    return builder.create();