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.
Related
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" />
i am using spinner in my code which i had declare in my xml
xml code for spinner:-
<Spinner
android:id="#+id/spinner"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/button1"
android:layout_marginTop="133dp"
android:drawSelectorOnTop="true" />
so as can be seen that the spinner is down the one button which is also in that layout xml .Now when i run my app its loading all data correctly but the item is showing in the top of the spinner not in the down side of the spinner.Mentioning the screenshot how it appear
but when i m using the same spinner parallel to the button the spinner dropdown effect working very fine.Here is the screenshot how it appear.
so can any tell me how can we solve this kind of scenario.thanks for any reply.
try this hope this will help you
android:spinnerMode="dropdown"
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 collapsible menu item that is defined in XML like this:
<item
android:id="#+id/searchMenu"
android:icon="#android:drawable/ic_menu_search"
android:showAsAction="withText|always|collapseActionView"
android:actionLayout="#layout/collapsible_edittext"/>
Here's the collapsible_edittext.xml file:
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:imeOptions="actionSearch"
android:inputType="text"
android:hint="#string/search"/>
When the item is expanded, and you change the orientation of the phone, it crashes giving this error in logcat:
07-24 08:59:19.353: E/AndroidRuntime(1284): Caused by: java.lang.IllegalArgumentException: Wrong state class, expecting View State but received class android.widget.TextView$SavedState instead. This usually happens when two views of different type have the same id in the same hierarchy. This view's id is id/searchMenu. Make sure other views do not use the same id.
If I remove the android:id="#+id/searchMenu", everything works fine but I need the item id in my code. This error only happens on ICS (It even works in the jelly bean emulator) and I'm using ActionBarSherlock 4.1.0
I finally found the problem, in fact it was not even linked with ActionBarSherlock (I removed action bar sherlock from my project and it was still crashing).
I only needed to add an id to my EditText and that's it.
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/collapsibleEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:imeOptions="actionSearch"
android:inputType="text"
android:hint="#string/search"/>
I have a custom spinner layout with an image and a text view but I noticed that depending on the manufacturers skin you cant see the text because of the color ie. white on white, black on black.
I also noticed that none of my other non-custom spinners do this and seems to change automatically so my question is how can I get the text color to change so that it can be read?
this is a non-custom spinner
ArrayAdapter<CharSequence> cAdapter;
cAdapter = ArrayAdapter.createFromResource(this, R.array.colors,android.R.layout.simple_spinner_item);
int cSpinnerDD = android.R.layout.simple_spinner_dropdown_item;
cAdapter.setDropDownViewResource(cSpinnerDD);
color.setAdapter(cAdapter);
the custom spinner I just override the view to put the images in with text
here is the layout for it
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/icon_txt"
android:paddingLeft="25dp"
android:textSize="18sp"
android:layout_marginTop="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Use the default text color of the current theme (stored in the resource ID android.R.attr.textColorPrimary) :
<TextView android:id="#+id/icon_txt"
android:textColor="?android:attr/textColorPrimary"
... />
I think changing your theme changes the appearance of the listview.
I was also stucked at this point but what i did was that i made a new android app project and selected blank activity (in place of empty activity) and it worked (i know that's not a good solution but it solved my issue)