1

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>
Community
  • 1
  • 1
cit
  • 217
  • 5
  • 13
  • can we see the button_selection drawable? – Sherif elKhatib Sep 22 '12 at 16:30
  • 1
    Sure, but I don't think it is important, as it does select and display buttons correctly later by clicking/selecting them. – cit Sep 22 '12 at 17:06
  • Exactly! I have worked on this for several hours and can't find a solution. I would say it's bug in the API. Can anyone think of a workaround? – cit Sep 22 '12 at 19:56

2 Answers2

1

Well I found a workaround for this bug. I simply store externally weather a button is selected and override the ondraw-method of the button to set the right state every time it gets drawn. This has the added advantage, that the selections can be persisted much easier.

Here is the workaround (just a simple proof of concept, my production code is more sophisticated):

package com.example.buttonselection;

import java.util.HashSet;
import java.util.Set;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
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()) {
                    @Override
                    protected void onDraw(Canvas canvas) {
                        setSelected(selectedButtons.contains(getText()));
                        super.onDraw(canvas);
                    }
                };
                ((Button)rowView).setOnClickListener(new OnClickListener() {
                    public void onClick(View button) {
                        String text = ((Button)button).getText().toString();

                        if(selectedButtons.contains(text)) {
                            selectedButtons.remove(text);
                        } else {
                            selectedButtons.add(text);
                        }
                        button.invalidate();
                    }
                });
            }

            Button button = (Button)rowView;
            button.setText(name);
            button.setBackgroundResource(R.drawable.button_selection);

            return button;
        }

    }
    private Set<String> selectedButtons = new HashSet<String>();

    @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);

        // this selects the first button from the start
        selectedButtons.add("One");
    }

}

On a side note: One would think, that a version 4.1 API would not contain such obvious bugs. The time wasted on this is just frustrating and does not motivate to develop more for this system.

cit
  • 217
  • 5
  • 13
1

I'm having this same problem. As a workaround, I used a runnable to set the view to be selected.

for your case:

        @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);

            final Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    button.setSelected(true);
                }
            }, 50);

            return button;
        }

50 is just a random number I choose to use, you can change it as how you want.

This workaround works for me and I personally like this better than to override onDraw method.

Hopefully this helps someone.

Shinta S
  • 523
  • 7
  • 10