Yockie
Yockie

Reputation: 35

Combine AlertDialog with AutoCompleteText

So what I'm trying to do is to have an AlertDialog where the user can add their own groceries to the database. I want the program to give suggestions while the user is typing. I want to combine an AlertDialog with an AutoCompleteTextView. I followed this guide for the AutoComplete section https://developer.android.com/reference/android/widget/AutoCompleteTextView.html, but I can't seem to get it right.

In main:

public void add(View caller) {

        MaterialAlertDialogBuilder mBuilder = new MaterialAlertDialogBuilder(mainActivity.this);
        mBuilder.setTitle(R.string.dialog_title);
        mBuilder.setView(R.layout.add_items_dialog);
        mBuilder.setMessage("Enter a grocery");

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_dropdown_item_1line, GROCERIES);
        AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.name_text_field);
        textView.setAdapter(adapter);
        mBuilder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int which) {

            }
        });
        mBuilder.setNegativeButton(R.string.dismiss_label, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });
        AlertDialog mDialog = mBuilder.create();
        mDialog.show();

add_items_dialog.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/name_text_field"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/dp_16"
        android:layout_marginEnd="@dimen/dp_16"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:hint="@string/enter_text"
        android:textColorHint="@color/colorDarkGreen"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/et_input"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:inputType="text"
            android:singleLine="true" />

    </com.google.android.material.textfield.TextInputLayout>


</androidx.constraintlayout.widget.ConstraintLayout>

Error at the line textView.setAdapter(adapter):

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.AutoCompleteTextView.setAdapter(android.widget.ListAdapter)' on a null object reference

I guess that it has something to do with setting the adapter to the wrong textview but it can also be something entirely different. Any suggestions?

Upvotes: 0

Views: 96

Answers (1)

Shay Kin
Shay Kin

Reputation: 2657

You need to change mbuilder.addView(R.layout.add_itemes_dialog) by

View view =LayoutInflater.from(this).inflate(R.layout.add_itemes_dialog, null);
builder.setView(view);
// and to find your AutoCompleteTextView 
AutoCompleteTextView textView = (AutoCompleteTextView) view.findViewById(R.id.et_input);

and also in your xml file change TextInputEditText by MaterialAutoCompleteTextView and it should work

Upvotes: 1

Related Questions