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

  • Home
  • SEARCH
  • 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 3496820
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:17:46+00:00 2026-05-18T12:17:46+00:00

I am playing with UI Event Handling, and I have found something that I

  • 0

I am playing with UI Event Handling, and I have found something that I cannot find the explanation from the Android Dev: I have an ImageView, and a TextView , whenever I touch on the ImageView, the TextView show a message. But the following codes doesn’t work:

    public class ShowSomething extends Activity {
 private LinearLayout ll;

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

  setContentView(R.layout.main);  

  LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout);
  final TextView textview = (TextView)findViewById(R.id.textview);
  MyImageView image = new MyImageView(this, textview);
  image.setImageResource(R.drawable.icon);
  ll.addView(image, 48, 48);

 }
}

And MyImageView.java

public class MyImageView extends ImageView implements OnTouchListener{


 private TextView textview;

 public MyImageView(Context context,TextView textview) {
  super(context);
  this.textview = textview;
 }

 @Override
 public boolean onTouch(View v, MotionEvent event) {
  textview.setText("Event captured !");
  return true;
 }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Holder"
/>
</LinearLayout>

But when I attach an OnTouchListener on the MyImageView like this, it does work:
File ShowSomething.java

public class ShowSomething extends Activity {

 private LinearLayout ll;

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

  setContentView(R.layout.main);  

  LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout);
  final TextView textview = (TextView)findViewById(R.id.textview);


  MyImageView image = new MyImageView(this,textview);
  image.setImageResource(R.drawable.icon);
  image.setOnTouchListener(new OnTouchListener() {  
     @Override
     public boolean onTouch(View v, MotionEvent event) {
      textview.setText("Event captured!");      
      return false;
     }
    });
  ll.addView(image, 48, 48);

 } 

}

And file MyImageView.java

public class MyImageView extends ImageView { 


 private TextView textview;

 public MyImageView(Context context,TextView textview) {
  super(context);
  this.textview = textview;
 }


}

But as far as I know, the 2 implementation is the same (implement an Event Listener) – do I misunderstand anything ?

  • 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-18T12:17:46+00:00Added an answer on May 18, 2026 at 12:17 pm

    For the first case, the right way would be:

    MyImageView image = new MyImageView(this, textview);
    image.setImageResource(R.drawable.icon);
    image.setOnTouchListener(image);
    

    or call setOnTouchListener(this) inside your MyImageView class.

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

Sidebar

Related Questions

I have a program with a Geospace map embedded into it. The event handling
Playing with log4net, I have seen the possibility to use a per-thread stack of
While playing around with regexps in Scala I wrote something like this: scala> val
I have been playing with the Ruby library shoes. Basically you can write a
I have been playing with this for a while, but the closest I have
I have a Compact Framework 3.5 application that is responsible for scanning barcodes. There
I have an architectural question on handling message ordering. For purposes of this question,
Just playing around with the now released Silverlight 2.0. I'm trying to put a
I am playing with Microsoft's TreeView control and I am trying to force a
I'm currently playing around with HTML_QuickForm for generating forms in PHP. It seems kind

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.