Changing Background Color of Listview Items on Android

Android - Change background color of specific item of ListView

Override getView for ArrayAdapter. Based on the condition change color or do what is required. You have already provided params to ArrayAdapter constructor. So there is no need to inflate the view again. Get the same and do what is required. Similarly getItem(position) get's the data at a specidifed position.

http://developer.android.com/reference/android/widget/ArrayAdapter.html#getItem(int)

listViewMother.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayListMother) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = super.getView(position, convertView, parent);

if(getItem(position).equals("Somebody gets killed"))
{
// do something change color
row.setBackgroundColor (Color.RED); // some color
}
else
{
// default state
row.setBackgroundColor (Color.WHITE); // default coloe
}
return row;
}
});

Change item background color in simple listView

You're missing the point that the ListView re-uses its item layouts when they go out of screen (i.e. you scroll the list).

You need to save the background for each of your item and set it once the view is requested. That happens inside of ListView adapter's getView.

A quick fix would be to use a custom adapter on the go:

final boolean[] selectedItem = new boolean[listElement.length];

ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,
android.R.layout.simple_list_item_1, list1) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
if (selectedItem[position]) {
view.setBackgroundColor(Color.parseColor("#66F44336"));
} else {
view.setBackgroundColor(Color.parseColor("#EEEEEE"));
}
return view;
}
};

This lacks error checking but you should get the idea. Good luck!

Change background color of ListView Items if MATCH found (Android ListView)

In my opinion, an Activity shouldn't take care how an item from ListView is displayed, letting this job for an adapter (you're doing this in onCreate without having the rows for ListView created, resulting in a crash).

So what I would do is to create a class that has 2 fields: String value where you have your actual data to be displayed and a boolean hasBackground that indicates if that item should be displayed with a custom background or not. That class should look like this:

public class ListItem {
public String value;
public boolean hasBackground = false;

public ListItem(String value, boolean background) {
this.value = value;
this.hasBackground = background;
}
}

Then I would make the forexInstruments to have elements of type ListItem. I would add the elements like this: forexInstruments.add(getDefaultItem("USDGPY"));, where getDefaultItem is method that returns ListItem with background set to false.

private ListItem getDefaultItem(String value) {
return new ListItem(value, false);
}

After the list is initialised it have to be updated, so I created a method to find the common elements between those 2 lists, setting the hasBackground field to true for those elements.

private void updateCommonElements() {
for (int j = 0; j < selectedGlobalInstruments.size(); j++) {
for (int i = 0; i < forexInstruments.size(); i++) {
String instrumentList = "'" + forexInstruments.get(i).value + "'";
if (selectedGlobalInstruments.get(j).equals(instrumentList)) {
forexInstruments.get(i).hasBackground = true;
}
}
}
}

In the end, the onCreate would look like this:

forexInstruments = new ArrayList<>();
forexInstruments.add(getDefaultItem("USDGPY"));
forexInstruments.add(getDefaultItem("AUDUSD"));
forexInstruments.add(getDefaultItem("EURGBP"));
forexInstruments.add(getDefaultItem("USDJPY"));
forexInstruments.add(getDefaultItem("GBPUSD"));
forexInstruments.add(getDefaultItem("EURGPY"));
forexInstruments.add(getDefaultItem("USDCHF"));

updateCommonElements();

forexListView = (ListView) findViewById(R.id.listView);
MyAdapter adapter = new MyAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1);
adapter.addAll(forexInstruments);
forexListView.setAdapter(adapter);

where MyAdapter is a class extending ArrayAdapter, being responsible for colouring the background of a row or not.

public class MyAdapter extends ArrayAdapter<ListItem> {
int res = 0;
int textRes = 0;

public MyAdapter(Context context, int resource, int textViewResourceId) {
super(context, resource, textViewResourceId);
res = resource;
textRes = textViewResourceId;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(res, parent, false);
}
ListItem item = getItem(position);
if (item.hasBackground) {
convertView.setBackgroundColor(Color.BLUE);
} else {
convertView.setBackgroundColor(Color.TRANSPARENT);
}
TextView textView = (TextView) convertView.findViewById(textRes);
textView.setText(item.value);
return convertView;
}
}

And this is the final result in my simulator:
screenshot

Note: I used some Android default layouts, but they can be changed to your layouts in a few seconds.

How to change the color background of ONE item of listView

i changed your code try this

        final ArrayAdapter<String> adapter = new ArrayAdapter<String>(parentActivity, android.R.layout.simple_list_item_1, accountList) {


@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
ViewGroup.LayoutParams params = view.getLayoutParams();
view.setLayoutParams(params);

TextView textview = (TextView) view;
textview.setTextSize(15);
textview.setGravity(Gravity.CENTER);
textview.setTextColor(Color.WHITE);


if (Register.getAccounts().get(position).getNumber().equals(accountNumber) ){

textview.setBackgroundResource(R.color.colorPrimaryDark);

}else textview.setBackgroundResource(0);



return textview;
}
};

listView.setAdapter(adapter);


Related Topics



Leave a reply



Submit