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

  • Home
  • SEARCH
  • 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 8316091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:16:51+00:00 2026-06-08T21:16:51+00:00

I have a very basic program on GridView with some buttons as in the

  • 0

I have a very basic program on GridView with some buttons as in the code below. The program runs fine and buttons show in grid, but they do not respond when I click. If I change my code to a gridview of images, it works fine. I believe I am wrong somewhere in the gridView.setOnItemClickListener or in public View getView(). Ayn suggestion? thanks

public class MainActivity extends Activity {
    public String[] filenames ={"B1","B2","B3","B4","B5","B6"};        
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);           
        GridView gridView = (GridView) findViewById(R.id.gridview);
        gridView.setAdapter(new ButtonAdapter(this));  
        gridView.setOnItemClickListener(new OnItemClickListener(){
            public void onItemClick(AdapterView<?> parent, 
            View v, int position, long id){                
                Toast.makeText(getBaseContext(), 
                        "pic" + (position + 1) + " selected", 
                        Toast.LENGTH_SHORT).show();
            }
        });        
    }

    public class ButtonAdapter extends BaseAdapter {
        private Context context;    
        public ButtonAdapter(Context c){
            context = c;
        }
        public int getCount() {
            return filenames.length;
        }
        public Object getItem(int position) {
            return position;
        }     
        public long getItemId(int position) {
            return position;
        }
        public View getView(int position, View convertView, ViewGroup parent){
              Button btn;  
                 if (convertView == null) {   
                  btn = new Button(context);  
                  btn.setLayoutParams(new GridView.LayoutParams(100, 100));  
                  btn.setPadding(8, 8, 8, 8);  
                  }else {  
                  btn = (Button) convertView;  
                 }               
                 btn.setText(filenames[position]);    
                 btn.setTextColor(Color.WHITE);  
                 btn.setId(position);       
                 return btn; 
        }
    }    
}
  • 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-08T21:16:52+00:00Added an answer on June 8, 2026 at 9:16 pm

    You need to set Buttons property focusable and clickable to false. Because focus and click event is captured by button in current case, not on grid view item. Change your code to following:

    public class MainActivity extends Activity {
        public String[] filenames ={"B1","B2","B3","B4","B5","B6"};        
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);           
            GridView gridView = (GridView) findViewById(R.id.gridview);
            gridView.setAdapter(new ButtonAdapter(this));  
            gridView.setOnItemClickListener(new OnItemClickListener(){
                public void onItemClick(AdapterView<?> parent, 
                View v, int position, long id){                
                    Toast.makeText(getBaseContext(), 
                            "pic" + (position + 1) + " selected", 
                            Toast.LENGTH_SHORT).show();
                }
            });        
        }
    
        public class ButtonAdapter extends BaseAdapter {
            private Context context;    
            public ButtonAdapter(Context c){
                context = c;
            }
            public int getCount() {
                return filenames.length;
            }
            public Object getItem(int position) {
                return position;
            }     
            public long getItemId(int position) {
                return position;
            }
            public View getView(int position, View convertView, ViewGroup parent){
                  Button btn;  
                     if (convertView == null) {   
                      btn = new Button(context);  
                      btn.setLayoutParams(new GridView.LayoutParams(100, 100));  
                      btn.setPadding(8, 8, 8, 8);  
                      btn.setFOcusable(false);
                      btn.setClickable(false);
                      }else {  
                      btn = (Button) convertView;  
                     }               
                     btn.setText(filenames[position]);    
                     btn.setTextColor(Color.WHITE);  
                     btn.setId(position);       
                     return btn; 
            }
        }    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some very basic semaphore code that works great on Linux, but cannot
I'm having some trouble with a very basic CUDA program. I have a program
I have a very basic program in C++ compiled in VS 11 Beta on
I have the following part of a program, which emulates a very basic menu.
I have a very basic Visual Basic Program that I created in Visual Studio
So I have this weird looking problem: my very basic program generates an error
I have a very basic JavaScript question. I am writing a program which will
I have some complex code. Complex, but it was working. The I wanted to
I have a very basic program which displays a standard Textview and a extended
I wrote a very basic program but was not able to understand the behaviour

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.