Setting android:background / android:src programmatically












1















I need to set two attributes like in a xml:



<ImageButton
android:id="@+id/btn_friendsMainMenu"
android:src="@drawable/general_btn_header_friendlist"
android:background="@drawable/ripple"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />


As you see, there is a background and an src attribute. How do I set BOTH programmatically?



I only know of one: Which one is it? And what is the other one?



 btnBack.SetBackgroundResource(Resource.Drawable.thebook_backbutton);









share|improve this question




















  • 2





    You can use this methods: imageButton.setImageResource(); imageButton.setBackgroundResource();

    – Arti patel
    Jun 18 '18 at 10:30













  • Possible duplicate of How set background drawable programmatically in Android

    – Gautam Surani
    Jun 18 '18 at 10:45
















1















I need to set two attributes like in a xml:



<ImageButton
android:id="@+id/btn_friendsMainMenu"
android:src="@drawable/general_btn_header_friendlist"
android:background="@drawable/ripple"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />


As you see, there is a background and an src attribute. How do I set BOTH programmatically?



I only know of one: Which one is it? And what is the other one?



 btnBack.SetBackgroundResource(Resource.Drawable.thebook_backbutton);









share|improve this question




















  • 2





    You can use this methods: imageButton.setImageResource(); imageButton.setBackgroundResource();

    – Arti patel
    Jun 18 '18 at 10:30













  • Possible duplicate of How set background drawable programmatically in Android

    – Gautam Surani
    Jun 18 '18 at 10:45














1












1








1








I need to set two attributes like in a xml:



<ImageButton
android:id="@+id/btn_friendsMainMenu"
android:src="@drawable/general_btn_header_friendlist"
android:background="@drawable/ripple"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />


As you see, there is a background and an src attribute. How do I set BOTH programmatically?



I only know of one: Which one is it? And what is the other one?



 btnBack.SetBackgroundResource(Resource.Drawable.thebook_backbutton);









share|improve this question
















I need to set two attributes like in a xml:



<ImageButton
android:id="@+id/btn_friendsMainMenu"
android:src="@drawable/general_btn_header_friendlist"
android:background="@drawable/ripple"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />


As you see, there is a background and an src attribute. How do I set BOTH programmatically?



I only know of one: Which one is it? And what is the other one?



 btnBack.SetBackgroundResource(Resource.Drawable.thebook_backbutton);






android






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 29 '18 at 2:42









Cœur

17.6k9105145




17.6k9105145










asked Jun 18 '18 at 10:25









innomotion mediainnomotion media

356110




356110








  • 2





    You can use this methods: imageButton.setImageResource(); imageButton.setBackgroundResource();

    – Arti patel
    Jun 18 '18 at 10:30













  • Possible duplicate of How set background drawable programmatically in Android

    – Gautam Surani
    Jun 18 '18 at 10:45














  • 2





    You can use this methods: imageButton.setImageResource(); imageButton.setBackgroundResource();

    – Arti patel
    Jun 18 '18 at 10:30













  • Possible duplicate of How set background drawable programmatically in Android

    – Gautam Surani
    Jun 18 '18 at 10:45








2




2





You can use this methods: imageButton.setImageResource(); imageButton.setBackgroundResource();

– Arti patel
Jun 18 '18 at 10:30







You can use this methods: imageButton.setImageResource(); imageButton.setBackgroundResource();

– Arti patel
Jun 18 '18 at 10:30















Possible duplicate of How set background drawable programmatically in Android

– Gautam Surani
Jun 18 '18 at 10:45





Possible duplicate of How set background drawable programmatically in Android

– Gautam Surani
Jun 18 '18 at 10:45












9 Answers
9






active

oldest

votes


















3














Use setImageResource() to set android:src to your ImageButton




setImageResource() Sets a drawable as the content of this ImageView.




SAMPLE CODE



btnBack.setImageResource(R.drawable.ic_camera);


Use setBackgroundResource() to set android:background to your ImageButton



SAMPLE CODE



btnBack.setBackgroundResource(R.color.colorAccent);





share|improve this answer





















  • 1





    absolutely perfect! thanks a lot.

    – innomotion media
    Jun 18 '18 at 10:46











  • @innomotionmedia welcome happy to help you

    – Nilesh Rathod
    Jun 18 '18 at 10:47



















2














you can do it by this code:



private void initView() {

rootLayout =new LinearLayout(this);
rootLayout.setBackgroundDrawable(getResources().getDrawable(R.drawable.layer_5));
imgLogo=new ImageView(this);
imgLogo.setImageDrawable(getResources().getDrawable(R.drawable.splash_logo));

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
350,
350);


imgLogo.setLayoutParams(params);
rootLayout.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
rootLayout.addView(imgLogo);
setContentView(rootLayout);
}


and call initView method in your onCreate






share|improve this answer

































    1














    imageView.setBackgroundResource(R.drawable.some_bg_res); // for setting background 'android:background'
    imageView.setImageResource(R.drawable.some_res); // for setting src 'android:src'





    share|improve this answer































      0














      setImageResource() is for android:src. ImageButton inherits it from ImageView.



      btnBack.SetImageResource(Resource.Drawable.drawable_name)





      share|improve this answer































        0














        setBackgroundResource() sets the background - it differs from setBackground() by taking a ressource id (as int) as input.



        I am fairly sure setImage() is the method setting the 'src' attribute in xml. It also comes in some different variants. If you want to set a drawable as in your example use setImageDrawable().






        share|improve this answer































          0














          You can set image to ImageView programatically in Android please use bellow like of code.



          imageView.setImageResource(R.drawable.android_image3);





          share|improve this answer































            0















            android:src is set with setImageResource()




            and




            android:background is set with setBackgroundResource()




            In code



            ImageButton btn = (ImageButton)findViewById(R.id.btn_friendsMainMenu);
            btn.setImageResource(R.drawable.general_btn_header_friendlist)
            btn.setBackgroundResource(R.drawable.ripple)



            ImagResource will be on top of the BackgroundResource.







            share|improve this answer































              0














              There are already true answers but a better approach would be to put this attributes in styles.xml to and give that style to the buttons you want to use to increase clarity and reduce the number of lines you have to write. When your application gets bigger, setting everything from activity/fragment will become unmaintainable.



              If your only goal is to change it in the code then take a look at this.



              How to programmatically set style attribute in a view






              share|improve this answer































                -1














                ImageButton btn = (ImageButton)findViewbyId(R.id.img_btn)

                btn.setImageResource(R.drawable.image)
                btn.setBackgroundResource(R.drawable.ripple)





                share|improve this answer





















                • 3





                  While this code may answer the question, providing additional context regarding how and why it solves the problem would improve the answer's long-term value.

                  – Alexander
                  Jun 18 '18 at 13:02











                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%2f50907384%2fsetting-androidbackground-androidsrc-programmatically%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                9 Answers
                9






                active

                oldest

                votes








                9 Answers
                9






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                3














                Use setImageResource() to set android:src to your ImageButton




                setImageResource() Sets a drawable as the content of this ImageView.




                SAMPLE CODE



                btnBack.setImageResource(R.drawable.ic_camera);


                Use setBackgroundResource() to set android:background to your ImageButton



                SAMPLE CODE



                btnBack.setBackgroundResource(R.color.colorAccent);





                share|improve this answer





















                • 1





                  absolutely perfect! thanks a lot.

                  – innomotion media
                  Jun 18 '18 at 10:46











                • @innomotionmedia welcome happy to help you

                  – Nilesh Rathod
                  Jun 18 '18 at 10:47
















                3














                Use setImageResource() to set android:src to your ImageButton




                setImageResource() Sets a drawable as the content of this ImageView.




                SAMPLE CODE



                btnBack.setImageResource(R.drawable.ic_camera);


                Use setBackgroundResource() to set android:background to your ImageButton



                SAMPLE CODE



                btnBack.setBackgroundResource(R.color.colorAccent);





                share|improve this answer





















                • 1





                  absolutely perfect! thanks a lot.

                  – innomotion media
                  Jun 18 '18 at 10:46











                • @innomotionmedia welcome happy to help you

                  – Nilesh Rathod
                  Jun 18 '18 at 10:47














                3












                3








                3







                Use setImageResource() to set android:src to your ImageButton




                setImageResource() Sets a drawable as the content of this ImageView.




                SAMPLE CODE



                btnBack.setImageResource(R.drawable.ic_camera);


                Use setBackgroundResource() to set android:background to your ImageButton



                SAMPLE CODE



                btnBack.setBackgroundResource(R.color.colorAccent);





                share|improve this answer















                Use setImageResource() to set android:src to your ImageButton




                setImageResource() Sets a drawable as the content of this ImageView.




                SAMPLE CODE



                btnBack.setImageResource(R.drawable.ic_camera);


                Use setBackgroundResource() to set android:background to your ImageButton



                SAMPLE CODE



                btnBack.setBackgroundResource(R.color.colorAccent);






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jun 18 '18 at 10:38

























                answered Jun 18 '18 at 10:32









                Nilesh RathodNilesh Rathod

                30.6k82956




                30.6k82956








                • 1





                  absolutely perfect! thanks a lot.

                  – innomotion media
                  Jun 18 '18 at 10:46











                • @innomotionmedia welcome happy to help you

                  – Nilesh Rathod
                  Jun 18 '18 at 10:47














                • 1





                  absolutely perfect! thanks a lot.

                  – innomotion media
                  Jun 18 '18 at 10:46











                • @innomotionmedia welcome happy to help you

                  – Nilesh Rathod
                  Jun 18 '18 at 10:47








                1




                1





                absolutely perfect! thanks a lot.

                – innomotion media
                Jun 18 '18 at 10:46





                absolutely perfect! thanks a lot.

                – innomotion media
                Jun 18 '18 at 10:46













                @innomotionmedia welcome happy to help you

                – Nilesh Rathod
                Jun 18 '18 at 10:47





                @innomotionmedia welcome happy to help you

                – Nilesh Rathod
                Jun 18 '18 at 10:47













                2














                you can do it by this code:



                private void initView() {

                rootLayout =new LinearLayout(this);
                rootLayout.setBackgroundDrawable(getResources().getDrawable(R.drawable.layer_5));
                imgLogo=new ImageView(this);
                imgLogo.setImageDrawable(getResources().getDrawable(R.drawable.splash_logo));

                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                350,
                350);


                imgLogo.setLayoutParams(params);
                rootLayout.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
                rootLayout.addView(imgLogo);
                setContentView(rootLayout);
                }


                and call initView method in your onCreate






                share|improve this answer






























                  2














                  you can do it by this code:



                  private void initView() {

                  rootLayout =new LinearLayout(this);
                  rootLayout.setBackgroundDrawable(getResources().getDrawable(R.drawable.layer_5));
                  imgLogo=new ImageView(this);
                  imgLogo.setImageDrawable(getResources().getDrawable(R.drawable.splash_logo));

                  RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                  350,
                  350);


                  imgLogo.setLayoutParams(params);
                  rootLayout.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
                  rootLayout.addView(imgLogo);
                  setContentView(rootLayout);
                  }


                  and call initView method in your onCreate






                  share|improve this answer




























                    2












                    2








                    2







                    you can do it by this code:



                    private void initView() {

                    rootLayout =new LinearLayout(this);
                    rootLayout.setBackgroundDrawable(getResources().getDrawable(R.drawable.layer_5));
                    imgLogo=new ImageView(this);
                    imgLogo.setImageDrawable(getResources().getDrawable(R.drawable.splash_logo));

                    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    350,
                    350);


                    imgLogo.setLayoutParams(params);
                    rootLayout.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
                    rootLayout.addView(imgLogo);
                    setContentView(rootLayout);
                    }


                    and call initView method in your onCreate






                    share|improve this answer















                    you can do it by this code:



                    private void initView() {

                    rootLayout =new LinearLayout(this);
                    rootLayout.setBackgroundDrawable(getResources().getDrawable(R.drawable.layer_5));
                    imgLogo=new ImageView(this);
                    imgLogo.setImageDrawable(getResources().getDrawable(R.drawable.splash_logo));

                    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    350,
                    350);


                    imgLogo.setLayoutParams(params);
                    rootLayout.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
                    rootLayout.addView(imgLogo);
                    setContentView(rootLayout);
                    }


                    and call initView method in your onCreate







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Jun 18 '18 at 10:36









                    Vishva Dave

                    3,79621840




                    3,79621840










                    answered Jun 18 '18 at 10:36









                    dariushdariush

                    8118




                    8118























                        1














                        imageView.setBackgroundResource(R.drawable.some_bg_res); // for setting background 'android:background'
                        imageView.setImageResource(R.drawable.some_res); // for setting src 'android:src'





                        share|improve this answer




























                          1














                          imageView.setBackgroundResource(R.drawable.some_bg_res); // for setting background 'android:background'
                          imageView.setImageResource(R.drawable.some_res); // for setting src 'android:src'





                          share|improve this answer


























                            1












                            1








                            1







                            imageView.setBackgroundResource(R.drawable.some_bg_res); // for setting background 'android:background'
                            imageView.setImageResource(R.drawable.some_res); // for setting src 'android:src'





                            share|improve this answer













                            imageView.setBackgroundResource(R.drawable.some_bg_res); // for setting background 'android:background'
                            imageView.setImageResource(R.drawable.some_res); // for setting src 'android:src'






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jun 18 '18 at 10:31









                            Bhavesh RanganiBhavesh Rangani

                            915717




                            915717























                                0














                                setImageResource() is for android:src. ImageButton inherits it from ImageView.



                                btnBack.SetImageResource(Resource.Drawable.drawable_name)





                                share|improve this answer




























                                  0














                                  setImageResource() is for android:src. ImageButton inherits it from ImageView.



                                  btnBack.SetImageResource(Resource.Drawable.drawable_name)





                                  share|improve this answer


























                                    0












                                    0








                                    0







                                    setImageResource() is for android:src. ImageButton inherits it from ImageView.



                                    btnBack.SetImageResource(Resource.Drawable.drawable_name)





                                    share|improve this answer













                                    setImageResource() is for android:src. ImageButton inherits it from ImageView.



                                    btnBack.SetImageResource(Resource.Drawable.drawable_name)






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Jun 18 '18 at 10:30









                                    Sergey GlotovSergey Glotov

                                    16.8k117288




                                    16.8k117288























                                        0














                                        setBackgroundResource() sets the background - it differs from setBackground() by taking a ressource id (as int) as input.



                                        I am fairly sure setImage() is the method setting the 'src' attribute in xml. It also comes in some different variants. If you want to set a drawable as in your example use setImageDrawable().






                                        share|improve this answer




























                                          0














                                          setBackgroundResource() sets the background - it differs from setBackground() by taking a ressource id (as int) as input.



                                          I am fairly sure setImage() is the method setting the 'src' attribute in xml. It also comes in some different variants. If you want to set a drawable as in your example use setImageDrawable().






                                          share|improve this answer


























                                            0












                                            0








                                            0







                                            setBackgroundResource() sets the background - it differs from setBackground() by taking a ressource id (as int) as input.



                                            I am fairly sure setImage() is the method setting the 'src' attribute in xml. It also comes in some different variants. If you want to set a drawable as in your example use setImageDrawable().






                                            share|improve this answer













                                            setBackgroundResource() sets the background - it differs from setBackground() by taking a ressource id (as int) as input.



                                            I am fairly sure setImage() is the method setting the 'src' attribute in xml. It also comes in some different variants. If you want to set a drawable as in your example use setImageDrawable().







                                            share|improve this answer












                                            share|improve this answer



                                            share|improve this answer










                                            answered Jun 18 '18 at 10:31









                                            LarsLars

                                            12810




                                            12810























                                                0














                                                You can set image to ImageView programatically in Android please use bellow like of code.



                                                imageView.setImageResource(R.drawable.android_image3);





                                                share|improve this answer




























                                                  0














                                                  You can set image to ImageView programatically in Android please use bellow like of code.



                                                  imageView.setImageResource(R.drawable.android_image3);





                                                  share|improve this answer


























                                                    0












                                                    0








                                                    0







                                                    You can set image to ImageView programatically in Android please use bellow like of code.



                                                    imageView.setImageResource(R.drawable.android_image3);





                                                    share|improve this answer













                                                    You can set image to ImageView programatically in Android please use bellow like of code.



                                                    imageView.setImageResource(R.drawable.android_image3);






                                                    share|improve this answer












                                                    share|improve this answer



                                                    share|improve this answer










                                                    answered Jun 18 '18 at 10:33









                                                    Lovekush VishwakarmaLovekush Vishwakarma

                                                    985816




                                                    985816























                                                        0















                                                        android:src is set with setImageResource()




                                                        and




                                                        android:background is set with setBackgroundResource()




                                                        In code



                                                        ImageButton btn = (ImageButton)findViewById(R.id.btn_friendsMainMenu);
                                                        btn.setImageResource(R.drawable.general_btn_header_friendlist)
                                                        btn.setBackgroundResource(R.drawable.ripple)



                                                        ImagResource will be on top of the BackgroundResource.







                                                        share|improve this answer




























                                                          0















                                                          android:src is set with setImageResource()




                                                          and




                                                          android:background is set with setBackgroundResource()




                                                          In code



                                                          ImageButton btn = (ImageButton)findViewById(R.id.btn_friendsMainMenu);
                                                          btn.setImageResource(R.drawable.general_btn_header_friendlist)
                                                          btn.setBackgroundResource(R.drawable.ripple)



                                                          ImagResource will be on top of the BackgroundResource.







                                                          share|improve this answer


























                                                            0












                                                            0








                                                            0








                                                            android:src is set with setImageResource()




                                                            and




                                                            android:background is set with setBackgroundResource()




                                                            In code



                                                            ImageButton btn = (ImageButton)findViewById(R.id.btn_friendsMainMenu);
                                                            btn.setImageResource(R.drawable.general_btn_header_friendlist)
                                                            btn.setBackgroundResource(R.drawable.ripple)



                                                            ImagResource will be on top of the BackgroundResource.







                                                            share|improve this answer














                                                            android:src is set with setImageResource()




                                                            and




                                                            android:background is set with setBackgroundResource()




                                                            In code



                                                            ImageButton btn = (ImageButton)findViewById(R.id.btn_friendsMainMenu);
                                                            btn.setImageResource(R.drawable.general_btn_header_friendlist)
                                                            btn.setBackgroundResource(R.drawable.ripple)



                                                            ImagResource will be on top of the BackgroundResource.








                                                            share|improve this answer












                                                            share|improve this answer



                                                            share|improve this answer










                                                            answered Jun 18 '18 at 10:41









                                                            Tom OTom O

                                                            2817




                                                            2817























                                                                0














                                                                There are already true answers but a better approach would be to put this attributes in styles.xml to and give that style to the buttons you want to use to increase clarity and reduce the number of lines you have to write. When your application gets bigger, setting everything from activity/fragment will become unmaintainable.



                                                                If your only goal is to change it in the code then take a look at this.



                                                                How to programmatically set style attribute in a view






                                                                share|improve this answer




























                                                                  0














                                                                  There are already true answers but a better approach would be to put this attributes in styles.xml to and give that style to the buttons you want to use to increase clarity and reduce the number of lines you have to write. When your application gets bigger, setting everything from activity/fragment will become unmaintainable.



                                                                  If your only goal is to change it in the code then take a look at this.



                                                                  How to programmatically set style attribute in a view






                                                                  share|improve this answer


























                                                                    0












                                                                    0








                                                                    0







                                                                    There are already true answers but a better approach would be to put this attributes in styles.xml to and give that style to the buttons you want to use to increase clarity and reduce the number of lines you have to write. When your application gets bigger, setting everything from activity/fragment will become unmaintainable.



                                                                    If your only goal is to change it in the code then take a look at this.



                                                                    How to programmatically set style attribute in a view






                                                                    share|improve this answer













                                                                    There are already true answers but a better approach would be to put this attributes in styles.xml to and give that style to the buttons you want to use to increase clarity and reduce the number of lines you have to write. When your application gets bigger, setting everything from activity/fragment will become unmaintainable.



                                                                    If your only goal is to change it in the code then take a look at this.



                                                                    How to programmatically set style attribute in a view







                                                                    share|improve this answer












                                                                    share|improve this answer



                                                                    share|improve this answer










                                                                    answered Jun 18 '18 at 11:38









                                                                    SamSam

                                                                    314




                                                                    314























                                                                        -1














                                                                        ImageButton btn = (ImageButton)findViewbyId(R.id.img_btn)

                                                                        btn.setImageResource(R.drawable.image)
                                                                        btn.setBackgroundResource(R.drawable.ripple)





                                                                        share|improve this answer





















                                                                        • 3





                                                                          While this code may answer the question, providing additional context regarding how and why it solves the problem would improve the answer's long-term value.

                                                                          – Alexander
                                                                          Jun 18 '18 at 13:02
















                                                                        -1














                                                                        ImageButton btn = (ImageButton)findViewbyId(R.id.img_btn)

                                                                        btn.setImageResource(R.drawable.image)
                                                                        btn.setBackgroundResource(R.drawable.ripple)





                                                                        share|improve this answer





















                                                                        • 3





                                                                          While this code may answer the question, providing additional context regarding how and why it solves the problem would improve the answer's long-term value.

                                                                          – Alexander
                                                                          Jun 18 '18 at 13:02














                                                                        -1












                                                                        -1








                                                                        -1







                                                                        ImageButton btn = (ImageButton)findViewbyId(R.id.img_btn)

                                                                        btn.setImageResource(R.drawable.image)
                                                                        btn.setBackgroundResource(R.drawable.ripple)





                                                                        share|improve this answer















                                                                        ImageButton btn = (ImageButton)findViewbyId(R.id.img_btn)

                                                                        btn.setImageResource(R.drawable.image)
                                                                        btn.setBackgroundResource(R.drawable.ripple)






                                                                        share|improve this answer














                                                                        share|improve this answer



                                                                        share|improve this answer








                                                                        edited Jun 18 '18 at 12:10









                                                                        Nilesh Rathod

                                                                        30.6k82956




                                                                        30.6k82956










                                                                        answered Jun 18 '18 at 11:07









                                                                        Khushi GajjarKhushi Gajjar

                                                                        13




                                                                        13








                                                                        • 3





                                                                          While this code may answer the question, providing additional context regarding how and why it solves the problem would improve the answer's long-term value.

                                                                          – Alexander
                                                                          Jun 18 '18 at 13:02














                                                                        • 3





                                                                          While this code may answer the question, providing additional context regarding how and why it solves the problem would improve the answer's long-term value.

                                                                          – Alexander
                                                                          Jun 18 '18 at 13:02








                                                                        3




                                                                        3





                                                                        While this code may answer the question, providing additional context regarding how and why it solves the problem would improve the answer's long-term value.

                                                                        – Alexander
                                                                        Jun 18 '18 at 13:02





                                                                        While this code may answer the question, providing additional context regarding how and why it solves the problem would improve the answer's long-term value.

                                                                        – Alexander
                                                                        Jun 18 '18 at 13:02


















                                                                        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%2f50907384%2fsetting-androidbackground-androidsrc-programmatically%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

                                                                        Monofisismo

                                                                        Angular Downloading a file using contenturl with Basic Authentication

                                                                        Olmecas