onClick doesn't work with ImageView?

Put your problem here if it does not fit any of the other categories.

onClick doesn't work with ImageView?

Postby sasuke » Wed Jan 09, 2008 6:50 am

Hello guys, did something missing with my code, because onClick doesn't work with ImageView? What's the problem?

Thank you

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class test extends Activity {
  3.  
  4.     Context ctx;
  5.  
  6.     /** Called when the activity is first created. */
  7.  
  8.     @Override    
  9.  
  10.     public void onCreate(Bundle icicle) {
  11.  
  12.         super.onCreate(icicle);
  13.  
  14.         ctx = this;
  15.  
  16.        
  17.  
  18.         AbsoluteLayout abs = new AbsoluteLayout(ctx);        
  19.  
  20.         AbsoluteLayout.LayoutParams lay = new AbsoluteLayout.LayoutParams(0,500,0,0);
  21.  
  22.         abs.setLayoutParams(lay);
  23.  
  24.  
  25.  
  26.         ImageView img = new ImageView(ctx);
  27.  
  28.         img.setImageDrawable(getResources().getDrawable(R.drawable.up));
  29.  
  30.         img.setLayoutParams(new AbsoluteLayout.LayoutParams(100,100,0,200));
  31.  
  32.         img.setOnClickListener(listen);
  33.  
  34.  
  35.  
  36.         abs.addView(img);
  37.  
  38.         setContentView(abs);
  39.  
  40.     }
  41.  
  42.    
  43.  
  44.     private OnClickListener listen = new OnClickListener()
  45.  
  46.     {
  47.  
  48.         @Override
  49.  
  50.             public void onClick(View v)
  51.  
  52.             {
  53.  
  54.                 ctx.showAlert("title", "clicked", "ok", true);
  55.  
  56.             }
  57.  
  58.     };     
  59.  
  60. }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
sasuke
Experienced Developer
Experienced Developer
 
Posts: 57
Joined: Fri Nov 30, 2007 1:25 pm

Top

ImageView can receive click events

Postby ben » Fri Jan 11, 2008 4:57 am

Just add the following line:


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. img.setFocusable=(true);
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


Or if you use xml layout file:

android:focusable="true"
Ben
ben
Junior Developer
Junior Developer
 
Posts: 15
Joined: Thu Dec 13, 2007 3:23 am

Postby sasuke » Fri Jan 11, 2008 5:43 am

hello ben, it still didn't work... :roll:
sasuke
Experienced Developer
Experienced Developer
 
Posts: 57
Joined: Fri Nov 30, 2007 1:25 pm

Postby snowtiger » Fri Jan 11, 2008 9:47 am

Try this one, its working fine in my apps:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. img.setOnClickListener(new View.OnClickListener() {
  2.  
  3.                         public void onClick(View view) {
  4.  
  5.                                 ctx.showAlert("title", "clicked", "ok", true);
  6.  
  7.                         }
  8.  
  9.                 });
Parsed in 0.034 seconds, using GeSHi 1.0.8.4

The only difference I can see, is "View.OnClickListener".
snowtiger
Developer
Developer
 
Posts: 34
Joined: Fri Dec 21, 2007 10:44 am

Postby sasuke » Fri Jan 11, 2008 4:20 pm

Hello snowtiger.
It's weird. It still didn't work in my apps... :? :?

can you paste your code here?

Thank you.
sasuke
Experienced Developer
Experienced Developer
 
Posts: 57
Joined: Fri Nov 30, 2007 1:25 pm

Postby plusminus » Fri Jan 11, 2008 6:02 pm

Hello sasuke,

this is probably the reason why there is a ImageButton-Class:

This makes it work:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ImageButton img = new ImageButton(ctx);
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby ben » Fri Jan 11, 2008 9:13 pm

The problem with the ImageButton is that it is a button. Sometimes you want a flat clickable image without the beveled state of the button. If you want to extend anything, you are better off using ImageView in this case.

sasuke:

The focusable statement worked for me great.
Ben
ben
Junior Developer
Junior Developer
 
Posts: 15
Joined: Thu Dec 13, 2007 3:23 am

Postby plusminus » Fri Jan 11, 2008 9:34 pm

Hello ben,

but you can make it look flat, like done :src: with the Christmas Calendar.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby sasuke » Sat Jan 12, 2008 1:42 am

Hello all,

ben wrote:The focusable statement worked for me great

it's not working for me.. :lol:

I've solved the problem by creating a class that extends the ImageView
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class MyButton extends ImageView{        
  2.         public MyButton(Context context,AttributeSet attr, Map inflateParams)
  3.         {
  4.                 super(context,attr,inflateParams);             
  5.         }
  6.  
  7.         @Override
  8.         public boolean onMotionEvent(MotionEvent event) {
  9.             int action = event.getAction();
  10.             boolean mCurDown = action == MotionEvent.ACTION_DOWN;          
  11.             boolean mCurUp = action == MotionEvent.ACTION_UP;
  12.             if (mCurDown) {                    
  13.                 this.setImageDrawable(getResources().getDrawable(R.drawable.down_2));
  14.                 performClick();    
  15.             } else
  16.             if (mCurUp) {
  17.                 this.setImageDrawable(getResources().getDrawable(R.drawable.up_2));
  18.             }
  19.             return true;
  20.         }
  21. }
  22.  
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


to access it :
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         MyButton but = (MyButton)findViewById(R.id.imgv);              
  2.         but.setOnClickListener(listener);
  3.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


It works :o :o
sasuke
Experienced Developer
Experienced Developer
 
Posts: 57
Joined: Fri Nov 30, 2007 1:25 pm

Re: onClick doesn't work with ImageView?

Postby albereseanes » Fri Dec 28, 2012 7:40 am

A primary example of this was the destruction of Henry Eastman, creator of the Eastman-Kodak Company, two months after his pension. Before getting his own lifestyle, he remaining a note that read: "To my buddies.
albereseanes
Freshman
Freshman
 
Posts: 5
Joined: Fri Dec 28, 2012 6:22 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Exabot [Bot], MSNbot Media and 17 guests