This question already has answers here:
Spinner does not wrap text -- is this an Android bug?
(15 answers)
Closed 3 years ago.
I have two spinner and EditText controls within a table layout view on a separate row. The spinners are populated with data. My problem is the data (texts) that are populated into the spinners are too lengthy to fit the screen size. Therefore, the spinners are forced to stretch unnecessarily stretching other controls on another row.
It's a must for me to show the texts in the spinner. Hence using ellipses is not an option. If it's possible how can I wrap the lengthy text on the spinners?
Step 1. TextView with wrapped text
The first thing to do is to to force simple TextView to wrap text. Its easy:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="false"
android:text="very long text that will be wrapped to next line" />
Note the singleLine attribute here.
Step 2. Custom layout
Now we should somehow set singleLine attribute to false in TextView used by Spinner to show the item in the list.
In your code you probably have place where you create adapter to use it with Spinner:
this.mAdapter = ArrayAdapter.createFromResource(this, R.array.Planets,
android.R.layout.simple_spinner_dropdown_item);
The idea is to copy the android.R.layout.simple_spinner_dropdown_item layout to your project. Then modify it by setting singleLine attribute to false in CheckedTextView:
For this, add file to res/layout folder named multiline_spinner_dropdown_item.xml with next code:
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="false"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:ellipsize="marquee" />
Note that this file is identical to android.R.layout.simple_spinner_dropdown_item layout, except it has singleLine set to false now.
Step 3. Creating Adapter with custom layout
Modify your adapter creating code to:
this.mAdapter = ArrayAdapter.createFromResource(this, R.array.Planets,
R.layout.multiline_spinner_dropdown_item);
Here is screenshot from modified SpinnerActivity example from Android SDK:
Define a custom layout and use it with the spinner and adapter.
Related
When we build a ListView in android studio, we need to use an ArrayAdapter.
What is the task of second argument in constructor of ArrayAdapter ?
I cannot understand what is android.R.layout.simple_list_item_1 used for ?
This layout describe how the item list looks like.Maybe you want every item contain a text view and image.You should specify these details in this layout.
android.R.layout.simple_list_item_1 is a layout in which the data from your ArrayAdapter gets populated(added).
This is the id of the layout that you need to use for populating each of the item in the list. If it says android.R.layout that means you are going to use one of the standard android layouts. simple_list_item_1 This is the name of the file which will populate each row of the list. try changing this to simple_list_item_2 to see how the layout in list changes.
You can also use your custom adaptors and custom layouts(which would be in majority of the cases in day to day apps).
For full list of standard layouts available Go here
This argument defines how list items would appear in ListView, There are many layouts you can also try them out or you can make your own custom layout to modify apperance of listitems in Listview by using CustomAdapter.
1.create custom layout name custom_layout.xml & paste bellow code
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="16sp" >
</TextView>
Use like R.layout.custom_layout instead of android.R.layout.simple_list_item_1
My requirement is to have Spinner dropdown width similar to the Spinner View.
So that when drop down is expanded, it can completely overlap with Spinner view.
But i got into a problem.
I have created 2 spinners to explain my situation.
Dimension
form_screen_equipment_combo_box_dropdown_vertical_offset = -50dp
One from XML layout. (Gives me expected result)
<Spinner
android:id="#+id/equipment_selector"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/combo_box_bg"
android:dropDownVerticalOffset="#dimen/form_screen_equipment_combo_box_dropdown_vertical_offset"
android:dropDownWidth="match_parent"
android:popupBackground="#drawable/combo_box_bg"
android:spinnerMode="dropdown" />
Another dynamically from java code. (Problematic Spinner)
Spinner view = new Spinner(context, Spinner.MODE_DROPDOWN);
**"view.setPadding(0, 0, 0, 0);" This line solved my problem.**
view.setLayoutParams(new LayoutParams(android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
CustomSpinnerAdapter equipmentAdapter = new CustomSpinnerAdapter(context, 0, Arrays.asList(values));
view.setAdapter(equipmentAdapter);
view.setPrompt(title);
view.setPopupBackgroundDrawable(context.getResources().getDrawable(R.drawable.combo_box_bg));
view.setDropDownWidth(android.view.ViewGroup.LayoutParams.MATCH_PARENT);
view.setDropDownVerticalOffset(context.getResources().getDimensionPixelOffset(R.dimen.form_screen_equipment_combo_box_dropdown_vertical_offset));
view.setBackground(context.getResources().getDrawable(R.drawable.combo_box_bg));
In second spinner i have unexpected space, i checked through UIAutomator and it seems dropdown view's width is lesser than the Spinner view.
Attached screenshot of second spinner for reference.
Can you please suggest required code changes to fix this problem ?
Thanks in advance.
I have edited my answer above. It seems Spinner widget add padding by default when created programmatically. Had to reset that 0.
I have a Spinner where the text both inside the spinner and the choices when the spinner is expanded (drop down view) can be quite long depending on the locale. i set a custom view for both the spinner view and the drop down view that should allow the text lines to wrap,
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
style="?android:attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="none"
android:minHeight="?android:attr/listPreferredItemHeight"
android:singleLine="false" />
and in the code,
spinnerPermission = (Spinner) layout.findViewById(R.id.permission_spinner);
ArrayAdapter<CharSequence> permissionAdapter = ArrayAdapter.createFromResource(getActivity(), R.array.add_share_dialog_permissions,
R.layout.multiline_spinner_dropdown_item);
permissionAdapter.setDropDownViewResource(R.layout.multiline_spinner_dropdown_item);
spinnerPermission.setAdapter(permissionAdapter);
this works fine in android 2, but in android 4, the text in the drop down view still won't wrap,
although it's not clear from the image, the text in the spinner view does wrap correctly. I can't tell for sure, but it seems like the container around the drop down views is not constrained by the screen and expands off the screen to the right. That would prevent the text from wrapping, because as far as the TextView is concerned, there's plenty of space.
Here is the spinner's popup view in hierarchy viewer,
Any ideas?
I have solved this by custom layout where I have TextView inside LinearLayout. Both TextView and LinearLayout have fixed width 200dp. Then it wraps the text correctly both on Android 2.3 and 4.0.
CheckedTextView extends TextView, which has inputType, maxLines, minLines attributes.
Try applying android:inputType="textMultiline" attribute to your CheckedTextView, that should help.
Create custom layout, first Goto res folder->create New XML Layout file
eg: simple_spinner_layout.xml
link:http://justpaste.it/edit/2866098/aab2f5f3
Its work for me.
Plz vote for this answer if u have satisfied.
Hi I have a spinner, which contains radio button elements. My spinner loads the elements, but everything is invisible. I think you can understand my situation from the image. an I get any suggestions.
This is the code I am using,
String[] reminder={"5 minutes","10 minutes","15 minutes","20 minutes","25 minutes","30 minutes","35 minutes","45 minutes"};
spinnerAdapter = new ArrayAdapter<String>(mContext,
android.R.layout.simple_spinner_item, reminder);
spinnerAdapter
.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
reminder_spinner.setPrompt("Reminders");
reminder_spinner.setAdapter(spinnerAdapter);
reminder_spinner.setOnItemSelectedListener(new ItemSelect());
The fact that the item text shows up when it is selected leads me to believe it could be a styling issue, ie white font on white background. Here is the layout for simple_spinner_dropdown_item:
<CheckedTextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"/>
I would define another xml layout using the above as a template, and pass that to setDropDownViewResource instead.
add this
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);`
in your code and remove
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);`
I have three main things I want to change with the appearance of a ListView.
1) get rid of horizontal dividers that print between items
2) change font size of test displayed in listview
3) change the padding around each item displayed
It looks like after reading for a while that 2) and 3) should be controlled by changing these properties for the TextView used by the ListView, but I don't yet understand how to do that. Can someone answer these with a little more detail?
1) Set divider height to 0 --- setDividerHeight(0) and set divider color to transparent --- setDivider(new ColorDrawable(0x00FFFFFF))
2) If you're using a list of text views then you can continue using a simple adapter like ArrayAdapter but you will need to create a custom text view. You can add something like this to res/layout
test_text.xml
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize=<SIZE IN SP>
android:textColor="#FFF" />
3) Add padding to your textview above
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text"
android:padding="5dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize=<SIZE IN SP>
android:textColor="#FFF" />
Use your new layout with your ArrayAdapter
eg.
ArrayAdapter(Context context, int resource, int textViewResourceId, List<T> objects)
ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(this, R.layout.test_text, R.id.text, myData);
All of those can be controlled via styles. The docs on styles explain everything you need to do.
In short, you create a style resource - derived from whatever you want to use as your base - where you override all the relevant items, like the divider, the padding the font size.
Then, you either apply that style to your ListView in the XML file, or you create a theme to it and call setTheme() BEFORE you call setContentView().
EDIT: I should mention the obvious answer that kcoppock pointed out - if this is specific to just one element in your layout, just modify that particular element in your layout file.