1

I have to get the background color of the selected row in my listview programmatically. I wrote this for coloring the row:

View v;    
v.setBackgroundColor(context.getResources().getColor(R.color.childArticle));

But I can't figure out how to get the color of the row because I have to do something like this:

colorOfSelectedRow = v.getBackgroundColor();
if(colorOfSelectedRow == MY_COLOR) {
    // Do something
}

Thank you!

EDIT: I would like to know the color of the pressed row but not all the rows are colored with the same color!

Luca Alberto
  • 1,195
  • 3
  • 11
  • 30

3 Answers3

1

You can set background color id within Tag like below and also get Id of Color that is mentioned in color.xml

first set color code id into tag .

view.setTag(R.color.childArticle);

Then when you want to get background color get tag from view and parse its value and get color code from color.xml file.

int ColorId = Integer.parseInt(view.getTag().toString());
Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
  • and how can i get the color?? I need to retreive the color of the selected row in my listview when i press it. I tried this: `ColorDrawable viewBackground = (ColorDrawable) listViewOrder.getBackground(); int colorId = viewBackground.getColor();` but it gives me **-1**.. – Luca Alberto Feb 20 '17 at 09:42
  • you need to set color in tag as well . so tag holds color code id by this you can access color from color code. – Chetan Joshi Feb 20 '17 at 09:44
  • Your code give me always the same color but i have some rows with no background color and rows with red background color. when i press a row i would like to know if the selected row is red or not. with your code i have always the same result – Luca Alberto Feb 20 '17 at 09:48
  • I undestand the first part when I have to set the Tag, but how i said before, I am outside of the listview onclick listener. so how can i get the tag of the current selected row from outside? for eg. from the action of a button? (i already have the listview object inside the button action listener) – Luca Alberto Feb 20 '17 at 11:00
  • you can get Parent View of Button that is view in which you had set Tag. – Chetan Joshi Feb 20 '17 at 11:01
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/136145/discussion-between-luca-alberto-and-chetan). – Luca Alberto Feb 20 '17 at 11:03
0

This code will output the colour string ouf the clicked row or nothing if there is no colour background set. But you didn't specify how exactly you set the background colour.

ListView listView = (ListView) findViewById(R.id.listView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
adapter.add("Blue");
adapter.add("None");
adapter.add("Red");
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    int color;

    Drawable background = view.getBackground();
    if (background instanceof ColorDrawable) {
      color = ((ColorDrawable) background).getColor();
      Log.d("MainActivity", Integer.toHexString(color));
    }
  }
});
listView.setAdapter(adapter);
findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
    listView.getChildAt(0).setBackgroundColor(ContextCompat.getColor(MainActivity.this, android.R.color.holo_blue_dark));
    listView.getChildAt(2).setBackgroundColor(ContextCompat.getColor(MainActivity.this, android.R.color.holo_red_dark));
  }
});
Agraphie
  • 33
  • 2
  • 7
  • EDIT: it return always "ffffffff". are you sure is for the selected row? – Luca Alberto Feb 20 '17 at 09:54
  • can you check my updated answer? It is for the clicked row, but you can change it to the selected row. When do you want to get the colour? On click of at some stage when several rows are selected? – Agraphie Feb 20 '17 at 10:02
  • I am actualy outsite of the listView listener. Try to imagine to click a button and it gives to you the color of selected row in the listview. i don't know how to get the current selected row color of the listview from the outside of the setOnItemClickListener! – Luca Alberto Feb 20 '17 at 10:28
  • I understand. But you said you have access to the clicked view. The view is all you need here. Can you maybe post your full example? If you do not have the clicked view object, then you have to use Tags, a static variable or similar. – Agraphie Feb 20 '17 at 10:32
0
final int MY_COLOR = R.color.childArticle; 
View v;    
v.setBackgroundColor(context.getResources()
.getColor(R.color.childArticle));
view.setTag(R.color.childArticle);
Integer colorOfSelectedRow = (Integer) v.getTag();
if(colorOfSelectedRow == MY_COLOR) {
// Do something
}

Use this..

Yogeesh Hegde
  • 31
  • 1
  • 3
  • I'm writing a test, so I want to fetch the actual color, not a tag. It seems that for every set*() there should be a get*(), at least for test, right? – Phlip Aug 14 '19 at 18:33