I created my own widget, a LinearLayout with a TextView (label) and a Spinner.
It looks like this in XML:
<?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="50dp"
android:orientation="horizontal"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
style="@style/MySpinner>
<TextView android:id="@+id/label"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textStyle="bold"
android:layout_weight="0.4"
android:textSize="18sp"
android:padding="8dp"
android:enabled="false"/>
<Spinner android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:entries="@array/thats_me_age_values"
android:layout_weight="0.6"
android:spinnerMode="dialog"
android:padding="8dp"/>
</LinearLayout>
The LinearLayout should get all touch events so it can properly use it’s style (background changes on pressed). The style sets the background attribute to this selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- pressed -->
<item android:state_pressed="true" android:state_focused="false">
<shape android:shape="rectangle" android:visible="true">
<corners android:topLeftRadius="7dp" android:topRightRadius="7dp"/>
<solid android:color="@color/dark_yellow"/>
<stroke android:width="1dp" android:color="@color/grey"/>
</shape>
</item>
<!-- focused -->
<item android:state_focused="true">
<shape android:shape="rectangle" android:visible="true">
<corners android:topLeftRadius="7dp" android:topRightRadius="7dp"/>
<solid android:color="@color/dark_yellow"/>
<stroke android:width="1dp" android:color="@color/grey"/>
</shape>
</item>
<!-- default -->
<item>
<shape android:shape="rectangle" android:visible="true">
<corners android:topLeftRadius="7dp" android:topRightRadius="7dp"/>
<solid android:color="@color/yellow"/>
<stroke android:width="1dp" android:color="@color/grey"/>
</shape>
</item>
</selector>
I have tried overriding the onInterceptTouch methode for the LinearLayout. The log messages are shown on pressed but the background does not change.
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.i("TOUCH", "onTouchEvent");
return super.onTouchEvent(event);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
onTouchEvent(ev);
Log.i("TOUCH", "onInterceptTouchEvent");
return false;
}
I think adding android:addStatesFromChildren=”true” to LinearLayout will solve your problem… So when you click/focus your spinner linear layout will change it state to clicked/focused.