0

I am trying to create an activity that will display an image on the onClick of a button. So far, I have successfully made my application fade the image in when you click the button, but I want the image to be displayed over the button that has been pressed.

From my understanding you want to go about this using LayoutParams which I have attempted to do, but it seems to be getting the wrong values from my Button.getX() and getY(). Here is my code:

button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ConstraintLayout.LayoutParams layoutParamsButton1 = new ConstraintLayout.LayoutParams(((int)button1.getX()), ((int)button1.getY()));
            whatImage[0].setLayoutParams(layoutParamsButton1);

            fadeInAndHideImage(whatImage[0]);
        }
    });

whatImage[] is just an array of ImageViews because I want to change their locations around, just fyi.

Anyway, what I think might be happening is a problem with the getX() and getY() methods retuning wrong values, because my buttons are constrained with android.support.constraint.Guidelines. Also, I don't think this is a problem, but I have to cast them as (int)s because they both return floats. There might be some rounding errors but I doubt getX() and getY() would return values that are by the half or quarter pixel.

Thanks

EDIT

Just tried to add ((int)uno.getX()), ((int)firstRail.getY()) into my LayoutParams instead of the buttons x and y, (uno and firstRail are guidelines that I use for the button), but it unfortunately put the button in the same exact spot as the LayoutParams with the buttons getX() and getY() methods.

Also, it seems to scale the image down when it moves it, which is also odd

EDIT 2

Ugh, after printing to out to see what exactly these values are, button1.getX() and firstRail.getX() both return 0.0. Is this even possible while using a Constraint layout??

cjnash
  • 1,228
  • 3
  • 19
  • 37

1 Answers1

1

Try:

whatImage[0].setX(button1.getX());

whatImage[0].setY(button1.getY());

Also keep in mind that resize of image could happend if you have some padding attribute added to your ImageView into xml

cjnash
  • 1,228
  • 3
  • 19
  • 37
Radu Selea
  • 26
  • 3
  • Sweet! It seemed to have worked. I did, however take out the `- whatImage[0].getWidth()/2` because that part was throwing it to the left when it doesn't need to be along with the second part on setY(). And yes, thank you for adding in the part about padding. – cjnash Jul 26 '17 at 15:08