MRefaat
MRefaat

Reputation: 535

Buttons strange behavior when pressing

I've an Android activity that has some buttons in the bottom of it, these buttons have a one pressed and a three released as a default,

enter image description here

and when any of them pressed it's background supposed to change to the background of the pressed buttons and the others background supposed to change to the background of the released buttons, but when clicking any button I got that result:

enter image description here

this is my code:

xml:

<LinearLayout
            android:id="@+id/GrandThree"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_marginTop="10dp"
            android:layout_gravity="center"
            android:gravity="center"
            android:orientation="horizontal"
            android:background="@android:color/transparent">

            <Button
                android:id="@+id/BedRoom_bottom"
                android:layout_width="200dp"
                android:layout_height="fill_parent"
                android:background="@drawable/light_bottom_buttons_pressed"
                android:text="Bedroom"
                android:textSize="20dp"
                android:textColor="#FFFFFF"
                android:layout_gravity="center"
                android:layout_marginTop="3dp"
                android:layout_marginLeft="3dp"
                android:layout_marginBottom="3dp"
                android:layout_marginRight="3dp"
                android:tag="pressed"
                android:clickable="true" />

            <Button
                android:id="@+id/livingroom"
                android:layout_width="200dp"
                android:layout_height="fill_parent"
                android:background="@drawable/light_bottombuttons"
                android:text="Living Room"
                android:layout_gravity="center"
                android:textSize="20dp"
                android:padding="20px"
                android:textColor="#FFFFFF"
                android:layout_marginTop="3dp"
                android:layout_marginLeft="3dp"
                android:layout_marginBottom="3dp"
                android:layout_marginRight="3dp"
                android:tag="released"
                android:clickable="true" />
            <!.. then two buttons the same way ..!>
</LinearLayout>

JAVA:

        // the onClickListener method for the Navigation buttons
            public OnClickListener onNavigationClick = new OnClickListener() {

             @Override
                public void onClick(View v) {


                  if(v == bedRoom){

                String tag = bedRoom.getTag().toString().trim();
                    if (tag.equals("released")) {

                       bedRoom.setBackgroundColor(R.drawable.light_bottom_buttons_pressed);
                       bedRoom.setTag("pressed");
                       livingRoom.setBackgroundColor(R.drawable.light_bottombuttons);
                       livingRoom.setTag("released");
                       masterBedroom.setBackgroundColor(R.drawable.light_bottombuttons);
                       masterBedroom.setTag("released");
                       kitchen.setBackgroundColor(R.drawable.light_bottombuttons);
                       kitchen.setTag("released");

      }
   }
       //then another 3 blocks the same way for the other buttons
}
};

Hint: light_bottombuttons & light_bottom_buttons_pressed are shapes with gradient colors:

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

    <gradient 
        android:startColor="#353535"
        android:centerColor="#212121"
        android:endColor="#121212"  <!.. the values of the other is just upside down these values ..!>
        android:angle="270"/>

    </shape>

Upvotes: 0

Views: 149

Answers (3)

Karthik Balakrishnan
Karthik Balakrishnan

Reputation: 4393

Create a file called button-drawable.xml in your drawable folder containing this:

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

Add a tag to all the buttons in the layout file.

android:background="@drawable/button_drawable"

Now, in your button's click listener set the button's selected state to true any time a new button is chosen.

Example code :

public OnClickListener onNavigationClick = new OnClickListener() {

         @Override
            public void onClick(View v) {


              if(v == bedRoom){

            String tag = bedRoom.getTag().toString().trim();
                if (tag.equals("released")) {

                   bedRoom.setTag("pressed");
                   bedRoom.setSelected(true);
                   livingRoom.setTag("released");
                   livingRoom.setSelected(false);
                   masterBedroom.setTag("released");
                   masterBedroom.setSelected(false);
                   kitchen.setTag("released");
                   kitchen.setSelected(false);

  }
}
    //then another 3 blocks the same way for the other buttons
} 
};

Upvotes: 1

Dreagen
Dreagen

Reputation: 1743

When you get your drawable you should do it like the following

        bedRoom.setBackgroundColor(MyActivity.this.getResources().getDrawable(R.drawable.light_bottom_buttons_pressed));

MyActivity Would change to be whatever your activity name is

Upvotes: 0

vipul mittal
vipul mittal

Reputation: 17401

Use radio group and radio button for this. Your radio group will contain multiple buttons and at a time only one can be selected.

Upvotes: 0

Related Questions