how to change text size of android-spinner - android

How do I change the text size of a spinner in android.
I have read that if you get your items from a resource (like R.array.items) you use ArrayAdapter.createFromResource(context, R.array.items, R.layout.textview), but what if a get my items from a list that changes, how would I then change text size of the spinner.

You need to redefine 2 resource xml's
Adapter dropdown view resource
Spinner item resource
Spinner spinner=new Spinner(context);
ArrayAdapter adapter = new ArrayAdapter(context, R.layout.my_spinner_item);
adapter.setDropDownViewResource(R.layout.my_spinner_dropdown_item);
E.g. my_spinner_dropdown_item.xml resource can be defined as:
<MyCheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/simpleSpinnerDropDownItem"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight" />
where MyCheckedTextView class has to be defined by you (extending CheckedTextView) - so it's a place where you can define your text size/style - whatever

Related

Error: You must supply a resource ID for a TextView

I have list that retrive a form my sql with (course id- course name).
I use a hash her for the list, I create an adapter which give me the list and I set it to spinner.
However there is a problem in log cat that said:
as Very well Explain by Luksprog :
The ArrayAdapter requires the resource ID to be a TextView XML exception means you don't supply what the ArrayAdapter expects. When you use this constructor:
new ArrayAdapter<String>(this, R.layout.a_layout_file, this.file)
R.Layout.a_layout_file must be an xml layout where the first element must be a TextView, something like this:
<?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"
//other attributes
/>
If you want your list row layout to be something a little different then a simple TextView element use this constructor:
new ArrayAdapter<String>(this, R.layout.a_layout_file,
R.id.the_id_of_a_textview_from_the_layout, this.file)
where you supply the id of a layout that can contain various views, but also must contain a TextView with and id(third parameter) that you pass to your ArrayAdapter so it can know where to put the Strings
If you check http://developer.android.com/reference/android/widget/ArrayAdapter.html#ArrayAdapter(android.content.Context, int, T[]), you will find that the array adapter is expecting that the second parameter is a resource id of a text view not a layout..
initialize your adapter using:
new ArrayAdapter(MainActivity.this, android.R.layout.simple_spinner_item, coursesList);

Changing ListView text color

I have ListView that shows data:
<ListView
android:id="#+android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
The headlines is an array:
List headlines;
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, headlines);
setListAdapter(adapter);
How to change the text color, style and size? There isn't any android:textColor on the ListView.
Find the file simple_list_item_1.xml from your sdk installation, copy it to your project's layout folder, rename it and make whatever color/text changes you want in there. Then replace the layout call in your program with your new layout name (don't forget that since you will be using your own list row layout you need to remove the android. from in front of the layout call).
It should look something like this:
ArrayAdapter adapter = new ArrayAdapter(this, R.layout.your_row_layout, headlines);
Hope this helps!

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

How to show own view in Spinner Widget instead of Text View?

I have a selection for some items using a spinner widget. At the moment the spinner will load a simple Textview and show the name of the item.
It is possible to define an own view to show inside the spinner row? I would suspect it being similar to a custom List row.
I simply want to show an individual icon left from the spinner text for each item in the list.
Yes, you can do that - as you may know, Spinner is a type of AdapterView, which means that you can adapt your own data to show in such View.
The approach is pretty similar to the one with ListView.
Excerpt from ApiDemos:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, INTERPOLATORS);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(adapter);
You can define your own layout and pass it to the SpinnerAdapter subclass that you use.
Yes, it is very easy.
The main thing is that, define TextView as the root element in the layout file.
Do as below:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, INTERPOLATORS);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(adapter);
simple_spinner_dropdown_item.xml file :
<?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="50px"
android:textColor="#000000"
android:textSize="20px"
android:gravity="center_vertical" >
</TextView>

Categories

Resources