android how to wrap spinner drop down text (android 4) - android

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.

Related

Android RecyclerView item text too long

I have a RecyclerView list where some items (text) are too long for the width allowed by the device. I can use android:ellipsize="end" to indicate that the text is truncated but I want to be able to show user the whole text. I can use android:scrollHorizontally="true" and then the text will scroll but there is no visual indication for the user that he needs to scroll it and the text just looks truncated. What would be the good UX for this case? Thanks.
1、you can edit your textView like this
xml
<TextView
...
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:text="......"/>
It will auto scrollHorizontal,Or you can use a autoFitTextView in you list item,auto fit textview.
You can add android:scrollbars="horizontal" to your recyclerview to show the scrollbar.
Turned out that the solution is to wrap the item in HorizontalScrollView and set android:layout_gravity="fill_horizontal". This allows for scrolling and there is a horizontal scroll bar for long items.
So, after many investigations, I've found the problem solution:
This happens due to MARGINS and PADDINGS (and maybe other offset parameters) inside your TextView and its parents. Just remove them and see the result.
I hope it helped new googlers!
PS:
If you want to save your offset parameters try to change the layout width parameter inside your TextView and its parents to wrap content (but somewhere also try 0dp or match parent), it also worked for me. So your UI will look exactly as you want!

Programmatic Spinner spacing issue

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.

Align spinner values to right instead of left

In spinner xml, I'm using:
android:entries="#array/cars"
where cars is list of numeric items.
The spinner automatically align values to left, and I can't find in xml/code how to change it.
You have to use a custom view here to give alignment to the Spinner values. Create a CustomView like this answer has created and add android:gravity to the TextView as right.
UPDATE:
Set the CustomView to your adapter using
adapter.setDropDownViewResource(android.R.layout.your_custom_created_view);
You can use TextAlignment on properties and select TextEnd,
or android:textAlignment="textEnd" on XML simple
You can try with spinner item text alignment right end :
view.textAlignment = View.TEXT_ALIGNMENT_TEXT_END

How to wrap lengthy text in a spinner? [duplicate]

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.

How to set a long string in a text view in a single line with horizontal scroll

actually i have keep one scrollview. Inside that scroll view I set one textview at run time I want to set text in that textview. The string which I'm going to set is some what big in length so i could not get the string in a single line and i can get my string two or three lines. My scroll view layout width size is 250px. I dont want to exceed that size...My expectation is i want to see that string within that scrollview as single line if the string is exceeds the scroll size then it should horizontally scroll in text view. I tried some functions like setting horizontal scroll to scrollview and textview but nothing is work out.
Pls help me how to solve this problem.
urs,
s.kumaran.
you have to use these two Xml attributes in your TextView widget:
android:scrollHorizontally="true"
android:singleLine="true"
So your xml layout must contain something like this:
<TextView
android:text="Single-line text view that scrolls automatically if the text is too long to fit in the widget"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit ="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:scrollHorizontally="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
you can get the correspondant method for these attributes if you are creating Your TextView with code, refer to TextView documentation :
setHorizontallyScrolling(boolean)
setTransformationMethod(TransformationMethod)
setMarqueeRepeatLimit(int)
try this ,,
TextView.setHorizontallyScrolling(true)
TextView.setLines(1);
where did you add the textview..Inside scrollview we able to add only one view...
Take TextView and HorizontalScrollView. Just put textview inside the HorizontalScrollView. And yes make sure to mentioned android:singleLine="true" inside the TextView.
I had the same issue with a TextView inside a table and nothing detailed here solved the fact that it wouldn't scroll horizontally (automatically, which might not be the desired effect from OP, but it's quite unclear).
While comparing some code that did work, I found out the TextView must be selected for scrolling to start:
TextView text_view = new TextView(context);
text_view.setLines(1);
text_view.setHorizontallyScrolling(true);
text_view.setMarqueeRepeatLimit(-1); // At this point the view is not scrolling!
...
text_view.setSelected(true); // Get scrolling to start
Seems crazy, but it works.
for me, this one line in the EditText xml was enough:
android:singleLine="true"
You didn't specify if this was in XML or java code, but here's the java code to get that working:
// Allow textView to scroll
textView.setSingleLine(true);
textView.setHorizontallyScrolling(true);
textView.setEllipsize(TextUtils.TruncateAt.MARQUEE);
textView.setMarqueeRepeatLimit(-1);
textView.setSelected(true);
textView.setPadding(10, 0, 10, 0);
The only unnecessary item here is the padding, which I find to look best so the text does not touch the border of our 250px TextView.

Categories

Resources