How can I align a TextView
with the progress of a horizontal ProgressBar
. I want to put a TextView
right above the progress position of the ProgressBar
. The progress might be changed. Also, I want the TextView
to be kept in one line, although the text length might be changed.
Asked
Active
Viewed 2,683 times
4
-
look at this [link](http://stackoverflow.com/questions/18574271/create-custom-seekbar-in-android) – Imtiyaz Khalani Feb 19 '14 at 13:59
-
Thank U for reply. It is definitely looks like a `SeekBar` without considering the space between the `TextView` and `ProgressBar`. Maybe, I should have a try. – ZhangLei Feb 19 '14 at 14:24
-
I think it is really a trouble to display all the text and keep it in one line if my text in `TextView` is much longer than a thumb image of a `SeekBar`. – ZhangLei Feb 19 '14 at 15:12
-
i do not have exact requirement so i just posted a link which may helps you.describe requirement in detail.thanx – Imtiyaz Khalani Feb 20 '14 at 05:53
-
see https://stackoverflow.com/a/61147300/6478047 – Manohar Apr 10 '20 at 19:28
2 Answers
0
In the parent layout which contains your TextView and Progress bar set the gravity to center so that it will keep both of them center alligned.
<LinearLayout
android:layout_width="match_parent"
android:layout_width="wrap_content"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_width="wrap_content"
android:text="Hiee"/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_width="wrap_content"/>
</LinearLayout>

Atul O Holic
- 6,692
- 4
- 39
- 74
-
Thank you for reply. But your code is keeping the `TextView` alligned with `ProgressBar`, which is not what I expected. I want the `TextView` to be alligned with the progress of the `ProgressBar`, but not the `ProgressBar` itself. – ZhangLei Feb 19 '14 at 14:11
-
In that case, as you dynamically set your progress try adding those numbers as margin to your TextView in the direction of your progress. You will certainly have to keep trying on this to get the desired output. – Atul O Holic Feb 19 '14 at 14:13
-
Yes! I was trying to set different margins. However, as the `android:layout_width` of the `TextView` was set to be `wrap_content`, I could not find a method of getting the width of this `TextView`. `WRAP_CONTENT` is -2. – ZhangLei Feb 19 '14 at 14:19
-
1Here's a link for this I guess, http://stackoverflow.com/questions/16021764/in-android-how-to-get-the-width-of-the-textview-which-is-set-to-wrap-content – Atul O Holic Feb 19 '14 at 14:22
0
Calculate the x position of thumb from progressBar and apply it to textview
var xPosition= (((seekBar.right - seekBar.left) / seekBar.max) * seekBar.progress ) + seekBar.left
progressTextView.translationX = xPosition.toFloat() - (progressTextView.width/2)

Manohar
- 22,116
- 9
- 108
- 144