Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android AutoCompleteTextView DropDown Position

I am using following code for auto complete however, dropdown items comes above the AutoCompleteTextView. I want it to show up beneath the AutoCompleteTextView. How can I do that?

package com.example.autocomplete;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class MainActivity extends Activity {
     protected void onCreate(Bundle icicle) {
         super.onCreate(icicle);
         setContentView(R.layout.activity_main);

         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                 android.R.layout.simple_list_item_1, COUNTRIES);
         AutoCompleteTextView textView = (AutoCompleteTextView)
                 findViewById(R.id.countries_list);
         textView.setAdapter(adapter);
     }

     private static final String[] COUNTRIES = new String[] {
         "Belgium","Belgam","Bellam", "France Belgium", "Italy", "Germany", "Spain"
     };
 }

xml:

<RelativeLayout 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"
    tools:context="${relativePackage}.${activityClass}" >

    <AutoCompleteTextView
        android:id="@+id/countries_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="100dp"
        android:dropDownHeight="100dp"
        android:ems="10"
        android:text="AutoCompleteTextView" >

        <requestFocus />
    </AutoCompleteTextView>

</RelativeLayout>
like image 412
Figen Güngör Avatar asked Oct 16 '14 09:10

Figen Güngör


People also ask

How to create AutoCompleteTextView field in xml in android?

In android, we can create an AutoCompleteTextView control in two ways either manually in an XML file or create it in the Activity file programmatically. First we create a new project by following the below steps: Click on File, then New => New Project. After that include the Kotlin support and click on next.

What is Android Completionhint attribute in AutoCompleteTextView?

This defines the hint view displayed in the drop down menu. This defines the number of characters that the user must type before completion suggestions are displayed in a drop down menu. This is the View to anchor the auto-complete dropdown to.

How do I set autocomplete text on Android?

If you want to get suggestions , when you type in an editable text field , you can do this via AutoCompleteTextView. It provides suggestions automatically when the user is typing. The list of suggestions is displayed in a drop down menu from which the user can choose an item to replace the content of the edit box with.


2 Answers

Looks like this must work:

android:dropDownHeight="100dp"

Founded here: https://stackoverflow.com/a/7648028/1723525

Edit:

I tried with a new Activity and the suggestions ara shown below. Maybe you have to change the dropDownHeight to 150dp because you have marginTop of 100dp. I guess that the dropDownHeight must be higher than the Y position (= padding + margin). Let me know if it works.

like image 97
Francesco verheye Avatar answered Oct 20 '22 22:10

Francesco verheye


Set height of dropdown and set space between dropdown and edittext. Dropdown will display above and below view in dropdownanchor. Height = 200dp, dropdown will not overlap keyboard.

                <RelativeLayout
                    android:id="@+id/dropdownAutoCompleteTextView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingBottom="8dp"
                    android:paddingTop="8dp">
                    <AutoCompleteTextView
                        android:id="@+id/autoTextViewState"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:digits="@string/get_started_petname_digits"
                        android:dropDownAnchor="@+id/dropdownAutoCompleteTextView"
                        android:dropDownHeight="200dp"
                        android:hint="Colorado"
                        android:imeOptions="actionNext"
                        android:inputType="textEmailAddress|textCapWords"
                        android:maxLength="15"
                        android:nextFocusDown="@+id/editZipCode"/>
                </RelativeLayout>
like image 10
Anh Duy Avatar answered Oct 21 '22 00:10

Anh Duy