Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8564689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:15:19+00:00 2026-06-11T17:15:19+00:00

I have a GridView that I am filling with a custom subclass of ArrayAdapter.

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T17:15:20+00:00Added an answer on June 11, 2026 at 5:15 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a gridview I am populating it by creating a datatable filling that
I have a gridview that contains 2 item template 1-checkbox 2-textbox like this: <asp:GridView
I have a gridview that works fine with SQLDataSource. Edit, Delete buttons works perfectly.
I have a gridview that has its DataSourceID property set to a custom ObjectDataSource
I have a gridview that I'm trying to validate from code behind. In this
I have a gridview that is databound to a list of custom objects. On
I have a gridview that is inflated by multiple buttons. I used just a
I have a gridview that needs to be exported to Excel. I have managed
I have a gridview that shows an image as part of one of its
I have a gridview that displays items details, I added two template fields one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.