I'm using Spinner in my android app, but I've a problem. It works fine, but its color is darker than they use to have, but I don't know why. Is not a problem with themes or styles, is more, I used Spinners recently for other project and It works perfectly. Did it happen to you? How did you solve it?
I don't use any custom component for this example, so I don't attach the code, but If you need it just ask for.
it may be because you passed different resource id to spinner arrayAdapter.. try passing android.R.layout.simple_spinner_item to your adapter
yourSpinnerArrayAdapter =new ArrayAdapter(this,android.R.layout.simple_spinner_item, your_list);
hope this helps...:)
You can create a custom layout for spinner, it will be more customizable
custom_spinner.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:gravity="left"
android:textColor="#000000"
android:padding="10dip"
/>
And your spinner adapter
yourSpinnerArrayAdapter = new ArrayAdapter(this,R.layout.custom_spinner, your_list);
Related
I have an issue where if I'm using a basic ArrayAdapter to display items in a spinner, all the spacing on those items is lost if I open the spinner, dismiss it by tapping outside of it, then open it again. It appears other attributes like color and such are still kept intact.
Here is how I am creating the spinner:
binding.fragmentTypeSpinner.setAdapter(new ArrayAdapter<>(mContext,
R.layout.item_simple_spinner,
typeArray));
item_simple_spinner.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/item_simple_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorTransparent"
android:textSize="17sp"
android:padding="10dp" />
How it looks at first:
How it looks after:
I tried with just the basic android spinner item layout as well and still no dice. Has this happened to anyone else before? I searched but couldn't come up with much.
I have a custom spinner in my Xamarin Android app using MvvmCross. The only reason I'm using custom is because the model holds a class and I want the spinner to populate the values with a property on that class. However, the appearance of the drop down doesn't look like the rest of my app. It's missing radio buttons on the right and the spacing is off. How do I make this custom spinner look just like the rest?
<Mvx.MvxSpinner
style="#style/spinner_input"
local:MvxItemTemplate="#layout/item_spinner"
local:MvxDropDownItemTemplate="#layout/item_spinnerdropdown"
local:MvxBind="ItemsSource ProductCategoryOptions; SelectedItem SelectedProductCategory" />
Item_Spinner.axml - this part seems to be just right, the appearance of the drop down once a value is selected looks just like others.
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/black"
android:text="Test"
local:MvxBind="Text Caption" />
Item_SpinnerDropDown.axml - I think this is the file that is wrong. The appearance of the drop-down doesn't match.
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/black"
android:text="Test"
local:MvxBind="Text Caption" />
Checkable spinners and also supporting activated state indicators is something we've recently worked on within MvvmCross - for example, see this issue and associated commits https://github.com/MvvmCross/MvvmCross/issues/481
To make an MvxSpinner support the default styles then you should be able to simply specify no local:MvxItemTemplate and no local:MvxDropDownItemTemplate - in this case then the spinner uses resources of:
global::Android.Resource.Layout.SimpleDropDownItem1Line
global::Android.Resource.Layout.SimpleSpinnerDropDownItem
This will just use the standard Android layout templates, but will rely on your list item ToString() implementations, rather than using the Caption properties.
This is similar to what the spinner and adapter use within a "normal" Android app - e.g. see http://developer.android.com/guide/topics/ui/controls/spinner.html
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.planets_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
If you instead wanted to write your own spinner layouts and to base them on the Android layouts then it's probably easiest to look into the Android source to find the original layouts - e.g. looking at: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4_r1/frameworks/base/core/res/res/layout/simple_spinner_dropdown_item.xml/
One small advanced warning here is that the standard MvvmCross listitem will wrap these displayed list items in a framelayout before using it. This won't have an effect on many layouts - but might effect some. If it effects your's then you may need to write your own custom list item view (based on https://github.com/MvvmCross/MvvmCross/blob/v3.1/Cirrious/Cirrious.MvvmCross.Binding.Droid/Views/MvxBaseListItemView.cs)
One final note on this - please note that https://github.com/MvvmCross/MvvmCross/issues/481 is still open, plus there is a query on the framelayout wrapping of list items in https://github.com/MvvmCross/MvvmCross/issues/539 - so please do be aware that changes and updates may happen in this area - releases in the near future may change the default appearance here.
The following works for Item_SpinnerDropDown.axml
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
local:MvxBind="Text Caption" />
Is there a way of using the standard android xml files (simple list item multiple choices) and only adjust some of the properties so that I can also use the standard adapters? For now I made a new view xml and copied the code from simple_list_item_multiple_choice. There I only added a background color. This works, but now I have the almost same code 2 times.
I also know that custom view and custom adapter would work, but I hope there is a more "smart" solution for this (in my case really only the background color matters, the rest is fine). It seems a lot more code for only changing the background color of an item.
I thought with include/merge tags I could include the standard xml and justify to my needs, but this doesnt work (also because I couldn't use the standard arrayAdapter anymore)
This is how my view looks now:
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceMedium"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:ellipsize="end"
android:singleLine="true"
android:background="#color/white"
/>
Did you check this out? Styles
I am not sure if this is what your question meant but according to what I could grasp, you were talking of styles only
I have a Spinner which displays single digit numbers (1-6).
When I test on a phone, it's virtually impossible to click on a number because they are so narrow (font size is 12sp) and the only area which appears to be clickable is the text itself.
Ideally I'd like the user to be able click on a greater width on the Spinner drop down than just the text. I've tried padding with spaces but these are suppressed, and I've tried using PaddingLeft/Right but again there's no difference.
Here is the Spinner
<Spinner
android:id="#+id/spinner_period1"
android:layout_toRightOf="#id/spinner_weekday1"
android:layout_below="#id/col1day"
android:layout_height="wrap_content"
android:prompt="#string/enterperiod"
android:layout_width="80dp"
android:entries="#array/periodlist"
android:layout_marginRight="340dp"
android:layout_marginBottom="20dip"
/>
Note that I also set the Spinner text attributes using:
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/textview"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="12sp"
android:textColor="#768766"
/>
Any ideas? Thanks in advance
Mark
Try making the TextView wider in the android:layout_width attribute.
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/textview"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:textSize="12sp"
android:textColor="#768766"
/>
That could possibly fix your issue or make it better at least.
Try setting your TextView layout width to some arbitrary value rather than "wrap_content". That should make the whole thing clickable, as opposed to just the wrapped content.
Just adding some padding to the spinner helped me fix this issue.
<Spinner
android:id="#+id/spinner"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:padding="25dp"
android:scrollbarSize="20dp" />
Way 1:
Display the spinner's options as a dropdown by using android:spinnerMode. Therefore have a look at the API (Android API Spinner). Or play around with android:dropDownWidth.
Way 2 (I used it in an APP but at the moment I have no access to the code):
Maybe you could set another layout while populating your spinner with text - instead of assigning the values in the XML.
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.planets_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
It's copied from the following link Android Spinner. Maybe you could use one of the layout templates of android.R .
I have searched a long time and cannot find a solution to my problem. I am trying to create a Dialog with an AutoCompleteTextView. I followed the tutorial on the Android developer website, and it worked great. I have been successful using layouts on Dialogs before, so I thought this would be just as easy. I created a layout for my Dialog and I made sure that the AutoCompleteTextView has an ID. Here's where the interesting stuff happens...
dialog.setContentView(R.layout.auto_layout);
AutoCompleteTextView auto_tv = (AutoCompleteTextView)findViewById(R.id.role_ac);
Here is the layout as well.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Role" />
<AutoCompleteTextView android:id="#+id/role_ac" android:layout_width="280dip" android:layout_height="wrap_content"/>
<Button android:layout_height="wrap_content"
android:layout_width="fill_parent" android:text="Done"
android:id="#+id/auto_doneButton" />
</LinearLayout>
For some reason, auto_tv is null even though it does exist in the layout auto_layout. The only way I have been able to get an AutoCompleteTextView in a Dialog is by building the layout programmatically. Why is the AutoCompletTextView null when I try to use it? Did I forget something in my layout or am I not constructing the object correctly? Any help on this would be greatly appreciated. Thanks.
You're mixing two different contexts in your calls to setContentView and findViewById. In the first statement you're setting the content view of the object dialog. In the second statement you're looking for an view in the parent activity. You want to make both calls using the dialog object. The correct syntax should be:
dialog.setContentView(R.layout.auto_layout);
AutoCompleteTextView auto_tv = (AutoCompleteTextView) dialog.findViewById(R.id.role_ac);
Ok. Just figured it out. Looked at too much code to catch my mistake. Here's the solution.
dialog.setContentView(R.layout.auto_layout);
AutoCompleteTextView auto_tv = (AutoCompleteTextView)dialog.findViewById(R.id.role_ac);
I needed to specify to use the Dialog's layout and not the main application's layout that was implied by not using dialog.findViewByID.