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.
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.
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())
Are you going to provide the user with a Dialog box / selection mechanism where the user gets to pick the image ?
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 .
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
Related
I am trying to make an app using App Inventor.
The app has more functions, but I am stuck at the I would say "basic" one.
What I want to do is basically change the background color (or color of any element) by calling the color from list stored in TinyDB.
I have two screens Screen1 and a Menu screen.
I am tring to call TinyDB while Screen1 initializes and set background color of app on the first in the list.
Further I want to let user to choose a color from list on the other screen "Menu". After picking a color change the color on both screens.
I tried to make it following the instruction https://sites.google.com/site/blocks123/playing-with-colors , but it is little bit confusing for me.
On the printscreen i have the TinyDB blocks on the Screen1.
Is it possible on Screen1 just call/initialize TinyDB but has the TinyDB blocks on other screen "Menu"?
And when I have TinyDB and lists of color how to change the color on the chosen one?
Thank you Taifun. Your tutorial was helpful for me, evethought it is bit confusing with variables which aren´t described there. Anyway I tried to do it a bit on my way and I think I am going the right direction.
As it shown on second image I made it on the "Menu" screen and trying to save my choice to [TinyDB].
It works almost fine but except the error when "Menu" screen inicialize. I don´t understand why I am getting this message while to value is in the list? And the other way how to pass the BCurrentColor variable to "Screen1"? If I use set.Screen1.BackgroundColor it doesntWork
Well now I know I am close, I can almost fell it in the air. It is properly change background color on "Menu" screen but some how it doesn´t saves the choice to [TinyDB] as I describes at comments. Where I went wrong?
I hope when I save the color "code" to database I can call it on the other screen.
As Taifun mentioned the problem was with the variables BCurrentColor and BCurrentColorName. After I changed them to values and the trigger after selecting color from list sets it to required color.
Then I can call it from TinyDB. It works fine.
I want to
use a TextView (or similar) to show dynamic text inside of a free-form shape (as shown below)
enable the user to change the text
render the result into a Bitmap
It's a relative easy task on iOS but it seems nobody did this on Android?!
I hope someone has an idea how to solve it, can't find anything on the net...
Any user of my app can create a new "Subject" and select an icon for it (from an icon set I define/provide). There is an ImageButton that presents the currently selected icon or the default icon, and when a user clicks on it, a popup with a gridview in it opens, allowing the user to change that icon into whatever icon he finds nice.
To do that I need an array or a list of the icons available for selection in order to make a grid view out of them.
The best solution I can think of is creating a global array/list of strings referring to those icons, but I'm not sure how good of a solution it is.
Using a database is a bad option since it takes time to re-query every time I need to use an icon and it's not very efficient.
Another option would be to just get all of the drawables from a folder into an array, but then unwanted drawables may also be included.
So my question is what is really the best way of doing it?
I handled this in one of my apps by naming each icon and giving it a number. image1.png, image2.png etc
I then use this to retrieve the image I want.
public Drawable GetIcon(Context c, Integer ImageId) {
return c.getResources().getDrawable(c.getResources().getIdentifier("image" + Integer.toString(ImageId), "drawable", c.getPackageName()));
}
This way I only ever need to know the index of the drawable I want.
I have some images that are impossible for visually impaired users to use at all. I want to be consistent in my code so I have content descriptions. Is there a specific string that will make it so Talkback doesn't say anything when the user touches the images.
EDIT: Previous answer was to not add the tag in XML. The correct answer is to add the value "#null". This prevents a string from being spoken (same as an adding the attribute), but #null will also prevent the LINT tool from yelling at you about a missing attribute.
What I'd like to do is change the state (really, the background) of an EditText to reflect validity of its contents. E.g. if the user enters 999 where 999 is contextually invalid, the EditText should have a red border in place of the default orange border, likewise once the text is valid it should have a green border.
Methods I've explored:
Changing the style of the EditText programmatically via something like editor.setStyle(R.styles.SomeID). Seems to be impossible within android.
Adding custom states (state_valid, state_invalid) in R.attr, associating them with red/ green 9-patches, then calling drawable.setState() with one of these states. This worked in the sense that the state could be read back via getState(), but the border did not change colour.
Setting the background resource directly upon detection of (in)validity. This works ok, causing the correct visual effect, but seems a little hokey, and allows only one state (e.g. I have to manually check for whether the EditText is pressed, enabled etc).
Given limited UI real-estate I am hesitant to introduce a separate UI element to visually feedback the text's validity to the user, hence my desire to display it in the EditText itself.
So.. is this something that's even feasible? It seems like a fairly common use case, so has anyone achieved what I'm trying to do in a straightforward and elegant manner?
I would recommend changing the text color to indicate validity, rather than changing the color of the focus ring by any of the techniques you describe (of which only #3 seems practical).
Another possibility is to try setCompoundDrawablesWithIntrinsicBounds() to modify an icon on the left or right side of the EditText contents to indicate validity. I remember discussing this technique with somebody a few months back and forget if they got it working or not.
And, of course, another option is to not allow invalid input, via a custom input filter or listener or something.
Well, I'd just extend the EditText class and build the desired functionality on top ( using the third approach you are suggesting, because it works :-) ). Doing this, you have to walk the way only once, and are open to change your implementation once you know the best way ( I would have personally solved it also using the third approach, seems fine to me ).
i think a call to invalidateDrawable(yourDrawable) would work with approach number 2.
i didn't try .. but it make sense