Spinner ArrayAdapter crashing with custom layout - android

I am trying to experiment with a custom Spinner / ArrayAdapter. Following is my code:
myspinner=(Spinner)findViewById(R.id.myspinner);
myadapter=new ArrayAdapter<String>(this,R.layout.mytextview,R.id.mytv,sample_data);
myadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
myspinner.setOnItemSelectedListener(this);
myspinner.setAdapter(myadapter);
Following is the custom layout mytextview that contains mytv:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:id="#+id/mylayout" android:layout_width="match_parent" android:layout_height="wrap_content">
<View android:layout_width="match_parent" android:layout_height="15dip" android:id="#+id/myview1" android:background="#color/bordertop"></View>
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="30dip" android:id="#+id/mytv"></TextView>
<View android:layout_width="match_parent" android:layout_height="15dip" android:id="#+id/myview2" android:background="#color/borderbottom"></View>
</LinearLayout>
When i'm press the spinner to bring the popup, Android crashes. The came logic works perfectly fine, when I comment out the setDropDownViewResource method. I still don't understand what view setDropDownViewResource actually sets. Can anyone show me how it looks like by default. And, if one wants to override it with a custom layout - does the layout need to have a specific structure?

here is what i have typically used for spinners:
Spinner distancespinner = (Spinner) findViewById(R.id.distance);
ArrayAdapter<CharSequence> distanceadapter = ArrayAdapter.createFromResource(
this, R.array.distance, android.R.layout.simple_spinner_item);
distanceadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
distancespinner.setAdapter(distanceadapter);
distancespinner.setOnItemSelectedListener(new distanceSelector());
(obviously replace distance with whatever you want)
<Spinner
android:id="#+id/distance"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:prompt="#string/app_name"
/>
to me it looks like your problem is in this line :
myadapter=new ArrayAdapter<String>(this,R.layout.mytextview,R.id.mytv,sample_data);
but then i dont know, your ArrayAdapter is based on instead of as i have and as per Spinner "the android.R.layout.simple_spinner_item ID references a layout for the standard spinner appearance, defined by the platform. Then setDropDownViewResource(int) is called to define the appearance for each item when the widget is opened (simple_spinner_dropdown_item is another standard layout defined by the platform)."
i think if you want to customize your spinner, .setDropDownViewResource(); is where to do it.

Related

how to change the text on an Android Spinner view element

the many other answers do tell me that android:textColor is the one that does the trick. However no matter what attribute I change in my styles, the app still shows default Black text.In fact, I cannot even see an attribute called textColor or the likes of it in the layout editor - attribs tab.
See here: Spinner with custom text font and color
Here you can make a custom xml file in layout folder where you can add this:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#333333"
android:padding="10dp"
android:textStyle="bold" />
And then in your code mention it like this:
val adapter = ArrayAdapter.createFromResource(this, R.array.array_name, R.layout.custom_spinner) // where array_name consists of the items to show in Spinner
adapter.setDropDownViewResource(R.layout.custom_spinner) // where custom-spinner is
mycustom xml file.
And then set the adapter.
I hope my solution would work,
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#ff0000" />
And then change your declaration of the spinner to use the R.layout.spinner_item:
ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.planets_array, R.layout.spinner_item);
spinner.setAdapter(adapter);

Customize Spinner from XML

I have a spinner that loads data from string-array and I would like to customize it from the XML layout and not from Java code (as I saw in many examples).
Is it possible to change the text color and the spinner style from XML?
Yes you can, here is how you add a spinner layout
item_spinner.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_margin="#dimen/small_margin"
android:background="#color/white"
android:ellipsize="marquee"
android:paddingLeft="#dimen/margin_small"
android:singleLine="true"
android:text="--Select--"
android:textColor="#color/text_title"
/>
and use in your adapter like this
ArrayAdapter<String> itemsAdapter =
new ArrayAdapter<String>(this,R.layout.item_spinner, items);
and you are done. customize but keep the textView id same that is how adapter will set text on it.

How to change spinner text color in the actionbar

in my project I have this custom layout inflated in the actionbar:
Link to image
But i would like to have the spinner text of white color.
I've seen a lot of topics on the web about this, but I can't fix the problem yet.
How can I fix this problem? How can I change the text color of my spinner bysetting it to white color?
EDIT:
the xml code of spinner is this:
<Spinner
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textColor="#ffffff"
android:id="#+id/spinner"></Spinner>
It's very simple, I tried many things but nothing..
In your activity you should have something like this:
(Basically you are getting the Spinner and setting it an adapter. This adapter is the responsible for filling it)
Spinner spinner = (Spinner) findViewById(R.id.your_spinner_id);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.your_selected_spinner_item, myArrayList<String));
adapter.setDropDownViewResource(R.layout.your_dropdown_item);
spinner.setAdapter(adapter);
And you can have this XMLs: (put them on res/layout)
your_selected_spinner_item.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myLayoutID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="20sp"/>
And also
your_dropdown_item.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myDropdownLayoutID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="20sp"/>
Create a Layout file like this:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/spinner_ref_id"
android:layout_width="match_parent"
android:padding="20dp"
android:textColor="#fff"
android:textSize="20sp"
android:text="Hello"
android:layout_height="wrap_content">
</TextView>
Assuming that you are using ArrayAdapter as your Spinner Adapter.
Hope it helps!!!

Adding LinearLayout to ListView using an adapter

I am trying to create a ListView where each entry in the list consists of a LinearLayout. I have an ArrayList that I have defined here:
ArrayList<LinearLayout> menuList;.
Later in my code, I define
LinearLayout dailyMenuLayout = new LinearLayout(ReturnMenus.this);, and every time I complete a layout to be added to the ListView, I use menuList.add(DailyMenuList)
The adapter that I have been trying to use is as follows -- but it crashes the app every time that it is triggered.
ListView myListView = (ListView)findViewById(android.R.id.list);
ArrayAdapter<LinearLayout> adapter = new ArrayAdapter<LinearLayout>(ReturnMenus.this, R.id.linear_layout_item, menuList);
myListView.setAdapter(adapter);
And this is the XML for the single row in the ListView:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linear_layout_item"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content" >
</LinearLayout>
</LinearLayout>
Can somebody show me how to accomplish this? Thanks.
Use something like following layout for the row layout in your list rather than you current layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
.
.
.
.
.
</LinearLayout>
Here if you do not know exactly how many textView that you are going to use, use maximum number of TextView that using in your application. Then in your adapter class inside getView() you can get those TextView. If some rows not going to use all of these textViews then set them invisible as like this:
TextView txtView3 = (TextView)findViewById(R.id.text3);
txtView3.setVisibility(View.GONE)
Following tutorial helps you to create the listView.
http://www.vogella.com/tutorials/AndroidListView/article.html
You should bind data to adapter,such as strings,not the layout.
To see how to use ListView and Adapter,please check the demo project in your-android-sdk-dir\samples\android-XX\ApiDemos.

Android custom layout for AutoCompleteTextView

I may be missing something simple here, but I have an AutoCompleteTextView that contains some very long items. When one is clicked, it shows the text correctly in the EditText and spans it over several lines.
However, I want it to be in multiple lines on the popup as well so that the users can see which item they are selecting.
Here is my custom layout:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="none"
android:maxLines="100"
android:scrollHorizontally="false" />
Here is my initialisation of the array and adapter:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.dropdown_item_wrap_line,
getResources().getStringArray(R.array.building_descriptions));
mBuildingDesc.setAdapter(adapter);
My guess is that your AutoCompleteTextView is limited to singleLine, so using a custom layout with default TextView should work neatly.
You will need to make a new Layout and name it custom_item.xml, then do it like this...
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/autoCompleteItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:textSize="14sp"
/>
</LinearLayout>
Then while using Adapter on AutoCompleteTextView do
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.custom_item, R.id.autoCompleteItem, StringArray);
textView.setAdapter(adapter);

Categories

Resources