I need to place "start point" in center of seek bar.
How can i do like this?
One possible solution would be to set seekBar. setMax(20) (or android:max="20" in XML), and whenever you use or display the value, multiply it by 10. The SeekBar would then appear to move at least 20 at a time.
xml create a layout and inside the layout add a SeekBar. Specify the height width of SeekBar and the max progress that you want to use set progress to 0. This will create a customized Seekbar inside activity_main.
Well according to android.developers.com, A SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch the thumb and drag left or right to set the current progress level or use the arrow keys. Placing focusable widgets to the left or right of a SeekBar is discouraged.
Android SeekBar is a kind of ProgressBar with draggable thumb. The end user can drag the thum left and right to move the progress of song, file download etc.
I faced the same problem. Thanks to Commonsware to point to right direction. I wrote a class inspired by code.google.com/p/range-seek-bar) to get the solution.
https://github.com/vashisthg/StartPointSeekBar
As Above all said, it can be achieved my implementing our own custom seekbar.
I tried in the following way and it worked out for me.
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:layout_margin="20dp">
<com.example.customseekbar.CustomSeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="50"
android:progressDrawable="@android:color/transparent"
android:id="@+id/customSeekBar"
/>
</RelativeLayout>
MainActivity.java
package com.example.customseekbar;
import com.example.suricustomseekbar.R;
import android.app.Activity;
import android.os.Bundle;
import android.widget.SeekBar;
public class MainActivity extends Activity {
SeekBar mseekBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mseekBar = (SeekBar) findViewById(R.id.customSeekBar);
mseekBar.setProgress(50);
}
}
CustomSeekBar.java
package com.example.customseekbar;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.SeekBar;
public class CustomSeekBar extends SeekBar {
private Rect rect;
private Paint paint ;
private int seekbar_height;
public CustomSeekBar(Context context) {
super(context);
}
public CustomSeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
rect = new Rect();
paint = new Paint();
seekbar_height = 6;
}
public CustomSeekBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected synchronized void onDraw(Canvas canvas) {
rect.set(0 + getThumbOffset(),
(getHeight() / 2) - (seekbar_height/2),
getWidth()- getThumbOffset(),
(getHeight() / 2) + (seekbar_height/2));
paint.setColor(Color.GRAY);
canvas.drawRect(rect, paint);
if (this.getProgress() > 50) {
rect.set(getWidth() / 2,
(getHeight() / 2) - (seekbar_height/2),
getWidth() / 2 + (getWidth() / 100) * (getProgress() - 50),
getHeight() / 2 + (seekbar_height/2));
paint.setColor(Color.CYAN);
canvas.drawRect(rect, paint);
}
if (this.getProgress() < 50) {
rect.set(getWidth() / 2 - ((getWidth() / 100) * (50 - getProgress())),
(getHeight() / 2) - (seekbar_height/2),
getWidth() / 2,
getHeight() / 2 + (seekbar_height/2));
paint.setColor(Color.CYAN);
canvas.drawRect(rect, paint);
}
super.onDraw(canvas);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With