Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - SeekBar starting point

I have two SeekBar in my view, and I declare them similarly in the layout:

<SeekBar android:id="@+id/select_price"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_marginTop="5dp" />
<TextView android:id="@+id/max_price"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="0 VND"
          android:layout_gravity="center" />

<LinearLayout android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_marginTop="20dp"
              android:orientation="horizontal">
    <ImageView android:layout_width="20dp"
               android:layout_height="20dp"
               android:src="@drawable/ic_distance"
               android:layout_marginRight="5dp" />
    <TextView android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textSize="18sp"
              android:text="@string/max_distance" />
</LinearLayout>

<SeekBar android:id="@+id/select_distance"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_marginTop="5dp"/>
<TextView android:id="@+id/distance"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="0 Km"
          android:layout_gravity="center" />

And here is the code where I use them:

SeekBar sb = (SeekBar) v.findViewById(R.id.select_price);
            sb.setMax(1000);
            sb.setProgress(10);
            sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){

                @Override
                public void onProgressChanged(SeekBar seekBar, int progress,
                        boolean fromUser) {
                    TextView text = (TextView) v.findViewById(R.id.max_price);
                    text.setText(Integer.toString(progress) + PlaceListActivity.this.getResources().getString(R.string.dong));
                    maxPrice = progress;
                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {
                    // TODO Auto-generated method stub

                }

            });

            SeekBar seek = (SeekBar) v.findViewById(R.id.select_distance);
            seek.setMax(10);
            seek.setProgress(1);
            seek.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){

                @Override
                public void onProgressChanged(SeekBar seekBar, int progress,
                        boolean fromUser) {
                    TextView text = (TextView) v.findViewById(R.id.distance);
                    text.setText(Integer.toString(progress) + PlaceListActivity.this.getResources().getString(R.string.km));
                    maxDistance = progress;
                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {
                    // TODO Auto-generated method stub

                }

            });

But when I debug the app in my device, the first seekbar is okay but the another gets weird and I don't know why. This is the screenshot:

enter image description here

As you can see, the starting point in the first bar isn't from the start. What could be the reason for this? I really hope that someone can help me out with this. Thanks.

like image 594
huong Avatar asked May 17 '13 12:05

huong


2 Answers

If you want both SeekBars to start at the beginning while still setting a minimum value of 10 and 1 for price and distance respectively, set them both to zero and just add the minimum value programatically like so:

    SeekBar sb = (SeekBar) v.findViewById(R.id.select_price);
    sb.setMax(1000);
    sb.setProgress(0); // Set it to zero so it will start at the left-most edge
    sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            progress = progress + 10; // Add the minimum value (10)
            TextView text = (TextView) v.findViewById(R.id.max_price);
            text.setText(Integer.toString(progress) + PlaceListActivity.this.getResources().getString(R.string.dong));
            maxPrice = progress;
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {}

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {}

    });

    SeekBar seek = (SeekBar) v.findViewById(R.id.select_distance);
    seek.setMax(10);
    seek.setProgress(0); // Set it to zero so it will start at the left-most edge
    seek.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            progress = progress + 1; // Add the minimum value (1)
            TextView text = (TextView) v.findViewById(R.id.distance);
            text.setText(Integer.toString(progress) + PlaceListActivity.this.getResources().getString(R.string.km));
            maxDistance = progress;
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {}

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {}

    });
like image 114
Krauxe Avatar answered Oct 12 '22 06:10

Krauxe


Pretty obvious according to your code - the first SeekBar is at 1% (progress 10, max 1000), the second SeekBar is at 10% (progress 1, max 10).

So your second Seekbar is not at the start. Just adjust the values...

like image 43
SirKnigget Avatar answered Oct 12 '22 06:10

SirKnigget