Android: add pressed effect on a button with image, text under it and custom border












0














I'm working on a project where I have a Main Menu with buttons that have image and text under the image, they are already done:



main menu buttons



To achieve that button layout with rounded corners, I put as background an xml called border_button:



<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="3dp" />
<solid android:color="@color/colorPrimary" />
</shape>


And to keep background image and text with rounded corners, my button xml is like that:



        <Button
android:id="@+id/buttonMeusDados"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2sp"
android:layout_weight="1"

android:background="@drawable/border_button"
android:drawableTop="@drawable/button_image_icon_meus_dados"
android:text="@string/my_data"
android:textColor="@android:color/white" />


The problem now is that those buttons don't return any feedback to the user when pressed, they are static, no pressed animation



I found a lot of ways how to do that, but I'm not being able to mix them with what I already have, if I make the pressed effect animation, I lose all my retangle layout with rounded corners and etc



For the pressed effect I mainly followed @Ljdawson answer from this topic: click effect on button in Android but no success, as I explained above



How can I do it?










share|improve this question



























    0














    I'm working on a project where I have a Main Menu with buttons that have image and text under the image, they are already done:



    main menu buttons



    To achieve that button layout with rounded corners, I put as background an xml called border_button:



    <?xml version="1.0" encoding="utf-8"?>
    <shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="3dp" />
    <solid android:color="@color/colorPrimary" />
    </shape>


    And to keep background image and text with rounded corners, my button xml is like that:



            <Button
    android:id="@+id/buttonMeusDados"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="2sp"
    android:layout_weight="1"

    android:background="@drawable/border_button"
    android:drawableTop="@drawable/button_image_icon_meus_dados"
    android:text="@string/my_data"
    android:textColor="@android:color/white" />


    The problem now is that those buttons don't return any feedback to the user when pressed, they are static, no pressed animation



    I found a lot of ways how to do that, but I'm not being able to mix them with what I already have, if I make the pressed effect animation, I lose all my retangle layout with rounded corners and etc



    For the pressed effect I mainly followed @Ljdawson answer from this topic: click effect on button in Android but no success, as I explained above



    How can I do it?










    share|improve this question

























      0












      0








      0







      I'm working on a project where I have a Main Menu with buttons that have image and text under the image, they are already done:



      main menu buttons



      To achieve that button layout with rounded corners, I put as background an xml called border_button:



      <?xml version="1.0" encoding="utf-8"?>
      <shape
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:shape="rectangle">
      <corners android:radius="3dp" />
      <solid android:color="@color/colorPrimary" />
      </shape>


      And to keep background image and text with rounded corners, my button xml is like that:



              <Button
      android:id="@+id/buttonMeusDados"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_margin="2sp"
      android:layout_weight="1"

      android:background="@drawable/border_button"
      android:drawableTop="@drawable/button_image_icon_meus_dados"
      android:text="@string/my_data"
      android:textColor="@android:color/white" />


      The problem now is that those buttons don't return any feedback to the user when pressed, they are static, no pressed animation



      I found a lot of ways how to do that, but I'm not being able to mix them with what I already have, if I make the pressed effect animation, I lose all my retangle layout with rounded corners and etc



      For the pressed effect I mainly followed @Ljdawson answer from this topic: click effect on button in Android but no success, as I explained above



      How can I do it?










      share|improve this question













      I'm working on a project where I have a Main Menu with buttons that have image and text under the image, they are already done:



      main menu buttons



      To achieve that button layout with rounded corners, I put as background an xml called border_button:



      <?xml version="1.0" encoding="utf-8"?>
      <shape
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:shape="rectangle">
      <corners android:radius="3dp" />
      <solid android:color="@color/colorPrimary" />
      </shape>


      And to keep background image and text with rounded corners, my button xml is like that:



              <Button
      android:id="@+id/buttonMeusDados"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_margin="2sp"
      android:layout_weight="1"

      android:background="@drawable/border_button"
      android:drawableTop="@drawable/button_image_icon_meus_dados"
      android:text="@string/my_data"
      android:textColor="@android:color/white" />


      The problem now is that those buttons don't return any feedback to the user when pressed, they are static, no pressed animation



      I found a lot of ways how to do that, but I'm not being able to mix them with what I already have, if I make the pressed effect animation, I lose all my retangle layout with rounded corners and etc



      For the pressed effect I mainly followed @Ljdawson answer from this topic: click effect on button in Android but no success, as I explained above



      How can I do it?







      android xml image button text






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 '18 at 20:46









      Adriano Carvalho

      31




      31
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Your border_button.xml should be a selector. Like in the following example:



          <selector xmlns:android="http://schemas.android.com/apk/res/android">

          <!-- the order of items is important. The first one that matches the state is rendered. -->
          <item android:state_pressed="true" android:drawable="@drawable/button_pressed"/>
          <item android:drawable="@drawable/button_normal" />
          </selector>

          <!-- to make transition more visible you can slow it down.
          Add this inside of selector tag, after the xmlns:android="..."
          android:exitFadeDuration="@android:integer/config_longAnimTime"
          -->


          button_pressed and button_normal are xml files that draw pressed and normal states of the button, examples given bellow:



          <!-- button_pressed.xml -->
          <?xml version="1.0" encoding="utf-8"?>
          <shape
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:shape="rectangle">
          <corners android:radius="3dp" />
          <solid android:color="@color/colorAccent" />
          </shape>


          and



          <!-- button_normal.xml -->
          <?xml version="1.0" encoding="utf-8"?>
          <shape
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:shape="rectangle">
          <corners android:radius="3dp" />
          <solid android:color="@color/colorPrimary" />
          </shape>





          share|improve this answer



















          • 1




            Worked like a charm! Thanks a lot!
            – Adriano Carvalho
            Nov 22 '18 at 23:08











          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%2f53437801%2fandroid-add-pressed-effect-on-a-button-with-image-text-under-it-and-custom-bor%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Your border_button.xml should be a selector. Like in the following example:



          <selector xmlns:android="http://schemas.android.com/apk/res/android">

          <!-- the order of items is important. The first one that matches the state is rendered. -->
          <item android:state_pressed="true" android:drawable="@drawable/button_pressed"/>
          <item android:drawable="@drawable/button_normal" />
          </selector>

          <!-- to make transition more visible you can slow it down.
          Add this inside of selector tag, after the xmlns:android="..."
          android:exitFadeDuration="@android:integer/config_longAnimTime"
          -->


          button_pressed and button_normal are xml files that draw pressed and normal states of the button, examples given bellow:



          <!-- button_pressed.xml -->
          <?xml version="1.0" encoding="utf-8"?>
          <shape
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:shape="rectangle">
          <corners android:radius="3dp" />
          <solid android:color="@color/colorAccent" />
          </shape>


          and



          <!-- button_normal.xml -->
          <?xml version="1.0" encoding="utf-8"?>
          <shape
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:shape="rectangle">
          <corners android:radius="3dp" />
          <solid android:color="@color/colorPrimary" />
          </shape>





          share|improve this answer



















          • 1




            Worked like a charm! Thanks a lot!
            – Adriano Carvalho
            Nov 22 '18 at 23:08
















          0














          Your border_button.xml should be a selector. Like in the following example:



          <selector xmlns:android="http://schemas.android.com/apk/res/android">

          <!-- the order of items is important. The first one that matches the state is rendered. -->
          <item android:state_pressed="true" android:drawable="@drawable/button_pressed"/>
          <item android:drawable="@drawable/button_normal" />
          </selector>

          <!-- to make transition more visible you can slow it down.
          Add this inside of selector tag, after the xmlns:android="..."
          android:exitFadeDuration="@android:integer/config_longAnimTime"
          -->


          button_pressed and button_normal are xml files that draw pressed and normal states of the button, examples given bellow:



          <!-- button_pressed.xml -->
          <?xml version="1.0" encoding="utf-8"?>
          <shape
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:shape="rectangle">
          <corners android:radius="3dp" />
          <solid android:color="@color/colorAccent" />
          </shape>


          and



          <!-- button_normal.xml -->
          <?xml version="1.0" encoding="utf-8"?>
          <shape
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:shape="rectangle">
          <corners android:radius="3dp" />
          <solid android:color="@color/colorPrimary" />
          </shape>





          share|improve this answer



















          • 1




            Worked like a charm! Thanks a lot!
            – Adriano Carvalho
            Nov 22 '18 at 23:08














          0












          0








          0






          Your border_button.xml should be a selector. Like in the following example:



          <selector xmlns:android="http://schemas.android.com/apk/res/android">

          <!-- the order of items is important. The first one that matches the state is rendered. -->
          <item android:state_pressed="true" android:drawable="@drawable/button_pressed"/>
          <item android:drawable="@drawable/button_normal" />
          </selector>

          <!-- to make transition more visible you can slow it down.
          Add this inside of selector tag, after the xmlns:android="..."
          android:exitFadeDuration="@android:integer/config_longAnimTime"
          -->


          button_pressed and button_normal are xml files that draw pressed and normal states of the button, examples given bellow:



          <!-- button_pressed.xml -->
          <?xml version="1.0" encoding="utf-8"?>
          <shape
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:shape="rectangle">
          <corners android:radius="3dp" />
          <solid android:color="@color/colorAccent" />
          </shape>


          and



          <!-- button_normal.xml -->
          <?xml version="1.0" encoding="utf-8"?>
          <shape
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:shape="rectangle">
          <corners android:radius="3dp" />
          <solid android:color="@color/colorPrimary" />
          </shape>





          share|improve this answer














          Your border_button.xml should be a selector. Like in the following example:



          <selector xmlns:android="http://schemas.android.com/apk/res/android">

          <!-- the order of items is important. The first one that matches the state is rendered. -->
          <item android:state_pressed="true" android:drawable="@drawable/button_pressed"/>
          <item android:drawable="@drawable/button_normal" />
          </selector>

          <!-- to make transition more visible you can slow it down.
          Add this inside of selector tag, after the xmlns:android="..."
          android:exitFadeDuration="@android:integer/config_longAnimTime"
          -->


          button_pressed and button_normal are xml files that draw pressed and normal states of the button, examples given bellow:



          <!-- button_pressed.xml -->
          <?xml version="1.0" encoding="utf-8"?>
          <shape
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:shape="rectangle">
          <corners android:radius="3dp" />
          <solid android:color="@color/colorAccent" />
          </shape>


          and



          <!-- button_normal.xml -->
          <?xml version="1.0" encoding="utf-8"?>
          <shape
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:shape="rectangle">
          <corners android:radius="3dp" />
          <solid android:color="@color/colorPrimary" />
          </shape>






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 22 '18 at 21:41

























          answered Nov 22 '18 at 21:33









          N. Matic

          1688




          1688








          • 1




            Worked like a charm! Thanks a lot!
            – Adriano Carvalho
            Nov 22 '18 at 23:08














          • 1




            Worked like a charm! Thanks a lot!
            – Adriano Carvalho
            Nov 22 '18 at 23:08








          1




          1




          Worked like a charm! Thanks a lot!
          – Adriano Carvalho
          Nov 22 '18 at 23:08




          Worked like a charm! Thanks a lot!
          – Adriano Carvalho
          Nov 22 '18 at 23:08


















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53437801%2fandroid-add-pressed-effect-on-a-button-with-image-text-under-it-and-custom-bor%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

          Berounka

          Sphinx de Gizeh

          Different font size/position of beamer's navigation symbols template's content depending on regular/plain...