Disable scrolling View Programmatically?

I would like to enable ScrollView and disable it with the click of a button.
Disable means that if ScrollView was not there .. and enable it returns ScrollView.

I want this because I have a gallery with text images, and when I click the button, the screen orientation changes, so the text becomes larger in landscape orientation. And I want ScrollView so that the image does not stretch and the text becomes unreadable.

scrollview.Enabled=false/setVisibility(false) does nothing.

XML:

 <ScrollView 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> </ScrollView> 

thank

Edit1: I cannot use the visibility (left), since this would also hide the gallery that I want to hide the ScrollView effect. When there is a ScrollView, the images in the Gallery become scrollable and do not fit on the screen, so you need to scroll to see the whole image, I do not want to turn it off / on at the click of a button.

I tried this:

 ((ScrollView)findViewById(R.id.QuranGalleryScrollView)).setOnTouchListener(null); ((ScrollView)findViewById(R.id.QuranGalleryScrollView)).setHorizontalScrollBarEnabled(false); ((ScrollView)findViewById(R.id.QuranGalleryScrollView)).setVerticalScrollBarEnabled(false); ((ScrollView)findViewById(R.id.QuranGalleryScrollView)).setEnabled(false); 

Nevertheless, the images in the Gallery scroll and do not fit on the screen. What is the solution?

+97
android android-scrollview
Apr 23 2018-11-11T00: 00Z
source share
17 answers

A few points to start:

  1. You cannot disable ScrollView scrolling. You will need to extend ScrollView and override the onTouchEvent method onTouchEvent that it returns false when any condition is met.
  2. The Gallery component scrolls horizontally regardless of whether it is in ScrollView or not - ScrollView provides only vertical scrolling (horizontal scrolling is required for horizontal scrolling)
  3. You seem to say that you have a problem with stretching the image itself - it has nothing to do with ScrollView, you can change the way ImageView scales from android:scaleType android:scaleType (XML) setScaleType or setScaleType method - for example, ScaleType.CENTER will not stretch your image and will center it in the original size

You can change 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); } } 

Would you 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 ScrollView to your special LockableScrollView ).

Then call

 ((LockableScrollView)findViewById(R.id.QuranGalleryScrollView)).setScrollingEnabled(false); 

disable view scrolling.

I think that you have more than just the problem of disabling scrolling, although in order to achieve the desired result (for example, the gallery will be scrolled using the above code) - I would recommend doing some more research on each of the three components (Gallery, ScrollView, ImageView) to see what properties everyone has and how he behaves.

+168
Apr 23 2018-11-11T00:
source share

I went like this:

  scrollView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub return isBlockedScrollView; } }); 
+69
Mar 15 '13 at 7:22
source share

Found this simple solution by simply installing

 ScrollView.requestDisallowInterceptTouchEvent(true); 
+9
Jan 20 '14 at 10:48
source share

Disablend ScrollView

 ScrollView sw = (ScrollView) findViewById(R.id.scrollView1); sw.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return true; } }); 
+4
Sep 11 '13 at 5:06 on
source share

to start, I used the code posted in the first comment, but I changed it as follows:

  public class LockableScrollView extends ScrollView { public LockableScrollView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub } public LockableScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public LockableScrollView(Context context) { super(context); } // 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 if (mScrollable) return super.onTouchEvent(ev); // only continue to handle the touch event if scrolling enabled return mScrollable; // mScrollable is always false at this point default: return super.onTouchEvent(ev); } } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: // if we can scroll pass the event to the superclass if (mScrollable) return super.onInterceptTouchEvent(ev); // only continue to handle the touch event if scrolling enabled return mScrollable; // mScrollable is always false at this point default: return super.onInterceptTouchEvent(ev); } } } 

then i called it that way

 ((LockableScrollView)findViewById(R.id.scrollV)).setScrollingEnabled(false); 

because i tried

 ((LockableScrollView)findViewById(R.id.scrollV)).setIsScrollable(false); 

but he said setIsScrollable is undefined

Hope this helps you

+4
Feb 27 '14 at 11:17
source share

Here is a simpler solution. Override onTouch() for ScrollView OnTouchListener and return false if you want to bypass scrolling by clicking. Software scrolling still works, and there is no need to extend the ScrollView class.

 mScroller.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return isScrollable; } }); 
+2
Jun 26 '12 at 15:43
source share

I don’t have enough points to comment on the answer, but I wanted to say that the mikec answer worked for me, except that I had to change it in order to return! isScrollable, for example:

 mScroller.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return !isScrollable; } }); 
+2
Sep 21 '12 at 13:17
source share
 @Override public boolean onInterceptTouchEvent(MotionEvent ev) { switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: // if we can scroll pass the event to the superclass if (mScrollable) return super.onInterceptTouchEvent(ev); // only continue to handle the touch event if scrolling enabled return mScrollable; // mScrollable is always false at this point default: return super.onInterceptTouchEvent(ev); } } 
+1
Jul 13 2018-11-11T00:
source share

@JosephEarl +1 He has a great solution that worked fine for me with some minor changes to his programming programming.




Here are the small changes I made:

LockableScrollView Class:

 public boolean setScrollingEnabled(boolean enabled) { mScrollable = enabled; return mScrollable; } 

MainActivity:

 LockableScrollView sv; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); sv = new LockableScrollView(this); sv.setScrollingEnabled(false); } 
+1
Nov 07
source share

I had a layout over NestedScrollView that used a touch event and turned off scrolling:

 progressBarLayout.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { return true; } }); 

and enable:

 progressBarLayout.setOnTouchListener(null); 
+1
Dec 19 '16 at 12:54 on
source share
 public class LockableScrollView extends ScrollView { 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); } @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public LockableScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } public void setScrollable(boolean enabled) { mScrollable = enabled; } public boolean isScrollable() { return mScrollable; } @Override public boolean onTouchEvent(MotionEvent ev) { return mScrollable && super.onTouchEvent(ev); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { return mScrollable && super.onInterceptTouchEvent(ev); } } 
+1
Mar 15 '18 at 15:32
source share

Does it help?

 ((ScrollView)findViewById(R.id.QuranGalleryScrollView)).setOnTouchListener(null); 
0
Apr 23 2018-11-11T00:
source share

You can expand the gallery and use some flag to disable scrolling if you want:

 public class MyGallery extends Gallery { public boolean canScroll; public MyGallery(Context context, AttributeSet attrs) { canScroll = true; super(context, attrs); } public void setCanScroll(boolean flag) { canScroll = flag; } @Override public boolean onScroll(android.view.MotionEvent e1, android.view.MotionEvent e2, float distanceX, float distanceY) { if (canScroll) return super.onScroll(e1,e2,distancex,distancey); else return false; } @Override public boolean onSingleTapUp(MotionEvent e) { if (canScroll) return super.onSingleTapUp(ey); else return false; } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { if (canScroll) return super.onFling(e1,e2,velocityX,velocityY); else return false; } } 
0
Oct 28
source share

You can create a CustomScrollView for which you can disable its interaction with other views. Although this can sometimes annoy the end user. Use it with caution.

This is the code:

 import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.ViewParent; public class CustomScrollView extends android.widget.ScrollView { public CustomScrollView(Context context) { super(context); } public CustomScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public CustomScrollView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { /* Prevent parent controls from stealing our events once we've gotten a touch down */ if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) { ViewParent p = getParent(); if (p != null) p.requestDisallowInterceptTouchEvent(true); } return false; } } 

How to use CustomScrollView?

You can add CustomScrollView as a parent to the screen on which you want to add another Scrollview as a child view, and the whole screen scrolls. I used this for RelativeLayout.

 <?xml version="1.0" encoding="utf-8"?> <**package.**CustomScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/some_id" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:tools="http://schemas.android.com/tools"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> #**Inside this I had a TableLayout and TableRow. Inside the TableRow,** #**I had a TextView which I made scrollable from Java Code.** #**textView.setMovementMethod(new ScrollingMovementMethod());** </RelativeLayout> </ankit.inventory.ankitarora.inventorymanagementsimple.CustomScrollView> 

It worked for my case.

0
Mar 02 '17 at 18:58
source share

ScrollView / HorizontalScrollView contains a private OverScroller object. You can get mScroller (this object) and call OverScroller # forceFinished. But these are private fields, and you should get them using reflections.

 private void stopScrolling(HorizontalScrollView view) { try { Class<? extends HorizontalScrollView> hsvClass = view.getClass(); Field fieldScroller = hsvClass.getDeclaredField("mScroller"); fieldScroller.setAccessible(true); OverScroller overScroller = (OverScroller) fieldScroller.get(view); overScroller.forceFinished(true); } catch (Exception e) { e.printStackTrace(); } } 

But you should be aware that using reflection in your code is dangerous. And this is a big crutch!

0
Apr 15 '19 at 13:08
source share

As you can see in the documentation , you cannot set the visibility to false. In your case, you should probably use:

 scrollview.setVisibility(Visibility.GONE); 
-2
Apr 23 2018-11-11T00:
source share

at the touch of a button just

 ScrollView sView = (ScrollView)findViewById(R.id.ScrollView01); sView.setVerticalScrollBarEnabled(false); sView.setHorizontalScrollBarEnabled(false); 

so that the scrollbar is not turned on when the button is pressed

-four
Apr 23 2018-11-11T00:
source share



All Articles