Android: In Gridview how do I make the images clickable

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 }; }

--------------Solutions-------------

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.

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

Category:android Time:2010-08-07 Views:1

Related post

  • Android Hello GridView Tutorial Will Not Display Images 2011-07-24

    First time poster! I am new to Android development and have been following Google's HelloView tutorials without problems... until the HelloGridView tutorial. For some reason I cannot get any images to display, only a black background. I originally fo

  • Android: In gridview, how to add a label on the image 2011-08-18

    http://developer.android.com/resources/tutorials/views/hello-gridview.html In the above example code, I want to add a text on each image. Please help me how to do this? --------------Solutions------------- The following link (Gridview with Icon and T

  • wp7 equivalent of android's gridview 2012-01-12

    What's wp7 equivalent of android's gridview? I tried Grid but it is not clear to that it its suposed to work like android's GridView. I want something like this: --------------Solutions------------- There is an equivalent for grid view is Wrappanel Y

  • Android: Simple GridView that displays text in the grids 2009-06-11

    I'm following the example on the android tutorial about the GridView, but instead of showing image, i want to just simple show some text using a TextView. it turns out seems to be harder than i thought. it might seems like this is totally unnecessary

  • Android - Changing GridView text color and styling 2010-07-14

    I am very new to android development. I have a activity with a GridView. I need to change GridView's text color and change each row's background color. How can I do this ? Thank You in advance. DineshNS --------------Solutions------------- GridView i

  • Android custom gridview layout (textview with imageview on top) 2011-01-24

    I'm trying to work out how to create the layout for a gridview for my game. I have a gridview which will be the level selector. At the moment I have each gridview item as a TextView, just showing the Level Number (1, 2, 3, etc). I would like to add 3

  • Android increase gridview click speed 2011-03-04

    I'm using a GridView in Android. I need to register as many clicks as possible, but gridview reacts to clicks very slow, even if there is no acutal code to execute... is there a possibility to speed this up? --------------Solutions------------- Depen

  • Android: center gridview horizontally 2011-05-26

    I need to center grid view horizontally in my android layout.xml. I have searched google for quite a while but didnt succeed in finding an answer. I can only change gridview horizontal position by changing strechMode, but then my items are not near o

  • (android-layout) GridView with Image and Text adds extraspace around Image 2011-06-02

    I want a Gridview with Images and corresponding Text over them. It nearly does work, but there is some extraspace around my images. I first thought the space would be below textview und the imageview, but when removing the text, same space has been t

  • Android Bind GridView using external Images? 2011-06-23

    Hi i want to Bind GridView with images from the sd-card but not in the res/drawable folder? below is code where i am using images from res/drawable folder //RICE// public static int[] mPictureIds_rice = { R.drawable.chinese_fried_rice, R.drawable.nas

  • Android: wrap GridView in a dialog 2011-06-24

    Does anyone know how to wrap a GridView? Despite setting layout_width to wrap_content both on the GridView and parent LinearLayout the GridView is still bigger than its 3 column content (approx. double the content width). Tried also to apply fixed la

  • Android Honeycomb GridView Widgets 2011-07-18

    Can anyone give me an example where I can understand how to use GridView on RemoveViews for Honeycomb? I see only StackWidgets into Android Developers examples. --------------Solutions------------- Refer to Android source code. Browser code has bookm

  • Android Calendar Gridview - How to keep info about every cell in my Calendar? 2011-08-28

    I'm trying to build a calendar that looks like Android internal Calendar. The thing that I would like to know is how to keep track of every cell in my View. I have a Gridcell adapter that extends Base Adapter, here is the code: public View getView(in

  • show data in android using gridview from database 2011-09-01

    I want to display data, from a database, in a gridview. Means I have a data in my table (let it's a customer detail) and I want to show it in gridview (or any other control to display details) same as we done in asp.net. Solution:-- public void FillG

  • Android: Updating gridview multiple times after x seconds 2012-01-20

    I am having a problem updating the view in android every x seconds. To get to know how android works I am writing a small game. It has a GameController which holds the game loop. Inside the loop, the logic is executed and afterwards the gui will be i

  • Android - Custom GridView with fling - error 2012-03-02

    I have I problem with my custom GridView. I wrote the custom GridViewClass to enable the fling gesture within the GridView. However, there are some errors I don't quite understand. My code: <pl.flamis.GestureGridView android:layout_height="wrap_co

  • Android obtaining gridview similar to google play 2013-08-01

    I am looking to obtain a layout like this: I am trying to create a gridview item like the one used here for 'albums', but i am trying for a simpler version with just an image on top and text below it. Here is my XML code: <?xml version="1.0" encod

  • Android's GridView and a problem with size of its elements 2011-06-28

    I'm trying to make each element of GridView the same size (width and height) without any luck. Is there somewhere an option to do that or do I have to really make my own GridView like View? --------------Solutions------------- Have you tried setting

  • Android - ListView doesn't receive OnItemClick for textview with clickable links 2011-10-31

    I have a ListView that contains a TextView in each row besides a number of other views. The TextView renders html content which may contain links. The below code appears in the list adapter. m_textview.setMovementMethod(LinkMovementMethod.getInstance

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 0.422 (s). 13 q(s)