Spinner Does Not Wrap Text -- Is This an Android Bug

Spinner does not wrap text -- is this an Android bug?

In holo theme spinner by default uses dropdown mode. And all moves with overriding default styles just move to switching spinner mode to dialog mode which succesfully wraps multiline text as in api 11. Instead you can create spinner with new Spinner(context, Spinner.MODE_DIALOG) or in xml: android:spinnerMode="dialog". But it's not resolve the problem, because it's dialog, not dropdown.

I have found another solution for this trouble: Override getDropDownView method in ArrayAdapter and put setSingleLine(false) in post method of view. So when view completely created it wraps the text to appropriate lines.

@Override
public View getDropDownView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = new TextView(_context);
}

TextView item = (TextView) convertView;
item.setText("as

Related Topics



Related Topics



Related Topics



Related Topics



Related Topics



Related Topics



Related Topics



Related Topics



Related Topics



Related Topics



Related Topics



Related Topics



Related Topics



Related Topics



Related Topics



Related Topics



Related Topics



Related Topics



Related Topics



Related Topics



Related Topics



Related Topics



Related Topics



Related Topics



Related Topics



Related Topics



Related Topics



Related Topics



Related Topics



Related Topics



Related Topics



Related Topics



Related Topics



Related Topics

ddd");
final TextView finalItem = item;
item.post(new Runnable() {
@Override
public void run() {
finalItem.setSingleLine(false);
}
});
return item;
}

UPDATE:

And here is another answer.

Manually wrap listview in PopupWindow and show it under TextView on click and hide it on listItem click.

Simple implementation just to show idea:

public class MySpinner extends TextView {
private PopupWindow _p;
private ListView _lv;
public MySpinner(Context context) {
super(context);
init();
}
public MySpinner(Context context, AttributeSet attributeSet){
super(context, attributeSet);
init();
}

private void init(){
setBackgroundResource(R.drawable.spinner_background);
final List<String> list = new ArrayList<String>();
list.add("Very long text AAAAAAAAAAAAAAAA");
list.add("1 Very long text AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
list.add("2 Very long text A");
list.add("3 Very long text AAAAAAAAA");

setMinimumWidth(100);
setMaxWidth(200);

_lv = new ListView(getContext());
_lv.setAdapter(new ArrayAdapter<String>(getContext(), R.layout.simple_list_item_1, list));
_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
_p.dismiss();
setText(list.get(i));
}
});

setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {

_p = new PopupWindow(getContext());
_p.setContentView(_lv);
_p.setWidth(getWidth());
_p.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
_p.setTouchable(true);
_p.setFocusable(true);
_p.setOutsideTouchable(true);
_p.showAsDropDown(view);
}
});
}
}

Wrapping text in spinner android

<?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="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:textColor="#ffffff"/>

Use this textview in your spinner adapter in place of the android default textview you are using in .setAdapter method of the Spinner. see below R.layout.spinner_textview textview is this textview i have posted above.

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getApplicationContext(), 
R.array.Array,
R.layout.spinner_textview);
adapter.setDropDownViewResource (android.R.layout.simple_spinner_dropdown_item);

Spinner with long text not working fine

For my problem I found this solution :

Spinner language = (Spinner) findViewById(com.Orange.R.id.current_language_text);

ArrayAdapter adapter = new ArrayAdapter(this,
com.Orange.R.layout.my_spinner_textview, languages);
adapter.setDropDownViewResource(com.Orange.R.layout.multiline_spinner_dropdown_item);
language.setAdapter(adapter);

where languages is a String[] and my_spinner_textview.xml is :

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textview_spinner"
style="?android:attr/spinnerItemStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@drawable/textorange_selected"
android:paddingLeft="5dp"
android:singleLine="true"
android:ellipsize="end"
/>

How to apply ellipsis to the actual spinner instead of the dropdown views?

Put android:ellipsize="end" on the TextView that you are using in your SpinnerAdapter (quest_row.xml, I assume).

Your question shows that you tried android:ellipsize="marquee", which will not work and isn't what you are looking for anyway ("it should add ellipsis at the end").

Toolbar Spinner doesn't show a text

I don't think you want to set the theme for you Spinner, as it should inherit its attributes from the theme overlay you specified in your Toolbar. But, I don't think it inherits the popup theme you specified, so you should set that in your Spinner as well. Also, there is no need for an extra LinearLayout to wrap your Spinner. You can either use the Spinner as the root object in your xml:

<Spinner xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/spinner"
android:layout_width="wrap_content" android:layout_height="wrap_content"
app:popupTheme="@style/AppTheme.PopupOverlay"/>

Or place it directly in your Toolbar:

<android.support.v7.widget.Toolbar android:id="@+id/toolbar"
android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" android:fitsSystemWindows="true"
app:layout_scrollFlags="scroll|exitUntilCollapsed" app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_collapseMode="pin" app:popupTheme="@style/AppTheme.PopupOverlay">

<Spinner android:id="@+id/spinner"
android:layout_width="wrap_content" android:layout_height="wrap_content"
app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.v7.widget.Toolbar>

Lastly, make sure your are not displaying the title in your Toolbar:

setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);


Related Topics



Leave a reply



Submit