How to set rounded buttons with background color and change color on pressed

Jcoder

In my android app there is a rounded rectangle button with green colored background. i did this using .xml file

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle" >

<solid android:color="#B5D397" />

<corners
    android:bottomLeftRadius="10dp"
    android:bottomRightRadius="10dp"
    android:topLeftRadius="10dp"
    android:topRightRadius="10dp" />


</shape>

and

 android:background="@drawable/rounded_btn"

in layout file

but when i press button is wasn't showing any effect(no change is color) so i used

@Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Button view = (Button) v;
             view.getBackground().setColorFilter(0x77000000, PorterDuff.Mode.SRC_ATOP);

and color of button changes to dark green after pressing it. Till here everything is working fine, but problem is after releasing button color remains dark green. i want it to be like as it was before pressing.I referred few examples which says to use selector in .xml file i.e

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:color="#c0c0c0"
        android:state_selected="true"/>
    <item
        android:color="#ffffff"
        android:state_pressed="true"/>
    <item
        android:color="#9A9A9A"
        android:state_focused="false"
        android:state_pressed="false"
        android:state_selected="false"/>
</selector>

Which also needs android:background="@drawable/btn_state" but i have already used android:background=@drawable/rounded_btn

So how to give both effect together

i also tried using OnTouchListener

button.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
 // show interest in events resulting from ACTION_DOWN
 if(event.getAction()==MotionEvent.ACTION_DOWN) return true;
 // don't handle event unless its ACTION_UP so "doSomething()" only runs once.
 if(event.getAction()!=MotionEvent.ACTION_UP) return false;
 doSomething();
  button.setPressed(true);                   
  return true;
}
});

but this disables my OnclickListener() method and i dont want to use OnTouchListener()

i know this is silly but i am new to android thanks a lot

justDroid

Make two different shape xml. one with green color and other using another color..

And use them in selector.xml

 <item android:drawable="@drawable/rounded_btn_green" android:state_selected="true"/>

<item android:drawable="@drawable/rounded_btn_green" android:state_pressed="true"/>

<item android:drawable="@drawable/rounded_btn_green" android:state_focused="true"/>

<item android:drawable="@drawable/rounded_btn"/>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to set rounded buttons with background color and change color on pressed

From Dev

how to set a custom background color in a rounded imageview programmatically android

From Dev

How to change or set background color in 8086 assembly?

From Dev

How to change color of drawable set as android:background?

From Dev

How to change color of drawable set as android:background?

From Dev

RoundButtonTemplate change Background and Icon Color on pressed state

From Dev

Android set background color button pressed

From Dev

Android set background color button pressed

From Dev

How can I change color of background using a group of radio buttons?

From Dev

How to introduce rounded cells with a background color?

From Dev

Swift change button background color and image color while pressed

From Dev

Swift change button background color and image color while pressed

From Dev

Change UIActionSheet buttons background color and button font

From Dev

iOS: change background color using multiple buttons

From Dev

How to disable the background color when HomeUp is pressed?

From Dev

How to change/set background color of a single row in a datagrid?

From Dev

How to change UIBezierPath background color?

From Java

How to change background color of the snackbar?

From Java

How to change the background color of UIStackView?

From Dev

How to change the background color in Wordpress

From Dev

How to change the background color of a SFSafariViewController?

From Dev

How to change background color at JFrame

From Dev

How to change background color in ggvis?

From Dev

How to change the InfoWindow Background color

From Dev

how to change the background color of checkbox?

From Dev

How to change NSToolBar background color?

From Dev

How to change background color of UINavigationItem?

From Dev

How to change background color on jumbotron?

From Dev

How to change background color on condition

Related Related

  1. 1

    How to set rounded buttons with background color and change color on pressed

  2. 2

    how to set a custom background color in a rounded imageview programmatically android

  3. 3

    How to change or set background color in 8086 assembly?

  4. 4

    How to change color of drawable set as android:background?

  5. 5

    How to change color of drawable set as android:background?

  6. 6

    RoundButtonTemplate change Background and Icon Color on pressed state

  7. 7

    Android set background color button pressed

  8. 8

    Android set background color button pressed

  9. 9

    How can I change color of background using a group of radio buttons?

  10. 10

    How to introduce rounded cells with a background color?

  11. 11

    Swift change button background color and image color while pressed

  12. 12

    Swift change button background color and image color while pressed

  13. 13

    Change UIActionSheet buttons background color and button font

  14. 14

    iOS: change background color using multiple buttons

  15. 15

    How to disable the background color when HomeUp is pressed?

  16. 16

    How to change/set background color of a single row in a datagrid?

  17. 17

    How to change UIBezierPath background color?

  18. 18

    How to change background color of the snackbar?

  19. 19

    How to change the background color of UIStackView?

  20. 20

    How to change the background color in Wordpress

  21. 21

    How to change the background color of a SFSafariViewController?

  22. 22

    How to change background color at JFrame

  23. 23

    How to change background color in ggvis?

  24. 24

    How to change the InfoWindow Background color

  25. 25

    how to change the background color of checkbox?

  26. 26

    How to change NSToolBar background color?

  27. 27

    How to change background color of UINavigationItem?

  28. 28

    How to change background color on jumbotron?

  29. 29

    How to change background color on condition

HotTag

Archive