Reputation: 2527
I have the following simple selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/main_menu_button_background_pressed" android:state_pressed="true" />
<item android:drawable="@drawable/main_menu_button_background" />
</selector>
and the following activity's xml:
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#FF0000"
android:weightSum="2">
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="settings"
android:id="@+id/btnMainMenuSettings"
android:layout_gravity="center_horizontal"
android:background="@drawable/main_menu_button_selector"
android:layout_weight="1" />
<com.test.myapp.custom_views.CustomMainMenuButton
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="settings"
android:layout_gravity="center_horizontal"
android:background="@drawable/main_menu_button_selector"
android:layout_weight="1" />
</LinearLayout>
this is my custom view:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher" />
<TextView
style="@style/main_menu_button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/app_name" />
</LinearLayout>
Here is the java code:
public class CustomMainMenuButton extends LinearLayout implements View.OnClickListener {
private Context mContext;
public CustomMainMenuButton(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.custom_main_menu_button, this, true);
}
@Override
public void onClick(View v) {
}
}
The selector works fine for the button, but for the custom view I get only the normal state, the custom view is not clickable and nothing happens when I tap the custom view. Any idea how can I make it work?
Upvotes: 1
Views: 2283
Reputation:
Please add android:clickable="true"
in
<com.test.myapp.custom_views.CustomMainMenuButton
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="settings"
android:layout_gravity="center_horizontal"
android:background="@drawable/main_menu_button_selector"
android:layout_weight="1" />
Upvotes: 1
Reputation: 6563
Your layout is not clickable, it is default state for all layouts. Use
setClickable(true)
in custom view implementation or in xml layout android:clickable="true"
You should get something like:
<com.test.myapp.custom_views.CustomMainMenuButton
android:layout_width="match_parent"
android:layout_height="0dp"
android:clickable="true"
android:text="settings"
android:layout_gravity="center_horizontal"
android:background="@drawable/main_menu_button_selector"
android:layout_weight="1" />
Upvotes: 3
Reputation: 24848
Try to change selector as :
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/main_menu_button_background_pressed" android:state_pressed="true"></item>
<item android:drawable="@drawable/main_menu_button_background_pressed" android:state_focused="true"></item>
<item android:drawable="@drawable/main_menu_button_background" android:state_enabled="true" android:state_focused="false" android:state_pressed="false"></item>
<item android:drawable="@drawable/main_menu_button_background_pressed" android:state_enabled="false"></item>
</selector>
Upvotes: 2