How i can set an image as background of my autocomlete list?
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp"
android:textColor="#000">
</TextView>
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_friends);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, friendsListNameOrder);
textView.setAdapter(adapter);
Thank you
in your xml put the background attribute for your TextView:
<TextView android:background="#drawable/your_img" ... other attributes />
This might help someone else!.. I think what you were looking for was this :
textView.setDropDownBackgroundResource();
OR
searchBox.setDropDownBackgroundDrawable();
Related
I have a spinner that loads data from string-array and I would like to customize it from the XML layout and not from Java code (as I saw in many examples).
Is it possible to change the text color and the spinner style from XML?
Yes you can, here is how you add a spinner layout
item_spinner.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_margin="#dimen/small_margin"
android:background="#color/white"
android:ellipsize="marquee"
android:paddingLeft="#dimen/margin_small"
android:singleLine="true"
android:text="--Select--"
android:textColor="#color/text_title"
/>
and use in your adapter like this
ArrayAdapter<String> itemsAdapter =
new ArrayAdapter<String>(this,R.layout.item_spinner, items);
and you are done. customize but keep the textView id same that is how adapter will set text on it.
in my project I have this custom layout inflated in the actionbar:
Link to image
But i would like to have the spinner text of white color.
I've seen a lot of topics on the web about this, but I can't fix the problem yet.
How can I fix this problem? How can I change the text color of my spinner bysetting it to white color?
EDIT:
the xml code of spinner is this:
<Spinner
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textColor="#ffffff"
android:id="#+id/spinner"></Spinner>
It's very simple, I tried many things but nothing..
In your activity you should have something like this:
(Basically you are getting the Spinner and setting it an adapter. This adapter is the responsible for filling it)
Spinner spinner = (Spinner) findViewById(R.id.your_spinner_id);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.your_selected_spinner_item, myArrayList<String));
adapter.setDropDownViewResource(R.layout.your_dropdown_item);
spinner.setAdapter(adapter);
And you can have this XMLs: (put them on res/layout)
your_selected_spinner_item.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myLayoutID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="20sp"/>
And also
your_dropdown_item.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myDropdownLayoutID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="20sp"/>
Create a Layout file like this:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/spinner_ref_id"
android:layout_width="match_parent"
android:padding="20dp"
android:textColor="#fff"
android:textSize="20sp"
android:text="Hello"
android:layout_height="wrap_content">
</TextView>
Assuming that you are using ArrayAdapter as your Spinner Adapter.
Hope it helps!!!
I may be missing something simple here, but I have an AutoCompleteTextView that contains some very long items. When one is clicked, it shows the text correctly in the EditText and spans it over several lines.
However, I want it to be in multiple lines on the popup as well so that the users can see which item they are selecting.
Here is my custom layout:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="none"
android:maxLines="100"
android:scrollHorizontally="false" />
Here is my initialisation of the array and adapter:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.dropdown_item_wrap_line,
getResources().getStringArray(R.array.building_descriptions));
mBuildingDesc.setAdapter(adapter);
My guess is that your AutoCompleteTextView is limited to singleLine, so using a custom layout with default TextView should work neatly.
You will need to make a new Layout and name it custom_item.xml, then do it like this...
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/autoCompleteItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:textSize="14sp"
/>
</LinearLayout>
Then while using Adapter on AutoCompleteTextView do
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.custom_item, R.id.autoCompleteItem, StringArray);
textView.setAdapter(adapter);
I try to set text of ListView in center of layout. I use LinearLayout and set gravity = center, but it always shows in left
Here is layout:
<?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="fill_parent">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:gravity="center">
<ListView android:id="#+id/lv_function" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:gravity="center" />
</LinearLayout>
</LinearLayout>
In my code, I use:
String[] functions = { "Login", "Register", "Setting", "About", "Exit" };
lvMenu = (ListView) findViewById(R.id.lv_function);
ArrayAdapter<String> arr = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, functions);
// set adapter for ListView.
lvMenu.setAdapter(arr);
Here is result:
result
Do you have any idea? Please help me, thanks!
You have to create your own layout for your listview item. Soemthing like this
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<TextView
android:id="#id/textItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Then, in your code, your going to have to do this
new ArrayAdapter<String>(this, R.layout.yourRowItemLayoutHere, R.id.textItem, functions);
Hope that helped
layout_gravity = Impact with layout in your child views
gravity - Impact with content in your child views
I'm try to change text color and align item in spinner to center of it how can I do this
here is my code
String[] li={"1","2","3"};
final Spinner combo = (Spinner)findViewById(R.id.widget30);
ArrayAdapter<String> a = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, li);
combo.setAdapter(a);
Thanks
Use a custom view for that, and specify by calling:
a.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Note that you have to use your own view here: R.id.my_simple_spinner_dropdown_item
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee" />