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 6328997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:33:55+00:00 2026-05-24T17:33:55+00:00

im writing my first android app (im not new to java). heres my problem:

  • 0

im writing my first android app (im not new to java). heres my problem:
i use an ImageView to show a graphic. I would like to write a method thas is called when the ImageView is touched and gets the touch coordinates as parameters.

Here is my current solution:

img.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent e) {
            clickfield((int) e.getX(), (int) e.getY());
            toast.setText((int) e.getX() + ", " + (int) e.getY());
            toast.show();
            return true;
        }
    });  

...

public void clickfield(int x, int y) {
   ...
}

I have two problems with this:
At first, the coordinates that it returns are not the real coordinates on the phone. my vm has a resolution of 480 x 800 pixels. As the ImageView is 480*480 pixels large, the y value should be between 0 and 480. The actaual value is between 0 and 320.This could be solved by multiplying it by 1.5, but this doesnt make sense to me.

The second problem is that the 480 * 480 Bitmap which is the content of the ImageView appears in the middle of the screen. I would prefer to display it at the top, especially beacause the the above code detects something like 0, 72 if i touch the up left corner of the image instead of 0, 0.
Which property of the image view do I have to change?

If there is any reference/tutorial that answers my question, it would suffice if you post a link.

my xml layout:

<?xml version="1.0" encoding="utf-8"?> 
  <LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <ImageView 
      android:src="@drawable/icon" 
      android:id="@+id/ImgTest" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:clickable="true" 
      android:layout_gravity="top">
    </ImageView> 
</LinearLayout>
  • 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-24T17:33:56+00:00Added an answer on May 24, 2026 at 5:33 pm

    The coordinates you get relate to the the (Image)View to which the listener is attached.
    Your image gets scaled to fit that view. So your view is 320px wide or high.
    If you want to get the actual pixel coordinate, you have to calculate that from the Views width/height and the image ones. Almost like you did, but with a dynamic factor, since you will get different ones for every screen size.

    The positioning of your actual image is also related to that. If the image is bigger or smaller than your view, it gets resized while maintaining its aspect ratio. If this doesn’t match your screens aspect ratio, you will get some borders. To position the image on top of your layout, you should let the ImageView automatically resize its height to the required size of the image. You can do that via setting the android:layout_height to wrap_content in your layout file (inside the ImageView tag). If it still stays in the middle, you probably have to also adjust your outer layouts gravity. See android:gravity (what you want here is top).

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

Sidebar

Related Questions

I am writing my first serious wxWidgets program. I'd like to use the wxConfig
I'm writing my first location based android app, but got confused about some of
I'm writing my first android app (I'm a noob at android, but decent at
I am writing my first simple Android App: It loads temperature samples from a
I started writing my first Android app, and chose SDK 2.0.1, before I had
I'm writing my first 2D app for Android using OpenGL. I'm writing it on
I'm in the process of writing my first android app, a simple media player
I am writing my first android app. So am still in the learning phase.
I'm new to writing app for android. I've got program writing experience in non
I'm writing my first client/server android app, and need an advice regarding server architecture.

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.