Removing deafult background of ImageButton in Android but keep onClick highlight










3















I have a few ImageButtons in my app and I'v removed the default grey background with



android:background="@android:color/transparent" or android:background="@null"



The problem is, it also removes the onClick highlight background (orange in API8 and blue in API16)



I'v read so many Q&A here, people are all suggesting to use selector.Instead of making another image for each of the buttons, I'd like to have a background color highlight only. Is there a simple way to achieve this?



solution: programmatically



import android.view.View;
import android.view.View.OnTouchListener;

// ...

btn.setOnTouchListener(new OnTouchListener()
@Override
public boolean onTouch(View v, MotionEvent event)
if(event.getAction() == (MotionEvent.ACTION_DOWN))
// set background highlight color
btn.setBackgroundResource(R.color.blue);

if(event.getAction() == (MotionEvent.ACTION_UP))
// restore transparent
btn.setBackgroundResource(
getResources().getColor(android.R.color.transparent));

return false;

);


It isn't that simple, but does the job and saves me a lot of time making another image for each button while I don't really need fancy onclick styles.










share|improve this question
























  • how is your solution simpler than creating a selector and applying it to all buttons (especially when the background is either transparent or solid color)?

    – Andrii Chernenko
    Jan 15 '13 at 21:21















3















I have a few ImageButtons in my app and I'v removed the default grey background with



android:background="@android:color/transparent" or android:background="@null"



The problem is, it also removes the onClick highlight background (orange in API8 and blue in API16)



I'v read so many Q&A here, people are all suggesting to use selector.Instead of making another image for each of the buttons, I'd like to have a background color highlight only. Is there a simple way to achieve this?



solution: programmatically



import android.view.View;
import android.view.View.OnTouchListener;

// ...

btn.setOnTouchListener(new OnTouchListener()
@Override
public boolean onTouch(View v, MotionEvent event)
if(event.getAction() == (MotionEvent.ACTION_DOWN))
// set background highlight color
btn.setBackgroundResource(R.color.blue);

if(event.getAction() == (MotionEvent.ACTION_UP))
// restore transparent
btn.setBackgroundResource(
getResources().getColor(android.R.color.transparent));

return false;

);


It isn't that simple, but does the job and saves me a lot of time making another image for each button while I don't really need fancy onclick styles.










share|improve this question
























  • how is your solution simpler than creating a selector and applying it to all buttons (especially when the background is either transparent or solid color)?

    – Andrii Chernenko
    Jan 15 '13 at 21:21













3












3








3








I have a few ImageButtons in my app and I'v removed the default grey background with



android:background="@android:color/transparent" or android:background="@null"



The problem is, it also removes the onClick highlight background (orange in API8 and blue in API16)



I'v read so many Q&A here, people are all suggesting to use selector.Instead of making another image for each of the buttons, I'd like to have a background color highlight only. Is there a simple way to achieve this?



solution: programmatically



import android.view.View;
import android.view.View.OnTouchListener;

// ...

btn.setOnTouchListener(new OnTouchListener()
@Override
public boolean onTouch(View v, MotionEvent event)
if(event.getAction() == (MotionEvent.ACTION_DOWN))
// set background highlight color
btn.setBackgroundResource(R.color.blue);

if(event.getAction() == (MotionEvent.ACTION_UP))
// restore transparent
btn.setBackgroundResource(
getResources().getColor(android.R.color.transparent));

return false;

);


It isn't that simple, but does the job and saves me a lot of time making another image for each button while I don't really need fancy onclick styles.










share|improve this question
















I have a few ImageButtons in my app and I'v removed the default grey background with



android:background="@android:color/transparent" or android:background="@null"



The problem is, it also removes the onClick highlight background (orange in API8 and blue in API16)



I'v read so many Q&A here, people are all suggesting to use selector.Instead of making another image for each of the buttons, I'd like to have a background color highlight only. Is there a simple way to achieve this?



solution: programmatically



import android.view.View;
import android.view.View.OnTouchListener;

// ...

btn.setOnTouchListener(new OnTouchListener()
@Override
public boolean onTouch(View v, MotionEvent event)
if(event.getAction() == (MotionEvent.ACTION_DOWN))
// set background highlight color
btn.setBackgroundResource(R.color.blue);

if(event.getAction() == (MotionEvent.ACTION_UP))
// restore transparent
btn.setBackgroundResource(
getResources().getColor(android.R.color.transparent));

return false;

);


It isn't that simple, but does the job and saves me a lot of time making another image for each button while I don't really need fancy onclick styles.







android background onclick






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 15 '13 at 21:34







user1643156

















asked Jan 15 '13 at 20:53









user1643156user1643156

1,51793155




1,51793155












  • how is your solution simpler than creating a selector and applying it to all buttons (especially when the background is either transparent or solid color)?

    – Andrii Chernenko
    Jan 15 '13 at 21:21

















  • how is your solution simpler than creating a selector and applying it to all buttons (especially when the background is either transparent or solid color)?

    – Andrii Chernenko
    Jan 15 '13 at 21:21
















how is your solution simpler than creating a selector and applying it to all buttons (especially when the background is either transparent or solid color)?

– Andrii Chernenko
Jan 15 '13 at 21:21





how is your solution simpler than creating a selector and applying it to all buttons (especially when the background is either transparent or solid color)?

– Andrii Chernenko
Jan 15 '13 at 21:21












4 Answers
4






active

oldest

votes


















4














You really need to use a selector for this.



You can use colors for your backgrounds in your selectors, it doesn't need to be a PNG resource (just set the background to a color, instead of a drawable resource).



You will place this code in your drawable folder, with a specific name (like button_sel.xml).



Then you set this as the background to your button in your XML it would look like this:



android:background="@drawable/button_sel"


The selector would look something like this:



<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@color/ab_background"
android:state_focused="true"
android:state_pressed="true"/>
<item
android:drawable="@color/ab_background"
android:state_focused="false"
android:state_pressed="true"/>
<item
android:drawable="@color/ab_background_on"
android:state_focused="true"
android:state_pressed="false"/>
<item
android:drawable="@color/ab_background_on"
android:state_focused="false"
android:state_pressed="false"/>
</selector>





share|improve this answer























  • Thanks Booger. I'm new to Android programming, and I thought I had to use an image in selector.

    – user1643156
    Jan 15 '13 at 21:39


















5














There are only two attributes relevant in this case - background and src. If you want to use the same background for all image buttons (with color highlight), you need to add the part of background which changes from button to button to your image set by src, which is not too clean and neat (but of course you can do that if you want).



So I think the best here is to use selectors for background, as suggested by many users.



use the following code:



<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/holo_blue_light" android:state_pressed="true"/>
<item android:drawable="@android:color/transparent"/>
</selector>





share|improve this answer

























  • I would'v accepted your answer if you had your edited version posted before Booger...

    – user1643156
    Jan 15 '13 at 21:36











  • @user1643156 no problem with that :)

    – Andrii Chernenko
    Jan 15 '13 at 21:41


















0














Don't think so, you can search through the API to find the predefined selector for the ImageButton, than copy the code and alter the selector the way you need it. So you would create a custom ImageButton.



But I don't think there is an easy way, just like you wanted it with just changing a color






share|improve this answer






























    0














    I'm having the same problem but now it has been solved. If you are using android:background="@null it will resulted of no background at all and no button pressed animation will be appeared to the user.



    Just adding this line of code to make an image button become flashing upon clicked. This is the proper way to alert the user an image button has been clicked.



     android:background="?android:selectableItemBackground





    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%2f14346720%2fremoving-deafult-background-of-imagebutton-in-android-but-keep-onclick-highlight%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      4














      You really need to use a selector for this.



      You can use colors for your backgrounds in your selectors, it doesn't need to be a PNG resource (just set the background to a color, instead of a drawable resource).



      You will place this code in your drawable folder, with a specific name (like button_sel.xml).



      Then you set this as the background to your button in your XML it would look like this:



      android:background="@drawable/button_sel"


      The selector would look something like this:



      <selector
      xmlns:android="http://schemas.android.com/apk/res/android">
      <item
      android:drawable="@color/ab_background"
      android:state_focused="true"
      android:state_pressed="true"/>
      <item
      android:drawable="@color/ab_background"
      android:state_focused="false"
      android:state_pressed="true"/>
      <item
      android:drawable="@color/ab_background_on"
      android:state_focused="true"
      android:state_pressed="false"/>
      <item
      android:drawable="@color/ab_background_on"
      android:state_focused="false"
      android:state_pressed="false"/>
      </selector>





      share|improve this answer























      • Thanks Booger. I'm new to Android programming, and I thought I had to use an image in selector.

        – user1643156
        Jan 15 '13 at 21:39















      4














      You really need to use a selector for this.



      You can use colors for your backgrounds in your selectors, it doesn't need to be a PNG resource (just set the background to a color, instead of a drawable resource).



      You will place this code in your drawable folder, with a specific name (like button_sel.xml).



      Then you set this as the background to your button in your XML it would look like this:



      android:background="@drawable/button_sel"


      The selector would look something like this:



      <selector
      xmlns:android="http://schemas.android.com/apk/res/android">
      <item
      android:drawable="@color/ab_background"
      android:state_focused="true"
      android:state_pressed="true"/>
      <item
      android:drawable="@color/ab_background"
      android:state_focused="false"
      android:state_pressed="true"/>
      <item
      android:drawable="@color/ab_background_on"
      android:state_focused="true"
      android:state_pressed="false"/>
      <item
      android:drawable="@color/ab_background_on"
      android:state_focused="false"
      android:state_pressed="false"/>
      </selector>





      share|improve this answer























      • Thanks Booger. I'm new to Android programming, and I thought I had to use an image in selector.

        – user1643156
        Jan 15 '13 at 21:39













      4












      4








      4







      You really need to use a selector for this.



      You can use colors for your backgrounds in your selectors, it doesn't need to be a PNG resource (just set the background to a color, instead of a drawable resource).



      You will place this code in your drawable folder, with a specific name (like button_sel.xml).



      Then you set this as the background to your button in your XML it would look like this:



      android:background="@drawable/button_sel"


      The selector would look something like this:



      <selector
      xmlns:android="http://schemas.android.com/apk/res/android">
      <item
      android:drawable="@color/ab_background"
      android:state_focused="true"
      android:state_pressed="true"/>
      <item
      android:drawable="@color/ab_background"
      android:state_focused="false"
      android:state_pressed="true"/>
      <item
      android:drawable="@color/ab_background_on"
      android:state_focused="true"
      android:state_pressed="false"/>
      <item
      android:drawable="@color/ab_background_on"
      android:state_focused="false"
      android:state_pressed="false"/>
      </selector>





      share|improve this answer













      You really need to use a selector for this.



      You can use colors for your backgrounds in your selectors, it doesn't need to be a PNG resource (just set the background to a color, instead of a drawable resource).



      You will place this code in your drawable folder, with a specific name (like button_sel.xml).



      Then you set this as the background to your button in your XML it would look like this:



      android:background="@drawable/button_sel"


      The selector would look something like this:



      <selector
      xmlns:android="http://schemas.android.com/apk/res/android">
      <item
      android:drawable="@color/ab_background"
      android:state_focused="true"
      android:state_pressed="true"/>
      <item
      android:drawable="@color/ab_background"
      android:state_focused="false"
      android:state_pressed="true"/>
      <item
      android:drawable="@color/ab_background_on"
      android:state_focused="true"
      android:state_pressed="false"/>
      <item
      android:drawable="@color/ab_background_on"
      android:state_focused="false"
      android:state_pressed="false"/>
      </selector>






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Jan 15 '13 at 21:11









      BoogerBooger

      16.1k64760




      16.1k64760












      • Thanks Booger. I'm new to Android programming, and I thought I had to use an image in selector.

        – user1643156
        Jan 15 '13 at 21:39

















      • Thanks Booger. I'm new to Android programming, and I thought I had to use an image in selector.

        – user1643156
        Jan 15 '13 at 21:39
















      Thanks Booger. I'm new to Android programming, and I thought I had to use an image in selector.

      – user1643156
      Jan 15 '13 at 21:39





      Thanks Booger. I'm new to Android programming, and I thought I had to use an image in selector.

      – user1643156
      Jan 15 '13 at 21:39













      5














      There are only two attributes relevant in this case - background and src. If you want to use the same background for all image buttons (with color highlight), you need to add the part of background which changes from button to button to your image set by src, which is not too clean and neat (but of course you can do that if you want).



      So I think the best here is to use selectors for background, as suggested by many users.



      use the following code:



      <?xml version="1.0" encoding="utf-8"?>
      <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:drawable="@android:color/holo_blue_light" android:state_pressed="true"/>
      <item android:drawable="@android:color/transparent"/>
      </selector>





      share|improve this answer

























      • I would'v accepted your answer if you had your edited version posted before Booger...

        – user1643156
        Jan 15 '13 at 21:36











      • @user1643156 no problem with that :)

        – Andrii Chernenko
        Jan 15 '13 at 21:41















      5














      There are only two attributes relevant in this case - background and src. If you want to use the same background for all image buttons (with color highlight), you need to add the part of background which changes from button to button to your image set by src, which is not too clean and neat (but of course you can do that if you want).



      So I think the best here is to use selectors for background, as suggested by many users.



      use the following code:



      <?xml version="1.0" encoding="utf-8"?>
      <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:drawable="@android:color/holo_blue_light" android:state_pressed="true"/>
      <item android:drawable="@android:color/transparent"/>
      </selector>





      share|improve this answer

























      • I would'v accepted your answer if you had your edited version posted before Booger...

        – user1643156
        Jan 15 '13 at 21:36











      • @user1643156 no problem with that :)

        – Andrii Chernenko
        Jan 15 '13 at 21:41













      5












      5








      5







      There are only two attributes relevant in this case - background and src. If you want to use the same background for all image buttons (with color highlight), you need to add the part of background which changes from button to button to your image set by src, which is not too clean and neat (but of course you can do that if you want).



      So I think the best here is to use selectors for background, as suggested by many users.



      use the following code:



      <?xml version="1.0" encoding="utf-8"?>
      <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:drawable="@android:color/holo_blue_light" android:state_pressed="true"/>
      <item android:drawable="@android:color/transparent"/>
      </selector>





      share|improve this answer















      There are only two attributes relevant in this case - background and src. If you want to use the same background for all image buttons (with color highlight), you need to add the part of background which changes from button to button to your image set by src, which is not too clean and neat (but of course you can do that if you want).



      So I think the best here is to use selectors for background, as suggested by many users.



      use the following code:



      <?xml version="1.0" encoding="utf-8"?>
      <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:drawable="@android:color/holo_blue_light" android:state_pressed="true"/>
      <item android:drawable="@android:color/transparent"/>
      </selector>






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Jan 15 '13 at 21:18

























      answered Jan 15 '13 at 21:00









      Andrii ChernenkoAndrii Chernenko

      6,31055269




      6,31055269












      • I would'v accepted your answer if you had your edited version posted before Booger...

        – user1643156
        Jan 15 '13 at 21:36











      • @user1643156 no problem with that :)

        – Andrii Chernenko
        Jan 15 '13 at 21:41

















      • I would'v accepted your answer if you had your edited version posted before Booger...

        – user1643156
        Jan 15 '13 at 21:36











      • @user1643156 no problem with that :)

        – Andrii Chernenko
        Jan 15 '13 at 21:41
















      I would'v accepted your answer if you had your edited version posted before Booger...

      – user1643156
      Jan 15 '13 at 21:36





      I would'v accepted your answer if you had your edited version posted before Booger...

      – user1643156
      Jan 15 '13 at 21:36













      @user1643156 no problem with that :)

      – Andrii Chernenko
      Jan 15 '13 at 21:41





      @user1643156 no problem with that :)

      – Andrii Chernenko
      Jan 15 '13 at 21:41











      0














      Don't think so, you can search through the API to find the predefined selector for the ImageButton, than copy the code and alter the selector the way you need it. So you would create a custom ImageButton.



      But I don't think there is an easy way, just like you wanted it with just changing a color






      share|improve this answer



























        0














        Don't think so, you can search through the API to find the predefined selector for the ImageButton, than copy the code and alter the selector the way you need it. So you would create a custom ImageButton.



        But I don't think there is an easy way, just like you wanted it with just changing a color






        share|improve this answer

























          0












          0








          0







          Don't think so, you can search through the API to find the predefined selector for the ImageButton, than copy the code and alter the selector the way you need it. So you would create a custom ImageButton.



          But I don't think there is an easy way, just like you wanted it with just changing a color






          share|improve this answer













          Don't think so, you can search through the API to find the predefined selector for the ImageButton, than copy the code and alter the selector the way you need it. So you would create a custom ImageButton.



          But I don't think there is an easy way, just like you wanted it with just changing a color







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 15 '13 at 20:59









          NickolausNickolaus

          3,68823153




          3,68823153





















              0














              I'm having the same problem but now it has been solved. If you are using android:background="@null it will resulted of no background at all and no button pressed animation will be appeared to the user.



              Just adding this line of code to make an image button become flashing upon clicked. This is the proper way to alert the user an image button has been clicked.



               android:background="?android:selectableItemBackground





              share|improve this answer





























                0














                I'm having the same problem but now it has been solved. If you are using android:background="@null it will resulted of no background at all and no button pressed animation will be appeared to the user.



                Just adding this line of code to make an image button become flashing upon clicked. This is the proper way to alert the user an image button has been clicked.



                 android:background="?android:selectableItemBackground





                share|improve this answer



























                  0












                  0








                  0







                  I'm having the same problem but now it has been solved. If you are using android:background="@null it will resulted of no background at all and no button pressed animation will be appeared to the user.



                  Just adding this line of code to make an image button become flashing upon clicked. This is the proper way to alert the user an image button has been clicked.



                   android:background="?android:selectableItemBackground





                  share|improve this answer















                  I'm having the same problem but now it has been solved. If you are using android:background="@null it will resulted of no background at all and no button pressed animation will be appeared to the user.



                  Just adding this line of code to make an image button become flashing upon clicked. This is the proper way to alert the user an image button has been clicked.



                   android:background="?android:selectableItemBackground






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 15 '18 at 10:30









                  Agilanbu

                  1,2021420




                  1,2021420










                  answered Oct 18 '16 at 2:15









                  millemille

                  314




                  314



























                      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%2f14346720%2fremoving-deafult-background-of-imagebutton-in-android-but-keep-onclick-highlight%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







                      這個網誌中的熱門文章

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

                      Museum of Modern and Contemporary Art of Trento and Rovereto