Home » Android » android – how to keep textview below progress of progressbar?-Exceptionshub

android – how to keep textview below progress of progressbar?-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

How can i set the position of the x so that it will align correctly below progressbar in all screens?
Below is my activity class.What changes i need to make for the code to work properly?

public class ProgressBarAvtivity extends AppCompatActivity {
    ProgressBar progressBar;
    TextView tvProgress;
    int d = 60;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_progress_bar_avtivity);
        progressBar = ( ProgressBar ) findViewById(R.id.progressBar);
        progressBar.setProgress(d);
        tvProgress = ( TextView ) findViewById(R.id.tvProgress);
        tvProgress.setText(String.valueOf(d));
        tvProgress.setX();
    }
}

Below is my xml. Please check.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Activity.ProgressBarAvtivity">
    <ProgressBar
        android:id="@+id/progressBar"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginTop="10dp"
        android:padding="10dp"
        android:progressDrawable="@drawable/progreesbar" />
    <TextView
        android:id="@+id/tvProgress"
        android:layout_marginLeft="10dp"
        android:textSize="15sp"
        android:text="35"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>
How to&Answers:

Try below solution

private int progressStatus=0;
private Handler handler = new Handler();

Point maxSizePoint = new Point();
getWindowManager().getDefaultDisplay().getSize(maxSizePoint);
final int maxX = maxSizePoint.x;

new Thread(new Runnable() {
    public void run() {
        while (progressStatus < 100) {
            progressStatus += 1;
            // Update the progress bar and display the
            //current value in the text view
            handler.post(new Runnable() {
                public void run() {
                    progressBar.setProgress(progressStatus);
                    int val = (progressStatus * (progressBar.getWidth() - 2 )) / progressBar.getMax();
                    int textViewX = val - (txtView.getWidth() / 2);
                    int finalX = txtView.getWidth() + textViewX > maxX ? (maxX - txtView.getWidth() - 16) : textViewX + 16/*your margin*/;
                    txtView.setX(finalX < 0 ? 16/*your margin*/ : finalX);
                    txtView.setText(String.valueOf(progressStatus));
                }
            });
            try {
                // Sleep for 200 milliseconds.
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}).start();

Output

enter image description here

I hope this can help you!