Skip to content Skip to sidebar Skip to footer

Vertical Seekbar In Android

I am working on a Player and I want to use Vertical Seekbar to control volume level. Can anybody tell me how to align seekbar vertically, If you have some code it would be great.

Solution 1:

Here is a good code to align seek-bar vertically: Vertical Seekbar

Solution 2:

I've made a vertical SeekBar by overriding the onDraw method and flipping it 90 degrees

VerticalSeekBar

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;

publicclassVerticalSeekBarextendsSeekBar {

    publicVerticalSeekBar(Context context) {
        super(context);
    }

    publicVerticalSeekBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    publicVerticalSeekBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    protectedvoidonSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(h, w, oldh, oldw);
    }

    @OverrideprotectedsynchronizedvoidonMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(heightMeasureSpec, widthMeasureSpec);
        setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
    }

/*@Override
    public synchronized void setProgress(int progress)  // it is necessary for calling setProgress on click of a button
    {
        super.setProgress(progress);
        onSizeChanged(getWidth(), getHeight(), 0, 0); 
    }*/protectedvoidonDraw(Canvas c) {
        c.rotate(-90);
        c.translate(-getHeight(), 0);

        super.onDraw(c);
    }

    @OverridepublicbooleanonTouchEvent(MotionEvent event) {
        if (!isEnabled()) {
            returnfalse;
        }

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
            case MotionEvent.ACTION_UP:
                setProgress(getMax() - (int) (getMax() * event.getY() / getHeight()));
                onSizeChanged(getWidth(), getHeight(), 0, 0);
                break;

            case MotionEvent.ACTION_CANCEL:
                break;
        }
        returntrue;
    }
}

Use it in your XML layout:

<yourPackage.name.VerticalSeekBar
  android:id="@+id/seekBar1"
  android:layout_width="wrap_content"
  android:layout_height="200dp"
  />

For API 11 and later, can use seekbar's XML attributes(android:rotation="270") for vertical effect.

<SeekBar
android:id="@+id/seekBar1"android:layout_width="match_parent"android:layout_height="wrap_content"android:rotation="270"/>

Solution 3:

Add these lines to build.gradle.

dependencies {
    compile'com.h6ah4i.android.widget.verticalseekbar:verticalseekbar:0.7.2'
}

Layout XML

<!-- This library requires pair of the VerticalSeekBar and VerticalSeekBarWrapper classes --><com.h6ah4i.android.widget.verticalseekbar.VerticalSeekBarWrapperandroid:layout_width="wrap_content"android:layout_height="150dp"><com.h6ah4i.android.widget.verticalseekbar.VerticalSeekBarandroid:id="@+id/mySeekBar"android:layout_width="0dp"android:layout_height="0dp"android:max="100"android:progress="0"android:splitTrack="false"app:seekBarRotation="CW90" /><!-- Rotation: CW90 or CW270 --></com.h6ah4i.android.widget.verticalseekbar.VerticalSeekBarWrapper>

Java code

publicclassTestVerticalSeekbarextendsAppCompatActivity {
    privateSeekBarmseekbar=null;


    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_vertical_seekbar);

        mseekbar = (SeekBar) findViewById(R.id.mySeekBar);

        mseekbar.setOnSeekBarChangeListener(newSeekBar.OnSeekBarChangeListener() {
            intprogressChanged=0;

            publicvoidonProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                progressChanged = progress;
            }

            publicvoidonStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub
            }

            publicvoidonStopTrackingTouch(SeekBar seekBar) {
                Toast.makeText(getApplicationContext(), "seek bar progress:" + progressChanged,
                        Toast.LENGTH_SHORT).show();
            }
        });
    }

}

Post a Comment for "Vertical Seekbar In Android"