Disable swiping between tabs

AndroidAndroid FragmentsAndroid Tabs

Android Problem Overview


I set up sliding tabs with two Fragments each Fragment has a Button which goes to a WebView. The problem with this is when the WebView Button is clicked the sliding tabs are still activated and when a user tries to navigate within the WebView you end up swiping to the other tab. Is there a way in an on click method to disable the swiping ability of the tabs? Any help would be hugely appreciated!

Here the code:

public class MyWebViewClass extends Fragment {

private WebView mWebView;
private Button mButton;

public MyWebViewClass() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_webview, container, false);

    mWebView = (WebView) view.findViewById(R.id.WebView);

    mButton = (Button) view.findViewById(R.id.Button1);
    mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mWebView.setVisibility(View.VISIBLE);
            mButton.setVisibility(View.GONE);
            mWebView.getSettings().setJavaScriptEnabled(true);
            mWebView.loadUrl("www.google.com");
        }
    });

    return view;
}

Android Solutions


Solution 1 - Android

This answer can be applied to any ViewPager actually no matter what is the library you are using to implement the tabs or even a normal ViewPager without tabs.

The library you are using neokree/MaterialTabs is backed with a ViewPager that is responsible for the swiping effect and you can disable that by using the new ViewPager2 or providing your own custom ViewPager.

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import androidx.viewpager.widget.ViewPager

class CustomViewPager(context: Context, attrs: AttributeSet) : ViewPager(context, attrs) {

    var isPagingEnabled: Boolean = true

    @SuppressLint("ClickableViewAccessibility")
    override fun onTouchEvent(event: MotionEvent): Boolean {
        return isPagingEnabled && super.onTouchEvent(event)
    }

    override fun onInterceptTouchEvent(event: MotionEvent): Boolean {
        return isPagingEnabled && super.onInterceptTouchEvent(event)
    }
}

This class provides a ViewPager that is swiping enabled and you can turn it off by viewPager.isPagingEnabled = false

No to mention that you have to change the XML layout to your new custom ViewPager rather than the original one.

<androidx.viewpager.widget.ViewPager 
   ...
   />

to

<my.package.CustomViewPager 
   ...
   />

Solution 2 - Android

Kotlin:

Using ViewPager2...

viewPager.isUserInputEnabled = false

Swiping between tab is disabled and the user can still tap tabs at the top to go between tabs.

Documentation on isUserInputEnabled

Solution 3 - Android

The simplest way is to setOnTouchListener and return true for ViewPager.

original answer: https://stackoverflow.com/a/13392198/4293813

Solution 4 - Android

Accepted Answer's AndroidX and Kotlin Version:

ViewPager with paging disabled all the time

package your.packg.name //CustomViewPager's location actually

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import androidx.viewpager.widget.ViewPager

class CustomViewPager(
    context: Context?,
    attrs: AttributeSet?
) :
    ViewPager(context!!, attrs) {
    override fun onTouchEvent(event: MotionEvent): Boolean {
        return false
    }

    override fun onInterceptTouchEvent(event: MotionEvent): Boolean {
        return false
    }
}


ViewPager with paging switchable to on or off at anytime.

package your.packg.name //CustomViewPager's location actually

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import androidx.viewpager.widget.ViewPager

class CustomViewPager(
    context: Context?,
    attrs: AttributeSet?
) :
    ViewPager(context!!, attrs) {
    var isPagingEnabled = false   //Modify this in code for switch

    override fun onTouchEvent(event: MotionEvent): Boolean {
        return isPagingEnabled && super.onTouchEvent(event)
    }

    override fun onInterceptTouchEvent(event: MotionEvent): Boolean {
        return isPagingEnabled && super.onInterceptTouchEvent(event)
    }
}


Lastly, find ViewPager line in XML file and modify according to your CustomViewPager location, i.e.:

<your.packg.name.CustomViewPager ...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDarragh O&#39;FlahertyView Question on Stackoverflow
Solution 1 - AndroidAhmed HegazyView Answer on Stackoverflow
Solution 2 - AndroidBenView Answer on Stackoverflow
Solution 3 - AndroidReza NazeriView Answer on Stackoverflow
Solution 4 - AndroidXfce4View Answer on Stackoverflow