How to programmatically set the layout_align_parent_right attribute of a Button in Relative Layout?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







264















I have a relative layout which I am creating programmatically:



 RelativeLayout layout = new RelativeLayout( this );
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);


Now I have two buttons which I want to add in this relative layout. But the problem is both buttons are being shown on the left of the RelatiiveLayout overlapping on each other.



buttonContainer.addView(btn1);
buttonContainer.addView(btn2);


Now I want to know how can I programmatically set the the android:layout_alignParentRight="true"
or android:layout_toLeftOf="@id/btn" attribute of buttons as we do in the xml?










share|improve this question































    264















    I have a relative layout which I am creating programmatically:



     RelativeLayout layout = new RelativeLayout( this );
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,
    LayoutParams.WRAP_CONTENT);


    Now I have two buttons which I want to add in this relative layout. But the problem is both buttons are being shown on the left of the RelatiiveLayout overlapping on each other.



    buttonContainer.addView(btn1);
    buttonContainer.addView(btn2);


    Now I want to know how can I programmatically set the the android:layout_alignParentRight="true"
    or android:layout_toLeftOf="@id/btn" attribute of buttons as we do in the xml?










    share|improve this question



























      264












      264








      264


      51






      I have a relative layout which I am creating programmatically:



       RelativeLayout layout = new RelativeLayout( this );
      RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,
      LayoutParams.WRAP_CONTENT);


      Now I have two buttons which I want to add in this relative layout. But the problem is both buttons are being shown on the left of the RelatiiveLayout overlapping on each other.



      buttonContainer.addView(btn1);
      buttonContainer.addView(btn2);


      Now I want to know how can I programmatically set the the android:layout_alignParentRight="true"
      or android:layout_toLeftOf="@id/btn" attribute of buttons as we do in the xml?










      share|improve this question
















      I have a relative layout which I am creating programmatically:



       RelativeLayout layout = new RelativeLayout( this );
      RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,
      LayoutParams.WRAP_CONTENT);


      Now I have two buttons which I want to add in this relative layout. But the problem is both buttons are being shown on the left of the RelatiiveLayout overlapping on each other.



      buttonContainer.addView(btn1);
      buttonContainer.addView(btn2);


      Now I want to know how can I programmatically set the the android:layout_alignParentRight="true"
      or android:layout_toLeftOf="@id/btn" attribute of buttons as we do in the xml?







      android android-relativelayout android-layoutparams






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 17 '18 at 0:55









      Onik

      11.7k104264




      11.7k104264










      asked Jan 9 '11 at 11:12









      Fahad AliFahad Ali

      1,323294




      1,323294
























          3 Answers
          3






          active

          oldest

          votes


















          609














          You can access any LayoutParams from code using View.getLayoutParams. You just have to be very aware of what LayoutParams your accessing. This is normally achieved by checking the containing ViewGroup if it has a LayoutParams inner child then that's the one you should use. In your case it's RelativeLayout.LayoutParams. You'll be using RelativeLayout.LayoutParams#addRule(int verb) and RelativeLayout.LayoutParams#addRule(int verb, int anchor)



          You can get to it via code:



          RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)button.getLayoutParams();
          params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
          params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);

          button.setLayoutParams(params); //causes layout update





          share|improve this answer





















          • 1





            what if the anchor has no id? params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);TextView tvUserName = new TextView(act);

            – Saint Robson
            Feb 5 '14 at 10:31








          • 17





            how about adding the ID?

            – user2161301
            Jan 10 '15 at 12:40






          • 1





            What if button is on a RecycleView? Isn't it a problem adding same or opposite rules to the same widget?

            – János
            Oct 12 '16 at 11:51













          • Its working only for once, if I change alignment its not getting updated. May I know what can be the issue ??

            – Shshank Bhong
            Feb 21 '18 at 7:56











          • I tried using that but it goes red in my code for addRule ?

            – user10147255
            Aug 28 '18 at 5:41



















          11















          1. you need to create and id for the
            buttons you need to refference:
            btn1.setId(1);

          2. you can use the params variable to
            add parameters to your layout, i
            think the method is addRule(), check
            out the android java docs for this
            LayoutParams object.






          share|improve this answer


























          • I would highly appreciate any piece of code. I dont find any method in button.

            – Fahad Ali
            Jan 9 '11 at 11:30











          • Setting id doesn't help since it's expecting a real resource id instead of just a number since setId is being annotated with @IdRes.

            – Kevin Tan
            May 20 '15 at 6:45











          • that's new then, in the past you could take a resource number from layouts for example and use it as a base for resource id that will not collide.i guess google must have blocked that/

            – codeScriber
            Jun 25 '15 at 19:41



















          1














          For adding a RelativeLayout attribute whose value is true or false use 0 for false and RelativeLayout.TRUE for true:



          RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) button.getLayoutParams()
          params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE)


          It doesn't matter whether or not the attribute was already added, you still use addRule(verb, subject) to enable/disable it. However, post-API 17 you can use removeRule(verb) which is just a shortcut for addRule(verb, 0).






          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4638832%2fhow-to-programmatically-set-the-layout-align-parent-right-attribute-of-a-button%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            609














            You can access any LayoutParams from code using View.getLayoutParams. You just have to be very aware of what LayoutParams your accessing. This is normally achieved by checking the containing ViewGroup if it has a LayoutParams inner child then that's the one you should use. In your case it's RelativeLayout.LayoutParams. You'll be using RelativeLayout.LayoutParams#addRule(int verb) and RelativeLayout.LayoutParams#addRule(int verb, int anchor)



            You can get to it via code:



            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)button.getLayoutParams();
            params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);

            button.setLayoutParams(params); //causes layout update





            share|improve this answer





















            • 1





              what if the anchor has no id? params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);TextView tvUserName = new TextView(act);

              – Saint Robson
              Feb 5 '14 at 10:31








            • 17





              how about adding the ID?

              – user2161301
              Jan 10 '15 at 12:40






            • 1





              What if button is on a RecycleView? Isn't it a problem adding same or opposite rules to the same widget?

              – János
              Oct 12 '16 at 11:51













            • Its working only for once, if I change alignment its not getting updated. May I know what can be the issue ??

              – Shshank Bhong
              Feb 21 '18 at 7:56











            • I tried using that but it goes red in my code for addRule ?

              – user10147255
              Aug 28 '18 at 5:41
















            609














            You can access any LayoutParams from code using View.getLayoutParams. You just have to be very aware of what LayoutParams your accessing. This is normally achieved by checking the containing ViewGroup if it has a LayoutParams inner child then that's the one you should use. In your case it's RelativeLayout.LayoutParams. You'll be using RelativeLayout.LayoutParams#addRule(int verb) and RelativeLayout.LayoutParams#addRule(int verb, int anchor)



            You can get to it via code:



            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)button.getLayoutParams();
            params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);

            button.setLayoutParams(params); //causes layout update





            share|improve this answer





















            • 1





              what if the anchor has no id? params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);TextView tvUserName = new TextView(act);

              – Saint Robson
              Feb 5 '14 at 10:31








            • 17





              how about adding the ID?

              – user2161301
              Jan 10 '15 at 12:40






            • 1





              What if button is on a RecycleView? Isn't it a problem adding same or opposite rules to the same widget?

              – János
              Oct 12 '16 at 11:51













            • Its working only for once, if I change alignment its not getting updated. May I know what can be the issue ??

              – Shshank Bhong
              Feb 21 '18 at 7:56











            • I tried using that but it goes red in my code for addRule ?

              – user10147255
              Aug 28 '18 at 5:41














            609












            609








            609







            You can access any LayoutParams from code using View.getLayoutParams. You just have to be very aware of what LayoutParams your accessing. This is normally achieved by checking the containing ViewGroup if it has a LayoutParams inner child then that's the one you should use. In your case it's RelativeLayout.LayoutParams. You'll be using RelativeLayout.LayoutParams#addRule(int verb) and RelativeLayout.LayoutParams#addRule(int verb, int anchor)



            You can get to it via code:



            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)button.getLayoutParams();
            params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);

            button.setLayoutParams(params); //causes layout update





            share|improve this answer















            You can access any LayoutParams from code using View.getLayoutParams. You just have to be very aware of what LayoutParams your accessing. This is normally achieved by checking the containing ViewGroup if it has a LayoutParams inner child then that's the one you should use. In your case it's RelativeLayout.LayoutParams. You'll be using RelativeLayout.LayoutParams#addRule(int verb) and RelativeLayout.LayoutParams#addRule(int verb, int anchor)



            You can get to it via code:



            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)button.getLayoutParams();
            params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);

            button.setLayoutParams(params); //causes layout update






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited May 9 '13 at 22:58

























            answered Jan 9 '11 at 12:04









            Rich SchulerRich Schuler

            38.2k66458




            38.2k66458








            • 1





              what if the anchor has no id? params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);TextView tvUserName = new TextView(act);

              – Saint Robson
              Feb 5 '14 at 10:31








            • 17





              how about adding the ID?

              – user2161301
              Jan 10 '15 at 12:40






            • 1





              What if button is on a RecycleView? Isn't it a problem adding same or opposite rules to the same widget?

              – János
              Oct 12 '16 at 11:51













            • Its working only for once, if I change alignment its not getting updated. May I know what can be the issue ??

              – Shshank Bhong
              Feb 21 '18 at 7:56











            • I tried using that but it goes red in my code for addRule ?

              – user10147255
              Aug 28 '18 at 5:41














            • 1





              what if the anchor has no id? params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);TextView tvUserName = new TextView(act);

              – Saint Robson
              Feb 5 '14 at 10:31








            • 17





              how about adding the ID?

              – user2161301
              Jan 10 '15 at 12:40






            • 1





              What if button is on a RecycleView? Isn't it a problem adding same or opposite rules to the same widget?

              – János
              Oct 12 '16 at 11:51













            • Its working only for once, if I change alignment its not getting updated. May I know what can be the issue ??

              – Shshank Bhong
              Feb 21 '18 at 7:56











            • I tried using that but it goes red in my code for addRule ?

              – user10147255
              Aug 28 '18 at 5:41








            1




            1





            what if the anchor has no id? params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);TextView tvUserName = new TextView(act);

            – Saint Robson
            Feb 5 '14 at 10:31







            what if the anchor has no id? params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);TextView tvUserName = new TextView(act);

            – Saint Robson
            Feb 5 '14 at 10:31






            17




            17





            how about adding the ID?

            – user2161301
            Jan 10 '15 at 12:40





            how about adding the ID?

            – user2161301
            Jan 10 '15 at 12:40




            1




            1





            What if button is on a RecycleView? Isn't it a problem adding same or opposite rules to the same widget?

            – János
            Oct 12 '16 at 11:51







            What if button is on a RecycleView? Isn't it a problem adding same or opposite rules to the same widget?

            – János
            Oct 12 '16 at 11:51















            Its working only for once, if I change alignment its not getting updated. May I know what can be the issue ??

            – Shshank Bhong
            Feb 21 '18 at 7:56





            Its working only for once, if I change alignment its not getting updated. May I know what can be the issue ??

            – Shshank Bhong
            Feb 21 '18 at 7:56













            I tried using that but it goes red in my code for addRule ?

            – user10147255
            Aug 28 '18 at 5:41





            I tried using that but it goes red in my code for addRule ?

            – user10147255
            Aug 28 '18 at 5:41













            11















            1. you need to create and id for the
              buttons you need to refference:
              btn1.setId(1);

            2. you can use the params variable to
              add parameters to your layout, i
              think the method is addRule(), check
              out the android java docs for this
              LayoutParams object.






            share|improve this answer


























            • I would highly appreciate any piece of code. I dont find any method in button.

              – Fahad Ali
              Jan 9 '11 at 11:30











            • Setting id doesn't help since it's expecting a real resource id instead of just a number since setId is being annotated with @IdRes.

              – Kevin Tan
              May 20 '15 at 6:45











            • that's new then, in the past you could take a resource number from layouts for example and use it as a base for resource id that will not collide.i guess google must have blocked that/

              – codeScriber
              Jun 25 '15 at 19:41
















            11















            1. you need to create and id for the
              buttons you need to refference:
              btn1.setId(1);

            2. you can use the params variable to
              add parameters to your layout, i
              think the method is addRule(), check
              out the android java docs for this
              LayoutParams object.






            share|improve this answer


























            • I would highly appreciate any piece of code. I dont find any method in button.

              – Fahad Ali
              Jan 9 '11 at 11:30











            • Setting id doesn't help since it's expecting a real resource id instead of just a number since setId is being annotated with @IdRes.

              – Kevin Tan
              May 20 '15 at 6:45











            • that's new then, in the past you could take a resource number from layouts for example and use it as a base for resource id that will not collide.i guess google must have blocked that/

              – codeScriber
              Jun 25 '15 at 19:41














            11












            11








            11








            1. you need to create and id for the
              buttons you need to refference:
              btn1.setId(1);

            2. you can use the params variable to
              add parameters to your layout, i
              think the method is addRule(), check
              out the android java docs for this
              LayoutParams object.






            share|improve this answer
















            1. you need to create and id for the
              buttons you need to refference:
              btn1.setId(1);

            2. you can use the params variable to
              add parameters to your layout, i
              think the method is addRule(), check
              out the android java docs for this
              LayoutParams object.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Oct 9 '15 at 6:14









            camelCaseCoder

            1,2021229




            1,2021229










            answered Jan 9 '11 at 11:27









            codeScribercodeScriber

            3,48953258




            3,48953258













            • I would highly appreciate any piece of code. I dont find any method in button.

              – Fahad Ali
              Jan 9 '11 at 11:30











            • Setting id doesn't help since it's expecting a real resource id instead of just a number since setId is being annotated with @IdRes.

              – Kevin Tan
              May 20 '15 at 6:45











            • that's new then, in the past you could take a resource number from layouts for example and use it as a base for resource id that will not collide.i guess google must have blocked that/

              – codeScriber
              Jun 25 '15 at 19:41



















            • I would highly appreciate any piece of code. I dont find any method in button.

              – Fahad Ali
              Jan 9 '11 at 11:30











            • Setting id doesn't help since it's expecting a real resource id instead of just a number since setId is being annotated with @IdRes.

              – Kevin Tan
              May 20 '15 at 6:45











            • that's new then, in the past you could take a resource number from layouts for example and use it as a base for resource id that will not collide.i guess google must have blocked that/

              – codeScriber
              Jun 25 '15 at 19:41

















            I would highly appreciate any piece of code. I dont find any method in button.

            – Fahad Ali
            Jan 9 '11 at 11:30





            I would highly appreciate any piece of code. I dont find any method in button.

            – Fahad Ali
            Jan 9 '11 at 11:30













            Setting id doesn't help since it's expecting a real resource id instead of just a number since setId is being annotated with @IdRes.

            – Kevin Tan
            May 20 '15 at 6:45





            Setting id doesn't help since it's expecting a real resource id instead of just a number since setId is being annotated with @IdRes.

            – Kevin Tan
            May 20 '15 at 6:45













            that's new then, in the past you could take a resource number from layouts for example and use it as a base for resource id that will not collide.i guess google must have blocked that/

            – codeScriber
            Jun 25 '15 at 19:41





            that's new then, in the past you could take a resource number from layouts for example and use it as a base for resource id that will not collide.i guess google must have blocked that/

            – codeScriber
            Jun 25 '15 at 19:41











            1














            For adding a RelativeLayout attribute whose value is true or false use 0 for false and RelativeLayout.TRUE for true:



            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) button.getLayoutParams()
            params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE)


            It doesn't matter whether or not the attribute was already added, you still use addRule(verb, subject) to enable/disable it. However, post-API 17 you can use removeRule(verb) which is just a shortcut for addRule(verb, 0).






            share|improve this answer




























              1














              For adding a RelativeLayout attribute whose value is true or false use 0 for false and RelativeLayout.TRUE for true:



              RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) button.getLayoutParams()
              params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE)


              It doesn't matter whether or not the attribute was already added, you still use addRule(verb, subject) to enable/disable it. However, post-API 17 you can use removeRule(verb) which is just a shortcut for addRule(verb, 0).






              share|improve this answer


























                1












                1








                1







                For adding a RelativeLayout attribute whose value is true or false use 0 for false and RelativeLayout.TRUE for true:



                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) button.getLayoutParams()
                params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE)


                It doesn't matter whether or not the attribute was already added, you still use addRule(verb, subject) to enable/disable it. However, post-API 17 you can use removeRule(verb) which is just a shortcut for addRule(verb, 0).






                share|improve this answer













                For adding a RelativeLayout attribute whose value is true or false use 0 for false and RelativeLayout.TRUE for true:



                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) button.getLayoutParams()
                params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE)


                It doesn't matter whether or not the attribute was already added, you still use addRule(verb, subject) to enable/disable it. However, post-API 17 you can use removeRule(verb) which is just a shortcut for addRule(verb, 0).







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jun 10 '18 at 5:43









                shoeshoe

                500527




                500527






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4638832%2fhow-to-programmatically-set-the-layout-align-parent-right-attribute-of-a-button%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Bressuire

                    Vorschmack

                    Quarantine