-2

Goal:
When click on the button, I would like to change the picture's X and Y coordinate to android:layout_x 35dp android:layout_y: 541dp by using java code and not XML code.

Questions:
I cannot find the correct syntax code in order do it.

Do you know how to do it?

Thank you!

Info:
*I'm newbie in android


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.jfdimarzio.myapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="158dp"
        android:layout_y="77dp"
        android:src="@drawable/cat" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="testtest"
        android:text="Button" />



</AbsoluteLayout>

package com.jfdimarzio.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsoluteLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class MainActivity extends AppCompatActivity
{

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

        ImageView myImageView = (ImageView) findViewById(R.id.imageView2);
        myImageView .setImageResource(R.drawable.cat);
    }

    public void testtest(View view)
    {
        AbsoluteLayout relativeLayout = new AbsoluteLayout(this);
        ImageView myImageView = (ImageView) findViewById(R.id.imageView2);

        myImageView.layout(10,10,10,10);
        //myImageView.layout(3,3,3,3);


    }
}
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
HelloWorld1
  • 13,688
  • 28
  • 82
  • 145
  • Possible duplicate of [Android - setting X,Y of image programmatically](https://stackoverflow.com/questions/6418726/android-setting-x-y-of-image-programmatically) – Ishaan Javali Jan 26 '19 at 23:08

1 Answers1

1

Haven't seen AbsoluteLayout for years o.o

In Android, a View's location isn't actually specified by either the View itself or the ViewGroup that contains the View. Instead, it's specified by the LayoutParams that accompanies the View.

In your xml file, when creating your View, the width, height, margins, and the x and y values are all within a LayoutParams object that is automatically created and set onto the View.

The LayoutParams is the component that tells the container how it should position the View.

So if you want to update the x and y of your ImageView, you'll need to edit from it's LayoutParams.

ImageView myImageView = (ImageView) findViewById(R.id.imageView2);
AbsoluteLayout.LayoutParams params = (AbsoluteLayout.LayoutParams) myImageView.getLayoutParams();
params.x = NEW_X_VALUE; //Please enter this yourself
params.y = NEW_Y_VALUE; //Be careful of DP to PX conversions
myImageView.setLayoutParams(params);
myImageView.requestLayout();

So what I'm doing here is fetching the LayoutParams that was created through the xml and changing the x and y values within it. Afterwards, I set the modified LayoutParams back into the ImageView.

I believe setLayoutParams() is already enough to cause the parent container to update the View's location, but just incase it doesn't... requestLayout() should.

Jackey
  • 3,184
  • 1
  • 11
  • 12