I have a GridView that I am filling with a custom subclass of ArrayAdapter. This adapter returns buttons that I have customized to be selectable (see Android ImageButton with a selected state?). This works so far and clicking the buttons selects them (which is visible with a selector-background).
The problem is: I cannot set these buttons to a selected state from the beginning. They simply display unselected when I first start the View.
I have created a simple test project to illustrate the problem:
package com.example.buttonselection;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridView;
public class MainActivity extends Activity {
public class SelectButtonAdapter extends ArrayAdapter<String> {
public SelectButtonAdapter(Context context) {
super(context, 0);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
String name = getItem(position);
View rowView = convertView;
if (rowView == null || !(rowView instanceof Button)) {
rowView = new Button(getContext());
((Button)rowView).setOnClickListener(new OnClickListener() {
public void onClick(View button) {
if (button.isSelected()){
button.setSelected(false);
} else {
button.setSelected(true);
}
}
});
}
Button button = (Button)rowView;
button.setText(name);
button.setBackgroundResource(R.drawable.button_selection);
button.setSelected(true); // this does not work
return button;
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView grid = (GridView)findViewById(R.id.gridview);
SelectButtonAdapter adapter = new SelectButtonAdapter(this);
adapter.add("One");
adapter.add("Two");
adapter.add("Three");
grid.setAdapter(adapter);
}
}
Because of this, I cannot even restore the state of the buttons that I saved with onSaveInstanceState. How can I solve or workaround this problem?
I am grateful for any help!
EDIT: here is my button_selection.xml, though this should be ok as selecting the buttons later works fine.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<solid android:color="@color/bet_button_pressed" />
<stroke
android:width="2dip"
android:color="@color/white" />
</shape>
</item>
<item android:state_selected="true">
<shape>
<solid android:color="@color/bet_button_selected" />
<stroke
android:width="2dip"
android:color="@color/white" />
</shape>
</item>
<item>
<shape>
<gradient
android:angle="90"
android:startColor="@color/bet_button_dark_green"
android:endColor="@color/bet_button_light_green"
android:centerX="0.5"
android:centerY="0.5" />
<stroke
android:width="2dip"
android:color="@color/white" />
</shape>
</item>
</selector>