I am creating an arrayadapter with this line:
adapter=new ArrayAdapter<String>(getActivity(),R.layout.message_bar,strArr);
And this is my message_bar.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:layout_marginRight="6dp"
android:layout_marginLeft="4dp"
android:paddingTop="10dp"
android:paddingLeft="15dp"
android:paddingRight="5dp"
android:gravity="left"
android:layout_gravity="left"
android:text="dfgdfgdf"
android:textSize="12dp"
android:background="#drawable/mes4" />
Can I change this xml's background and gravity before creating adapter with java ?
You have to use a custom adapter.
class Myadapter extends ArrayAdapter<String>{
LayoutInflater inflater=null;
public Myadapter(Context context, int resource, int textViewResourceId,
List<String> objects) {
super(context, resource, textViewResourceId, objects);
inflater = (LayoutInflater)getLayoutInflater();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row = convertView;
if(convertView==null)
row= inflater.inflate(R.layout.custom_list_item, null);
// CHANGE COLOR HERE
((TextView)row.findViewById(R.id.txtTitle)).setBackground( Color.Red );
return row;
}
}
or override getview:
new ArrayAdapter<String>(getActivity(),R.layout.message_bar,strArr)
{
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
if(convertView==null)
{
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row =vi.inflate(R.layout.message_bar, null);
}
TextView txt= (TextView)row.findViewById(R.id.message);
//CHANGE COLOR HERE
return row;
}
};
Related
I used textview and checkbox as items of listView. My code that i wrote for listview worked properly. But when I have added checkbox into the listview onclicklistener event don't work now. I use this code in xml file:
My xml code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="5" >
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="#+id/textView_Item_Listview"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:gravity="right"
android:padding="3dp"
android:textSize="25sp" />
</LinearLayout>
My java code:
private class Adapter_collection extends ArrayAdapter<String> {
public Adapter_collection(Context context, int resource, int textViewResourceId,
String[] name_collection_tbl_collection) {
super(context, resource, textViewResourceId, name_collection_tbl_collection);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.listview_items, parent, false);
TextView txt_item_list_collection = (TextView) row.findViewById(R.id.textView_Item_Listview);
CheckBox checkBox=(CheckBox)row.findViewById(R.id.checkBox_Item_Listview);
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked ) {
Toast.makeText(getApplicationContext(), String.valueOf(position), Toast.LENGTH_SHORT).show();
}
});
txt_item_list_collection.setText(name_collection_tbl_collection[position]);
return row;
}
}
list_collections.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Intent goTo_SubjectsActivity = new Intent(Collection_List_Activity.this, Subjects_Activity.class);
startActivity(goTo_SubjectsActivity);
Intent_values.id_collection = id_tbl_collection[arg2];
}
});
The possible Causes are if you have touchable elements in your List_item,it will block the list_item Touch,so onItemclickListener on your ListView Wont Work.
if you want to implement onClickListener to your ListView you can add
android:descendantFocusability="blocksDescendants"
in you list_item parent that is inside LinearLayout
Try to assign the onclicklistener to the textview, instead of the list.
private class Adapter_collection extends ArrayAdapter<String> {
...
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.listview_items, parent, false);
TextView txt_item_list_collection = (TextView) row.findViewById(R.id.textView_Item_Listview);
//add this
txt_item_list_collection.setOnClickListener(new OnClickListener() { ... });
txt_item_list_collection.setText(name_collection_tbl_collection[position]);
return row;
}
}
can i make the size of my item and especially the dropdownitems the same size as the spinner? They are a little bit smaller at the moment. i want them to fill the whole Spinner.
Can anyone help me?
edit:
it looks like this:
http://image-upload.de/image/vjYswg/cbc92fd3e9.jpg
and i want this:
http://image-upload.de/image/JdVZ5z/1f73d79751.jpg
oh and i have to use API 14, so i can't set dropdownwidth
i have set the dropdownviewresource to the following xml:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape = "rectangle">
<solid
android:color="#666666" />
<corners
android:radius="3dp" />
</shape>
edit: sry, this was my spinner layout, for dropdown_items i use
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="43dp"
android:orientation="horizontal" >
<ImageView
android:layout_height="30dp"
android:layout_width="30dp"
android:layout_marginRight="5dp"
android:layout_gravity="center_vertical"
android:contentDescription="#layout/spinner_item"
android:src="#drawable/circledsync" />
<de.util.FontDropDownTextView
android:id = "#+id/spinner_dropdown_item_textView"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="0.90"
android:gravity="center_vertical"
android:textColor="#color/spinner_text" />
</LinearLayout>
i also use a custom array adapter:
public class MyArrayAdapter extends ArrayAdapter<String> {
private int myTextId = 0;
private int myDropDownResource;
private int myDropDownTextId;
private int myResource;
private LayoutInflater myInflater;
public MyArrayAdapter(Context context, int textViewResourceId, List<String> names) {
super(context, textViewResourceId, names);
myTextId = myDropDownTextId = textViewResourceId;// remember it, so we can find the real TextView
myInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public MyArrayAdapter(Context context, int layoutId, int textViewResourceId, List<String> names) {
super(context, layoutId, textViewResourceId, names);
myTextId = myDropDownTextId = textViewResourceId;// remember it, so we can find the real TextView
myResource = myDropDownResource = layoutId;
myInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void setDropDownViewResource(int resource) {
super.setDropDownViewResource(resource);
myDropDownResource = resource;
}
public void setDropDownViewResource(int resource, int textId) {
super.setDropDownViewResource(resource);
myDropDownResource = resource;
myDropDownTextId = textId;
}
public View getView(int position, View convertView, ViewGroup parent) {
View cell = MyGetView(position, convertView, parent, myResource, myTextId);
setFontForChild(cell);
return cell;
}
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View cell = MyGetView(position, convertView, parent, myDropDownResource, myDropDownTextId);
setFontForChild(cell);
return cell;
}
private void setFontForChild(View layoutCell)
{
View realTextView = layoutCell.findViewById(myTextId);
if(realTextView instanceof TextView)
{
Typeface tf = Typeface.createFromAsset(
getContext().getAssets(),"fonts/calibri.otf");
((TextView)realTextView).setTypeface(tf);
}
//else realTextView is null or is not a TextView.
}
private View MyGetView(int position, View convertView, ViewGroup parent,
int resource, int fieldId) {
View view;
TextView text;
if (convertView == null) {
int res = (resource==0)? fieldId : resource;
view = myInflater.inflate(res, parent, false);
} else {
view = convertView;
}
try {
if (fieldId == 0) {
// If no custom field is assigned, assume the whole resource is a TextView
text = (TextView) view;
} else {
// Otherwise, find the TextView field within the layout
text = (TextView) view.findViewById(fieldId);
}
} catch (ClassCastException e) {
Log.e("ArrayAdapter", "You must supply a resource ID for a TextView");
throw new IllegalStateException(
"ArrayAdapter requires the resource ID to be a TextView", e);
}
String item = getItem(position);
if (item instanceof CharSequence) {
text.setText((CharSequence)item);
} else {
text.setText(item);
}
return view;
}
I got same problem. I changed style of my Spinner:
<item name="android:paddingLeft">0dp</item>
<item name="android:paddingRight">0dp</item>
It resolved my problem.
Try this in java code:
spinner.setPopupBackgroundResource(R.drawable.spinner_style);
And in your spinner_style.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#ffffff" />
</shape>
Add a call like this:
Spinner spinner = (Spinner)findViewById(...);
spinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Try this in xml file:-
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="14pt"
Use size as per your requirement.
Try with custom Spinner it will solve your problem.
Sample:
Initiate:
spinner.setAdapter(new MyCustomAdapter(AssessmentActivity.this,
R.layout.spinner_test, spinnerData));
In your custom adapter:
public class MyCustomAdapter extends ArrayAdapter<String>{
List<String>draft_filter_array_ = new ArrayList<String>();
public MyCustomAdapter(Context context, int textViewResourceId,
List<String> draft_filter_array) {
super(context, textViewResourceId, draft_filter_array);
draft_filter_array_ = draft_filter_array;
}
#Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
return getCustomView(position, convertView, parent);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//return super.getView(position, convertView, parent);
LayoutInflater inflater=getLayoutInflater();
View row=inflater.inflate(R.layout.spinner_test, parent, false);
TextView label=(TextView)row.findViewById(R.id.weekofday);
label.setText(draft_filter_array_.get(position));
return row;
}
}
And in your spinner_test.xml :
<?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" >
<TextView
android:id="#+id/weekofday"
android:layout_width="match_parent"
android:layout_height="42dp"
android:layout_gravity="center"
android:gravity="center"
android:text="Sunday"
android:textColor="#000000"
android:textSize="18sp"
/>
</LinearLayout>
I want to make a custom ListView with buttons as Items. Now I'm using the OnItemClickListener and for each click, a toast message comes up, but it only comes up, if i press out of the buttons, but in the ListView(see picture)
How can I do it, that the toast comes, if i press on the buttons, not out of them.
Adaptercode:
public class MainListAdapter extends ArrayAdapter<Games> {
Context context;
int layoutResourceId;
Games data[] = null;
public MainListAdapter(Context context, int layoutResourceId, Games[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
View row = convertView;
ListHolder holder = null;
if(row == null){
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ListHolder();
holder.btn = (Button)row.findViewById(R.id.listViewButton);
row.setTag(holder);
}else{
holder = (ListHolder)row.getTag();
}
Games games = data[position];
holder.btn.setText(games.name);
return row;
}
static class ListHolder {
Button btn;
}
}
In the Acitivty:
MainListAdapter adapter = new MainListAdapter(this, R.layout.listview_item_row, games_data);
listView1 = (ListView)findViewById(R.id.listView1);
final View header = (View)getLayoutInflater().inflate(R.layout.listview_newgame_row, null);
listView1.addHeaderView(header);
listView1.setAdapter(adapter);
listView1.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(getApplicationContext(), "clicked", Toast.LENGTH_LONG).show();
}
});
listview_item_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/listViewButton"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:focusable="false"
android:background="#drawable/button" />
IN your Adupter class make two objects for button.
And setonclicklistner for the buttons in adupter view
E.g.
vi = inflater.inflate(R.layout.list_row_contact, null);
LinearLayout ll_adpt_main = (LinearLayout)vi.findViewById(R.id.ll_adpt_main);
TextView name = (TextView) vi.findViewById(R.id.name);
TextView discription = (TextView) vi.findViewById(R.id.number);
ImageView iv = (ImageView)vi.findViewById(R.id.iv);
iv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
return vi;
}
Make a separate onclicklistener for buttons. OnItemclicklistener is for a list item.
Firstly you have to create an xml layout like this.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
Then create an adapter class:
class MyAdapter extends ArrayAdapter<View.OnClickListener> {
public MyAdapter(Context context, int textViewResourceId, View.OnClickListener[] objects) {
super(context, textViewResourceId, objects);
}
public MyAdapter(Context context, int resource, int textViewResourceId, List<View.OnClickListener> objects) {
super(context, resource, textViewResourceId, objects);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.event_layout, null);
Button b = (Button) convertView.findViewById(R.id.btn);
View.OnClickListener l = getItem(position);
b.setOnClickListener(l);
return convertView;
}
}
The layout of the activity should be like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LayoutActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".YourActivityName" >
<Button
android:id="#+id/btnHeader1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="#+id/btnHeader2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ListView
android:id="#+id/your_list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
Now to set list items:
ListView list = (ListView)findViewById(R.id.your_list);
View.OnClickListener[] listeners = ....;
MyAdapter adapter = new MyAdapter(context, R.layout.your_list_layout_adapter, listeners);
list.setAdapter(adapter);
I hope to have satisfied all your requests :)
I made a listview in a fragment. Now I need to add a simple icon to the listview items. The icon must be the same on every item. This will be a simple arrow (imageview).
Is this possible with the listview I made? And if yes, how do I do it?
So like this format:
My text here --space--space-- >
The code for the fragment:
public class BiblioFragment extends Fragment {
final String[] items = new String[] { "Astma en alcohol", "Astma en huisdieren", "Astma en lichaamsgewicht",
"Astma en ouder worden", "Astmamedicatie", "Bekende mensen met astma", "Longfunctieonderzoek", "Reizen en vakantie",
"Sociaal leven", "Weetjes over astma", "Enzovoort", "Enzovoort", "Enzovoort" };
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_biblio, container, false);
ListView list = (ListView)view.findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, items);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Log.v("TAG", "CLICKED row number: " + arg2);
Intent myIntent = new Intent(getActivity(), BiblioDetail.class);
myIntent.putExtra("welkerij", arg2);
startActivity(myIntent);
}
});
return view;
}
}
My XML for that fragment:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff">
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</LinearLayout>
In onCreateView
ListView list = (ListView)view.findViewById(R.id.listView1);
CustomAdapter cus = new CustomAdapter(getActivity(),items);
list.setAdapter(cus);
Use a custom adapter
public class CustomAdapter extends BaseAdapter {
String items[];
LayoutInflater mInflater;
public CustomAdapter(Context context, String[] items) {
mInflater = LayoutInflater.from(context);
this.items = items;
}
#Override
public int getCount() {
return items.length;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView ==null)
{
convertView = mInflater.inflate(R.layout.list_item,parent,false);
holder = new ViewHolder();
holder.tv = (TextView) convertView.findViewById(R.id.textView1);
holder.iv = (ImageView) convertView.findViewById(R.id.imageView1);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.tv.setText(items[position])
// use holder.iv to set whatever image you want according to the position
return convertView;
}
static class ViewHolder
{
ImageView iv;
TextView tv;
}
}
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="42dp"
android:layout_marginTop="32dp"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/imageView1"
android:layout_marginLeft="64dp"
android:layout_marginTop="14dp"
android:layout_toRightOf="#+id/imageView1"
android:text="TextView" />
</RelativeLayout>
Snap
Edit:
If you feel this is over kill you can use a compound textview with image on the left and text on the right.
In the above I have used list_item.xml which is a custom layout. I inflate that layout in getView of custom adapter. I set the text to textview. I have set the default launcher icon to imageview. You can change it though.
I have also used a ViewHolder
http://developer.android.com/training/improving-layouts/smooth-scrolling.html
Also check
How ListView's recycling mechanism works
Create ListAdapter
public class ListAdapter extends ArrayAdapter<Item> {
public ListAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
// TODO Auto-generated constructor stub
}
private List<Item> items;
public ListAdapter(Context context, int resource, List<Item> items) {
super(context, resource, items);
this.items = items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.itemlistrow, null);
}
Item p = items.get(position);
if (p != null) {
ImageView ICon = ( ImageView) v.findViewById(R.id.description);
}
}
return v;
}
In your Activity
ListView yourListView = (ListView) findViewById(R.id.itemListView);
// get data from the table by the ListAdapter
ListAdapter customAdapter = new ListAdapter(this, R.layout.itemlistrow, List<yourItem>);
yourListView .setAdapter(customAdapter);
I'm creating a Spinner dynamically like the code below:
private void createCoordinationSpinner() {
TextView tvwCoordination = new TextView(this);
this.spinnerCoordination = new Spinner(this);
//Some code to setup textview...
LinearLayout.LayoutParams paramsSpinnerCoordination = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
paramsSpinnerCoordination.setMargins(10, 0, 10, 0);
this.spinnerCoordination.setLayoutParams(paramsSpinnerCoordination);
this.spinnerCoordination.setBackgroundResource(R.drawable.rounded_border);
this.spinnerCoordination.setPrompt("Coordination");
this.spinnerCoordination.setOnItemSelectedListener(spinnerItemClickListener);
//Adding both views to an existing LinearLayout, that is possible to have another views, instead of spinner
linearSchoolCoordination.addView(tvwCoordination, 0);
linearSchoolCoordination.addView(spinnerCoordination, 1);
linearSchoolCoordination.setGravity(Gravity.BOTTOM);
}
//Thats the line that sets the adapter:
adapterCoordination = new ItemSpinnerAdapter(context, R.layout.coordinationspinnerlayout, arrayCoordination);
rounded_border.xml:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/gray_dialog" />
<padding android:left="8dp"
android:top="8dp"
android:right="8dp"
android:bottom="8dp" />
<corners android:radius="20dp" />
</shape>
coordinationspinnerlayout.xml:
<LinearLayout android:orientation="vertical"
android:background="#color/transparent2"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/transparent2"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/transparent2"
android:gravity="center_vertical" >
<ImageView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/imageSpinnerItem"
android:src="#drawable/itemlistviewicon_nochildren"
android:layout_marginTop="5dp"
android:layout_marginLeft="8dp"
android:layout_marginBottom="5dp"/>
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/tvwSpinnerCoordination"
android:layout_marginLeft="10dp"
android:textSize="16dip"
android:textColor="#color/black"
android:text="TextView"
android:layout_weight="1"/>
<ImageView android:layout_height="20dp"
android:layout_width="20dp"
android:id="#+id/imageSpinnerDownArrow"
android:src="#drawable/spinnerdownarrow"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_marginRight="10dp"/>
</LinearLayout>
<View android:background="#color/silver"
android:layout_height="1dp"
android:layout_width="fill_parent"
android:id="#+id/linearSpinnerSeparator"/>
</LinearLayout>
</LinearLayout>
And finally, the adapter, ItemSpinnerAdapter.java:
public class ItemSpinnerAdapter extends ArrayAdapter<CoordinationData>
{
private int tvwID;
private List<CoordinationData> coordinationList;
public ItemSpinnerAdapter(Context context, int textViewResourceId, List<CoordinationData> coordinationList)
{
super(context, textViewResourceId, coordinationList);
this.tvwID = textViewResourceId;
this.coordinationList = coordinationList;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
View view = convertView;
if (view == null)
{
LayoutInflater layInf = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layInf.inflate(tvwID, null);
holder = new ViewHolder();
holder.tvwDescription = (TextView) view.findViewById(R.id.tvwSpinnerCoordination);
holder.description = coordinationList.get(position).getCoordinationName();
view.findViewById(R.id.linearSpinnerSeparator).setVisibility(View.GONE);
view.setTag(holder);
}
else
{
holder = (ViewHolder) view.getTag();
holder.description = coordinationList.get(position).getCoordinationName();
}
if (holder.tvwDescription != null)
holder.tvwDescription.setText(holder.description);
return view;
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
View view = convertView;
if (view == null)
{
LayoutInflater layInf = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layInf.inflate(tvwID, null);
holder = new ViewHolder();
holder.tvwDescription = (TextView) view.findViewById(R.id.tvwSpinnerCoordination);
holder.tvwDescription.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
holder.description = coordinationList.get(position).getCoordinationName();
view.findViewById(R.id.imageSpinnerDownArrow).setVisibility(View.GONE);
view.setTag(holder);
}
else
{
holder = (ViewHolder) view.getTag();
holder.description = coordinationList.get(position).getCoordinationName();
}
if (holder.tvwDescription != null)
holder.tvwDescription.setText(holder.description);
return view;
}
public class ViewHolder
{
TextView tvwDescription;
String description;
}
}
adding it to a LinearLayout that already exists in my activity's XML layout file.
Along with my CustomSpinnerAdapter, it works pretty well.
But I still need to change some properties and I'm stucked...
Since my app is using blue texts and backgrounds, I'd like to set the selection of a spinner item highlight color. Its default is orange.
Since I'm developing for API 10, I'm using limited Spinner methods.
Any suggestions?
Thanks in advance!
EDIT: is that possible to change prompt header background color?
EDIT2: adding further code.
EDIT:
You're using a spinner, so you need to implement [getDropDownView][1].
If you want to change the selected color of your View you need to create a Color List Drawable like so
In Colors/list_color.xml
<selector>
<item android:state_selected="true">SomeColor</item>
</selector>
in your view
<YourView
android:background="#color/list_color" />
See this resource for more info
take the custum adapter
mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View arg1,
int position, long arg3) {
_position = position;
LinearLayout linear_ayout = (LinearLayout)parent.getChildAt(0);
linear_ayout.setBackgroundColor(Color.TRANSPARENT);
TextView tv = (TextView) linear_ayout.getChildAt(0);
tv.setTextColor(Color.WHITE);
gname_spinner=mySpinner.getSelectedItem().toString();
if (mySpinner.getSelectedItem().toString().equalsIgnoreCase("All Groups"))
filItemAdapter("A", "");
else
filItemAdapter("G", mySpinner.getSelectedItem().toString());
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
Logger.d("******", "not selected");
filItemAdapter("A", "");
}
});
MyCustomAdapter custom_adapter = new MyCustomAdapter(
getApplicationContext(), R.layout.custum_spinner_group,
group_list);
mySpinner.setAdapter(custom_adapter);
public class MyCustomAdapter extends ArrayAdapter<String> {
List<String> group_list;
public MyCustomAdapter(Context context, int textViewResourceId,
List<String> group_list) {
super(context, textViewResourceId, group_list);
this.group_list = group_list;
}
#Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView,
ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.custum_spinner_group, parent, false);
TextView text_group = (TextView) row.findViewById(R.id.text_group_name);
LinearLayout layout = (LinearLayout) row
.findViewById(R.id.layout_group_name);
text_group.setText(group_list.get(position));
if (position == _position) {
layout.setBackgroundColor(getResources().getColor(R.color.spinner_background));
text_group.setTextColor(getResources().getColor(R.color.white));
} else {
layout.setBackgroundColor(Color.TRANSPARENT);
text_group.setTextColor(getResources().getColor(R.color.spinner_background));
}
Log.d("selected group", ":"+text_group.getText());
return row;
}
}
After long time searching for a solution,
I found out that I can't set style or change its selection color dynamically 'cause of the API version that I'm developing.