How to change Gridview's cell colour dynamically? - android

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;
}
};

Related

How to change textColor of a spinner programmatically

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

How to make selection highlight of list rows with different colors?

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;
}

Color with different color in list view

I have a simple list view and i have to color every alternative row with different color.
Is it possible to color listView with different color.
Yes. Assuming ArrayAdapter, you will have to do something like this:
public View getView(int position, View convertView, ViewGroup parent) {
...
if (position % 2 == 0) { // Even numbered row
// set a color as background for view
} else { // Odd numbered row
// set another color as background for view
}
...
}
Is it possible to color listView with different color.?
Yes, its possible. You can use a listview with custom adapter.
Follow this tutorial ..
Yes, its possible , for this use getView()
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = (ViewHolder) convertView.getTag();
if (position == 0){
holder.layout.setBackgroundColor(Color.RED);
}
if (position == 1){
holder.layout.setBackgroundColor(Color.BLUE);
}
}
So On.....

Changing the background color of Spinner dropdown button

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

Android spinner behavior

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.

Categories

Resources