Figen Güngör
Figen Güngör

Reputation: 12559

AutoCompleteTextView creates a margin for dropdown list

Dropdown list comes with right and left margin even though I didn't set margin in my dropdown list item layout. I want it to match to the autocompletetextview's width. How can I achieve that?

Here is my AutoCompleteTextView xml code:

<LinearLayout
                android:id="@+id/headerlogo"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_margin="0dp"
                android:layout_weight="1"
                android:clickable="true"
                android:gravity="center_vertical"
                android:orientation="horizontal"
                android:weightSum="9" >

                <AutoCompleteTextView
                    android:id="@+id/autocomplete_name"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:layout_weight="7"
                    android:background="@drawable/edittextback"
                    android:ems="10"
                    android:textSize="15sp"
                    android:hint="@string/codehint"
                    android:textColorHint="@color/hintgrey"
                    android:paddingRight="30dp"
                    android:paddingLeft="10dp"
                    android:singleLine="true"
                    android:ellipsize="end"
                    android:completionThreshold="1">
                </AutoCompleteTextView>

                <Button
                    android:id="@+id/btnsearch"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:layout_weight="2"
                    android:background="@drawable/buttondarkblue"
                    android:text="X"
                    android:textColor="@color/white" />
            </LinearLayout>

And item layout for dropdown list:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical">
      <TextView
        android:id="@+id/route"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:singleLine="true"
        android:ellipsize="end"
        android:padding="5dp"
        android:textColor="@color/loginblue"
        android:text="ASD"></TextView>
</LinearLayout>

Upvotes: 4

Views: 4562

Answers (2)

Vitaliy
Vitaliy

Reputation: 69

You can use 2 methods setDropDownHorizontalOffset(int) and setDropDownWidth(int)

For example, you can do something like this to match to autocompletetextview:

input.setDropDownHorizontalOffset(0)
input.setDropDownWidth(input.getWidth())

FYI 1st method can use negative values. So you can set a dropdown layout width more the autocompletetextview width:

int offset = 64
input.setDropDownHorizontalOffset(-1 * offset)
input.setDropDownWidth(input.getWidth() + offset * 2)

Upvotes: 2

Arash
Arash

Reputation: 698

I don't know, you've got your answer. But may helps.

If you assign a layout for dropdown list in the custom adapter and set margins for the TextView or paddings for the root view (for example, LinearLayout), it doesn't work. I did as follow:

I set background for the dropdown list in style.xml file:

<item name="android:popupBackground">@drawable/dropdown_list_bg</item>

And then in dropdown_list_bg.xml file i wrote these lines:

<item android:right="1dip" android:left="1dip">
    <shape>
        <solid android:color="@color/dropdownListBg"/>
        <corners android:radius="@dimen/corner"/>
    </shape>
</item>

As you can see i set "padding right" and "padding left" with "android:right" and "android:left" attributes in the drawable xml file.

Upvotes: 1

Related Questions