Darragh O'Flaherty Darragh O'Flaherty -4 years ago 71
Android Question

Disable swiping between tabs

I set up sliding tabs with two

s each
has a
which goes to a
. The problem with this is when the
is clicked the sliding tabs are still activated and when a user tries to navigate within the
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

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() {
public void onClick(View v) {

return view;

Answer Source

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 providing your own custom ViewPager.

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;

 * Created by hegazy on 2/13/15.
public class CustomViewPager extends android.support.v4.view.ViewPager{
    private boolean enabled;

    public CustomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.enabled = true;

    public boolean onTouchEvent(MotionEvent event) {
        return enabled ? super.onTouchEvent(event) : false;

    public boolean onInterceptTouchEvent(MotionEvent event) {
        return enabled ? super.onInterceptTouchEvent(event) : false;

    public void setPagingEnabled(boolean enabled) {
        this.enabled = enabled;

    public boolean isPagingEnabled() {
        return enabled;


This class provides a ViewPager that is swiping enabled and you can turn it off by mViewPager.setPagingEnabled(false);

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



Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download