-2

I want to change the background color and text color of a button when pressed(it should remain in the state until again button is pressed). Also if pressed again i need the old state of button. Anyone help me please

Here is the screenshots (Normal state) enter image description here

Some buttons pressed state enter image description here

Here is the code what i have tried

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/beco_white"
        android:layout_marginTop="@dimen/dp10"
        android:orientation="horizontal">

        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent">

            <FrameLayout
                android:id="@+id/button_oberon_mall"
                android:layout_width="92dp"
                android:layout_height="30dp"
                android:layout_marginLeft="@dimen/dp10"
                android:minWidth="92dp">

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/blue_rounded_corner_new"
                    android:text="Oberon Mall"
                    android:fontFamily="sans-serif-regular"
                    android:textAllCaps="false"
                    android:textColor="@color/beco_black"
                    android:textSize="13sp"/>

            </FrameLayout>
        </FrameLayout>

        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent">

            <FrameLayout
                android:id="@+id/button_centre_square_mall"
                android:layout_width="134dp"
                android:layout_height="30dp"
                android:layout_marginLeft="@dimen/dp10"
                android:layout_marginRight="@dimen/dp10"
                android:minWidth="134dp">

                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="@drawable/blue_rounded_corner_new"
                        android:text="Centre Square Mall"
                        android:fontFamily="sans-serif-regular"
                        android:textAllCaps="false"
                        android:textColor="@color/beco_black"
                        android:textSize="13sp"/>
            </FrameLayout>
        </FrameLayout>

        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent" >

            <FrameLayout
                android:id="@+id/button_lulu_mall"
                android:layout_width="81dp"
                android:layout_height="30dp">

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/blue_rounded_corner_new"
                    android:text="Lulu Mall"
                    android:fontFamily="sans-serif-regular"
                    android:textAllCaps="false"
                    android:textColor="@color/beco_black"
                    android:textSize="13sp"/>
            </FrameLayout>
        </FrameLayout>

    </LinearLayout>

blue_round_corner_new.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:state_pressed="false">
<shape android:shape="rectangle">
    <!--apply button background transparent, full opacity-->
    <solid android:color="#dedfe0"/>
    <corners android:radius="15dp"/>
</shape>
</item >
<item
    android:state_pressed="true">
    <shape android:shape="rectangle">
        <!--apply button background transparent, full opacity-->
        <solid android:color="#4990d0"/>
        <corners android:radius="15dp"/>
    </shape>
</item>

In this code during pressing background color is changing only for a while(not retaining state)

Any one help me. Sorry for my english

vm345
  • 813
  • 12
  • 28
  • if you have set the background to that colour you have to set the background colour by code. No way you can do that by selector – Nithinlal Apr 05 '17 at 15:03
  • What do you mean by 'a while'? The button cannot magically go back to the old state. It either does that when it's re-rendered or something else is happening. – Vucko Apr 05 '17 at 15:09
  • Better use the external library to solve this issue check this link https://android-arsenal.com/details/1/2143 Is that helpfull pls let me know – Nithinlal Apr 05 '17 at 15:11
  • Possible duplicate of [Android : change button text and background color](http://stackoverflow.com/questions/19400722/android-change-button-text-and-background-color) – Brandon Zamudio Apr 05 '17 at 16:04

3 Answers3

1

I think the best option is implementing an onTouchListener to each button.

button.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_UP:
                        break;
                    case MotionEvent.ACTION_DOWN:
                        break;
                }

                return false;
            }
        });

When you get an ACTION_UP or ACTION_DOWN you only have to change the background

David Luque
  • 1,078
  • 5
  • 18
  • 30
1

Better you have to use an external library to solve this issue. The following library will help you to solve the issue

CheckableButton

AndroidCheckableButton

Nithinlal
  • 4,845
  • 1
  • 29
  • 40
0

There are 2 ways of putting thing on screen either XML(which you have used) or programmatically. Try putting that blue_round corner.xml in program call and if you want to keep in for long(I thought it like you are having multiple screen and switching screen is retaining the pressed state of button) then call the same in onResume() function to retain it back.

I think it will work, please put comments if you need any more help on this