I overwrite the behavior of a spinner, to add odd and even colors to the drop down list, in this way.
SimpleCursorAdapter productsListAdapter = new SimpleCursorAdapter(MyActivity.this, R.layout.spinner_drop_down_products, cursor, column, viewIds) {
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View view = super.getDropDownView(position, convertView, parent);
if (position % 2 == 0) {
view.setBackgroundColor(android.graphics.Color.rgb(255, 255, 255));
} else {
view.setBackgroundColor(android.graphics.Color.rgb(214, 214, 214));
}
return view;
}
};
The dropdown rows look as i expected ... but i loose the highlight on pressing each row.
What i forget to add to the code?
Thanks
Actually android framework uses selectors for all states like:
normal enabled
pressed
highlight
focused
The default selector for list is list_selector_background.xml You can see here.
You have to provide this by yourself. Say you have made a you_own_selector.xml
then you will give this as a background of your View like this:
view.setBackgroundColor(R.drawable.you_own_selector);
Here is another nice post about using selectors in android.
Related
I am developing a simple android application for learning gridview. My requirement is to create a 2x2 grid which changes colours dynamically like a serial light circuit one by one, and others should be in white colour.
Please suggest a way to do this.
Create custom ArrayAdapter
new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, student_array) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
int color = 0x00FFFFFF; // Transparent
if (condition) {
color = 0x00000000; // Black
}
view.setBackgroundColor(color);
return view;
}
};
I have a question.
how to make highlight selection of row in List View by different colors? but I want to do it correct with list selectors.
Please provide some simple for few colors
thank you
You may set up colors in your Adapter.getView() method:
public View getView(int position, View convertView, ViewGroup parent) {
View result = ...
// check convertView, inflate new layout, bla-bla-bla
if (position % 2 == 0) {
result.setBackgroundResource(R.drawable.even);
} else {
result.setBackgroundResource(R.drawable.odd);
}
return result;
}
I have a dropdown navigation list in my ActionBar. I'd like to change the background color of the list item background depending on their position, as well as the currently selected item.
I've created my own class that extends ArrayAdapter and changes the background like so:
#Override
public View getView (int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
v.setBackgroundColor(Color.parseColor(VideoGroup.getColorForId(position)));
return v;
}
#Override
public View getDropDownView (int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
v.setBackgroundColor(Color.parseColor(VideoGroup.getColorForId(position)));
return v;
}
This however results in the following – notice that the actual list items look pretty good, but the currently selected one does not.
How can I get the background color for Group 1 here to fill up the navigation button's area as a whole?
Unfortunately, the Spinner itself is an image, so it will be hard to make it change colors because you'll ideally need an image for each color. You won't be able to make the background on the text expand beyond the triangle, for example. It can definitely be done, but it will probably mean customizing things quite a bit.
An alternative (and maybe simpler) way to apply a color mask to a view is to use a filter:
spinner.getBackground().setColorFilter(THE_COLOR_GOES_HERE, PorterDuff.Mode.MULTIPLY);
I would like to have a listView that the first list item will have a red background and the second will have black.Is that possible?And if yes,how will i create the custom list adapter?
thanks!!
|Black item|
|Red item|
|Black item|
|Red item|
|Black item|
etc.
You should Override getView in your arrayadapter. One of the parameters passed into this method is a position. So you can just do the position % 2 to determine if the row is even or odd. Depending on what you want to do you can change you can inflate two totally different layouts there.
When you have the public View getView(int position, View convertView, ViewGroup parent) method make it apply different style for each position %2 == 0. This way you can easily make those items differ from each other :)
I hope this helped.
here's the getView from my latest project's adapter. I've simplified it to highlight a couple of things: 1. that you can use whatever criteria you like to decide what kind of view will be returned, and 2. that you can use LayoutInflater.inflate to get any kind of view at all, whatever the case may be.
public View getView(final int position, View convertView, ViewGroup parent)
{
View v;
int n = itemList.get(position);
if (n < 0)
{
v = inflator.inflate(R.layout.layout1, null);
}
else if (n > 0)
{
v = inflator.inflate(R.layout.layout2, null);
}
else
v = inflator.inflate(R.layout.layout3, null);
return v;
}
I have a ListView (set to CHOICE_MODE_SINGLE)
I have a SimpleCursorAdapter who fill my ListView. Now i work on selection.
serviceListView.setAdapter(
new SimpleCursorAdapter(this, R.layout.service_listitem, cursor, new String[] { "name" }, new int[] { R.id.service_name }) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final View renderer = super.getView(position, convertView, parent);
if (position == newSelectedPosition) {
renderer.setBackgroundResource(R.layout.list_view_layer_list);
} else {
renderer.setBackgroundResource(android.R.color.transparent);
}
return renderer;
}
}
);
So i want when i select a row my layout to be applied. This works fine.
But in some cases when i push for 2-3 secs a row and then drag a little bit and release the row i obtain 2 rows selected.
I try several ways to get ride of the initial selection, overwriting OnTouchListener, OnScrollListener, OnLongClickListener. No results.
Any help is welcome
Thanks
Have you tried calling notifyDataSetChanged() on your Adapter after the selection has been changed? That should cause all the rows to be rebound again, and all the views (except for the selected one) to revert back to the transparent background.
I quit this implementation.
I will try to "simulate" ListView by using a ScrollView with TextView-s for each row