0

There's an ImageView in my app with a drawable that can be changed by the user and a black TextView over it. If the user changes this image to one with a black background, he won't be able to see the text.

How can I identify if the image resource is R.drawable.imageA so I can set the text a different color?

I thought about setting a tag for the ImageView, but then I would have to set it for each option, when there's only two options I care about.

I tried:

if (imageview.getDrawable.equals(R.drawable.imageA)) {
    textView.setText(Color.WHITE);
    }

Also tried with getResource, but neither worked.

3 Answers 3

0

Aside from WebnetMobile.com's answer, you could just have a variable related to which image is currently displayed.

IE. When it starts up:

boolean isImageA = true;

And then change that to false when the event to change the background is triggered. All you have to do then is check whether it's true or false.

2
  • The problem is that I will have to set a value to every option. I have a scrollview with a lot of icons, and when the user clicks on icon X, it sets the imageview with a X image. If A is the one with the black background, and the person clicks on A, that's ok. But if after a while the person change to B, I would have to set B to false (otherwise, It would still be white), and C, and D.... Nov 9, 2012 at 0:46
  • Just use an integer then since it will be related to the item position anyway (IE. 0-10). Then you could easily compare the integer with the appropriate text color.
    – Cruceo
    Nov 9, 2012 at 0:52
0

You can't. You need to store it yourself on side and use for your comparisons, or extend ImageView and embed that functionality inside (i.e. by adding getDrawableId())

0
  1. Are you going to provide the user with a Dialog box / selection mechanism where the user gets to pick the image ?

  2. If the answer to the above is Yes, then this is the place where you should be able to capture this decision within your application, and then modify the color of the TextView .

  3. If the answer to 1 is no, then how does the user change the Image within your application ?

In your case, you may also be able to use reflection to achieve your goal. For example, take a look at this thread -> Android, getting resource ID from string?

Added later:

For a truly generalized solution, you may want to inspect the ImageView itself, to determine the optimal color to display. This has been demonstrated in this thread: Automatically change text color to assure readability

2
  • 1. No. 3.There's a scrollview with icons, and when the user clicks on one of them, the imageview is set with an especific image according to the icon. Nov 9, 2012 at 0:40
  • OK, assuming that there are two separate Activities on which this happens, as soon as the user makes a decision you can capture this state as boolean ( black text / white text ), and then when you launch the second Activity you can pass in this information via Intent parameter, which you will then read within the second Activity's onCreate method and modify TextView's color... Nov 9, 2012 at 0:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.