I have an ImageView on my scene that I would like to set the source of dynamically based on user input.
Let's say I have 4 images in my drawable folder: aaa.png, bbb.png, ccc.png, and ddd.png.
When my application loads I set the image to: aaa.png
myImageView.setImageResource(R.drawable.aaa);
now I have an EditText where a user can type in bbb and I want to change the image source to be the bbb.png, or user enters ccc, change source to ccc.png etc.
how can I dynamically set the parameter in setImageResource()? I tried playing around with the Drawable object to no avail...
If you want to allow open text input, you'll either have to use raw assets to fetch them by string name (see the sidenote on that page), or else use magical Java reflection to retrieve a field of the R class by name. Alternatively, you could keep a HashMap of strings to R.drawable integer values and look it up, but then you'd have to maintain that hashmap.
If you only want it to display images that you have loaded in your drawables you can use a Spinner where the id for the item is set to be the resource for the Drawable. That would be easier on your part and easier for the user.
If you want to use reflections have a look at the code below:
R.drawable ourRID = new R.drawable();
Field photoNameField = ourRID.getClass().getField("aaa");
myImageView.setImageResource(photoNameField.getInt(ourRID));
Hope it helps.
Related
I've an App where user can post photo and give some attributes to it.
Users can determine which category the photo belongs within a set of default categories, i try to show the categories options in a Spinner and the values come from a xml array compiled into the app.
This way i can show the categories in whichever language user is running the app.
The problem i have is to convert the value got from spinner back into a standard set for store in my database. Because depending of what language user set, i get the value as a different word and is a nightmare to create a converter which could handle it and translate from N different languages into my standard enum.
I would like to do something like radiobutton, like each option has a label (in whatever langague user wants) and a value (which i can define in a stardard way) so when i get selectedItemValue() i always get same value independent of language.
Is there any way to do that?
I advise you to use res/raw/ with different qualifiers depending on your locals instead of using string array.
It helps you to store json files in them. So you can have a list of custom objects which have both title and id.
And you store id in the database, and id is the same in all locals.
I'm building the Activity to take information on the users pet and make a sqlite database entry with it. I want to be able to take a picture and store the src as one of the rows in the entry.
I currently have an ImageButton displaying a default picture from drawable, but how do I extract the src from the ImageButton?
I figure if I can do that then it'll be easy to make the button clickable so the user can set their own (then when they hit the save button, it'll grab the src of the new pic instead, otherwise if they don't click it the default pic will get saved)
Please Check the Below answer may be helps you.
Bitmap bitmap = ((BitmapDrawable)imagebutton.getDrawable()).getBitmap();
I am trying to add few things like names, places to images. The idea is to have them sorted according to name or place when the users uses it.
I have the code working to take pictures and save it to the Phone's memory.
How can i add this Tag of information in my code?
Or even how can i add metadata to an image??
Off the top of my head, what you could do is create a database and have in three fields:
Names
Places
Image path
Use a GridView to show the images in your app(and load them images from the database). On GridView ItemClick you could prompt the user to add in tags into the image, later on you could use these fields to sort the image in you application.
Hope this helps.
I found what i was looking for we can add extra information to an image using ExifInterface. Thanks
is it possible to save the .xml file from the screen to main.xml?
my requirement is to.. first i create a xml files with some files
the user selects the required fields and select the save button.
from next onwards the application directly show the user specified fields only
. directly by saving the selected field in the xml and remaining fields should be invisible the main.xml code is directly change to newly userspecified view....
Have you considered using SharedPreferences? From what I've read from your description I think it should cover your needs.
I have a gridview with pictures of equipment to select from. Once selected, the user is taken to an activity that summarizes all the info they need to know for that model of equipment. I'd like for the Label or Title of that activity to display the selected model of equipment.
It looks like in java to be as simple as setTitle(string), but this functionality doesn't seem to exist in mono. Looked at this, but you can only use integer or an existing drawable. I need to set it to a string.
You can set that via the Title property. In general, where you see setFoo/getFoo methods in Java, those will be mapped to a property named Foo in C#.