Sriraksha
Sriraksha

Reputation: 569

how to keep textview below progress of progressbar?

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>

Upvotes: 1

Views: 2261

Answers (2)

hasan_shaikh
hasan_shaikh

Reputation: 1500

The above approach works perfectly if you have a single progress bar. But if you have multiple progress bars like below, you need to use setTranslationX enter image description here

private fun positionProgressValue(progressBar : ProgressBar, progressValueTextView : TextView, progress : Int){
    val maxSizePoint = Point()
    windowManager.defaultDisplay.getSize(maxSizePoint)
    val maxX = maxSizePoint.x
    Handler().post {
        progressBar.setProgress(progress)
        val position: Int = progress * (progressBar.getWidth() - 2) / progressBar.getMax()
        val textViewX: Int = position - progressValueTextView.getWidth() / 2
        val finalX = if (progressValueTextView.getWidth() + textViewX > maxX) maxX - progressValueTextView.getWidth() else textViewX
        progressValueTextView.translationX = finalX.toFloat()
        progressValueTextView.setText(progress.toString())
    }
}

Upvotes: 0

Hardik Talaviya
Hardik Talaviya

Reputation: 1496

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!

Upvotes: 4

Related Questions