0

In Android, is there a way to dynamically change the color of the view depending on what kind of background it is in?

For example, if I have a floating TextView with a white text color on a ListView with a black background, the text color will be visible clearly. However, as I scroll down, if the next section of ListView has a white background the texts in the TextView won't be visible anymore since it is white on white.

Is there any way in Android to change the text color of this TextView as it reaches the background with similar/same color?

JWL
  • 63
  • 3
  • 12

2 Answers2

0

not hard stuff

if view.getBackground() == .RED{
    yourStaff.text.setTextColor(Color.WHITE); 
}
Zoe
  • 27,060
  • 21
  • 118
  • 148
demopix
  • 159
  • 6
0

Well, it depends on the type of background.

If you are talking about a background as in a view(linearlayout, surfaceview, etc) wit ha color, you have to get the position the textview has on the screen, and set a color based on the background color at that point. But it depends on the type of color. A bitmap allows you to get a specific pixel:

bmp.getPixel(x, y);

And then you can determine the color based on that.

However, with a SurfaceView it becomes harder because there is no native method for that.

Further, a gradient-filled background makes it harder to get at a particular point. But if you use a view and have set the backgroundcolor, you can:

if (view.getBackground() == Color.RED){//View = the view with a background set using setBackground or android:background="@color or @drawable or whatever"
    tv.setTextColor(Color.WHITE); //then you adjsut the textview color
}

Please be aware that the above example only works if the view has a background defined as android:background or setBackground and there is a color. When you use a bitmap, you can get the pixel and the color based on that pixel


Community
  • 1
  • 1
Zoe
  • 27,060
  • 21
  • 118
  • 148