I was customized android default seek bar to show in vertically and using in fragments go get some feedback from user.
After first launch it is showing properly. But Once I submitted my first feedback I am relaunching the activity with fragments which contains my customized Vertical Seek Bar.
At that time thumb in Vertical Seekbar is coming to "0" but progress is not coming to "0" and showing some progress. :(
Below i am adding my Vertical Seek bar code please suggest me if i missed any thing .
public class VerticalSeekBar extends SeekBar {
private int mLastProgress = 0;
private OnSeekBarChangeListener mOnChangeListener;
public VerticalSeekBar(Context context) {
super(context);
}
public VerticalSeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
public synchronized int getMaximum() {
return getMax();
}
#Override
protected void onDraw(Canvas c) {
c.rotate(-90);
c.translate(-getHeight(), 0);
super.onDraw(c);
}
#Override
protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(heightMeasureSpec, widthMeasureSpec);
setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
}
#Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(h, w, oldh, oldw);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (!isEnabled()) {
return false;
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (mOnChangeListener != null) {
mOnChangeListener.onStartTrackingTouch(this);
}
setPressed(true);
setSelected(true);
break;
case MotionEvent.ACTION_MOVE:
super.onTouchEvent(event);
int progress = getMax() - (int) (getMax() * event.getY() / getHeight());
// Ensure progress stays within boundaries
if (progress < 0) {
progress = 0;
}
if (progress > getMax()) {
progress = getMax();
}
setProgress(progress); // Draw progress
if (progress != mLastProgress) {
// Only enact listener if the progress has actually changed
mLastProgress = progress;
if (mOnChangeListener != null) {
mOnChangeListener.onProgressChanged(this, progress, true);
}
}
onSizeChanged(getWidth(), getHeight(), 0, 0);
setPressed(true);
setSelected(true);
break;
case MotionEvent.ACTION_UP:
if (mOnChangeListener != null) {
mOnChangeListener.onStopTrackingTouch(this);
}
setPressed(false);
setSelected(false);
break;
case MotionEvent.ACTION_CANCEL:
super.onTouchEvent(event);
setPressed(false);
setSelected(false);
break;
}
return true;
}
public synchronized void setMaximum(int maximum) {
setMax(maximum);
}
#Override
public void setOnSeekBarChangeListener(OnSeekBarChangeListener onChangeListener) {
this.mOnChangeListener = onChangeListener;
}
public synchronized void setProgressAndThumb(int progress) {
setProgress(progress);
onSizeChanged(getWidth(), getHeight(), 0, 0);
if (progress != mLastProgress) {
mLastProgress = progress;
if (mOnChangeListener != null) {
mOnChangeListener.onProgressChanged(this, progress, true);
}
}
}
public synchronized void setMyProgress(int progress) {
setProgress(progress);
}
}
Finally i got the solution for my problem.
In .xml while creating vertical seekBar I used android:progressDrawable="#drawable/seek" but in seek.xml i forgot to declare titles as
item android:id="#android:id/secondaryProgress",
item android:id="#android:id/background" and
item android:id="#android:id/progress".
Related
I have one Vertical Seekbar in my android project. Vertical Seekbar is initialized in onCreate Method:
verticalSeekBar1 = (VerticalSeekBar) findViewById(R.id.verticalSeekbar1);
verticalSeekBar1.setOnSeekBarChangeListener(Listener1);
And I have to implement OnSeekBarChangeListener interface as shown below
private SeekBar.OnSeekBarChangeListener Listener1 = new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar arg0, int seekBarLevel, boolean changedByUser) {
verticalSeekBar1.setProgress(seekBarLevel);
Log.e(TAG, "onProgressChanged: UserChnagedSeekbar :: "+changedByUser);
if (changedBySpinner){
mSpinner.setSelection(0);
}
}
#Override
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
#Override
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
};
changedByUser is always return false. Whether I changed Seekbar or not. Please help me to get out of this problem. Thank you in advance.
My Custom Vertical Seekbar Class code is
public class VerticalSeekBar extends SeekBar {
private Context mContext;
public VerticalSeekBar(Context context) {
super(context);
init(context);
}
public VerticalSeekBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
public VerticalSeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context){
mContext=context;
}
protected void onSizeChanged(int w, int h, int oldWidth, int oldHeight) {
super.onSizeChanged(h, w, oldh, oldw);
}
#Override
protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(heightMeasureSpec, widthMeasureSpec);
setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
}
protected void onDraw(Canvas c) {
Paint paint=new Paint();
paint.setColor(Color.DKGRAY);
paint.setStrokeWidth(2.0f);
Rect rect=new Rect(0,0,getMeasuredWidth(),getMeasuredHeight());
//c.drawRect(rect,paint);
int vWidth=getMeasuredWidth();
int vHeight=getMeasuredHeight();
float startPointY=83;
float middlePointY = (vHeight)/2;
float lastPointY=vHeight-83;
paint.setColor(Color.DKGRAY);
c.drawLine(rect.left,startPointY,rect.centerX(),startPointY,paint);
c.drawLine(rect.left,middlePointY,rect.centerX(),middlePointY,paint);
c.drawLine(rect.left,lastPointY,rect.centerX(),lastPointY,paint);
paint.setColor(Color.DKGRAY);
float x=((vHeight/2)-startPointY)/3;
float f10dbPoint=x+startPointY;
float f5dbPoint=(2*x)+startPointY;
float fm5dbPoint=(vHeight/2)+x;
float fm10dbPoint=(vHeight/2)+(2*x);
float startX=rect.left+32;
c.drawLine(startX,f10dbPoint,rect.centerX(),f10dbPoint,paint);
c.drawLine(startX,f5dbPoint,rect.centerX(),f5dbPoint,paint);
c.drawLine(startX,fm5dbPoint,rect.centerX(),fm5dbPoint,paint);
c.drawLine(startX,fm10dbPoint,rect.centerX(),fm10dbPoint,paint);
c.rotate(-90);
c.translate(-getHeight(),0);
super.onDraw(c);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (!isEnabled()) {
return false;
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
int i=0;
i=getMax() - (int) (getMax() * event.getY() / getHeight());
setProgress(i);
Log.i("Progress",getProgress()+"");
onSizeChanged(getWidth(), getHeight(), 0, 0);
break;
case MotionEvent.ACTION_CANCEL:
break;
}
return true;
}
public void updateThumb(){
onSizeChanged(getWidth(), getHeight(), 0, 0);
}
}
I didn't got the answer that why it's returning value false. But I got the alternate solution to know weather user changed seekbar value or u have changed seekbar value coding. I was changing seekbar value using depending on spinner value. So take variable
boolean changedBySpinner=false;
Inside Spinner ItemClickListener make it true
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
changedBySpinner=true;
}
});
set touchClickListener on the seekbar (vertical seekbar)
verticalSeekBar1.setOnTouchListener((View.OnTouchListener) this);
Inside onTouchMethod make that variable true
changedBySpinner=false;
And inside seekbarChangeListener add below code
private SeekBar.OnSeekBarChangeListener Listener1 = new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar arg0, int seekBarLevel, boolean changedByUser) {
if (!changedBySpinner){
mSpinner.setSelection(0);
}
}
#Override
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
#Override
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
};
Perform your action which wanted to, inside if block. Like I am setting spinner value to 0th element.
Well, this is just an alternate solution. If you are facing the same problem you can use it.
I write a RecyclerViewRefresh follow SwipeRefreshLayout.class. When I pull the view until it doesn't move and then release,the view reset to the original. The issue is that view should trigger Timer and then that Timer reset the view. I couldn't find the reason.
Please tell me why offsetTopAndBottom() can make the view automatically back to the original place. Thanks.
I use setY() to solve this problem. But I also want to know why. And I read the offsetTopAndBottom()'s source,also can not find any clue.
RecyclerViewRefresh's code:
public class RecyclerViewRefresh extends LinearLayout {
private static final String LOG_TAG=RecyclerViewRefresh.class.getSimpleName();
private static final int INVALID_POINTER=-1;
//Default offset in dips from the top of the view to where the progress
//spinner should stop
private static final int DEFAULT_CIRCLE_TARGET=64;
private static final float DRAG_RATE=.5f;
private View headerView,footerView,thisView;
private View mTarget; //the target of the gesture
private ImageView arrowIv;
private TextView refreshTv;
private ProgressBar progressBar;
private OnPullToRefresh refreshListener=null;
private OnDragToLoad loadListener=null;
float startY=0;
private int headerHeight=0;
private boolean mReturningToStart;
private boolean mRefreshing=false;
private boolean mNestedScrollInProgress;
private int mCurrentTargetOffsetTop;
protected int mOriginalOffsetTop;
private boolean mIsBeingDragged;
private int mActivePointerId=INVALID_POINTER;
private float mInitailDownY;
private int mTouchSlop;
private float mTotalDragDistance=-1;
private float mInitialMotionY;
private float mSpinnerFinalOffset;
private boolean updateHeader=true;
private Handler handler=new Handler();
private Timer timer;
public RecyclerViewRefresh(Context context) {
super(context);
initView(context);
}
public RecyclerViewRefresh(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
}
public RecyclerViewRefresh(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context);
}
private void initView(Context context)
{
thisView=this;
mTouchSlop= ViewConfiguration.get(context).getScaledTouchSlop();
headerView=LayoutInflater.from(context).inflate(R.layout.header_layout,null);
footerView=LayoutInflater.from(context).inflate(R.layout.header_layout,null);
measureView(headerView);
arrowIv=(ImageView)headerView.findViewById(R.id.arrow);
refreshTv=(TextView)headerView.findViewById(R.id.tip);
progressBar=(ProgressBar)headerView.findViewById(R.id.progress);
headerHeight=headerView.getMeasuredHeight();
LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
headerView.getMeasuredHeight());
this.addView(headerView,lp);
setTopHeader(headerHeight);
final DisplayMetrics metrics=getResources().getDisplayMetrics();
mSpinnerFinalOffset=DEFAULT_CIRCLE_TARGET*metrics.density;
mTotalDragDistance=mSpinnerFinalOffset;
}
/**
* 通知父布局,占用的宽,高;
*
* #param view
*/
private void measureView(View view) {
ViewGroup.LayoutParams p = view.getLayoutParams();
if (p == null) {
p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
}
int width = ViewGroup.getChildMeasureSpec(0, 0, p.width);
int height;
int tempHeight = p.height;
if (tempHeight > 0) {
height = MeasureSpec.makeMeasureSpec(tempHeight,
MeasureSpec.EXACTLY);
} else {
height = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
}
view.measure(width, height);
}
private void setTopHeader(int height)
{
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB)
{
this.setY(-height);
}else{
LayoutParams lp=new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,height);
lp.topMargin=-height;
this.setLayoutParams(lp);
}
headerView.invalidate();
}
/**
* Set the listener to be notified when a refresh is triggered via the
* pull gesture.
* #param listener
*/
public void setOnPullToRefresh(OnPullToRefresh listener)
{
this.refreshListener=listener;
}
/**
* Set the listener to be notified when a load is triggered via the
* drag gesture
* #param listener
*/
public void setOnDragToLoad(OnDragToLoad listener)
{
this.loadListener=listener;
}
private void ensureTarget(){
if(mTarget==null){
for(int i=0;i<getChildCount();i++)
{
View child=getChildAt(i);
if(child instanceof RecyclerView)
{
mTarget=child;
break;
}
}
}
}
/**
* #return Whether it is possible for the child view of this layout to
* scroll up.Override this if the child view is a custom view.
*/
public boolean canChildScrollUp(){
if(mTarget==null)
{
ensureTarget();
}
if(Build.VERSION.SDK_INT<14)
{
if(mTarget instanceof AbsListView)
{
final AbsListView absListView=(AbsListView)mTarget;
return absListView.getChildCount()>0
&&(absListView.getFirstVisiblePosition()>0
||absListView.getChildAt(0).getTop()<absListView.getPaddingTop());
}else{
return ViewCompat.canScrollVertically(mTarget,-1)|| mTarget.getScrollY()>0;
}
}else{
return ViewCompat.canScrollVertically(mTarget,-1);
}
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
ensureTarget();
final int action=MotionEventCompat.getActionMasked(ev);
if(mReturningToStart && action == MotionEvent.ACTION_DOWN){
mReturningToStart = false;
}
if(!isEnabled() || mReturningToStart || canChildScrollUp()
||mRefreshing || mNestedScrollInProgress){
return false;
}
switch (action){
case MotionEvent.ACTION_DOWN:
setTargetOffsetTopAndBottom(mOriginalOffsetTop-headerView.getTop(),true);
mActivePointerId=MotionEventCompat.getPointerId(ev,0);
mIsBeingDragged=false;
final float initialDownY=getMotionEventY(ev,mActivePointerId);
if(initialDownY==-1){
return false;
}
mInitailDownY=initialDownY;
updateHeader=true;
break;
case MotionEvent.ACTION_MOVE:
if(mActivePointerId==INVALID_POINTER){
Log.e(LOG_TAG, "Got ACTION_MOVE event but don't have an active pointer id.");
return false;
}
final float y=getMotionEventY(ev,mActivePointerId);
if(y==-1){
return false;
}
final float yDiff=y-mInitailDownY;
if(yDiff>mTouchSlop && !mIsBeingDragged){
mInitialMotionY=mInitailDownY+mTouchSlop;
mIsBeingDragged=true;
}
break;
case MotionEventCompat.ACTION_POINTER_UP:
onSecondaryPointerUp(ev);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
mIsBeingDragged=false;
mActivePointerId=INVALID_POINTER;
break;
}
return mIsBeingDragged;
}
private float getMotionEventY(MotionEvent ev,int activePointerId){
final int index=MotionEventCompat.findPointerIndex(ev,activePointerId);
if(index<0){
return -1;
}
return MotionEventCompat.getY(ev,index);
}
private void setTargetOffsetTopAndBottom(int offset,boolean requiresUpdate){
if(this.getTop()<headerHeight+5)
{
this.offsetTopAndBottom(offset);
mCurrentTargetOffsetTop=this.getTop();
if(requiresUpdate && Build.VERSION.SDK_INT<11){
invalidate();
}
if(this.getTop()>headerHeight)
{
if(updateHeader){
updateHeader=false;
refreshTv.setText(getResources().getText(R.string.releasetorefresh));
RotateAnimation animation=new RotateAnimation(0,180,
Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
animation.setDuration(800);
animation.setFillAfter(true);
arrowIv.startAnimation(animation);
}
}
}
}
private void onSecondaryPointerUp(MotionEvent ev){
final int pointerIndex=MotionEventCompat.getActionIndex(ev);
final int pointerId=MotionEventCompat.getPointerId(ev,pointerIndex);
if(pointerId==mActivePointerId){
//This was our active pointer going up. Choose a new
//active pointer and adjust accordingly.
final int newPointerIndex=pointerIndex==0?1:0;
mActivePointerId=MotionEventCompat.getPointerId(ev,newPointerIndex);
}
}
#Override
public boolean onTouchEvent(MotionEvent event)
{
final int action=MotionEventCompat.getActionMasked(event);
int pointerIndex=-1;
if(mReturningToStart&&action==MotionEvent.ACTION_DOWN){
mReturningToStart=false;
}
if(!isEnabled() || mReturningToStart
|| canChildScrollUp() || mNestedScrollInProgress){
//Fail fast if we're not in a state where a swipe is possible
return false;
}
switch(action){
case MotionEvent.ACTION_DOWN:
mActivePointerId=MotionEventCompat.getPointerId(event,0);
mIsBeingDragged=false;
break;
case MotionEvent.ACTION_MOVE:{
pointerIndex=MotionEventCompat.findPointerIndex(event,mActivePointerId);
if(pointerIndex<0){
Log.e(LOG_TAG, "Got ACTION_MOVE event but have an invalid active pointer id.");
return false;
}
final float y=MotionEventCompat.getY(event,pointerIndex);
final float overscrollTop=(y-mInitialMotionY)*DRAG_RATE;
if(mIsBeingDragged){
if(overscrollTop>0){
moveSpinner(overscrollTop);
}else{
return false;
}
}
break;
}
case MotionEventCompat.ACTION_POINTER_DOWN:{
pointerIndex=MotionEventCompat.getActionIndex(event);
if(pointerIndex<0){
Log.e(LOG_TAG, "Got ACTION_POINTER_DOWN event but have an invalid action index.");
return false;
}
mActivePointerId=MotionEventCompat.getPointerId(event,pointerIndex);
break;
}
case MotionEvent.ACTION_POINTER_UP:
onSecondaryPointerUp(event);
break;
case MotionEvent.ACTION_UP:{
pointerIndex=MotionEventCompat.findPointerIndex(event,mActivePointerId);
if(pointerIndex<0){
Log.e(LOG_TAG, "Got ACTION_UP event but don't have an active pointer id.");
return false;
}
final float y=MotionEventCompat.getY(event,pointerIndex);
mIsBeingDragged=false;
finishSpinner();
mActivePointerId=INVALID_POINTER;
return false;
}
case MotionEvent.ACTION_CANCEL:
return false;
}
return true;
}
private void moveSpinner(float overscrollTop){
float originalDragPercent=overscrollTop/mTotalDragDistance;
float dragPercent=Math.min(1f,Math.abs(originalDragPercent));
float adjustedPercent=(float)Math.max(dragPercent-.4,0)*5/3;
float extraOS=Math.abs(overscrollTop)-mTotalDragDistance;
float slingshotDist=mSpinnerFinalOffset;
float tensionSlingshotPercent=Math.max(0,Math.min(extraOS,slingshotDist*2)/slingshotDist);
float tensionPercent=(float)((tensionSlingshotPercent/4)-Math.pow(
(tensionSlingshotPercent/4),2))*2f;
float extraMove=(slingshotDist)*tensionPercent*2;
int targetY=mOriginalOffsetTop+(int)((slingshotDist*dragPercent)+extraMove);
setTargetOffsetTopAndBottom(targetY-mCurrentTargetOffsetTop,true);
}
private void finishSpinner(){
if(this.getTop()>headerHeight){
setRefreshing(true,true);
}else{
//cancel refresh
mRefreshing=false;
animateOffsetToStartPosition();
}
}
private void setRefreshing(boolean refreshing,final boolean notify)
{
if(mRefreshing!=refreshing){
ensureTarget();
mRefreshing=refreshing;
if(mRefreshing){
refreshListener.onRefresh();
arrowIv.setVisibility(View.GONE);
progressBar.setVisibility(View.VISIBLE);
}else{
arrowIv.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.GONE);
animateOffsetToStartPosition();
}
}
}
public void setRefreshing(boolean refreshing){
if(!refreshing){
setRefreshing(refreshing,false);
}
}
private void animateOffsetToStartPosition(){
refreshTv.setText(getResources().getText(R.string.pulltorefresh));
arrowIv.clearAnimation();
Log.d(LOG_TAG,"getTop="+this.getTop()+" timer="+((timer==null)?"null":"notnumm"));
if(timer==null&&this.getTop()>0)
{
timer=new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
#Override
public void run() {
if(thisView.getTop()>0)
{
thisView.offsetTopAndBottom(-1);
mCurrentTargetOffsetTop = headerView.getTop();
if ( Build.VERSION.SDK_INT < 11) {
invalidate();
}
}else{
Log.d(LOG_TAG,"cancel");
timer.cancel();
timer=null;
}
}
});
}
},10,10);
}
}
/**
* Classes that wish to be notified when the pull gesture correctly
* triggers a refresh should implement this interface.
*/
public interface OnPullToRefresh{
public void onRefresh();
}
/**
* Classes that wish to be notified when the drag gesture correctly
* triggers a load should implement this interface.
*/
public interface OnDragToLoad{
public void onLoad();
}}
offsetTopAndBottom(offset) will add mTop and mBottom of View by offset.
private void animateOffsetToStartPosition(){
refreshTv.setText(getResources().getText(R.string.pulltorefresh));
arrowIv.clearAnimation();
Log.d(LOG_TAG,"getTop="+this.getTop()+" timer="+((timer==null)?"null":"notnumm"));
if(timer==null&&this.getTop()>0)
{
timer=new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
#Override
public void run() {
if(thisView.getTop()>0)
{
//this line says that if top of thisView is not 0,add mtop and mBottom of thisView by -1
//this timer will change the mTop to 0.thisView will be back to original place if mTop == 0.
thisView.offsetTopAndBottom(-1);
mCurrentTargetOffsetTop = headerView.getTop();
if ( Build.VERSION.SDK_INT < 11) {
invalidate();
}
}else{
Log.d(LOG_TAG,"cancel");
timer.cancel();
timer=null;
}
}
});
}
},10,10);
}
}
setY() calls setTranslationY(), which makes two calls to invalidateViewProperty(boolean invalidateParent, boolean forceRedraw).
In setTranslationY(), when it calls invalidateViewProperty, it is passing forceRedraw as true, which redraws the view and puts it back to the original state.
I want Vertical seekBar like below image (for android 4.O+)). its in Google Play Music App.
i have tried below way: but i can't set hight & width
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:rotation="270"
/>
i want like below:
right now i have used this Stack Answer but its too hard to manage multiple vertical seekbar.
i am looking for better way than this.
EDIT:
i have used below code from iDroid Explorer's answer try to display vertical seek bar:
private void setupEqualizerFxAndUI() {
for (short i = 0; i < 5; i++) {
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
layoutParams.weight = 3;
LinearLayout row = new LinearLayout(this);
row.setOrientation(LinearLayout.VERTICAL);
row.setLayoutParams(layoutParams);
TextView minDbTextView = new TextView(this);
minDbTextView.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
minDbTextView.setText(i + " dB");
TextView maxDbTextView = new TextView(this);
maxDbTextView.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
maxDbTextView.setText(i + " dB");
LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams1.weight = 2;
SeekBar bar = new SeekBar(this);
bar.setLayoutParams(layoutParams1);
bar.setMax(100);
bar.setProgress(20);
bar.setRotation(270);
bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
}
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
row.addView(minDbTextView);
row.addView(bar);
row.addView(maxDbTextView);
mLinearLayout.addView(row);
}
}
but it looks like below: indicator is not showing.
if i use only one seekBar then its look like below:
it same like use : android:rotation="270" in layout file.
iDroid Explorer's code is great and helped me a lot getting the job done, however it is missing some parts. Here's my hacked version, it works great for me and I hope it does for you too.
public class VerticalSeekBar extends SeekBar {
protected OnSeekBarChangeListener changeListener;
protected int x, y, z, w;
public VerticalSeekBar(Context context) {
super(context);
}
public VerticalSeekBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public VerticalSeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected synchronized void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(h, w, oldh, oldw);
this.x = w;
this.y = h;
this.z = oldw;
this.w = oldh;
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(heightMeasureSpec, widthMeasureSpec);
setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
}
#Override
protected void onDraw(Canvas c) {
c.rotate(-90);
c.translate(-getHeight(), 0);
super.onDraw(c);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (!isEnabled()) {
return false;
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
setSelected(true);
setPressed(true);
if (changeListener != null) changeListener.onStartTrackingTouch(this);
break;
case MotionEvent.ACTION_UP:
setSelected(false);
setPressed(false);
if (changeListener != null) changeListener.onStopTrackingTouch(this);
break;
case MotionEvent.ACTION_MOVE:
int progress = getMax() - (int) (getMax() * event.getY() / getHeight());
setProgress(progress);
onSizeChanged(getWidth(), getHeight(), 0, 0);
if (changeListener != null) changeListener.onProgressChanged(this, progress, true);
break;
case MotionEvent.ACTION_CANCEL:
break;
}
return true;
}
#Override
public synchronized void setOnSeekBarChangeListener(OnSeekBarChangeListener listener) {
changeListener = listener;
}
#Override
public synchronized void setProgress(int progress) {
if (progress >= 0)
super.setProgress(progress);
else
super.setProgress(0);
onSizeChanged(x, y, z, w);
if (changeListener != null) changeListener.onProgressChanged(this, progress, false);
}
}
Have you check the android Sample code of AudioFXDemo? If not then please check it and see whether it is helpful to you or not.
There is Equalizer which is same as Google Music app have.
Check below code in that Demo Example:
private void setupEqualizerFxAndUI() {
// Create the Equalizer object (an AudioEffect subclass) and attach it to our media player,
// with a default priority (0).
try {
System.out.println("setupEqualizerFxAndUI eEqualizer is: "+mEqualizer);
System.out.println("setupEqualizerFxAndUI mIRemoteService: "+mIRemoteService);
mEqualizer = new Equalizer(0,mIRemoteService.getAudioSessionId());
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (UnsupportedOperationException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
} catch (RuntimeException e) {
e.printStackTrace();
} // Error in this line
if(onOffBtn.isChecked()){
mEqualizer.setEnabled(true);
}
short bands = 5 ;
//System.out.println("bands are: "+bands);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
params.weight = 1;
LinearLayout newOne = new LinearLayout(this);
newOne.setLayoutParams(params);
newOne.setOrientation(LinearLayout.HORIZONTAL);
final short minEQLevel = mEqualizer.getBandLevelRange()[0];
final short maxEQLevel = mEqualizer.getBandLevelRange()[1];
//System.out.println("Minimum value::: "+minEQLevel);
//System.out.println("Maximum value::: "+maxEQLevel);
//VerticalSeekBar[] bar = new VerticalSeekBar[5];
for (short i = 0; i < bands; i++) {
final short band = i;
/* TextView freqTextView = new TextView(this);
freqTextView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
freqTextView.setGravity(Gravity.CENTER_HORIZONTAL);
freqTextView.setText((mEqualizer.getCenterFreq(band) / 1000) + " Hz");
newOne.addView(freqTextView);*/
LinearLayout row = new LinearLayout(this);
row.setOrientation(LinearLayout.VERTICAL);
TextView minDbTextView = new TextView(this);
minDbTextView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT));
minDbTextView.setText((minEQLevel / 100) + " dB");
minDbTextView.setTextColor(0xff000000);
TextView maxDbTextView = new TextView(this);
maxDbTextView.setLayoutParams(new ViewGroup.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
maxDbTextView.setText((maxEQLevel / 100) + " dB");
maxDbTextView.setTextColor(0xff000000);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 300);
layoutParams.setMargins(0, 10, 0, 10);
//--------------------------
// setClassicTone(maxEQLevel, minEQLevel);
VerticalSeekBar bar = new VerticalSeekBar(this);
bar = new VerticalSeekBar(this);
bar.setLayoutParams(layoutParams);
bar.setMax(maxEQLevel - minEQLevel);
bar.setProgress(mEqualizer.getBandLevel(band));
//bar.setMax(3000);
//bar.setProgress(mEqualizer.getBandLevel(band)+1500);
bar.setPadding(0, 10, 0, 10);
//bar.setProgressDrawable(R.drawable.scrubber_progress_horizontal_holo_light);
bar.setProgressDrawable(getResources().getDrawable(R.drawable.scrubber_progress_horizontal_holo_light));
bar.setThumb(getResources().getDrawable(R.drawable.scrubber_control_selector_holo));
//System.out.println("Progress:::"+(mEqualizer.getBandLevel(band)));
//bar[i].setProgress((maxEQLevel-minEQLevel)/2);
mEqualizer.setBandLevel(band,(short) ((maxEQLevel-minEQLevel)));
//System.out.println("Presets are: "+mEqualizer.getNumberOfPresets()+" And name is: "+mEqualizer.getPresetName(i));
//mEqualizer.setBandLevel(band, level)
bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
System.out.println("Seek bar change ");
presetsSpinner.setSelection(0);
mEqualizer.setBandLevel(band, (short) (progress + minEQLevel));
}
public void onStartTrackingTouch(SeekBar seekBar) {}
public void onStopTrackingTouch(SeekBar seekBar) {}
});
row.addView(maxDbTextView);
row.addView(bar);
row.addView(minDbTextView);
newOne.addView(row, params);
}
equalizerLayout.addView(newOne);
}
And the VerticalSeekBar is the class as below:
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.SeekBar;
public class VerticalSeekBar extends SeekBar {
public VerticalSeekBar(Context context) {
super(context);
}
public VerticalSeekBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public VerticalSeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(h, w, oldh, oldw);
}
#Override
protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(heightMeasureSpec, widthMeasureSpec);
setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
}
protected void onDraw(Canvas c) {
c.rotate(-90);
c.translate(-getHeight(), 0);
super.onDraw(c);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (!isEnabled()) {
return false;
}
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;
}
return true;
}
}
Hope above code help you. If not then let me know.
Enjoy Coding... :)
Apparently, equalizer is not part of Google Play Music, but rather a separate application called MusicFX (com.android.musicfx):
I/ActivityManager(536): START u0 {act=android.media.action.DISPLAY_AUDIO_EFFECT_CONTROL_PANEL cmp=com.android.musicfx/.Compatibility$Redirector (has extras)} from pid 4800
I/ActivityManager(536): START u0 {act=android.media.action.DISPLAY_AUDIO_EFFECT_CONTROL_PANEL flg=0x2000000 cmp=com.android.musicfx/.ActivityMusic (has extras)} from pid 4867
I/ActivityManager(536): Displayed com.android.musicfx/.ActivityMusic: +293ms (total +315ms)
The source code of MusicFX is available from Android repository.
Hope this helps.
I'm using a simple Vertical SeekBar that is working fine if I initiate it on the onCreate function. But if I initiate the SeekBar on a different thread the thumb position does not work as expected.
The code I'm using to initiate the SeekBar:
public class MainActivity extends Activity {
AbsoluteLayout mainLayout;
VerticalSeekBar seekBar;
Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
mainLayout=(AbsoluteLayout)findViewById(R.id.main_layout_id);
Thread thread1 =new Thread(runnable1);
thread1.start();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private Runnable runnable1 = new Runnable(){
#Override
public void run() {
seekBar=new VerticalSeekBar(context);
loadMainScreen.sendEmptyMessage(0);
}
};
Handler loadMainScreen = new Handler() {
#Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
AbsoluteLayout.LayoutParams parameters = new AbsoluteLayout.LayoutParams(40,200,150,30);
mainLayout.addView(seekBar,parameters);
}
};
}
I do not think the problem is on the SeekBar but I will show the code anyway.
public class VerticalSeekBar extends SeekBar implements OnSeekBarChangeListener {
public VerticalSeekBar(Context context) {
super(context);
this.setOnSeekBarChangeListener(this);
}
public VerticalSeekBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public VerticalSeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(h, w, oldh, oldw);
}
#Override
protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(heightMeasureSpec, widthMeasureSpec);
setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
}
protected void onDraw(Canvas c) {
c.rotate(-90);
c.translate(-getHeight(), 0);
super.onDraw(c);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (!isEnabled()) {
return false;
}
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;
}
return true;
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
}
I try changing the Context I'm passing to the variable context to getApplicationContext() and getBaseContext() but it did not work.
If I put the initialization on a runOnUiThread it crashes because the SeekBar is not initialized when I try to add to the layout. If I put both the initialization and the addView on a runOnUiThread it works but that is not what I want.
I was wondering if there is something I could do regarding the Context to make it work. Any different ideas are welcome.
Thanks in advance
I have a seekbar that I want to only allow changing with the thumb/handle portion. If the user taps anywhere else, I'd like that tap to be ignored.
Is there an inherant property to do this? If not, I already know I can set an onTouch listener and return true to "disable" it, but is there a way to detect when the thumb is tapped so that I can tell onTouch to return false in that case?
I assume you want to change thumb position via code, which is user restricted.so for that try this.may be could help you.
myseekBar.setOnSeekBarChangeListener( new OnSeekBarChangeListener() {
int originalProgress;
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
originalProgress = seekBar.getProgress();
}
#Override
public void onProgressChanged(SeekBar seekBar, int arg1, boolean fromUser) {
if( fromUser == true){
seekBar.setProgress(originalProgress);
}
}
});
I had the same issue , I fixed using a simple logic to compare the new Progress X OldProgress.
This is my code to a VerticalSlider that just allow change value when user click over Thumb.
This is the trick:
int tempProgress = (getMax() - (int) (getMax() * event.getY() / getHeight()));
int diff = Math.abs(tempProgress - progress);
if ( diff > 7 && !isMoving) return true;
isMoving = true;
This is all component source:
package android.widget;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
public class VerticalSeekBar extends SeekBar {
private OnSeekBarChangeListener onChangeListener;
private int progress = 0;
private Drawable mThumb;
private boolean isMoving;
public VerticalSeekBar(Context context) {
super(context);
}
public VerticalSeekBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public VerticalSeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(h, w, oldh, oldw);
}
#Override
protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(heightMeasureSpec, widthMeasureSpec);
setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
}
protected void onDraw(Canvas c) {
c.rotate(-90);
c.translate(-getHeight(), 0);
super.onDraw(c);
}
#Override
public void setOnSeekBarChangeListener(OnSeekBarChangeListener onChangeListener){
this.onChangeListener = onChangeListener;
}
public void setThumb(Drawable thumb) {
if (thumb != null) {
thumb.setCallback(this);
}
super.setThumb(thumb);
mThumb = thumb;
mThumb.setCallback(this);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (!isEnabled()) {
return false;
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if ( onChangeListener!= null)onChangeListener.onStartTrackingTouch(this);
setPressed(true);
setSelected(true);
break;
case MotionEvent.ACTION_MOVE:
int tempProgress = (getMax() - (int) (getMax() * event.getY() / getHeight()));
int diff = Math.abs(tempProgress - progress);
if ( diff > 7 && !isMoving) return true;
isMoving = true;
progress = getMax() - (int) (getMax() * event.getY() / getHeight());
if(progress < 18) {progress = 18;}
if(progress > getMax()) {progress = getMax();}
setProgressAndThumb(progress);
setPressed(true);
setSelected(true);
break;
case MotionEvent.ACTION_UP:
isMoving = false;
if ( onChangeListener!= null)onChangeListener.onStopTrackingTouch(this);
setPressed(false);
setSelected(false);
break;
case MotionEvent.ACTION_CANCEL:
isMoving = false;
super.onTouchEvent(event);
setPressed(false);
setSelected(false);
break;
}
return true;
}
public synchronized void setProgressAndThumb(int progress) {
setProgress(progress);
onSizeChanged(getWidth(), getHeight() , 0, 0);
if ( onChangeListener!= null) onChangeListener.onProgressChanged(this, progress, true);
}
#Override
public synchronized void setProgress(int progress) {
this.progress =progress;
super.setProgress(progress);
if ( onChangeListener !=null) onChangeListener.onProgressChanged(this, progress, true);
}
public synchronized void setMaximum(int maximum) {
setMax(maximum);
}
public synchronized int getMaximum() {
return getMax();
}
}
Regards!
// I solved this problem.
private int mOriginProgress;
private boolean mIsDrag;
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
mIsTouched = true;
mOriginProgress = seekBar.getProgress();
}
#Override
public void onProgressChanged(SeekBar seekBar, int p, boolean fromUser) {
if (mOriginProgress == p) {
return;
}
if (mIsDrag) {
// do something
} else {
if (Math.abs(mOriginProgress - p) > 5) {
seekBar.setProgress(mOriginProgress);
} else {
mIsDrag = true;
}
}
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
mIsDrag = false;
}