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);
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:
Note: I used some Android default layouts, but they can be changed to your layouts in a few seconds.
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!
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;
}
});
How to change background color of first item of a listview after setAdapter is used?
Try this
you can do that in getView method at your adapter and if you use ArrayAdapter
you can also override getView
method as following :
ArrayAdapter arrayAdapter = new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,strings){
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if(convertView==null) {
LayoutInflater Inflater = (LayoutInflater) MainActivity.this.getSystemService(MainActivity.this.LAYOUT_INFLATER_SERVICE);
view = Inflater.inflate(android.R.layout.simple_list_item_1, null);
}
else
{
view=convertView;
}
if(position==YOUR_TARGET_POSITION_TO_CHANGE_ITS_BACKGROUND)
{
view.setBackgroundColor(0x2196f3);
}
//Update Of textView
TextView txt1=(TextView)view.findViewById(android.R.id.text1);
text1.setText(strings[position]);
//Rest of your code
return view;
}
};
listView.setAdapter(arrayAdapter);
Change background color of listview to another color
You can use setNotifyOnChange(boolean) method and corresponding add(), remove etc. methods to control list state (adding, removing, changing items). Keep in mind, that changing state of backing array field won't trigger UI changes automatically without that. If you want to control changes manually, you can use notifyDataSetChanged() method of ArrayAdapter.
It's all because ArrayAdapter tries to instantiate views only once and reuse them for different array elements when scrolling down. View's state should be only modified in getView() which normally would be called only once per array element, when it's about to be rendered on screen first time. However, you can force 'redraw' using notifyDataSetChanged() at any time to keep UI state consistent with backing array field.
Related Topics
How to Get Imsi Number in Android Using Command Line
How to Display a Pdf Via Android Web Browser Without "Downloading" First
How to Keep a Bottom Nav Bar from Being Pushed Up on Keyboard Shown
How to Verify Purchase for Android App in Server Side (Google Play in App Billing V3)
How to Check If the Current Activity Has a Dialog in Front
Sending Files Using Post With Httpurlconnection
I Get Fragment Not Attached to a Context. What Context Need to Use
Android Studio - Textview Not Showing in Design View Layout
How to Make Service That Is Unremovable Even If Killed by the System
Recyclerview Indicates That Reaches the Last Item
How to Clear Navigation Stack After Navigating to Another Fragment in Android
How to Get the Json Response of a Post Request in a Webview
How to Show Only Specific Part of Website in Android Webview
Android Studio 0.4.3 - Task 'Assemble' Not Found in Root Project
Asking Camera Permission for Webview At Runtime
Chrome on Android Resizes Font
How to Get the Current Gps Location Programmatically in Android