noobEinstien
noobEinstien

Reputation: 3283

MultiAutoCompleteTextView not showing dropdown in AlertDialog

I am using MultiAutoCompleteTextView for showing suggestions while I am typing something. I put MultiAutoCompleteTextView in an AlertDialog. Now It is not showing dropdown.

My xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity=""
    android:orientation="vertical"
    android:padding="20dp">

    <TextView
        android:id="@+id/tv_query_statement"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:textColor="@color/colorTextWhite"/>

    <MultiAutoCompleteTextView
        android:id="@+id/et_query"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:focusableInTouchMode="true"
        android:padding="10dp"
        android:textColor="@color/colorTextWhite"/>

    <android.support.v7.widget.AppCompatButton
        android:id="@+id/btn_submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:padding="10dp"
        android:text="Submit"
        android:textColor="@color/colorTextWhite"/>
</LinearLayout>

Inside activity

private void setupQueryDialog() {
    final AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
    View view = LayoutInflater.from(getContext()).inflate(R.layout.layout_query_dialog, null);
    AppCompatButton btnsubmit = (AppCompatButton) view.findViewById(R.id.btn_submit);
    final MultiAutoCompleteTextView mQueryEditor = (MultiAutoCompleteTextView) view.findViewById(R.id.et_query);
    TextView mQueryStatement = (TextView) view.findViewById(R.id.tv_query_statement);

    String[] commands = QueryHelper.getAllSqlCommands(mTableDetailSource);
    ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_spinner_item, commands);

    mQueryEditor.setThreshold(1);
    mQueryEditor.setAdapter(adapter);
    mQueryEditor.showDropDown();

    mQueryEditor.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            /*if (editable.toString().length() > 0)
                mQueryEditor.showDropDown();*/
        }
    });
    dialog.setView(view);
    final Dialog d = dialog.create();
    btnsubmit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            submitQuery(mQueryEditor.getText().toString());
            d.dismiss();
        }
    });
    dialog.show();
}

Upvotes: 1

Views: 748

Answers (1)

Mable John
Mable John

Reputation: 5248

You may please change your implementation

Consider the thing that, when you want to show the drop-down. By the detailed investigation I found that some issue in your code.

Even though the question title says so I assume that the drop-down is actually visible (if you don't get any exception like WindowManager$BadTokenException).

But the problem is that the dialog come over the drop-down i.e, drop-down is hidden beneath the dialog and it's on your activity.

Lets check it out.

You just comment one line code below mQueryEditor.setAdapter(adapter);

String[] words = new String[]{
"ADD", "DELETE", "UPDATE", "DELETE FROM", "SELECT"};

ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, words);

mQueryEditor.setThreshold(1);
mQueryEditor.setAdapter(adapter);
mQueryEditor.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
//mQueryEditor.showDropDown();  // Need to comment this line

And add a touch listener, it will help you to show the drop-down when the user touch on it.

mQueryEditor.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        mQueryEditor.showDropDown();
        mQueryEditor.requestFocus();
        return false;
    }
});

Note: By invoking this dialog from activity onCreate() may produce WindowManager$BadTokenException and then the app will crash. Just try the same after all the critical activity process has finished.

Upvotes: 1

Related Questions