syedowaisali / crystal-range-seekbar

537 stars 164 forks source link

How to set current progress in thumb? #52

Closed mayurdg93 closed 7 years ago

mayurdg93 commented 7 years ago

How to set progress value in thumb on seekbar changes?

irfaan008 commented 7 years ago

@syedowaisali I am also struggling with this. How to set values in thumb. For example suppose I am having range seekbar ranging from 0 to 100. User selected 5-10. I saved this 5-10 to my database. Now on next opening of app I have set thumb to 5-10, while the range would still be 0-100. Please guide. I dont' see any method in library as well.

michaelprichardson commented 7 years ago

For the RangeSeekBar you can just use: _rangeseekbar.setMinStartValue(<some_start_value>); _rangeseekbar.setMaxStartValue(<some_end_value>); _rangeseekbar.apply();

The same can be done for the standard seekbar: _seekbar.setMinStartValue(<some_start_value>)); _seekbar.apply();

mayurdg93 commented 7 years ago

@irfaan008 I have set value in thumb on seekbar changes by creating custom thumb view and set it using method .setLeftThumbBitmap(bitmap).

Create Custom Thumb as below in custom_thumb.xml: `<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/location_thumb" android:layout_width="50dp" android:layout_height="50dp" android:background="@drawable/circle_shape_white">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_progressValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="25"
        android:textColor="@android:color/white"
        android:textSize="14sp"
        android:textStyle="bold" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="MI"
        android:textColor="@android:color/white"
        android:textSize="10sp" />
</LinearLayout>

`

Include it in Main Layout as below in activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#483d5d">

    <include layout="@layout/toolbar_black" />

    <include layout="@layout/custom_thumb" />

    <com.app.we.widget.seekbar.CrystalSeekbar
            android:id="@+id/rangeSeekbar4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="24dp"
            android:layout_marginRight="24dp"
            android:layout_marginTop="30dp"
            card_view:max_value="30"
            card_view:min_value="10" />
</RelativeLayout>

In above XML code, included custom thumb view in a manner that it will hide itself behind toolbar view.

And in MainActivity, on seek bar change get its value and set to the custom view. Then created its bitmap of custom thumb view which is not visible to the user. And set it again as the thumb of seeekbar.

In MainActivity:

@Bind(R.id.tv_progressValue)
CustomTextViewRegular tvProgressValue;
@Bind(R.id.location_thumb)
RelativeLayout location_thumb;
@Bind(R.id.rangeSeekbar4)
CrystalSeekbar rangeSeekbar4;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

       ViewTreeObserver vto = root.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
          rangeSeekbar4.onSeekbarChangeListener.valueChanged(rangeSeekbar4.getSelectedMinValue());
                radius = rangeSeekbar4.getSelectedMinValue() + "";
            }
        });

        setRangeSeekbar();
}

private void setRangeSeekbar() {
        TextView tv_progressValue = null;
        Bitmap bitmap = null;
        if (location_thumb != null) {
            location_thumb.setDrawingCacheEnabled(true);
            tv_progressValue = (TextView) location_thumb.findViewById(R.id.tv_progressValue);
            Bitmap b = Bitmap.createBitmap(location_thumb.getLayoutParams().width, location_thumb.getLayoutParams().height, Bitmap.Config.ARGB_8888);
            Canvas c = new Canvas(b);
            location_thumb.layout(location_thumb.getLeft(), location_thumb.getTop(), location_thumb.getRight(), location_thumb.getBottom());
            location_thumb.draw(c);
            bitmap = b;
        }
        // set properties
        rangeSeekbar4
                .setCornerRadius(10f)
                .setBarColor(Color.parseColor("#dbd3de"))
                .setBarHighlightColor(Color.parseColor("#f3f0f4"))
                .setMinValue(5)
                .setMaxValue(100)
                .setSteps(1)
                .setLeftThumbBitmap(bitmap)
                .setLeftThumbHighlightBitmap(bitmap)
                .setDataType(CrystalRangeSeekbar.DataType.INTEGER)
                .apply();

        // set listener
        final TextView finalTv_progressValue = tv_progressValue;
        rangeSeekbar4.setOnSeekbarChangeListener(new OnSeekbarChangeListener() {
            @Override
            public void valueChanged(Number minValue) {
                if (location_thumb != null) {
                    Bitmap b = Bitmap.createBitmap(location_thumb.getLayoutParams().width, location_thumb.getLayoutParams().height, Bitmap.Config.ARGB_8888);
                    Canvas c = new Canvas(b);
                    location_thumb.layout(location_thumb.getLeft(), location_thumb.getTop(), location_thumb.getRight(), location_thumb.getBottom());
                    location_thumb.draw(c);
                    rangeSeekbar4.setLeftThumbBitmap(b);
                    rangeSeekbar4.setLeftThumbHighlightBitmap(b);
                    finalTv_progressValue.setText(String.valueOf(minValue));
                } else {
                    Log.e("in valueChanged", "else");
                }
            }
        });
    }