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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:07:34+00:00 2026-06-14T13:07:34+00:00

I have an activity and an extended View class,that both use the same xml

  • 0

I have an activity and an extended View class,that both use the same xml layout. In the extended view I do some drawings on screen with touch, and want to pass the coordinate x of the touch to the Activity, whenever the user taps the screen.

So far I have used a getter method and call it from inside the activity but it ,only, passes the initializing value of X of the constructor and not the actual X coordinate the user has touched.

I am pretty sure I am missing something. Can you give me some directions?

EDIT : As suggested, instead of the getter practice, I tried using a public interface, still with X not changing. Here is my code:

My custom class:

public class ImageView1 extends View  {

 Context context;

 ArrayList<ViewWasTouchedListener> listeners = new 
                                   ArrayList<ViewWasTouchedListener>();
 ImageView1 img = (ImageView1) findViewById (R.id.imageView1);

 public float x=0;
 public float y=0;

 public ImageView1(Context context) {
        super(context);
        this.context=context;
        setFocusable(true);
    }


public ImageView1(Context context, AttributeSet attrs) {
    super(context, attrs); 
    this.context=context;
    setFocusable(true);

} 

@Override
public boolean onTouchEvent(MotionEvent event){
        switch (event.getAction()){
        case (MotionEvent.ACTION_DOWN): {

                 x = event.getX();
                 y = event.getY();
     for (ViewWasTouchedListener listener:listeners){
         Log.d("Touchpoint", String.valueOf(x) +"," + String.valueOf(y));
         listener.onViewTouched(x);
        }
    }

    return true;

   }  

@Override
public void onDraw(Canvas canvas){
    super.onDraw(canvas);

    ....
}

public void setWasTouchedListener(ViewWasTouchedListener listener){
    listeners.add(listener);
}   
}

My Main Activity:

 public class TargetPractise extends Activity implements ViewWasTouchedListener  {
 @Override

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.target_form);

    ImageView1 value = new ImageView1(TargetPractise.this);
    value.setWasTouchedListener(TargetPractise.this);   

 }

  public void onViewTouched(float x){
     Log.d("x",String.valueOf(x));

  }   
  }

My interface:

  public interface ViewWasTouchedListener {
void onViewTouched(float x);
 }

My xml layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical" >

 <android.archer.test.ImageView1
  android:id="@+id/imageView1"
  android:layout_width="300dip"
  android:layout_height="300dip"
  android:layout_gravity="center_horizontal"
  android:background="@drawable/target" >
</android.archer.test.ImageView1>
  • 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-06-14T13:07:35+00:00Added an answer on June 14, 2026 at 1:07 pm

    Define an interface and use a callback to let the activity know that x has changed. I like this approach because it works for any class interested in knowing when x has changed, not just your activity. Imagine you add a new type of view class but it also needs to know when your custom view has been touched (I do this with a graph and horizontal and vertical scales which can be dragged – the scales tell anyone who’s interested that they have changed).

    public Interface ViewWasTouchedListener {
        void onViewTouched(float x);
    }
    

    In your custom view

    ArrayList<ViewWasTouchedListener> listeners = new ArrayList<ViewWasTouchedListener>();
    
    ...
    
    public void setWasTouchedListener(ViewWasTouchedListener listener){
        listeners.add(listener);
    }
    

    In your touch event

    for (ViewWasTouchedListener listener:listeners){
       listener.onViewTouched(x);
    }
    

    In your Activity:

    public class Test extends Activity implements ViewWasTouchedListener {
    
    ...
    
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        ...
        MyExtendedView customView = (MyExtendedView)findViewById(R.id.myCustomeView);
        customView.setWasTouchedListener(this);
        ...
    }
    
    public void onViewTouched(float x){
       // do whatever you need to do
    }
    

    All from memory so please excuse typos and you should improve the view class by adding removeViewWasTouchedListener and checking that you do not add the same listener twice in setViewWasTouchedListener.

    You probably want y as well so just add it to the Interface definition.

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

Sidebar

Related Questions

I have two lists that use the same extended BaseAdapter in the same activity
I have a activity that extends listactivity, extended in this class i have a
I have an Activity that pulls an object from an Application extended class (application
in my app i have activity with XML layout, i want to build an
I have an activity with the following layout. So in this screen, initally the
I have extended the android View class, and now I want to define a
I have Activity with some data displayed in WebView that I load with WebView#loadDataWithBaseURL
I have extended the Application class in my app. In that class, I have
So I have an abstract class extended through the entire app that overrides the
I have activity records that have an inline formset showing appointments. I use the

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.