How can i set the view position in the center of the parent in java code?

40 views Asked by At

I want to set the view position in the center of the parent when the button is clicked. How can i do that?

Here is my condition, but when i click the button, the view goes to the upper left corner

    float pHeight = parent.getMeasuredHeight();
    float pWidth = parent.getMeasuredWidth();
   
    button_center.setOnClickListener(v -> {
        textView.setX(pHeight / 2);
        textView.setY(pWidth / 2);
    });
0

There are 0 answers