I’m using a GridView and an image selector so that my image is different when it’s pressed vs when it’s not pressed. Everything compiles, but when I run the application I get an error ” Binary XML file line #9: Error inflating class selector”.
Is it possible to use an image selector inside a gridview? When I take the selector out of the xml, it runs fine.
Here’s the xml for each grid item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" >
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/status_button_down"
android:state_pressed="true" />
<item android:drawable="@drawable/status_button_down"
android:state_focused="true" />
<item android:drawable="@drawable/status_button_up" />
</selector>
<TextView
android:id="@+id/grid_item_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Status"
android:textSize="9pt"
android:typeface="sans"
android:textColor="#000000"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal|bottom"
/>
</LinearLayout>
Here’s the code for the adapter for the grid view:
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
//return mThumbIds.length;
return 16;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
//ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
gridView = new View(mContext);
// get layout from mobile.xml
gridView = inflater.inflate(R.drawable.status_button, null);
// set value into textview
TextView textView = (TextView) gridView
.findViewById(R.id.grid_item_label);
textView.setText("Status");
} else {
//imageView = (ImageView) convertView;
gridView = (View) convertView;
}
return gridView;
}
}
The selector should actually be set in your “drawables” folder as its own .xml file. Then you can call it like this: