Assign view identifier programmatically in Android

I am trying to create a RelativeLayout with multiple children programmatically. For rules such as RelativeLayout.RIGHT_OF to work, child views must have matching identifiers.

However, when I try to assign an identifier, Android Studio puts it as an error:

 view.setId(123); ERROR: Expected resource of type id 
+7
android android-layout android-studio
Jul 10 '14 at 16:39
source share
4 answers

Found:

 view.setId(View.generateViewId()); 
+11
Jul 10 '14 at 16:41
source share

You have two options:

  • This is not a compiler error. This is simply an editor validation error, as it is not a common way to handle identifiers. So compile and run without any problems.
  • Use a predefined list of identifiers of type "id" as the accepted answer https://stackoverflow.com/a/166778/
+4
Jul 13 '14 at 9:20
source share

In Android Studio, click on the light bulb on the line with this β€œerror”. And select "Turn off verification" in the first submenu.

0
Apr 15 '15 at 19:26
source share

As others have said, you should not use integers directly in this case. There are very rare cases where you may need to do this or suppress validation warnings altogether. If so, you need to look into Lint . In Android Studio, you can click on the red light on the left side of the line of code. This will show a context menu with a list of suppression methods.

-one
Mar 20 '15 at 20:46
source share



All Articles