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" />
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 use latest Android Studio and SDK. In preview & real device i see this:
My code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.myappname.view.AboutActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listViewAbout" />
</RelativeLayout>
How i make subtitle text color is gray? Like this:
I'm going out on a limb and assume that you're using the row layout simple_list_item_2.xml (based on the screenshot) which gives you two rows. The problem, if you may call it that, is that depending on the SDK version, the styling for this layout has changed.
On SDK 23, it looks like this:
However, on say SDK 19, it looks like this:
Why?
To understand this we first need to take a look at the xml that generates the rows from simple_list_item_2.xml, you'll see it's a pretty simple layout that uses the now deprecated view TwoLineListItem but that's just a plus on why to use your custom layout.
<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/listPreferredItemHeight"
android:mode="twoLine"
android:paddingStart="?attr/listPreferredItemPaddingStart"
android:paddingEnd="?attr/listPreferredItemPaddingEnd">
<TextView android:id="#id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:textAppearance="?attr/textAppearanceListItem" />
<TextView android:id="#id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/text1"
android:layout_alignStart="#id/text1"
android:textAppearance="?attr/textAppearanceListItemSecondary" />
</TwoLineListItem>
The reason is because of the way the style textAppearanceListItemSecondary is resolved in each SDK version. The style is what gives the text the size, the color, etc. The evolution of the interface in Android has given birth to a huge ecosystem of themes and relying on the default styling will result in inconsistencies like the one you stumbled upon.
What to do about it?
You should use your own layout for this to allow for uniform styling across versions. To do so, please refer to any of the multiple questions covering this matter. But in short it just means creating a layout file, call it for example custom_row.xml and having the layout look exactly as you please. This also gives you total control over placement of the items, extra Views that you may need, and overhead in terms of coding is minimal compared to the SimpleAdapter or ArrayAdapter that perhaps you were using.
Note
You should consider moving your code towards RecyclerView instead of ListView if you haven't already.
You can set Textview property
android:textColor="#color/grey"
in you Adapter layout to change colour of your sub item
Hope this will help
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'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);
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 .