Adding a text and image on a button programmatically in android












0















I am trying to have an text and image on a button appear dynamically - the text needs to appear on the left and the image to appear on the right. To give an example, here is an image I would like to get.



ExampleButton



After searching through a few other examples on StackOverflow, tried the following code in my activity but it doesnt seem to be working. Can anyone please suggest what I am doing wrong ?



Code :



btn.setTextColor(Color.parseColor("#000000"));
btn.setText("SomeText");
btn.setTextSize(TypedValue.COMPLEX_UNIT_PX, mediumTextSize);

Drawable icon= getApplicationContext().getResources().getDrawable( R.drawable.cal);
icon.setBounds(0, 0, 0, 0); //Left,Top,Right,Bottom
btn.setCompoundDrawablesWithIntrinsicBounds( null, null, icon, null );









share|improve this question























  • Do you want to set image on button ?

    – Code Kadiya
    Dec 29 '18 at 18:41











  • I suggest to use ImageButton

    – Code Kadiya
    Dec 29 '18 at 18:42











  • Else you can create LinearLayout and apply style

    – Code Kadiya
    Dec 29 '18 at 18:44











  • How about leaving out the two lines before the last one and just using btn.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.cal, 0); ?

    – 0X0nosugar
    Dec 29 '18 at 18:48






  • 1





    The linked version of the method will take int values (resource identifiers)

    – 0X0nosugar
    Dec 29 '18 at 19:08
















0















I am trying to have an text and image on a button appear dynamically - the text needs to appear on the left and the image to appear on the right. To give an example, here is an image I would like to get.



ExampleButton



After searching through a few other examples on StackOverflow, tried the following code in my activity but it doesnt seem to be working. Can anyone please suggest what I am doing wrong ?



Code :



btn.setTextColor(Color.parseColor("#000000"));
btn.setText("SomeText");
btn.setTextSize(TypedValue.COMPLEX_UNIT_PX, mediumTextSize);

Drawable icon= getApplicationContext().getResources().getDrawable( R.drawable.cal);
icon.setBounds(0, 0, 0, 0); //Left,Top,Right,Bottom
btn.setCompoundDrawablesWithIntrinsicBounds( null, null, icon, null );









share|improve this question























  • Do you want to set image on button ?

    – Code Kadiya
    Dec 29 '18 at 18:41











  • I suggest to use ImageButton

    – Code Kadiya
    Dec 29 '18 at 18:42











  • Else you can create LinearLayout and apply style

    – Code Kadiya
    Dec 29 '18 at 18:44











  • How about leaving out the two lines before the last one and just using btn.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.cal, 0); ?

    – 0X0nosugar
    Dec 29 '18 at 18:48






  • 1





    The linked version of the method will take int values (resource identifiers)

    – 0X0nosugar
    Dec 29 '18 at 19:08














0












0








0








I am trying to have an text and image on a button appear dynamically - the text needs to appear on the left and the image to appear on the right. To give an example, here is an image I would like to get.



ExampleButton



After searching through a few other examples on StackOverflow, tried the following code in my activity but it doesnt seem to be working. Can anyone please suggest what I am doing wrong ?



Code :



btn.setTextColor(Color.parseColor("#000000"));
btn.setText("SomeText");
btn.setTextSize(TypedValue.COMPLEX_UNIT_PX, mediumTextSize);

Drawable icon= getApplicationContext().getResources().getDrawable( R.drawable.cal);
icon.setBounds(0, 0, 0, 0); //Left,Top,Right,Bottom
btn.setCompoundDrawablesWithIntrinsicBounds( null, null, icon, null );









share|improve this question














I am trying to have an text and image on a button appear dynamically - the text needs to appear on the left and the image to appear on the right. To give an example, here is an image I would like to get.



ExampleButton



After searching through a few other examples on StackOverflow, tried the following code in my activity but it doesnt seem to be working. Can anyone please suggest what I am doing wrong ?



Code :



btn.setTextColor(Color.parseColor("#000000"));
btn.setText("SomeText");
btn.setTextSize(TypedValue.COMPLEX_UNIT_PX, mediumTextSize);

Drawable icon= getApplicationContext().getResources().getDrawable( R.drawable.cal);
icon.setBounds(0, 0, 0, 0); //Left,Top,Right,Bottom
btn.setCompoundDrawablesWithIntrinsicBounds( null, null, icon, null );






android android-layout android-button






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 29 '18 at 18:37









PrashanthPrashanth

1,07911018




1,07911018













  • Do you want to set image on button ?

    – Code Kadiya
    Dec 29 '18 at 18:41











  • I suggest to use ImageButton

    – Code Kadiya
    Dec 29 '18 at 18:42











  • Else you can create LinearLayout and apply style

    – Code Kadiya
    Dec 29 '18 at 18:44











  • How about leaving out the two lines before the last one and just using btn.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.cal, 0); ?

    – 0X0nosugar
    Dec 29 '18 at 18:48






  • 1





    The linked version of the method will take int values (resource identifiers)

    – 0X0nosugar
    Dec 29 '18 at 19:08



















  • Do you want to set image on button ?

    – Code Kadiya
    Dec 29 '18 at 18:41











  • I suggest to use ImageButton

    – Code Kadiya
    Dec 29 '18 at 18:42











  • Else you can create LinearLayout and apply style

    – Code Kadiya
    Dec 29 '18 at 18:44











  • How about leaving out the two lines before the last one and just using btn.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.cal, 0); ?

    – 0X0nosugar
    Dec 29 '18 at 18:48






  • 1





    The linked version of the method will take int values (resource identifiers)

    – 0X0nosugar
    Dec 29 '18 at 19:08

















Do you want to set image on button ?

– Code Kadiya
Dec 29 '18 at 18:41





Do you want to set image on button ?

– Code Kadiya
Dec 29 '18 at 18:41













I suggest to use ImageButton

– Code Kadiya
Dec 29 '18 at 18:42





I suggest to use ImageButton

– Code Kadiya
Dec 29 '18 at 18:42













Else you can create LinearLayout and apply style

– Code Kadiya
Dec 29 '18 at 18:44





Else you can create LinearLayout and apply style

– Code Kadiya
Dec 29 '18 at 18:44













How about leaving out the two lines before the last one and just using btn.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.cal, 0); ?

– 0X0nosugar
Dec 29 '18 at 18:48





How about leaving out the two lines before the last one and just using btn.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.cal, 0); ?

– 0X0nosugar
Dec 29 '18 at 18:48




1




1





The linked version of the method will take int values (resource identifiers)

– 0X0nosugar
Dec 29 '18 at 19:08





The linked version of the method will take int values (resource identifiers)

– 0X0nosugar
Dec 29 '18 at 19:08












2 Answers
2






active

oldest

votes


















0














I noticed that the size of the image is very important when using it on other objects - when I adjusted the image to a smaller size, my code worked fine....



The same code that I was trying earlier worked for me after resizing the image...



                    Button btn = new Button(this);
btn.setTextColor(Color.parseColor("#000000"));
btn.setText("SomeText");
btn.setTextSize(TypedValue.COMPLEX_UNIT_PX, mediumTextSize);
btn.setGravity(Gravity.CENTER | Gravity.LEFT);
Drawable icon= getApplicationContext().getResources().getDrawable(R.drawable.cal);
btn.setBackgroundResource(R.drawable.light_bg);
icon.setBounds(0, 0, 0, 0); //Left,Top,Right,Bottom
btn.setCompoundDrawablesWithIntrinsicBounds( null, null, icon, null);


Thank you all for your help and suggestions !






share|improve this answer































    0














    Try this



               btn.setText("Some Text");
    btn.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
    btn.setGravity(Gravity.CENTER | Gravity.LEFT);
    Drawable myDrawable =
    getResources().getDrawable(R.drawable.ic_cancel_black_24dp);
    btn.setCompoundDrawablesWithIntrinsicBounds(null,null,myDrawable,null);





    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%2f53972324%2fadding-a-text-and-image-on-a-button-programmatically-in-android%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      I noticed that the size of the image is very important when using it on other objects - when I adjusted the image to a smaller size, my code worked fine....



      The same code that I was trying earlier worked for me after resizing the image...



                          Button btn = new Button(this);
      btn.setTextColor(Color.parseColor("#000000"));
      btn.setText("SomeText");
      btn.setTextSize(TypedValue.COMPLEX_UNIT_PX, mediumTextSize);
      btn.setGravity(Gravity.CENTER | Gravity.LEFT);
      Drawable icon= getApplicationContext().getResources().getDrawable(R.drawable.cal);
      btn.setBackgroundResource(R.drawable.light_bg);
      icon.setBounds(0, 0, 0, 0); //Left,Top,Right,Bottom
      btn.setCompoundDrawablesWithIntrinsicBounds( null, null, icon, null);


      Thank you all for your help and suggestions !






      share|improve this answer




























        0














        I noticed that the size of the image is very important when using it on other objects - when I adjusted the image to a smaller size, my code worked fine....



        The same code that I was trying earlier worked for me after resizing the image...



                            Button btn = new Button(this);
        btn.setTextColor(Color.parseColor("#000000"));
        btn.setText("SomeText");
        btn.setTextSize(TypedValue.COMPLEX_UNIT_PX, mediumTextSize);
        btn.setGravity(Gravity.CENTER | Gravity.LEFT);
        Drawable icon= getApplicationContext().getResources().getDrawable(R.drawable.cal);
        btn.setBackgroundResource(R.drawable.light_bg);
        icon.setBounds(0, 0, 0, 0); //Left,Top,Right,Bottom
        btn.setCompoundDrawablesWithIntrinsicBounds( null, null, icon, null);


        Thank you all for your help and suggestions !






        share|improve this answer


























          0












          0








          0







          I noticed that the size of the image is very important when using it on other objects - when I adjusted the image to a smaller size, my code worked fine....



          The same code that I was trying earlier worked for me after resizing the image...



                              Button btn = new Button(this);
          btn.setTextColor(Color.parseColor("#000000"));
          btn.setText("SomeText");
          btn.setTextSize(TypedValue.COMPLEX_UNIT_PX, mediumTextSize);
          btn.setGravity(Gravity.CENTER | Gravity.LEFT);
          Drawable icon= getApplicationContext().getResources().getDrawable(R.drawable.cal);
          btn.setBackgroundResource(R.drawable.light_bg);
          icon.setBounds(0, 0, 0, 0); //Left,Top,Right,Bottom
          btn.setCompoundDrawablesWithIntrinsicBounds( null, null, icon, null);


          Thank you all for your help and suggestions !






          share|improve this answer













          I noticed that the size of the image is very important when using it on other objects - when I adjusted the image to a smaller size, my code worked fine....



          The same code that I was trying earlier worked for me after resizing the image...



                              Button btn = new Button(this);
          btn.setTextColor(Color.parseColor("#000000"));
          btn.setText("SomeText");
          btn.setTextSize(TypedValue.COMPLEX_UNIT_PX, mediumTextSize);
          btn.setGravity(Gravity.CENTER | Gravity.LEFT);
          Drawable icon= getApplicationContext().getResources().getDrawable(R.drawable.cal);
          btn.setBackgroundResource(R.drawable.light_bg);
          icon.setBounds(0, 0, 0, 0); //Left,Top,Right,Bottom
          btn.setCompoundDrawablesWithIntrinsicBounds( null, null, icon, null);


          Thank you all for your help and suggestions !







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 29 '18 at 20:36









          PrashanthPrashanth

          1,07911018




          1,07911018

























              0














              Try this



                         btn.setText("Some Text");
              btn.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
              btn.setGravity(Gravity.CENTER | Gravity.LEFT);
              Drawable myDrawable =
              getResources().getDrawable(R.drawable.ic_cancel_black_24dp);
              btn.setCompoundDrawablesWithIntrinsicBounds(null,null,myDrawable,null);





              share|improve this answer




























                0














                Try this



                           btn.setText("Some Text");
                btn.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
                btn.setGravity(Gravity.CENTER | Gravity.LEFT);
                Drawable myDrawable =
                getResources().getDrawable(R.drawable.ic_cancel_black_24dp);
                btn.setCompoundDrawablesWithIntrinsicBounds(null,null,myDrawable,null);





                share|improve this answer


























                  0












                  0








                  0







                  Try this



                             btn.setText("Some Text");
                  btn.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
                  btn.setGravity(Gravity.CENTER | Gravity.LEFT);
                  Drawable myDrawable =
                  getResources().getDrawable(R.drawable.ic_cancel_black_24dp);
                  btn.setCompoundDrawablesWithIntrinsicBounds(null,null,myDrawable,null);





                  share|improve this answer













                  Try this



                             btn.setText("Some Text");
                  btn.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
                  btn.setGravity(Gravity.CENTER | Gravity.LEFT);
                  Drawable myDrawable =
                  getResources().getDrawable(R.drawable.ic_cancel_black_24dp);
                  btn.setCompoundDrawablesWithIntrinsicBounds(null,null,myDrawable,null);






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 2 at 5:19









                  Android GeekAndroid Geek

                  4,4791823




                  4,4791823






























                      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%2f53972324%2fadding-a-text-and-image-on-a-button-programmatically-in-android%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

                      Angular Downloading a file using contenturl with Basic Authentication

                      Olmecas

                      Can't read property showImagePicker of undefined in react native iOS