I apologize if this question has been answered elsewhere - I couldn't find anyone on SO with the same problem.
I added a SeekBar to a RelativeLayout that sits inside a nested ScrollView. However, I am unable to drag the thumb of the seekbar. I can click along its path to change it's value, but I can't drag it. Any help would be appreciated.
My XML looks like:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/containerView"
android:background="@android:color/transparent"
android:padding="0dip"
android:layout_margin="0dip"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbarStyle="insideOverlay"
android:id="@+id/scrollView" android:layout_alignParentTop="true" android:layout_alignParentLeft="true"
android:fillViewport="true">
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbarStyle="insideInset"
android:id="@+id/horizontalScrollView" android:fillViewport="true">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@+id/scrollContainer">
</RelativeLayout>
</HorizontalScrollView>
</ScrollView>
</RelativeLayout>
And my Java is:
SeekBar thisElement = new SeekBar(getActivity());
thisElement.setEnabled(true);
thisElement.setLayoutParams(lp);
thisElement.setAlpha(alphaVal);
thisElement.setMinimumHeight(elementHeight);
final int min = Integer.parseInt(getJsonPropertyValue(tmpJson, "elementSliderMinimum", "0"));
int max = Integer.parseInt(getJsonPropertyValue(tmpJson, "elementSliderMaximum", "100"));
int def = Integer.parseInt(getJsonPropertyValue(tmpJson, "elementSliderDefault", "50"));
thisElement.setMax(max - min);
thisElement.setProgress(def - min);
thisElement.setTag(elementCounter);
thisElement.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
String msg = "onTouch:" + v.getClass().toString();
Log.w("ZZ",msg);
return false;
}
});
thisElement.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
String elementSliderAction = getJsonPropertyValue(tmpJson, "elementSliderAction", "");
if (elementSliderAction.equalsIgnoreCase("changeLabel")) {
int labelNumber = -1;
String elementSliderLabel = getJsonPropertyValue(tmpJson, "elementSliderLabel", "");
for (int j=0; j<childItems.size(); j++) {
BT_item tmpElement = childItems.get(j);
if (tmpElement.getItemId().equalsIgnoreCase(elementSliderLabel)) {
TextView labelToChange = (TextView)scrollContainer.findViewWithTag(j+1000);
String newVal = "" + (progress - min);
labelToChange.setText(newVal);
}
}
}
else if (elementSliderAction.equalsIgnoreCase("saveToPref")) {
String elementSliderPrefName = getJsonPropertyValue(tmpJson, "elementSliderPrefName", "");
String newVal = "" + (progress - min);
setPrefString(elementSliderPrefName, newVal);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
scrollContainer.addView(thisElement);
I suspect this is because of the way my XML file is structured, but am not sure what the correct solution is. I can occasionally get it to drag, but only after trying over and over and over. Also, the OnTouchListener correctly identifies it as the SeekBar that's being touched when I try to drag.
try this
thisElement.setOnTouchListener(new SeekBar.OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
int action = event.getAction();
switch (action)
{
case MotionEvent.ACTION_DOWN:
// Disallow ScrollView to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(true);
break;
case MotionEvent.ACTION_UP:
// Allow ScrollView to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
// Handle Seekbar touch events.
v.onTouchEvent(event);
return true;
}
});
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