Programmatic Spinner spacing issue - android

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.

Related

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

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.

Empty space between listview header and first item

I've created an android application with a ListView. I've added both a header and footer to the list. But when adding a divider/separator it also creates an empty space between the header and the first ListView item. It does the same for the last ListView item and the footer.
The empty space is equivalent to the size of the divider between all the ListView items, with the difference that it doesn't draw the divider and just leaves empty space. I thought I found the solution with the xml attributes 'Footer dividers enabled' and 'Header dividers enabled'. But when setting them to false, it doesn't change anything. I even tried to set them programmatically with
list.setFooterDividerEnabled(false);
list.setHeaderDividerEnabled(false);
But it just doesn't work. Any way to fix that problem? I just don't want the empty space to be there, I want the first item to fit exactly to the header (same for the footer).
I stumbled upon the same problem, but in a slightly different situation than yours. My ListView has a header (a search box), but the first item below it contains a section header (a date, or a letter) rather than being a regular list item (with the actual content in form of an image, some text, and so on). As such, I want it not to be selectable, so in my custom adapter I have overridden areAllItemsEnabled to return false.
Big mistake, because that's exactly the culprit. See, it appears that, by design, the ListView implementation only draw dividers between two enabled items, but still reserve space for dividers between an enabled item and a disabled one even if those dividers will not be drawn. The fact this is a conscious design decision does not mean it's not stupid, of course. Most weird of all, this dividers drawing policy is based just on the value returned by areAllItemsEnabled instead of the values returned by single calls to isEnabled for subsequent items.
Thus, to work around it, I just had to return true from areAllItemsEnabled (I kept the overridden method and add a comment about this issue, otherwise I would not be able to remember it a month from now): lo and behold, white space disappeared, replaced by a divider. Now, if I want to show the ListView header and the first section header as being exactly adjacent, I just have to choose a divider color that's the same as the section header color.
Really hope that's the same case as yours, or that my solution helps you in some other way.
I tried a solution by 幻影浪子 that works (based on android-pulltorefresh):
View Layout (header.xml):
<?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="wrap_content" >
<RelativeLayout
android:id="#+id/content"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ProgressBar
style="#android:style/Widget.ProgressBar.Small.Inverse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="16dp"
android:layout_marginTop="2dp" />
</RelativeLayout>
</LinearLayout>
Inflating View:
m_headerView = inflater.inflate(R.layout.header, this, false);
Displaying View:
m_headerView.setPadding(0, 0, 0, 0);
m_headerView.setVisibility(View.VISIBLE);
Hiding View:
m_headerView.setPadding(0, -1000, 0, 0);
m_headerView.setVisibility(View.GONE);
It worked perfectly in our project. I hope it is helpful.
In getview method you can check if the item is first or last and set custom devider which will be of 0 height or single pixel height of transparent color.
goto the ListView properties in android layout and search for spacing tag... some how in android, when creating new layouts, it will defaults creation is spacing header spacing and border properties. check it , if it is available then remove it
Didn't find a great solution.
Set dividerHeight="0dp" and created my own dividers manually - either directly in the layout XML or dynamically in the adapter if you need more precise control.
Just do
list.setDividerHeight(0)
That should take care of it.

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.

My spinner items are invisble

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);`

Manually set width of spinner dropdown list in android

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);

Categories

Resources