Hey Oliver,
I'm just starting out on the Android (and Java) and was trying to
solve a very similar problem and gain access to clicks on a series of
ImageView objects when I stumbled across your post. I ended up solving
my problem and thought maybe it would help you also.
I'm putting together a card game, setup ImageView objects for each of
the 5 cards on the display. I wanted to get a click on any card and be
able to determine which card was clicked.
The views are defined as:
ImageView[] ImageCards = new ImageView[5];
I then borrowed from your example a bit and did this:
// Create an anonymous implementation of OnClickListener
private OnClickListener mCorkyListener = new OnClickListener() {
public void onClick(View v)
{
int cnt;
ImageView iv;
iv = (ImageView) v; // Get the View that was
clicked
// Find which card was clicked by walking through our
cardviews comparing to the one that was clicked
for(cnt=0;cnt<5;cnt++)
{
if( iv == ImageCards[cnt] )
{
// We found which card was clicked, go
display it to our debug console just for testing
System.out.format("Click on card %d%n", cnt);
}
}
}
};
// Next, in my main onCreate section
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Setup the handles for the individual card's imageviews
ImageCards[0] = (ImageView) findViewById(R.id.CardView1); //
CardViewx as defined in main.xml
ImageCards[1] = (ImageView) findViewById(R.id.CardView2);
ImageCards[2] = (ImageView) findViewById(R.id.CardView3);
ImageCards[3] = (ImageView) findViewById(R.id.CardView4);
ImageCards[4] = (ImageView) findViewById(R.id.CardView5);
// Connect each view to our private OnClickListener
int cnt;
for(cnt=0;cnt<5;cnt++)
{
ImageCards[cnt].setOnClickListener(mCorkyListener);
}
I'm sure it could be architected cleaner but this works like a
charm.... hard to argue with something that works when you're just
starting out!
On Apr 24, 5:02 am, "Oliver M. Batista" <[email protected]>
wrote:
> Hi Jack, well you guess right. It's a board game so I use the same
> drawable/image at the begining. The layout is this basically, squares using
> the same image. Can't I use the same image all over my game?
>
> Sorry don't send you the layout file it's because i'm writing from work =P
>
> thanks
>
> 2009/4/23 Jack Ha (T-Mobile USA) <[email protected]>
>
>
>
>
>
> > Can you post your layout file here?
>
> > Are the other imageviews on the activity pointing to the same image as
> > square00?
>
> > --
> > Jack Ha
> > Open Source Development Center
> > ・T・ ・ ・Mobile・ stick together
>
> > The views, opinions and statements in this email are those of
> > the author solely in their individual capacity, and do not
> > necessarily represent those of T-Mobile USA, Inc.
>
> > On Apr 22, 4:11 pm, "Oliver M. Batista" <[email protected]>
> > wrote:
> > > Hello guys, i'm having a problem with this imageview listener (see
> > > code below) well the click event is only triggered when I click on the
> > > square00 item (this is ok) but on the onclick method that does the
> > > action, i use the reference passed by the method and the action is
> > > applied to all the imageviews on the Activity but I want the action to
> > > be apllied only for the square00 imageview. Somebody knows what's
> > > wrong?
>
> > > public class HelloDroid extends Activity implements OnClickListener {
> > > /** Called when the activity is first created. */
>
> > > // Create an anonymous implementation of OnClickListener
> > > private OnClickListener mCorkyListener = new OnClickListener() {
> > > public void onClick(View v) {
> > > // do something when the button is clicked
>
> > > ImageView asd = (ImageView)v;
> > > asd.setAlpha(50);
>
> > > }
> > > };
>
> > > @Override
> > > public void onCreate(Bundle savedInstanceState) {
> > > super.onCreate(savedInstanceState);
> > > setContentView(R.layout.main);
>
> > > // Capture our img from layout
> > > ImageView img2 = (ImageView)findViewById(R.id.square00);
> > > // Register the onClick listener with the implementation above
> > > img2.setOnClickListener(mCorkyListener);
> > > }
>
> > > }
>
> --
> Oliver M. Batista
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---