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:
Second Click:
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);
}
}