0

I have the following xml in drawable.

<?xml version="1.0" encoding="utf-8"?>
<!--drawable/btn_send_comment.xml-->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false">
        <layer-list>
            <item android:bottom="0dp" android:left="2dp" android:right="0dp" android:top="2dp">
                <shape android:shape="rectangle">
                    <solid android:color="@color/fab_color_shadow"/>
                    <corners android:radius="2dp"/>
                </shape>
            </item>

            <item android:bottom="1dp" android:left="1dp" android:right="1dp" android:top="1dp">
                <shape android:shape="rectangle">
                    <solid android:color="@color/btn_send_normal"/>
                    <corners android:radius="2dp"/>
                </shape>
            </item>
        </layer-list>
    </item>
    <item android:state_pressed="true">
        <shape android:bottom="0dp" android:left="2dp" android:right="0dp" android:shape="rectangle" android:top="2dp">
            <solid android:color="@color/btn_default_light_normal"/>
            <corners android:radius="2dp"/>
        </shape>
    </item>

</selector>

I have the following code to change the state of the button to pressed:

package com.example.android.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ViewAnimator;

import com.example.android.R;


public class SearchPeopleButton extends ViewAnimator implements View.OnClickListener {
    public static final int STATE_SELECTED = 0;
    public static final int STATE_NOT_SELECTED = 1;

Context ctx;
private static final long RESET_STATE_DELAY_MILLIS = 2000;

private int currentState;

private OnSendClickListener onSendClickListener;

private Runnable revertStateRunnable = new Runnable() {
    @Override
    public void run() {
        setCurrentState(STATE_SELECTED);
    }
};

public SearchPeopleButton(Context context) {
    super(context);
    init();
}

public SearchPeopleButton(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

private void init() {
    LayoutInflater.from(getContext()).inflate(R.layout.view_search_button, this, true);
    ctx = getContext();
}

@Override
protected void onAttachedToWindow() {
    super.onAttachedToWindow();
    currentState = STATE_SELECTED;
    super.setOnClickListener(this);
}

@Override
protected void onDetachedFromWindow() {
    removeCallbacks(revertStateRunnable);
    super.onDetachedFromWindow();
}

public void setCurrentState(int state) {

    currentState = state;
    if (state == STATE_NOT_SELECTED) {

    } else if (state == STATE_SELECTED) {
        setPressed(true);
    }
    showNext();
}

@Override
public void onClick(View v) {
    if (onSendClickListener != null) {
        onSendClickListener.onSendClickListener(this);
    }
}

public void setOnSendClickListener(OnSendClickListener onSendClickListener) {
    this.onSendClickListener = onSendClickListener;
}

@Override
public void setOnClickListener(OnClickListener l) {
    //Do nothing, you have you own onClickListener implementation (OnSendClickListener)
}

public interface OnSendClickListener {
    public void onSendClickListener(View v);
}

}

Here is the button usage in my view:

        <com.example.android.view.SearchPeopleButton
        android:id="@+id/btnPeople"
        android:layout_width="72dp"
        android:layout_height="match_parent"
        android:layout_marginBottom="2dp"
        android:layout_marginLeft="8dp"
        android:background="@drawable/btn_send_comment"
        android:elevation="4dp"
        android:orientation="vertical"/>

Here is my view_search_button.xml

    <?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">

    <ImageView
        android:id="@+id/ivFeedAvatar"
        android:src="@drawable/ic_hashtag_grey"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</merge>

I can see that I am getting onClickEvents but still the color of the pressed state is not showing up.

lionelmessi
  • 1,116
  • 2
  • 10
  • 17
  • Can you post the code for SearchPeopleButton? – Theo Dec 31 '15 at 02:18
  • added the complete button code. – lionelmessi Dec 31 '15 at 02:25
  • SearchPeopleButton does not inherit from Button or another control that defines the pressed state, therefore the pressed state in your background will have no effect. – Theo Dec 31 '15 at 02:29
  • I'm a little confused, what is the point of the `OnSendClickListener` interface, I don't see a benefit of this, because I don't see what this gets you over just having a `View.OnClickListener` – mawalker Dec 31 '15 at 02:34
  • Can you post the contents of res/layout/view_search_button.xml? – Theo Dec 31 '15 at 02:37
  • @mawalker I would like to keep the inner handling of the states inside separate buttons code, thats why I kept it an interface. – lionelmessi Dec 31 '15 at 02:41
  • @Theo I added the xml as requested. What should be the suggested pattern to follow in order to get the desired behavior? – lionelmessi Dec 31 '15 at 02:41

2 Answers2

1

You can use an ImageButton control to display the image and support the pressed state. http://developer.android.com/reference/android/widget/ImageButton.html

Change your button usage to:

<ImageButton
    android:id="@+id/btnPeople"
    android:layout_width="72dp"
    android:layout_height="match_parent"
    android:layout_marginBottom="2dp"
    android:layout_marginLeft="8dp"
    android:background="@drawable/btn_send_comment"
    android:elevation="4dp"
    android:orientation="vertical"/>

You may also need to change your background xml drawable to include the proper images in both the pressed and unpressed states, depending on what you want to accomplish.

Theo
  • 5,963
  • 3
  • 38
  • 56
  • I changed it to ImageButton and now its changing color when pressed. How do I keep the same color until I press again? I would not like to use toggle. – lionelmessi Dec 31 '15 at 03:06
  • You are looking for a control that supports the selected state. Use either ToggleButton or this hack to add selected state to ImageButton: http://stackoverflow.com/a/3417158/483708 – Theo Dec 31 '15 at 03:13
0

Hi look into the following link

Step 1.pick colour and name of the theme and apply and pick the button and other as per your requirement.Copy and paste the downloaded code and apply the theme it will work.

http://android-holo-colors.com/

ManiTeja
  • 849
  • 1
  • 9
  • 13