Android – VerticalSeekBar – onStopTrackingTouch() does not work
Everybody! I’m new to Android development and I’m having trouble trying to set a specific position for the thumb of the search bar.
I want to set the progress of the search bar to its initial position when the user releases the thumb
(4 in this case), in order to do this I’m trying the method onStopTrackingTouch, but it doesn’t look like it’s called when the user releases the search bar thumb. Eclipse doesn’t prompt an error, the program works fine, the only problem is that when I release my thumb, it doesn’t return to its initial position.
More information: I’m using a search vertical, could this be the cause of the problem?
This is the activity code:
package renatox.play.bluetoothcommander;
import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.SeekBar;
public class MainActivity extends Activity implements SensorEventListener {
private VerticalSeekBar acelerador;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Sets the Main Activity layout
setContentView(R.layout.activity_main);
Gets the VerticalSeekBar
acelerador = (VerticalSeekBar) findViewById(R.id.acelerador);
acelerador
.setOnSeekBarChangeListener(new VerticalSeekBar.OnSeekBarChangeListener() {
I've tried using SeekBar.OnSeekBarChangeListener() and VerticalSeekBar.OnSeekBarChangeListener()
@Override
public void onStopTrackingTouch(SeekBar arg0) {
acelerador.setProgress(4);
Log.i("Acelerador", "O usuário soltou o acelerador");
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
Log.i("Acelerador", "O usuário pegou o acelerador");
}
Pega os valores do acelerador quando mudam
@Override
public void onProgressChanged(SeekBar arg0, int arg1,
boolean arg2) {
int velocidade = acelerador.getProgress();
Log.i("Acelerador", "Velocidade atual: " + velocidade);
}
});
}
This is the XML layout file:
<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:background="@drawable/fundo"
android:keepScreenOn="true"
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" >
<renatox.play.bluetoothcommander.VerticalSeekBar
android:id="@+id/acelerador"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_marginBottom="30dp"
android:layout_marginRight="5dp"
android:layout_marginTop="30dp"
android:max="8"
android:maxHeight="0dp"
android:minHeight="0dp"
android:progress="4"
android:progressDrawable="@drawable/progress"
android:thumb="@drawable/thumb" />
</RelativeLayout>
This is the VerticalSeekBar class
package renatox.play.bluetoothcommander;
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 {
protected OnSeekBarChangeListener changeListener;
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:
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;
}
}
Can anyone tell me what might happen?
Thank you very much!!
Solution
I
thought I’d post this as an official “answer” as it helps my code. (Answered by Renato Henz).
I’ve just added this to the “VerticalSeekBar.java”:
@Override public void setOnSeekBarChangeListener(OnSeekBarChangeListener mListener) { this.changeListener = mListener; }
If you need any further help, please tell me. Good luck with your code!
I found that adding this code, along with the previously mentioned code, helped solve some problems :
@Override
public synchronized void setProgress(int progress) {
super.setProgress(progress);
onSizeChanged(getWidth(), getHeight(), 0, 0);
}