Learn Pain Less

HomeOur TeamContact
Android
Create custom ScrollView to enable or disable scrolling Android
Pawneshwer Gupta
Pawneshwer Gupta
May 23, 2016
2 min

Table Of Contents

01
Create custom ScrollView to enable or disable scrolling Android
02
So how to use this custom ScrollView class ?
Create custom ScrollView to enable or disable scrolling Android

Create custom ScrollView to enable or disable scrolling Android

In android ScrollView there is no way or method to disable scrolling. but sometime if you want to disable scrolling of ScrollView while you are inside inner element, like if you want to disable ScrollView when you are inside Map view. So you can scroll Map view easily if you put Map view inside ScrollView. Because Map view has its own Scrolling feature which conflicts ScrollView’s scrolling and you are unable to scrolling map view. So at that time you think to disable Scrolling of ScrollView when you touch or make focus on Map view. But when you try to implement this then you get to know that there is no method to disable scrolling of ScrollView. You can disable the whole ScrollView but that is not a correct way to do this. So we can make our own ScrollView which having extra features are enable Scrolling or Disable scrolling.  and we can do this using Inheritance feature of OOPS. So we will create a class which will Extend ScrollView class, which means our class will have all features derive from Base (ScrollView) class. ok so lets do this :

  • create a new class and name it what you want, i will name it as MyScrollView. and extend ScrollView class.
  • Now you need to create constructor of your class, Android studio will give you 4 type of Constructor. extend all of them. because in future we can use that constructors.
  • override 2 methods of ScrollView “onInterceptTouchEvent” and “onTouchEvent”.
  • create a boolean variable which will check that scrolling is enabled or disabled. by default set its value to true.
  • now create boolean type method and return that variable which you have just created.
  • Now create a void method to enable and disable scrolling which requires a parameter of boolean type. i will name this method as setScrolling(boolean enable) as shown below :

So how to use this custom ScrollView class ?

  • Goto your xml file where you want to use ScrollView. if you already have ScrollView inside your xml then replace that ScrollView tag with your custom ScrollView’s class name and package name.
<ScrollView
android:layout_width="match_parent"
android:id="@+id/scrollView"
android:layout_height="match_parent">
</ScrollView>

for example :

change this to (i will use my package name and Class name, change accoring to yours) :

<com.learnpainless.myscrollvoew.MyScrollView
android:layout_width="match_parent"
android:id="@+id/scrollView"
android:layout_height="match_parent">
</com.learnpainless.myscrollvoew.MyScrollView>

Ok so now you have embedded your own ScrollView. to disable scrolling just call setScrolling() method to object of your ScrollView and pass value (true or false) where you want to disable. as shown in below example :

package com.learnpainless.myscrollvoew;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyScrollView scrollView = (MyScrollView) findViewById(R.id.scrollView);
scrollView.setScrolling(false); // to disable scrolling
scrollView.setScrolling(true); // to enable scrolling.
}
}

Subscribe to our newsletter!

We'll send you the best of our blog just once a month. We promise.

Tags

androidScrollViewcustom

Share


Pawneshwer Gupta

Pawneshwer Gupta

Software Developer

Pawneshwer Gupta works as a software engineer who is enthusiastic in creating efficient and innovative software solutions.

Expertise

Python
Flutter
Laravel
NodeJS

Social Media

Related Posts

How to use Text To Speech inside RecyclerView in Android
How to use Text To Speech inside RecyclerView in Android
July 27, 2020
1 min
Learn Pain Less  © 2024, All Rights Reserved.
Crafted with by Prolong Services

Quick Links

Advertise with usAbout UsContact Us

Social Media