How can I set the height of the spinner? I mean the dropdown items's height.
For I found in SDK12 my spinner will show a long long item list to show the dropdown items.
But it shows to long , how can i set the height?
You can call setDropDownViewResource method and pass corresponding layout, on ArrayAdapter.
In this example im setting simple_spinner_dropdown_item resource which is higher than regular and has a radiobutton. I think you can pass your custom layout to.
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
And in case you are making custom layout, you'll have to declare TextView as root element in order to adapter fill it with text.
Maybe it could help..
ArrayAdapter yourSpinnerAdapter = new ArrayAdapter(this,
R.layout.spinner_item, yourItem) {
#Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
convertView = super.getDropDownView(position, convertView,
parent);
convertView.setVisibility(View.VISIBLE);
ViewGroup.LayoutParams p = convertView.getLayoutParams();
p.height = 100; // set the height
convertView.setLayoutParams(p);
return convertView;
}
};
Related
The size of a textview in a listview is 19SP. I want to set the first position item's font size bigger, e.g. 21SP. How can I do it programmatically?
Thanks in advance!
You need to adjust the getView method from your adapter to suit your needs.
The method getView as defined here provides you with the position of the item in the dataset you are trying to display.
All you need to do is customize the inflated view based on the position like you are asking
getView(int position,
View convertView,
ViewGroup parent){
//inflate the view
View v = LayoutInflator.from(parent).inflate(R.layout.your_layout,parent,false);
TextView txt = (TextView) v.findViewById(R.id.your_txt);
if(position == 0){
txt.setTextSize(//get the dimen from resources);
}
return v;
}
So I use one adapter for my Spinner and AutoCompleteTextView, which extends ArrayAdapter<Object>. For View-related method, I only override getView.
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView tv = (TextView) view;
//I do something with TextView here, but won't affect the problem
return view;
}
Now, both declared like this:
adapterSpinner = new CustomAdapter(
this, R.layout.custom_layout_item, objects);
adapterService.setDropDownViewResource(
R.layout.custom_dropdown_layout);
spinner.setAdapter(adapterSpinner);
adapterAutoComplete = new CustomAdapter(
this, R.layout.custom_layout_item, objects);
adapterAutoComplete.setDropDownViewResource(
R.layout.custom_dropdown_layout);
autoComplete.setAdapter(adapterAutoComplete);
The result confusing me:
AutoCompleteTextView
Applies custom_layout_item to its dropdown View instead, and instead applies a style I gave in its element in xml to its TextView/EditText layout. It ignores custom_dropdown_layout I've set.
Spinner
Applies correctly all of them, its TextView view using custom_layout_item, and its dropdown view using custom_dropdown_layout. Though it ignores the style I gave to it in xml.
Functional, both works fine. But from UI, quite a mess... Why did it happen?
I would like to change the text color of only one item in a listview.
This change will be triggered by the result of a running asynctask.
So far I searched on google and all I found was to overwrite the getView() function of the adapter, but this approach is kind of hard since I would need to keep the id of the rows I want to color in a global variable that will be accessed by getView().
Is there another way to simply set the text color of an item from a listview when an event happens ?
EDIT
I create the listview this way:
myListView = (ListView) findViewById(R.id.listView);
listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow);
listAdapter.add("test");
myListView.setAdapter(listAdapter);
For setting a color for a list item definitely you need to override the getView() method of the Adapter. Here is a small example for updating the color of the list item without using the id of the item.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.simplerow) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
if (position % 2 == 0) { //Place the condition where you want to change the item color.
view.setBackgroundColor(Color.GRAY);
} else {
//Setting to default color.
view.setBackgroundColor(Color.WHITE);
}
return view;
}
};
In the above example, all the list item at even number positions will be in GREY color and others will be WHITE color. We cannot do this without implementing the getView(). For reference Click Here
You may set custom object vis color in adapter then change color in this adapter and call notifyDataSetChanged()
I am using the following code to setup my spinner :
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.country_code_spinner_item,
countrySpinnerOptions){
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
((TextView) v).setTextColor(Color.parseColor("#808080"));
return v;
}
};
This assigns the default color of the text to "#808080"
Now i have also implemented setOnTouchListener method for this spinner. What i want is although initially the color text of the spinner is 808080. But after the spinner is touched and a new text is chosen from a list. The color now permanently changes to BLACK.
How do i implement this ?
Thanks
I have a listView that uses the
SimpleAdapter
Each element of this list contains 2 textView
I wanna color(change the backgroud color) an item of this list, how can I do?
To change the background color of the whole view (not just the textview) and using a very simplistic approach you would have this call:
private void setupAdapter() {
SimpleAdapter adapter = new SimpleAdapter(this, data, resource, from, to) {
public View getView(int position, View convertView, ViewGroup parent) {
View superView = super.getView(position, convertView, parent);
superView.setBackgroundColor(R.color.black); // or whatever color
return superView;
};
};
// use the adapter as: myListView.setAdapter(adapter);
}
this could be done using android selector (xml). see the example here
One way to create custom SimpleAdapter class which overrides getView method and there you make an background change depending - I guess - on some condition.