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!
Related
When we build a ListView in android studio, we need to use an ArrayAdapter.
What is the task of second argument in constructor of ArrayAdapter ?
I cannot understand what is android.R.layout.simple_list_item_1 used for ?
This layout describe how the item list looks like.Maybe you want every item contain a text view and image.You should specify these details in this layout.
android.R.layout.simple_list_item_1 is a layout in which the data from your ArrayAdapter gets populated(added).
This is the id of the layout that you need to use for populating each of the item in the list. If it says android.R.layout that means you are going to use one of the standard android layouts. simple_list_item_1 This is the name of the file which will populate each row of the list. try changing this to simple_list_item_2 to see how the layout in list changes.
You can also use your custom adaptors and custom layouts(which would be in majority of the cases in day to day apps).
For full list of standard layouts available Go here
This argument defines how list items would appear in ListView, There are many layouts you can also try them out or you can make your own custom layout to modify apperance of listitems in Listview by using CustomAdapter.
1.create custom layout name custom_layout.xml & paste bellow code
<?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="match_parent"
android:textSize="16sp" >
</TextView>
Use like R.layout.custom_layout instead of android.R.layout.simple_list_item_1
for now radio button image on shown on right side of my spinnerand when I click on it, it open option list in popup box
what I want is to show arrow on right side of spinner and options list should be drop down instead of popup box with white background.
see image
How Can I do this, Do I need to create a custom spinner?
Here is the code
XML
<Spinner
android:id="#+id/type_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/text_color"/>
Java
type_Spinner = (Spinner) findViewById(R.id.type_spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, new String[]{"Buy","Sale","Rent","Let"});
type_Spinner.setAdapter(adapter);
1st approach
Change Spinner in xml like this
<Spinner
android:id="#+id/type_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/btn_dropdown" />
Change theme of that Activity to
android:Theme.Holo
In java class
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, new String[]{"Buy","Sale","Rent","Let"});
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
type_Spinner.setAdapter(adapter);
2nd approach
Instead of Spinner use a Button set background using android:background="#android:drawable/btn_dropdown" set gravity (not layout_gravity) to left|center_vertical open a PopupWindow on clicking of that Button set that Button as anchor of that PopUpWindow. In that PopUpWindow place a ListView and in OnItemClick change text with selected value in that Button using setText(java.lang.CharSequence)
Full code snippet for 2nd approach
If you use approach 1 then it will work on Post Gingerbread
version. Approach 2 will work on any version of android(not tested in
pre froyo).
In layout use the android:spinnerMode
android:spinnerMode="dropdown"
In activity use like this:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, new String[]{"Buy","Sale","Rent","Let"});
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
type_Spinner.setAdapter(adapter);
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);
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);`
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>