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 6234473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:29:31+00:00 2026-05-24T10:29:31+00:00

I have a GridView with 9 items. What I’m trying to do is when

  • 0

I have a GridView with 9 items. What I’m trying to do is when I click an item, its icon changes. The way I’m doing it returns

java.lang.ClassCastException: android.widget.LinearLayout

I have to classes:

MainLayout.java

public class MainLayout extends Activity implements OnItemClickListener
{   

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.mainlayout);

        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);      

         final ImageButton btn=(ImageButton)findViewById(R.id.logoimagebutton);
         btn.setBackgroundColor(Color.TRANSPARENT);

         btn.setOnTouchListener(new OnTouchListener() {

                public boolean onTouch(View v, MotionEvent event) {
                    if (event.getAction() == MotionEvent.ACTION_DOWN ) {
                        btn.setImageResource(R.drawable.logoselected);
                        Toast.makeText(getApplicationContext(), "Clicked", Toast.LENGTH_SHORT).show();
                        //return true;
                    }else if (event.getAction() == MotionEvent.ACTION_UP) {
                        btn.setImageResource(R.drawable.logo);
                    }

                    return false;
                }
            });

        final GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(new ImageAdapter(this));            

        gridview.setOnItemClickListener(new OnItemClickListener()
        {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id)
            {                   
                ImageView imageView = (ImageView) v;
                imageView.setImageResource(ImageAdapter.mThumbSelected[position]);
            }
        });   
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
    {
        // TODO Auto-generated method stub

    }

}

ImageAdapter.java

public class ImageAdapter extends BaseAdapter
{

    private Context mContext;

    public static Integer[] mThumbIds = 
    {
            //a collection of images
    };

    public static Integer[] mThumbSelected = 
    {
            //a collection of images
    };

    private String[] mLabelsIds = {//a collection of strings};

    public ImageAdapter(Context c)
    {
        // TODO Auto-generated constructor stub
        mContext = c;
    }

    @Override
    public int getCount()
    {
        // TODO Auto-generated method stub
        return mThumbIds.length;
    }

    @Override
    public Object getItem(int arg0)
    {
        // TODO Auto-generated method stub
        return mThumbIds[arg0];
    }

    @Override
    public long getItemId(int arg0)
    {
        // TODO Auto-generated method stub
        return arg0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {       
        View grid;

        if (convertView == null)
        {
            grid = new View(mContext);
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            grid = inflater.inflate(R.layout.menugrid, parent, false);
        } else
        {
            grid = (View) convertView;
        }

        ImageView imageView = (ImageView) grid.findViewById(R.id.imageicon);
        TextView textView = (TextView) grid.findViewById(R.id.imagelabel);
        imageView.setImageResource(mThumbIds[position]);
        textView.setText(mLabelsIds[position]);

        return grid;
    }
}

Here is my two xml files:

menugrid.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:orientation="vertical">

     <ImageView
      android:id="@+id/imageicon"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      />
     <TextView
      android:id="@+id/imagelabel"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"     
      android:textColor="#000000"
      android:textSize="16px"
      android:gravity="center_horizontal"/>
</LinearLayout>

mainlayout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainlayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:orientation="vertical"
    android:background="@drawable/background">

    <LinearLayout
        android:id="@+id/holdlogo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"    
        android:orientation="horizontal"                    
        android:background="@drawable/title_background">

        <ImageButton
            android:id="@+id/logoimagebutton"
            android:scaleType="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"    
            android:layout_gravity="center_vertical"    
            android:src="@drawable/logo"
            >
        </ImageButton>

    </LinearLayout>


    <GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridview"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:numColumns="auto_fit"
    android:layout_marginTop="10px"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:columnWidth="90dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
/>  
</LinearLayout>

Can anyone tell me what is wrong in my code? Or How to change the image of an item when pressing it? I think onTouchListener is more efficient, due to the fact that it has the event, so when event.getAction() == MotionEvent.ACTION_DOWN I show the image and when event.getAction() == MotionEvent.ACTION_UP I show the original icon.

Thanks in advance.

  • 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-05-24T10:29:33+00:00Added an answer on May 24, 2026 at 10:29 am

    I guess you reveice the ClassCastException here:

       public void onItemClick(AdapterView<?> parent, View v, int position, long id)
            {                   
                ImageView imageView = (ImageView) v;
                imageView.setImageResource(ImageAdapter.mThumbSelected[position]);
            }
    

    The View sent is actually the LinearLayout in which the ImageView resides in. You need to get the item using the int position and then manipulate it to your needs, or get the ImageView inside the LinearLayout, which is your Grid Item Layout.

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

Sidebar

Related Questions

I have a GridView populated from an ObjectDataSource with two items in its DataKeyNames
I have a GridView that displays items, each item is a ListView. the problem
I have a gridview that displays items details, I added two template fields one
I have a ASP.NET Website, where, in a GridView item template, automatically populated by
I have an ASP.NET GridView with a DropDownList (in an Item Template) and a
Currently I have a DetailsView attached to a GridView. When you select an item
I have a gridview in which there are two elements in each of its
I have stored a list of items from gridView into the registry as below:
I have a GridView that is created based on an array of items. I
I have a GridView that lists a bunch of items and one of the

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.