Android - assign and retrieve ID's dynamically - android

I know how to assing ID's dynamically by invoking setID(). For the ID's to be unique, I used to utilize ids.xml and pass to setID() ID's from the pre-generated pool of ID's.
Question 1: Is there any way to assign ID's without utilizing the ids.xml since I cannot anticipate how many ID's I will need in runtime?
I tried to bypass the first issue presented in Question 1 by dynamically assigning each of which an id based on its label's hash (each label is unique), but there is no way to gaurantee that ID's won't be colliding with ID's auto generated in R.java.
Question 1.1: How the ID naming collision can be resolved?
Question 2: Assume I have the ID value of which I assign and generate dynamically. Since the aformentioned ID does not appear in R.id, findViewById() won't be applicable for retrieving the view. Hence, how can the view be retrieved when the ID is known?
Answer 2: You'd be able to retrieve the view by its corresponding ID only after onCreate() has returned control (terminated).

From API level 17 you can get a new id by calling
View.generateViewId()
Details here.

Is there any way to assign ID's without utilizing the ids.xml since I
cannot anticipate how many ID's I will need in runtime?
This guarantees that every view has a unique ID
for(int i =0 ; i < yourIDcount ; i++){
yourView.setId(i);
}
how can the view be retrieved when the ID is known?
View.findViewById(yourView.getId());
can be used to get your view's id, since every view has a unique Id you can get back the view you wanted..
The word dynamic means which is created at runtime, since you assign id in onCreate it is assigned as the views id, since onCreate is called only once an activity is created, you can make sure that the id you assigned stays intact...

Related

Why does .event.set("fieldName", ObjectValue) not storing any data in Google Calendar API android?

I successfully intergarted Google Calendar API. I'm able to do CRUD. But now because of some requirements, I want to send some unique id to each events while creating from android app. For that I found one method called .set() this is a key value pair.
Event event = new Event()
.set("appointment_id", 55475)
.setSummary(summary)
.setLocation(location)
.setDescription(des);
But while fetching, I'm getting all data except event.get("appointment_id")
Why, even it is setting also. [If I'm doing here before executing insert like this: evetn.get("appointment_id"), I'm getting value, because this is locally I'm chocking]
I checked through debugging as well. See below:
But, I'm not getting when I'm fetching all events from Google calendar:
List<Event> items = events.getItems();
You are using the set method, but this is just an override of the com.google.api.client.json.GenericJson class, I believe this will only add this key-value to the final JSON which will be ignored by the API (as it is not an expected field).
Sincerely I don't know what is the point on creating a custom id when there is one directly integrated in calendar.
If you take a look at the insert method you can see that there is one field called id:
Opaque identifier of the event. When creating new single or recurring events, you can specify their IDs. Provided IDs must follow these rules:
characters allowed in the ID are those used in base32hex encoding, i.e. lowercase letters a-v and digits 0-9, see section 3.1.2 in RFC2938
the length of the ID must be between 5 and 1024 characters
the ID must be unique per calendar
Also in the same description for the field:
If you do not specify an ID, it will be automatically generated by the server.
My point being that if you need unique ID for events in calendar there is a built in method that will create them for you. And even if you need to supply your own id's you can do so informing this field.
Let me suggest you the official guide on event creation in which there is a more detailed view on the option you can take creating an Event.
Also look at the documentation for setId as this is the method you should be using instead of set.
Example:
Event event = new Event()
.setId("55475") // This has to be type String
.setSummary(summary)
.setLocation(location)
.setDescription(des);

How to use an id that was set programmatically

I have created a table row and set its ID. Now i want to find the row so i can put a fragment inside of it. So how can i find this row? Usually when i set the Id in xml i can easily find the row by R.id.tableRow1 . But since i set it programmatically this doesnt work.
This is my code:
TableRow tr1 = new TableRow(myView.getContext());
tr1.setId(1);
ChildFragmentIntro cfi = new ChildFragmentIntro();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(tr1.getId(), cfi); //This is where it goes wrong, tr1.getId()
transaction.commit();
Im i setting the id in a wrong way or is tr1.getId() the wrong method to retrieve it? If i do it in XML i.e. create a table row in the layout, set its id with the usual #+id/xxx, and use transaction.add(R.id.xxx, cfi); than it works fine. But now i want to do it programmatically.
You can use View.setId() to set an ID to your newly generated View. On API 17+ you can also generate a unique ID using View.generateViewId();
Also see this: Android: View.setID(int id) programmatically - how to avoid ID conflicts?

Android View ID as Strings

I'm building a Class to populate my layouts using a JSON Schema. Basically in JSON I have a definition { type: "checkbox", label: "Label text", value: true, id: "liquids" }. The problem is I don't just have to render the UI i also need to collect values later when the form is posted to submit it to my API service.
The ID of each element is a string when I create the View element on my layout I need to give it this id however View has setId() method however this can only be an integer also has to be unique.
Is there any way I can use the string as id ?
You can use Strings as a form of ids by passing them to your View's setTag() method.
Then, to find a particular View, use findViewWithTag()
Just make sure that your String ids are unique.
you could use the hash code of the string as the id.
view.setId(string.hashCode());

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.

Android: what are the default ids of views?

I ask this because I have exceptions from reports (from users from the market), mentioning that I have duplicated views with id 0x2 (or 0x3).
Since all my generated ids are really big, I think that the views with duplicated ids are views with no specifically defined ids.
My question is what are the ids of the views, that the developer hasn't explicitly assigned ids to them.
Thanks in advance,
Danail
The AAPT constantly updates your R file to generate unique hexadecimal values for each of your own IDs. In terms of IDs YOU create, they only need to be unique within the parent viewgroup. As always please post your stacktrace.
According to the source code, a View for which you haven't set an ID, has an ID of -1.
public static final int NO_ID = -1;
I would say no id is created if you do not specify an id to a view. Try creating a very simple application and create components with no id's , you'll notice that no id's are created in the R.java file.

Categories

Resources