3

For AndroidTV I have created a simple seek bar and text view. I am trying to align text view of seek bar thumb and trying to move with progress.

Tried these solutions but it's not working link1, link2

Sample Code
logic to get x position of text view

seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onProgressChanged(final SeekBar seekBar, final int progress, boolean fromUser) {
        if (fromUser) {
            int width = seekBar.getWidth()
                        - seekBar.getPaddingLeft()
                        - seekBar.getPaddingRight();
            float thumbPos = width
                             * seekBar.getProgress()
                             / seekBar.getMax();
            textView.setX(width );
        }
    }
}

XML

    <TextView
        android:id="@+id/textView"
        android:layout_width="160dp"
        android:layout_height="90dp"
        android:layout_gravity="start"
        android:background="#ffff00"/>

  <SeekBar
        android:id="@+id/previewSeekBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:min="1"
        android:max="10"/>

</RelativeLayout>

Expected

  1. Textview should move with seek bar progress.
  2. Textview should be in the center of thumb and above seek bar like in screenshot.
  3. Text view should move when progress > text view width/2 && progress
Logic
  • 2,230
  • 2
  • 24
  • 41
  • logic used to get x position seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(final SeekBar seekBar, final int progress, boolean fromUser) { if (fromUser) { int width = seekBar.getWidth() - seekBar.getPaddingLeft() - seekBar.getPaddingRight(); float thumbPos =width * seekBar.getProgress() / seekBar.getMax(); textView.setX(width ); } } – Kalpana Sahu May 31 '19 at 16:45
  • 1
    Kindly format your code "the logic that you explained". Its very hard to understand your logic. You just need to set correct X/Y values to the text view or you can translate them. The base logic is to calculate correct values of X and Y – Rahul Lohra May 31 '19 at 17:44

1 Answers1

5

Sorry for late answer, your logic is correct but it requires few changes.

float thumbPos = width * (seekBar.getProgress() / (float)seekBar.getMax())

textView.setTranslationX(thumbPos) ;

seekBar.getProgress() is an int and seekBar.getMax() is also an int, if divide them they will return integer value, but we need float(in decimal) that's why I have added the changes

Rahul Lohra
  • 824
  • 1
  • 10
  • 21
  • Rahul thanks for reply but it's not working even after change int to float.can you please give some sample? – Kalpana Sahu Jun 28 '19 at 14:39
  • 3
    Hi Kalpana I have uploaded a sample project, kindly run the app & check the code in MainActivity.kt url - https://github.com/iamdangerous/TranslateTextWithSeekbar – Rahul Lohra Jun 30 '19 at 14:05