How to add R.drawable resources to an array for android? - android

I would like to have sequence of set R.drawable been called in integer array so that it can be accessed later with choice.But I'll like to load to the R.drawable dynamically which corresponds to different names.It names needs to be had as per the external value input not hardcoded.I tried this in making many but like use list,set,array conversion etc.Kindly guide me with a snippet or example on this regard.Thank you.

I suppose, that best way to achieve this funcionality is by using ArrayAdapter of Integer.
You will simply add drawble resources by id.
If you need to get resource id, but you will get string input from user, you can use this code:
Android, getting resource ID from string?

Related

Android-Studio: How to make a resource string value the same as its name

I want to use the string resources to save some key values. So, I have something like this:
<string name="key_name">key_name</string>
The resource name and the resource value are the same. There is no need for me to make a difference between them, as it is just to cleanly store one value.
To reduce redundant information,
is there a way to just tell android-studio that the name equals the value? Something like this?
<string name="key_name"/>
I think you can't do it. It looks like you create a variable, you must name it and when you need you can set its value.
You can't create a variable with same values with name at the same time.
I think you have the wrong resource type. A string resource is used for mapping a name to a text. From what you're describing here you only need the name (and then can extrapolate the text from it).
Using strings with matching name and value shouldn't be necessary:
If you're going to be referencing them from Java or Kotlin code, you still need to know the name (i.e. R.string.key_name), so you might as well just use the string value there.
If it's for using inside XML code (where you want to pass the value of a string, e.g. text="#string/key_name") most of the time you can just use the raw string there (i.e. text="key_name").

Where are the resources' IDs?

Android will allocate ids for each pics in the res/drawable directory. Where are they?
I want to dynamically choose one pic form the pool and show it. How can I do that?
They are stored in the res/drawable folder.
If the file name is demo.png, then they can be accessed by R.drawable.demo
If you want to access a random drawable, store all the resource identifiers in a Integer ArrayList, and programatically generate a random function using Random(), and get that particular item from the arraylist. Then you'll have a random drawable every time.
Autogenerated ids are in the gen file, but not advisable to use them. It would be better to use the filenames directly through some predefined array of R.drawable.filename and randomly pick them.
There IDs are stored in the R.java file, but you cannot edit it, as your changes are over written each time.
You can also access resources by name, which may be a viable approach to solving your problem if you know the names of the resources or can derive them according to some pre-defined naming scheme. (for example images are named in the sequence image1, image2 and so on.
You have to map the name to the identifier using the getIdentifier() method of the Resources class.
String name = "resource" + rng.nextInt(count);
int resource = getResources().getIdentifier(name, "drawable", "com.package");
The documentation for this method says:
Note: use of this function is
discouraged. It is much more efficient
to retrieve resources by identifier
than by name.
This is true but need not be a problem if you are doing it in code that isn't performance sensitive.
Alternatively, if you don't mind listing the resources in XML, you could create a typed array that you can then randomly select from.

Storing strings in android XML resource file and referencing them

I'm wondering if I should use a resource XML file for to store all the possible recipes for me rather than using an ArrayList that is hardcoded, the problem is that I don't know how to call from a resource file using the method i have...
Here is a cut down version of what I want:
int recipeNumber = b.getInt("RECIPE"); //This is taken from another activity
final TextView rowTextView = new TextView(this); //Create a textview
rowTextView.setText(R.string.recipeNumber); //This is what i am struggling with
howToLinearLayout.addView(rowTextView); //Add textview to linearlayout
I don't know what to put in the part that references my resource file. I know I need:
rowTextView.setText(R. but I'm not sure what would come after that.
Im storing strings that would be for example:
<string name="1">One part Vodka, One part Coke</string>
<string name="2">One part Vodka, One part Lemonade</string>
This list will be quite long so id appreciate any other suggestions on storage, bare in mind I'm pretty new to this.
The recipeNumber int is what string will be called in to the textview.
Thanks for any help
I recommend storing the values outside your code, for example in XML or JSON (quicker and easier to work with). I would go with JSON. Really easy to load a list into an array, or other collection, and work with it anyway you want.
http://www.vogella.com/articles/AndroidJSON/article.html
You can then maintain your recipes, add new ones, maintain it etc without having to touch the code. Simply replace the XML or JSON (or whatever you choose) and rebuild your app.
Strings.xml is intended for pieces of text in your UI and code which you might want to localise and to avoid hard coding strings. It's not really intended for storing data.
To get string resources in code, use getResources().
rowTextView.setText(getResources().getString(R.string.recipeNumber));
To get an unknown resource identifier, but a known resource name, you can use the following method.
int identifier = getResources().getIdentifier("" + recipeNumber, "string", "com.your.package.name");
rowTextView.setText(getResources().getString(identifier));

Saving resources path in SQLite

I need to save the images in my resource folder in an SQLite database. This database is pre-loaded(copied to data/ path) so the there is no populating at runtime. I've tried to save it like this in text fields in SQLite: "R.drawable.picture1".
There are about 300 small size pictures(jpg) in my drawable folder. When I run the a query ofcourse the cursor returns the particulair string. Now I need to set this as ImageResource on an ImageView, but whatever I try it doesn't seem to work.
I've tried parsing it to an int, what doesn't seem to work. Whatever I try logcat keeps telling me R.id.picture1 isn't a valid integer. I've seen a possible solution some time ago, but I've searched for it all day but can't seem to find it.
What I dont want: Convert the images to byte arrays so I can save it as a blob. I just need a way to store and retrieve an drawable reference that I can use to set the drawable on the ImageView.
I'm not that experienced so I hope someone can help me out.
If you have the name of the resource you can lookup the resource identifier and use that for your ImageResource.
int resId = context.getResources().getIdentifier("picture1","drawable",context.getPackageName());
image.setImageResource(resId);;

Android Bitmap syntax

I want to load bitmaps into ImageViews in Android, but I don't want to use the R.drawable syntax because I have a lot of images with a standard naming convention so it's much easier to get the image with some logic. For example, all my images are named:
img1.png
img2.png
img3.png
So if a user selects a value, let's say x, I show "img" + x + ".png" in the ImageView. Looks like Bitmap.decodeFile is what I need, but I need to know the syntax of how to get to my drawables folder since that's where the images are. Or if there's perhaps a better way.
I realize I could do this with a switch statement instead of concatenating the image name but that would be a lot of lines since I have so many images.
One alternative is to use reflection to load images from R. You can looking static variables by string name. Keep in mind that reflection can be slow though. If you are going to be loading the same image multiple times, you might want to keep a cache of name -> R id.
Edit:
You can also access them by URI. See referring to android resources using uris.
The R.drawable value is a number by itself. You just need to use the first image id and add a fixed value to find the right image. For example:
R.drawable.image1 = 1234;
R.drawable.image2 = 1235;
etc.
So to get image 2 I would to R.drawable.image1 + 1.
Every time the R.java file is regenerated the numbers may change but the sequence will be the same. If you don't want to depend in this then you will have to look at the "assets" folder.
Looks like I've found a solution. The real problem is that I generate the resource names at runtime, but I can get the resource id like this:
getResources().getIdentifier("string1" + string2, "id", "com.company.package")
I can then pass that value to setImageResource or any other method that accepts a resource id.

Categories

Resources