Changing the width of AutoCompleteTextView in Android
Asked Answered
M

4

22

I've been stumped on this for a few days. I have an autoCompleteTextView and a button split 50/50 in my layout. The autocomplete works, but the suggestions only take up half of the screen (equivalent to the size that my textView takes up). How could I change this to take up the whole width?

Pardon my terrible paint skills:

Blue = AutoCompleteTextView Red = desired autocomplete suggestions

This seems like a trivial problem, but simply changing the width to fill_parent doesn't work. Any ideas xml or programatically?

SOLUTION: myAutoCompleteTextView.setDropDownWidth(ViewGroup.LayoutParams.MATCH_PARENT)

enter image description here

EDIT: My first question seems to be solved, but now if I switch my button and my autoCompleteTextView I get another weird problem. The width starts at where the textView starts. Leaving me with an undesireable result.

SOLUTION: See edit 1 from user: vikram

enter image description here

Millhon answered 22/7, 2013 at 16:55 Comment(0)
F
40

You can set this attribute in the description of this widget using the attribute:

android:dropDownWidth="match_parent"

If you would like to do this in java 'programmatically', use the method setDropDownWidth(int):

myAutoCompleteTextView.setDropDownWidth(int)

Edit 1:

Point pointSize = new Point();

getWindowManager().getDefaultDisplay().getSize(pointSize);

myAutoCompleteTextView.setDropDownWidth(pointSize.x);
Fessler answered 22/7, 2013 at 17:4 Comment(10)
How would I pass in the width of the root layout?Millhon
Well, you can calculate the screen width and pass that into setDropDownWidth(screenWidth). Or, you can pass in the MATCH_PARENT constant as setDropDownWidth(ViewGroup.LayoutParams.MATCH_PARENT). Or, you can have setDropDownWidth(-1), -1 being the value of ViewGroup.LayoutParams.MATCH_PARENT. Was this what you were asking?Fessler
Hmm... yeah that seems to work, but now I have another problem. I'm updating my question.Millhon
What are you using as the argument in setDropDownWidth(?)?Fessler
ViewGroup.LayoutParams.MATCH_PARENTMillhon
Ok. We'll get the screen width and set it explicitly. See Edit 1 above.Fessler
let us continue this discussion in chatMillhon
Great dude:) have been slogging for this answer:)Giule
Is there a way to center the dropDown PopUp and keep it with a width larger than the autocompletetextview itself but not with a MATCH_PARENT width?Persuade
@user3019105 Perhaps. You can try that by replacing myAutoCompleteTextView.setDropDownWidth(pointSize.x); with myAutoCompleteTextView.setDropDownWidth((pointSize.x + myAutoCompleteTextView.getWidth()) / 2);. I am not sure if this will center the drop-down horizontally, but its worth a try.Fessler
C
10

I find setDropDownAnchor() to be much more convenient.

Just pass the id of some view and drop down view will match its width and position (no matter where the actual text view is placed).

In your example that could be the parent of both: button and text view.

Claypool answered 13/10, 2015 at 0:7 Comment(0)
I
2

In my case AutoCompleteTextView occupied a whole width. Looking at change position of spinner dropdown list I set

android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"

to create left and right margins of a list.

enter image description here

Innes answered 1/6, 2020 at 20:23 Comment(0)
S
0

You can also adjust where the width start with:

myAutoCompleteTextView.setDropDownHorizontalOffset(int offset)
Subacute answered 5/5, 2015 at 10:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.