10

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?

Justin McGuire
  • 365
  • 1
  • 5
  • 18

2 Answers2

17

According to the API it's not forbidden or deprecated. Here is the best way of using it.

  1. 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>
    
  2. 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
  • RL. Maybe something happens with addContentView? – Justin McGuire Mar 18 '16 at 01:40
  • Ok, but...is it ok in comments? – Justin McGuire Mar 18 '16 at 01:44
  • Maybe you create another question. I'll try to answer there :) – arsent Mar 18 '16 at 01:54
  • 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