0

can any one help me to create balow image cutomize seek bar i have already go throught SeekBar with custom thumb and segmented text and SeekBar Thumb position issue

but i am not success to create my custome seekbar pls help me

Images is here

5
  • Can you post your code? What is the problem? Are there errors? Can you post them too and a logcat. Without them we have no idea what is wrong. Commented Sep 2, 2013 at 13:36
  • thanx RossC, i want to create seekbar as above , how can i create this? i am not getting any error so which helps you, but i am not getting required output , can you have some sample code or link which helps me than please provide thanx again.. Commented Sep 3, 2013 at 5:32
  • Ok, I can't really do anything other than link to 'SeekBar' and let you run with that. If you haven't tried anything, what can I do? stackoverflow.com/questions/16163215/android-styling-seek-bar Commented Sep 3, 2013 at 10:07
  • no problem but i already go throught it. thanx :-) Commented Sep 3, 2013 at 13:13
  • Great, glad to hear it! :) Commented Sep 3, 2013 at 13:49

1 Answer 1

10

Use Library From This Link: This link and put into your project,

Now Create A Seekbar Like This Class:

package com.tokaracamara.android.verticalslidevar;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
public class DemoSeek extends AbsVerticalSeekBar {
    public interface OnSeekBarChangeListener {
        void onProgressChanged(DemoSeek seekBar, int progress, boolean fromUser);

    void onStartTrackingTouch(DemoSeek seekBar);

    void onStopTrackingTouch(DemoSeek seekBar);
}

private OnSeekBarChangeListener mOnSeekBarChangeListener;

private Drawable progressDrawable;
private Rect barBounds, labelTextRect;
private Bitmap labelBackground;
private Point labelPos;
private Paint labelTextPaint, labelBackgroundPaint;

int viewWidth, barHeight, labelOffset;
//    private int thumbX;
float progressPosX;
private String expression;

public DemoSeek(Context context) {
    super(context);
    // Log.i("Seekbar", "DemoSeek");
    progressDrawable = getProgressDrawable();

    // labelBackground = BitmapFactory.decodeResource(getResources(),
    // R.drawable.thumb_marker);

    labelBackground = drawableToBitmap(mThumb);

    labelTextPaint = new Paint();
    labelTextPaint.setColor(Color.WHITE);
    labelTextPaint.setTypeface(Typeface.DEFAULT_BOLD);
    labelTextPaint.setAntiAlias(true);
    labelTextPaint.setDither(true);
    labelTextPaint.setTextSize(13f);

    labelBackgroundPaint = new Paint();

    barBounds = new Rect();
    labelTextRect = new Rect();

    labelPos = new Point();

}

public Bitmap drawableToBitmap(Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    }

    Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
            drawable.getIntrinsicHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);

    return bitmap;
}

@Override
void onProgressRefresh(float scale, boolean fromUser) {
    super.onProgressRefresh(scale, fromUser);

    if (mOnSeekBarChangeListener != null) {
        mOnSeekBarChangeListener.onProgressChanged(this, getProgress(),
                fromUser);
    }
}

public DemoSeek(Context context, AttributeSet attrs) {
    super(context, attrs);

    progressDrawable = getProgressDrawable();

    labelBackground = BitmapFactory.decodeResource(getResources(),
            R.drawable.thumb_with_arrow);

    labelTextPaint = new Paint();
    labelTextPaint.setColor(Color.WHITE);
    labelTextPaint.setTypeface(Typeface.DEFAULT_BOLD);
    labelTextPaint.setAntiAlias(true);
    labelTextPaint.setDither(true);
    labelTextPaint.setTextSize(15f);

    labelBackgroundPaint = new Paint();

    barBounds = new Rect();
    labelTextRect = new Rect();

    labelPos = new Point();
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    // Log.i("Seekbar", "onMeasure");
    if (labelBackground != null) {

        viewWidth = getMeasuredWidth();
        barHeight = getMeasuredHeight() - getPaddingTop()
                - getPaddingBottom();
        setMeasuredDimension(viewWidth + labelBackground.getWidth(),
                barHeight + labelBackground.getHeight() / 2);
    }

}

@Override
protected void onDraw(Canvas canvas) {
    canvas.save();
    if (labelBackground != null) {
        barBounds.left = getPaddingLeft();
        barBounds.top = (int) (labelBackground.getHeight() / 2f);
        barBounds.right = barBounds.left + viewWidth - getPaddingRight()
                - getPaddingLeft();
        barBounds.bottom = barBounds.top + barHeight - getPaddingBottom()
                - getPaddingTop();

        progressPosX = barBounds.top
                + ((float) this.getProgress() / (float) this.getMax())
                * barBounds.height() + getTopPaddingOffset();

        labelPos.y = getBottom() - (int) progressPosX - labelOffset
                + (int) (getProgress() * 0.1f);
        labelPos.x = getPaddingLeft();

        progressDrawable = getProgressDrawable();

        progressDrawable.setBounds(barBounds.left, barBounds.top,
                barBounds.right, getBottom());

        progressDrawable.draw(canvas);

        String pro = getProgress() * multiplier + "";
        if (expression != null) {
            pro = pro.concat(expression);
        }
        labelTextPaint.getTextBounds(pro, 0, pro.length(), labelTextRect);

        canvas.drawBitmap(labelBackground, labelPos.x, labelPos.y,
                labelBackgroundPaint);

        canvas.drawText(pro, labelPos.x + labelBackground.getWidth() / 2
                - labelTextRect.width() / 2 + 15, labelPos.y
                + labelBackground.getHeight() / 2 + labelTextRect.height()
                / 2 - 5, labelTextPaint);

    }
    canvas.restore();
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    invalidate();
    return super.onTouchEvent(event);

}

public void setOnSeekBarChangeListener(
        com.tokaracamara.android.verticalslidevar.DemoSeek.OnSeekBarChangeListener onSeekBarChangeListener) {
    mOnSeekBarChangeListener = (OnSeekBarChangeListener) onSeekBarChangeListener;
}

@Override
void onStartTrackingTouch() {
    if (mOnSeekBarChangeListener != null) {
        mOnSeekBarChangeListener.onStartTrackingTouch(this);
    }
}

@Override
void onStopTrackingTouch() {
    if (mOnSeekBarChangeListener != null) {
        mOnSeekBarChangeListener.onStopTrackingTouch(this);
    }
}


private int multiplier = 1;

public void setMultiplier(int multiplier) {
    this.multiplier = multiplier;
}

public int getMultiplier() {
    return multiplier;
}

public void setExpression(String expression) {
    this.expression = expression.trim();
}

public String getExpression() {
    return expression;
}

}

Create Xml Like This:

<com.tokaracamara.android.verticalslidevar.DemoSeek
            android:id="@+id/v_four_university"
            android:layout_width="@dimen/vs_width"
            android:layout_height="match_parent"
            android:clickable="false"
            android:focusable="false"
            android:longClickable="false"
            android:progress="50"
            android:progressDrawable="@drawable/progress_vertical"
            android:saveEnabled="false"
            android:thumb="@drawable/your thumb" />
Sign up to request clarification or add additional context in comments.

1 Comment

This is working code. by mistake I down voted.when I up vote this says "You last voted on this answer 16 hours ago. Your vote is now locked in unless this answer is edited.". @Kishan can you edit the answer. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.