4

I'm trying to change the foreground color and get the foreground color of a relative layout but I'm getting a warning that it is only available for API 23 (my app minimum is API 17).

I can't use the color as background since the appearance of the view is way different compared to applying a foreground color.

Is there a way to change the foreground color of the relative layout even on devices running on API 17?

EDIT:

This is what I've tried so far

programEventItem.setForeground(ContextCompat.getDrawable(context, R.color.light_gray));

EDIT 2:

This is what I'm trying to achieve. The user will be given tasks (let's say 4 or 14) and at first only first task will be unlocked to complete. The user has to complete these tasks one after the other (linearly). He/she can't skip the order and try 3rd one without doing the first two (or do a 10th task without doing 9 before it). If the user clicks on a task they unlocked then they will be shown the task to complete it. But, if the user clicks on a task that is locked a dialog will show reminding them that they have to complete the tasks in the order given to them. This is the reason these locked views should be clickable but their foreground should be a different color.

Srikar Reddy
  • 3,650
  • 4
  • 36
  • 58
  • `"I'm trying to change the foreground color and get the foreground color of a relative layout"` `RelativeLayout` does not have "foreground color", what it may have, however, is a "background color" – pskink Dec 15 '16 at 07:17
  • Simply, I'm applying a color as the foreground. I'm doing this to differentiate the items that accessible and ones that aren't accessible for the user to view. – Srikar Reddy Dec 15 '16 at 07:20
  • then see `StateListDrawable` and [state_enabled](https://developer.android.com/reference/android/graphics/drawable/StateListDrawable.html#attr_android:state_enabled) – pskink Dec 15 '16 at 07:23
  • I've already tried `StateListDrawable` & `setEnabled`. If I try `programEventItem.setEnabled(false)`, then the view isn't registering the click events. But, I want the view to be clickable. – Srikar Reddy Dec 15 '16 at 07:28
  • so you want the view to be clickable and "not accessible" ??? – pskink Dec 15 '16 at 07:31
  • yes. For more understanding, check my edit 2. Let me know, if you've doubts on what I'm trying to achieve? or why the view needs to have a different foreground color and still be clickable. – Srikar Reddy Dec 15 '16 at 07:41
  • `FrameLayout` is the only `View` with a foreground attribute prior to API 23. You could wrap your `RelativeLayout` layout in that, or just substitute it, if you don't really need a `RelativeLayout`. Or you could subclass `RelativeLayout` and override `dispatchDraw()` to draw the color over its children, after the `super` call. This is what I would do. – Mike M. Dec 15 '16 at 07:46
  • 1
    @MikeM.Thanks for the suggesstions. I've wrapped the `RrelativeLayout` inside `FrameLayout`. I wish this issue could have been addressed in one of the official support libaries. – Srikar Reddy Dec 15 '16 at 09:03

0 Answers0