I'm adding Buttons programmatically and the number of Buttons depends on some conditions. To add rules for RelativeLayout.LayoutParams that Buttons be aligned to the top of each other I need to set their IDs. All the answers from 2-3 years ago say that setId(int) is okay (e.g. setId(1)) but now it's forbidden (UPD. it's not ok only with int literals. With int variables everything is ok. Wonder why). How to do it nowadays?
Asked
Active
Viewed 1.1k times
10
-
Who says it is forbidden? – Shadab Ansari Mar 18 '16 at 00:57
-
@ShadabAnsari it was underlined with red, and the method itself has an annotation IdRes – Justin McGuire Mar 18 '16 at 01:08
-
@ShadabAnsari and when I did "setId(i)" in loop it threw NullPointerException for the previous Button though I checked the "previous" id starting with the second. – Justin McGuire Mar 18 '16 at 01:09
2 Answers
17
According to the API it's not forbidden or deprecated. Here is the best way of using it.
Create
res/values/ids.xml
and define<?xml version="1.0" encoding="utf-8"?> <resources> <item type="id" name="button1" /> <item type="id" name="button2" /> </resources>
once you have that, you can than use
setId
button1.setId(R.id.button1); button2.setId(R.id.button2);

arsent
- 6,975
- 3
- 32
- 31
-
when I tried "setId(1)" it was underlined with red. Thank you for your answer, gonna check it – Justin McGuire Mar 18 '16 at 01:06
-
Yes, it's because the function definition is `public void setId(@IdRes int id)` which forces you to pass an ID resource. Glad it helped. – arsent Mar 18 '16 at 01:10
-
It's not NPE anymore. But I could't solve my main problem - they keep overlapping each other though I wrote "lp.addRule(RelativeLayout.ALIGN_TOP,buttons[i-1].getId());" and then set it to buttons... – Justin McGuire Mar 18 '16 at 01:19
-
It's a different issue. You need to add another rule LeftOf or RightOf to the buttons. There is a solution here - http://stackoverflow.com/questions/10700273/relativelayout-add-rule-relativelayout-left-of-not-working – arsent Mar 18 '16 at 01:22
-
Solved it! Many thanks! Just replaced "ALIGN_TOP" with "BELOW". And one more question (I know it's another topic too but maybe I be lucky): how to place programmatically-created RL at the bottom but outside the screen? ("rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM)" didn't help, it's placed top-left) – Justin McGuire Mar 18 '16 at 01:32
-
It depends on the parent layout of the RL. ALIGN_PARENT_BOTTOM is for the items inside the new RL, so that won't work. What is your parent layout ? – arsent Mar 18 '16 at 01:35
-
-
-
-
Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/106669/discussion-between-justin-mcguire-and-arsent). – Justin McGuire Mar 18 '16 at 02:10
10
Since you said you can have any number of buttons then you can set id for each button using -
button.setId(View.generateViewId()); //Works for API 17 and above
If minSdk
is below 17, then you can use -
button.setId(counter++); //Each time counter will be increment giving a unique id

Shadab Ansari
- 7,022
- 2
- 27
- 45
-
-
-
So strange... it's not ok using int literals (underlines red) but works with int variables – Justin McGuire Mar 18 '16 at 01:25