Reputation: 1144
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.
Upvotes: 0
Views: 131
Reputation: 847
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/
Upvotes: 0
Reputation: 6073
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.
Upvotes: 1