Android - Does R.Resource ID changes in runtime - android

I have number of drawables to select in a View. When the user selects one of the drawable and on next time when the app is launch , I have to show only the selected drawable.
Is it good to store the Resource ID in shared preference and retrieve the selected drawable using Resource ID next time when the app is launched.?

All the R class related ids are generated at compile time so they won't change at runtime. There's nothing stopping you from storing this IDs in SharedPreferences, however, there's no gain whatsoever. It's almost always faster just to reference the R.drawable.x related id.

I know this is an older question, but I'd advise AGAINST storing the values in SharedPreferences.
When you release a newer version of the app, it's re-compiled. There is no guarantee, that the old value of let's say R.drawable.ic_example has the same value when drawables have been added/removed to the updated application. Therefore the stored value could no longer match with the Drawable asset you expect.

Related

How to represent user application settings in Android

Iam working on an android application and have trouble making a decision for the architecture saving application data.
Following case:
In the app the user has the possibility to create new general objects and give them properties he want. To support this, i want to give them a list with favorites before creating the input form, for example a car. It has color, weight, speed, horsepower etc.
So the user can choose a often picked object (for example the car) and will get the appropriate fields for the form he has to fill (color, weigth ...).
This list should be smart. The more you pick an item, the higher it appears in the list. And this presets have to be editable in preferences.
And thats the point. Should I implement my idea with the preferences framework from android (save it to xml as different preferences types and simply load due preferencebuilder) or should i create own xml objects and save it to self created user file location?
My second question: if i use the preference framework method .... is this made good for dynamically add entries at runtime? the ressources are in the res folder, but what if there are individual user entries? will they also be saved in the program folder or is there a special user data folder where the files (maybe encrypted) are in?
Thank you
Storing such complex data in SharedPreferences is tricky. What I mean is assuming user created 4 objects and each has 8 properties. You would store 4*8 values in sharedprefs and map them too.
What can be done is maintain an array list of objects created by user. Consecutive to that list maintain counter array list and keep swapping both lists internally as per number of times user has clicked the object. example:
List Name List Counter
ObjA 5
ObjB 3
ObjC 1
ObjD 1
Store these two lists in Shared Prefs.
Now, for the object's properties part (2 possibility arises) :
Maintain a mySQL DB and a table for each object's name. You can store values of each column in it IF you need to store every instance created of the object by user. (every time user clicks the object just show him/her the column names of table and store the values entered)
Example :
ObjA Table :
Color speed horsepower rpm
________________________________
red 20mph 100 3000
black 80mph 500 8000
Consecutively, if you don't want to store every instance value, you can make another sharedPrefs with object as key and an Arraylist of properties as value.

Best way to store Resource id in Database on Android

In my app i want to be able to store resource id of some icons in database. I suppose it is not save to do, cause in the later stages (application upgrades) i may want to add new icons to the app, so all id's may change? What is the best practice to store resource in case i do not want to store icons as blobs?
As an example, let's say i have a game 1.0, where i let user to choose an avatar for his character from a list of avatars. I want to store this avatars id in DB and be sure that if i add more avatars in game 1.1 - user will still see his choice.
The best way (in my opinion) is to store the resource entry name.
String iconName = getResources().getResourceEntryName(R.drawable.your_icon);
So your SQLite database column where you want to store the icon will have type TEXT. You're right that storing resource IDs is a bad practice because they're recreated when you compile the app, so you can't rely on them not changing (I learned this the hard way, all my app's icons got messed up).
To get the resource ID from the String when you need it, call
int resID = getResources().getIdentifier(iconName, "drawable", getPackageName());
Note that both getResources() and getPackageName() are methods from Context, so you need to have reference to your application/activity Context to be able to call these methods.

Android: image ID in layout directory

I have 100 images in my application of different cities and I want to divide these pictures in different groups, lets say in evening, morning, sunny, raining etc…
We know that when we call an image from layout folder by calling R.layout.image_1, android generates integer number for each image
For example:
R.layout.image_1 (223344), R.layout.image_2 (556677),
R.layout.image_3 (778899),
I can create one table having evening, morning fields and I can assign group of pictures to each of them with integer IDs which are (223344,556677) and I can call evening or morning group and i can display all images related to these group.
My question is: Does Android generate same number every time. Are these numbers are fixed? When ever the application runs.
If its true then upper idea will work for me. If this idea is incorrect then kindly guide me what is the decent approach to handle hundreds of PNGs in application.
Those numbers are not fixed. R will be regenerated and can have completely different numbers if you change something. That is why when comparing ids, you compare by the name instead.
Eg instead of
if (i == 223344)
do
if (i == R.layout.image_1)
Since R.layout.image_1 references the integer id, the name won't change (unless you change the layout xml name.
If you want to get a resource id dynamically (by a string representing the name), you should have a look at this method - Resources#getIdentifier().
First of all we generally put images in the drawable folder.
Does Android generate same number every time?
No.
Are these numbers fixed whenever the application runs?
Yes.
In fact, once your project is built, the ids will remain the same for that same build.
In other words for a certain generated APK file, the ids won't change.
So how can you take advantage of that to group your resources?
You could have a static int array that holds the ids:
public static final int[] IMAGES_MORNING = {R.drawable.morning0, R.drawable.morning1, etc};
public static final int[] IMAGES_EVENING = {R.drawable.evening0, R.drawable.evening1, etc};
Although a more structured method would be to store them in a database on your app's first launch.
Or you could use what A--C suggests:
For example to get all the ids of morning images
for(int i = 0; i < numberOfMorningImages ; i++){
int id = getResources().getIdentifier("morning" + i, "drawable", getPackageName());
// do something with the id
}
No, there's no guarantee that integers will be the same every time, so the solution you've described won't work. Unfortunately, there's no proper way to group drawables inside the res/drawable folder. As a workaround, you can store them inside the assets folder, where you can group them as you like. However, Android won't be able to handle different resolutions this way. The choice is up to you. Hope this helps.

Is there simple way to create user defaults values for TextView in Android?

I'm looking for simple way to create user defaults values for TextView in Android: I need the standart solution that user is seeing the previous value, that was entered by him to the TextView. Sure, there is way to store value of TextView at somewhere and then read it again while starting activity, but, may be, there is any simple tool to get what I wish ?
You can use SharedPreferences to store previous values and then get previous values from shared preferences to set as default values
Just to get your question correctly:
You want to present the values the user selected/set in his last visit?
You can save those values in the SharedPreferences - That's what they're made for.
You can give the google prediction api a try.

ViewImage change based on user input

i am currently trying change a imageView in my program, it needs to changed based what the user picks from the screen before, so i was wondering how i can access an image in my drawable folder through a string path?
all the examples i have seen have been either hard coded (R.drawable.XXX) or asking for the integer id value
Cheers
Make a HashMap using your string as a key and ID as a value.

Categories

Resources