Question

I am trying to make the images clickable so that when they are pressed it will send the user to another page or link.

How can I achive this? Currently each row in the GridView has 2 Buttons. How will it know which item in the GridView is clicked so that it performs a certain action, specific to the item that was clicked.

Thanks for any help!

This is in my image adapter class:

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setAdjustViewBounds(true);
       // imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        imageView.setPadding(4, 8, 4, 8);
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}

// references to our images
private Integer[] mThumbIds = {
        R.drawable.menu_about,R.drawable.menu_episodes
  };
 }
Was it helpful?

Solution

You may want to consider using the ImageButton class to do this for you. Its a button but instead of having a boring gray gradient you place an image there instead! Andriod ImageButton Docs From there you are free to use a click listener just like you would with a regular button.

OTHER TIPS

Just add a listener to the GridView

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

    gridView.setOnItemClickListener(new OnItemClickListener()
    {
        public void onItemClick(AdapterView parent,
        View v, int position, long id)
        {
            data.aktueltBilledeNr = position;
            Toast.makeText(getBaseContext(), "Viser billede " + (position + 1), Toast.LENGTH_SHORT).show();
            finish();
            //startActivity(new Intent(Resultatsoversigt_akt.this, Teoriproeveaktivitet2.class));
       }
    });

first of all find the position of the buttons(Here Image) and then use the switch case to perfrom what ever you say...

GridView gridView = (GridView) findViewById(R.id.gridview);    
gridView.setAdapter(new ImageAdapter());      
gridView.setOnItemClickListener(new OnItemClickListener() {       
    public void onItemClick(AdapterView parent,View v,int position,long     id)               {   
        switch(position) {
        case 0:
            Intent intent=new Intent(this,targer.class);
            StartActivity(intent);
            break;
        case 1:
            break;     
        .
        .
        .

        default:
            break;
        }
    }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top