0

When I click on a Button, that button is supposed to make a hidden SeekBar (visibility = "gone") visible at the coordinates (150, 150). However, the first time I click on that Button, the SeekBar is placed in a different location than the second time, as shown in the screenshots below:

First Click:

enter image description here

Second Click:

enter image description here

Below is my code:

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <Button
        android:id="@+id/button"
        android:onClick="makeSeekbarVisible"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button" />
    <SeekBar
        android:id="@+id/seekbar"
        android:visibility="gone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:padding="0dp"
        android:max="100"
        android:progressDrawable="@android:color/transparent"/>
</LinearLayout>

MainActivity.java:

package com.example.changingimageviewwidth;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void makeSeekbarVisible(View v) {
        // Make the image preview box visible
        SeekBar seekbar = findViewById(R.id.seekbar);
        seekbar.setVisibility(View.VISIBLE);
        seekbar.bringToFront();

        // Place the image preview box at a specified location
        seekbar.setX(150);
        seekbar.setY(150);
    }
}
Adam Lee
  • 436
  • 1
  • 14
  • 49

1 Answers1

0

As stated in setX/Y documentation:

Sets the visual y position of this view, in pixels. This is equivalent to setting the translationY property to be the difference between the y value passed in and the current top property.

It is happening because the initial visibility of the view is GONE. So when calling setY for the first time, the view is not drawn yet (checkout this) so the top property is 0. After a while the view is drawn and the top property is changed so the view is below the button; causing the y property to also change.

Timeline:

  1. Activity started and views are drawn: top=0, y=0
  2. Button is just pressed but the SeekBar not drawn yet: top=0, y=150
  3. SeekBar is drawn: top≃BUTTON_HEIGHT, y≃BUTTON_HEIGHT+150
  4. Button is pressed second time: top≃BUTTON_HEIGHT, y=150
navid
  • 1,022
  • 9
  • 20
  • How would I go about resolving this issue then? – Adam Lee Jun 10 '21 at 15:05
  • I actually don't see your expected behavior. But if you want it as your second picture, wait for the view to be drawn using onGlobalLayoutListener (https://stackoverflow.com/a/15578844/6094503) and after that make calls to setX/Y. – navid Jun 10 '21 at 17:36