-1

I have a ListView with custom rows. They have different background colors, set ind the getView() method of an ArrayAdapter:

public View getView(int position, View convertView, ViewGroup parent) {
    ...
    bgResourceId = R.color.white;
    if (items.get(position).status.contentEquals("ok")) {
        bgResourceId = R.color.green;
    } else  items.get(position).status.contentEquals("error")) {
        bgResourceId = R.color.red;
    }
    row.setBackgroundResource(bgResourceId);
    return row;
}

Clicking on a row launches a new Activity. After finishing the new Actvity, we return to the Activity with the ListView and at his point I need to highlight one of the rows.

Inside the onResume() method, NOT after touching or clicking the row. I do know which row (index) will get highlihgted.

Just a simple animation - maybe blink for one second, and after that, return to its original background color. Something like this:

enter image description here

Is that possible?

Dusan
  • 2,586
  • 4
  • 18
  • 34
  • 2
    *Is that possible?* ... yes, it is – Selvin Dec 11 '17 at 09:44
  • Follow [This](https://stackoverflow.com/questions/5058291/highlight-listview-selected-row) and add a custom animation on the view. – ADM Dec 11 '17 at 09:46
  • Thanks @Selvin, now, could you please tell me, how to do it? – Dusan Dec 12 '17 at 10:12
  • 1
    You don't provide enough information. Please post your adapter code, including the code which assigns different background colors. And do tell us on what occasion you want the row to be highlighted - after an event (other than onTouch or onClick) or after a timer has finished or when? And also: do we know which row (index) will get highlighted? – kalabalik Dec 13 '17 at 15:16
  • I just updated the question. – Dusan Dec 13 '17 at 16:55

1 Answers1

1

This is not as trivial as it may seem. You have to

  1. Make sure that the timing is right. onResume will not let you access item views yet, therefore you should use onWindowsFocusChanged. Put this in your activity:

    Handler handler = new Handler();
    int blink = 4;
    int regularBg = android.R.color.holo_orange_light;
    int blinkingBg = android.R.color.holo_orange_dark;
    
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            final View view = adapter.getViewByPosition(blink, listView);
            view.setBackgroundResource(blinkingBg);
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    view.setBackgroundResource(regularBg);
                    listView.setOnScrollListener(null);
                }
            }, 1000);
        }
    }
    
  2. Access item views through a custom method you add to the adapter (won't work through the regular getView). It is from over here.

    View getViewByPosition(int pos, ListView listView) {
        final int firstListItemPosition = listView.getFirstVisiblePosition();
        final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;
    
        if (pos < firstListItemPosition || pos > lastListItemPosition) {
            return listView.getAdapter().getView(pos, null, listView);
        } else {
            final int childIndex = pos - firstListItemPosition;
            return listView.getChildAt(childIndex);
        }
    }
    
  3. Make sure that the item you want to highlight is visible under all circumstances. If you cannot guarantee this, because there may be very small devices or the item you want to highlight is further down the list than in the list you show in your question, you will have to scroll to the correct position first. Only after the scroll is finished, you can safely change the item background. Therefore you would have to implement an onScrollListener for your ListView. For now, until you tell me otherwise, I assume that this is not necessary.

Here is how it looks:

enter image description here

kalabalik
  • 3,465
  • 2
  • 18
  • 42
  • Actually i'm scrolling the view to the right position in onResume(); I'll give it a try and let you know. – Dusan Dec 14 '17 at 18:32
  • Not exactly how I imagined, but its a good starting point. Thank you for your help. – Dusan Dec 14 '17 at 22:11