Disable ScrollView Programmatically?
Several points to begin with:
- You cannot disable the scrolling of a ScrollView. You would need to extend to ScrollView and override the
onTouchEvent
method to returnfalse
when some condition is matched. - The Gallery component scrolls horizontally regardless of whether it is in a ScrollView or not - a ScrollView provides only vertical scrolling (you need a HorizontalScrollView for horizontal scrolling)
- You seem to say you have a problem with the image stretching itself -- this has nothing to do with the ScrollView, you can change how an ImageView scales with the
android:scaleType
property (XML) or thesetScaleType
method - for instanceScaleType.CENTER
will not stretch your image and will center it at it's original size
You could modify ScrollView
as follows to disable scrolling
class LockableScrollView extends ScrollView {
...
// true if we can scroll (not locked)
// false if we cannot scroll (locked)
private boolean mScrollable = true;
public void setScrollingEnabled(boolean enabled) {
mScrollable = enabled;
}
public boolean isScrollable() {
return mScrollable;
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
// if we can scroll pass the event to the superclass
return mScrollable && super.onTouchEvent(ev);
default:
return super.onTouchEvent(ev);
}
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// Don't do anything with intercepted touch events if
// we are not scrollable
return mScrollable && super.onInterceptTouchEvent(ev);
}
}
You would then use
<com.mypackagename.LockableScrollView
android:id="@+id/QuranGalleryScrollView"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<Gallery android:id="@+id/Gallery"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="horizontal">
</Gallery>
</com.mypackagename.LockableScrollView>
in your XML file (just changed the ScrollView
to your special LockableScrollView
).
Then call
((LockableScrollView)findViewById(R.id.QuranGalleryScrollView)).setScrollingEnabled(false);
to disable scrolling of the view.
I think that you have more than just the issue of disabling scrolling though to achieve your desired result (the gallery will remain scrollable with the above code for instance) - I'd recommend doing some more research on each of the three components (Gallery, ScrollView, ImageView) to see what properties each one has and how it behaves.
How to disable and enable the scrolling on android ScrollView?
Try this way
Create Your CustomScrollview like this
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ScrollView;
public class CustomScrollView extends ScrollView {
private boolean enableScrolling = true;
public boolean isEnableScrolling() {
return enableScrolling;
}
public void setEnableScrolling(boolean enableScrolling) {
this.enableScrolling = enableScrolling;
}
public CustomScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CustomScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomScrollView(Context context) {
super(context);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (isEnableScrolling()) {
return super.onInterceptTouchEvent(ev);
} else {
return false;
}
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (isEnableScrolling()) {
return super.onTouchEvent(ev);
} else {
return false;
}
}
}
In your xml
// "com.example.demo" replace with your packagename
<com.example.demo.CustomScrollView
android:id="@+id/myScroll"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</com.example.demo.CustomScrollView>
In your Activity
CustomScrollView myScrollView = (CustomScrollView) findViewById(R.id.myScroll);
myScrollView.setEnableScrolling(false); // disable scrolling
myScrollView.setEnableScrolling(true); // enable scrolling
How can i disable a scrollview?
To make the scrollbars invisible use this attribute.
android:scrollbars="none"
Edit:
if there is need of runtime changes on visibility or state of enabling, for that we can use the scrolview.setEnabled(false)
or android:enabled=false
.
Nativescript: how to programmatically disable / enable ScrollView scrolling?
Ok, I found how to do that. It's actually pretty easy on iOS:
var scrollView = page.getViewById('YOUR_VIEW_ID')
scrollView.ios.scrollEnabled = false // to disable
scrollView.ios.scrollEnabled = true // to enable back
Android:
Also for my particular case, where I need to disable scrolling of the Scroll View, but drag and drop child views inside. On subview start dragging (panning) event we prevent touch events interception in our Scroll View by:
scrollView._nativeView.requestDisallowInterceptTouchEvent(true)
And enable them again on stop dragging (panning) subview event by:
scrollView._nativeView.requestDisallowInterceptTouchEvent(false)
If you have another case and just want to disable Scroll View scrolling you can use something like this (for Android):
scrollView._nativeView.setOnTouchListener(new android.view.View.OnTouchListener({
onTouch: function (view, motionEvent) {
console.log("DISABLED. onTouch event: Got motionEvent.getAction() " + motionEvent.getAction() + ".");
return true;
}
}))
How to disable work of ScrollView on inner SeekBar?
You need to temporary stop the ScrollView
scrolling and re-enable it when you're done manipulating the SeekBar
.
To achieve that: Instead of using ScrollView
, use the below customized one where Overriding onInterceptTouchEvent
did the trick.
public class LockableScrollView extends ScrollView {
// true if we can scroll (not locked)
// false if we cannot scroll (locked)
private boolean mScrollable = true;
public LockableScrollView(Context context) {
super(context);
}
public LockableScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public LockableScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void setScrollingEnabled(boolean enabled) {
mScrollable = enabled;
}
public boolean isScrollable() {
return mScrollable;
}
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {// if we can scroll pass the event to the superclass
return mScrollable && super.onTouchEvent(ev);
}
return super.onTouchEvent(ev);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// Don't do anything with intercepted touch events if
// we are not scrollable
return mScrollable && super.onInterceptTouchEvent(ev);
}
}
You can temporary stop the scrolling when you touch the SeekBar
by calling myScrollView.setScrollingEnabled(false)
; and enable it when you lift the finger off the Seekbar
as below
seekBar.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN)
myScrollView.setScrollingEnabled(false);
else if (event.getAction() == MotionEvent.ACTION_UP)
myScrollView.setScrollingEnabled(true);
return false;
}
});
Reference of LocableScrollView
How to disable HorizontalScrollView
- You cannot disable the scrolling of a ScrollView. You would need to
extend to ScrollView and override the onTouchEvent method to return
false when some condition is matched. - The Gallery component scrolls
horizontally regardless of whether it is in a ScrollView or not - a
ScrollView provides only vertical scrolling (you need a
HorizontalScrollView for horizontal scrolling). - You seem to say you
have a problem with the image stretching itself -- this has nothing
to do with the ScrollView, you can change how an ImageView scales
with the android:scaleType property (XML) or the setScaleType method
for instance ScaleType.CENTER will not stretch your image and will center it at it's original size.
You could modify ScrollView as follows to disable scrolling:
class LockableScrollView extends ScrollView {
// true if we can scroll (not locked)
// false if we cannot scroll (locked)
private boolean mScrollable = true;
public void setScrollingEnabled(boolean enabled) {
mScrollable = enabled;
}
public boolean isScrollable() {
return mScrollable;
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
// if we can scroll pass the event to the superclass
return mScrollable && super.onTouchEvent(ev);
default:
return super.onTouchEvent(ev);
}
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// Don't do anything with intercepted touch events if
// we are not scrollable
return mScrollable && super.onInterceptTouchEvent(ev);
}
}
then,
<com.mypackagename.LockableScrollView
android:id="@+id/QuranGalleryScrollView"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<Gallery android:id="@+id/Gallery"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="horizontal">
</Gallery>
</com.mypackagename.LockableScrollView>
in your XML file (just changed the ScrollView to your special LockableScrollView).
Then call
((LockableScrollView)findViewById(R.id.QuranGalleryScrollView)).setScrollingEnabled(false);
to disable scrolling of the view.
How to only disable scroll in ScrollView but not content view?
Only pass .horizontal
as the scroll axis if you want the view to scroll. Otherwise, pass the empty set.
struct TestView: View {
@Binding var shouldScroll: Bool
var body: some View {
ScrollView(axes, showsIndicators: false) {
Text("Your content here")
}
}
private var axes: Axis.Set {
return shouldScroll ? .horizontal : []
}
}
Related Topics
How to Change Spinner Text Size and Text Color
Show Image View from File Path
Sending Message Through Whatsapp
How to Add a Jar in External Libraries in Android Studio
Difference Between Fragmentpageradapter and Fragmentstatepageradapter
Cursorloader Usage Without Contentprovider
How to Return a Result (Startactivityforresult) from a Tabhost Activity
How to Make a Phone Call in Android and Come Back to My Activity When the Call Is Done
Difference Between Getdefaultsharedpreferences and Getsharedpreferences
Onitemclicklistener Not Working in Listview
How to Programmatically Add Views to Views
Eclipse Giving Error, Missing R.Java File After Recent Update
How to Add a Border to the Top and Bottom of an Android View
Android Youtube App Play Video Intent
How to Detect Swipe Direction Between Left/Right and Up/Down