0

For some reason when I set the x coordinate to screen_width - image_width, the image is displayed off the screen. The same scenario occurs with the y coordinate. Here is my code.

public class MainActivity extends AppCompatActivity {

    ImageView image;
    float height;
    float width;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        height = displayMetrics.heightPixels;
        width = displayMetrics.widthPixels;

        image.setImageResource(R.drawable.pigeon);
        image.setX(width - image.getMeasuredWidth());

In this case the image, which is a pigeon, is not displayed on the screen. And I expect it to be displayed so that the right border of the pigeon touches the right border of the screen.

Edit: It's not that I just want to position the image and be done with it. I want to be able to move the image to precise coordinates while the app is running, such as when a mouse click occurs.

Jonathan
  • 247
  • 3
  • 11
  • just wondering why dont you set the width and height of the image in the xml? `match_parent` does the job – Julfikar Dec 28 '18 at 03:33
  • I did. I'm only trying to set the position of the image here. The width and height are already set. – Jonathan Dec 28 '18 at 04:06
  • Are you trying to set the position to (0,0) – Julfikar Dec 28 '18 at 04:12
  • No, I want to set the position such that the right border of my image, touches the right border of the phone screen. – Jonathan Dec 28 '18 at 04:19
  • im not sure about your intention to do this positioning in such way but i would suggest you that before you proceed, try to find the native way of doing this.. you are trying to positioning from java and xml, which is kind of weird in this case since you are just trying to align an image only – Julfikar Dec 28 '18 at 07:18
  • another suggestion i can give u that, better add a border around the image so that you can clarify that the image is REALLY going out of the screen.. who knows, maybe its only an image resizing issue... just a friendly suggestion only :) – Julfikar Dec 28 '18 at 07:20
  • My purpose is not really to put an image at the edge of the screen. But rather to learn how to move an image to precise coordinates while the app is running. – Jonathan Dec 28 '18 at 07:21
  • 1
    Then it's totally different story. you can refer to this answer: https://stackoverflow.com/a/9398861/7081771 – Julfikar Dec 28 '18 at 07:22

1 Answers1

0

One way to align the image right is to use the XML layout. This is the easiest method.

Assuming you have an ImageView inside a RelativeLayout you can use the `layout_alignParentRight' attribute as follows:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity">
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/image_name"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        />
</RelativeLayout>

The layout_alignParentEnd attribute is an alternative for layout_alignParentRight for right to left language script. For a LinearLayout you can use layout_gravity attribute. Also, ensure that your ImageView is wrap_content and not match_parent in which case the image will expand to fill the entire ImageView.

If you want to do it programmatically, you can do it in the following manner:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = findViewById(R.id.imageView);
        imageView.setImageResource(R.drawable.pigeon);
        RelativeLayout.LayoutParams params = new 
        RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
        RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        imageView.setLayoutParams(params);
    }

Here we are aligning the child view of RelativeLayout right by adding the align right rule and then setting that layout parameter for the Image View. This can also be done for LinearLayout using layout_gravity feature.

I have tested this code for a sample image. Hope this solves your problem.

Neelabh
  • 31
  • 6
  • Its not that I just want to initialize the location of the image though. I want to be able to position it dynamically (to precise coordinates) such as on a mouse click. For example, maybe I want the image to move to position (374, 116) after a mouse click. – Jonathan Dec 28 '18 at 07:13
  • You can set params.leftMargin and params.topMargin with the precise coordinates. In your example. params.leftMargin = 374, and params.topMargin = 116. And then set the imageView.setLayoutParams(params). This should do the trick. – Neelabh Dec 28 '18 at 10:01
  • Thanks, I will try that – Jonathan Dec 28 '18 at 16:31