You may not need to set a TextView to "Clicked", if all you want to do is give the user the feedback that they have checked an item. An alternative might be to do the following. Note, this method will also help you set a textview to "clicked" if you still need to.
To check an item I would extend your RelativeLayout
as follows:
public class CheckableRelativeLayout extends RelativeLayout implements Checkable {
private boolean mChecked;
private static final int[] CHECKED_STATE_SET = {
android.R.attr.state_checked
};
public CheckableRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected int[] onCreateDrawableState(int extraSpace) {
final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
if (isChecked()) {
mergeDrawableStates(drawableState, CHECKED_STATE_SET);
}
return drawableState;
}
public void toggle() {
setChecked(!mChecked);
}
public boolean isChecked() {
return mChecked;
}
public void setChecked(boolean checked) {
if (mChecked != checked) {
mChecked = checked;
refreshDrawableState();
}
}
}
Then you can call parent.setItemChecked(position,true);
in onItemClick
which will actually set the item as checked within the AdapterView
.
Doing this will then allow you to set a background selector on the extended RelativeLayout
and the items will perform selected, pressed, checked etc actions. However, you don't need to use a background selector.
There is more, you can alway see whether the view is checked with gridview.getCheckedItemPosition()
which you may find useful if you want to actually set the textview to "Clicked".
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
parent.setItemChecked(position,true);
yourAdapter.ViewHolder vh = (yourAdapter.ViewHolder) v.getTag();
vh.textView.setText("Clicked");
}
});
In your adapter you can hold a reference to you extended relative layout in your static ViewHolder class and if checked (e.g. holder.extendedrelativeLayout.isChecked()
) then make sure you set the textView to "Clicked".