I've a spinner pointing on a CursorAdapter.
I set this adapter with a specific view.
I would like to fix the spinner's width, I tried a lot of stuff unsucessfully :
layout_width="100dp" // to force spinner with a specific size KO
reduce the layout width to 100dp // KO
use an ellipsize on a textview contained by the adapter // KO
I would like to block the spinner's width. Do you have an idea ?
You could try adding layout_padding to the left and right of it.
You can try creating a custom textview layout for your spinner to fix some properties.
For example, you can fix the width at 100 dp :
<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="100dip"
android:layout_height="wrap_content"
android:ellipsize="end" />
Then you can declare it like this:
mSpinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapterSpinner = ArrayAdapter.createFromResource(this, R.array.spinner_vals, R.layout.custom_simple_spinner_dropdown_item);
Related
I have spinner who collect data from DB and display on real time. The problem I am facing is that when the length of an item is quite long then the spinner Stretch itself and goes out of the screen/view. What I want is to have a fix length of my spinner and it should not stretch itself. Here is the view of my Spinner:
And below is the code:
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Java:
Spinner spinnerRub = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, lables);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Instead of using wrap content for the width , specify the width (say 120dp or..)based on your requirement.
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.
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.
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);`
Is it possible to set the width of a spinner dropdown list in code? I have a spinner populated with integers, and it does not look good with the list expanding to full width. Can I set the width to wrap the content somehow?
Spinner hSpinner = (Spinner) timerView.findViewById(R.id.timer_hour_spinner);
ArrayList<Integer> hList = new ArrayList<Integer>(21);
for (int i = 0; i <= 20; i++) { hList.add(i); }
ArrayAdapter hAdapter = new ArrayAdapter(RemindMe.this, android.R.layout.simple_spinner_item, hList);
hAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
hSpinner.setAdapter(hAdapter);
Thanks!
Linus
Just use:
setDropDownWidth(desiredWidth);
You can change the width of anything in code by adjusting its LayoutParams. The details of that varies by container (LinearLayout vs. RelativeLayout vs. ...).
However, I'm confused why you want to change the width "in code". Why not just set the width to be wrap_content in the layout XML?
It is straightforward to control the spinner dropdown appearance. Try this:
//Step 1. create the drop down list
static List<String> special_Spinner_Display_List = new ArrayList<String>();
// add your values to the list...(this is best done using a for loop)
special_Spinner_Display_List.add(item1);
special_Spinner_Display_List.add(item2); //etc., etc.
//Step 2. build the spinner
ArrayAdapter arrayAdapter_special = new ArrayAdapter(this,
R.layout.your_special_spinner, special_Spinner_Display_List);
arrayAdapter_special.setDropDownViewResource(R.layout.special_spinner_dropdown);
specialSpinner.setAdapter(arrayAdapter_special);
//Step 3. create an XML layout file called special_spinner_dropdown where you can
//style to your heart's content. Here's an example:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/SpinnerDropdown"
style="?android:attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#D5ECED"
android:ellipsize="marquee"
android:singleLine="true"
android:textSize="24sp"
android:textStyle="bold"
android:textColor="#color/black" />
That's it. Lemme know how it works!
Set dropdown width in xml file of spinner using the tag
android:dropDownWidth="#dimen/desired_width"
Explaination :
mDropDownWidth = pa.getLayoutDimension(R.styleable.Spinner_dropDownWidth,
ViewGroup.LayoutParams.WRAP_CONTENT);
this field is used for dropdown width when spinner item is initialized in Spinner class
for programatically changing spinner dropdown width above api level 16, use
mSpinner.setDropDownWidth(size_in_pixel);