0

I know you can set the int id of a View using the following:

 View.setId(int id);

But is there a way to use a string instead? I know what I could do is declare an array of arrays and each array contains a string and an int associated with that string and if the string is called then it gets the int associated with that string, but is there a simpler way to do it to set the id a as a string?

Also, when a View is created during runtime on the app, does it automatically generate a unique int id for it? Thanks for the help.

2
  • why would you use a String id? Commented Aug 1, 2014 at 20:04
  • Because I am making an app that requires the use of a string id for users so they don't have to deal with integers that might get confusing. Commented Aug 1, 2014 at 20:18

1 Answer 1

1
 but is there a simpler way to do it to set the id a as a string?

No, there is no. View has just the setId(int) method. If your string is a number, you can convert this to int and use as id for your view.

Also, when a View is created during runtime on the app, does it automatically generate a unique int id for it?

no you have to take care of it.

Sign up to request clarification or add additional context in comments.

3 Comments

So, if that's the case, could I implement it in such a way where in a loop it detects if an int id exists already? Using the getId method?
@user3864563 that's what will be returned if there's no ID associated with this View developer.android.com/reference/android/view/View.html#NO_ID
since api level 17 android introduced the method generateViewId. You can take a look to ist implmentation here: grepcode.com/file/repository.grepcode.com/java/ext/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.