Getting percentage value from seek bar?

11,766

Solution 1

Create a view in xml

 <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="100" >

    <SeekBar
        android:id="@+id/SeekBarId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="92" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginLeft="4dp"
        android:layout_weight="8"
        android:gravity="center"/>
</LinearLayout>

set onSeekBarChageListener on the seekBar, like this:

seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
                    {

                        public void onStopTrackingTouch(SeekBar bar)
                        {
                            int value = bar.getProgress(); // the value of the seekBar progress
                        }

                        public void onStartTrackingTouch(SeekBar bar)
                        {

                        }

                        public void onProgressChanged(SeekBar bar,
                                int paramInt, boolean paramBoolean)
                        {
                            textView.setText("" + paramInt + "%"); // here in textView the percent will be shown
                        }
                    });

Solution 2

String[] percent = { "0", "10", "20", "30", "40", "50", ... "100" };


seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
            public void onStopTrackingTouch(SeekBar seekBar) {
            }

            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                baths.setText("" + percent[progress] + "%");
            }
        });

Add do add in xml-layout in seekbar:-

android:max="10"
Share:
11,766
Horrorgoogle
Author by

Horrorgoogle

Hello, I am an android developer using Java and kotlin. Besides work, I loved to post articles on my blog PrAndroid, CodingWorkspace. I like to watching soccer, movies and traveling. I have my own 6H rule — (Home||Help||Happiness||Health||Humanity||Honesty) You can catch me following: Personal Website GitHub Google Play Store LinkedIn Angle.co Medium ###HappyCoding - #ILoveKotlin #PrAndroid #CodingWorkspace #APT3004

Updated on August 06, 2022

Comments

  • Horrorgoogle
    Horrorgoogle over 1 year

    I have just like this figure, which is get value from this, enter image description here

    Here I Used SeekBar,but could not get value where i mention in figure. would you guys help me this problem?

    So please also provide me information , what is the best solution to get this type of value?

    Here all are differenet seek bar.

    First SeekBar have 25% and second seek Baar 50%. If i have scroll seek bar then simultaneously increase or decrease the percentage in box.