Disable Tablayout

Disable TabLayout

I had the same problem and I solved it disabling touch event on tabs with the following code:

  LinearLayout tabStrip = ((LinearLayout)mTabLayout.getChildAt(0));
for(int i = 0; i < tabStrip.getChildCount(); i++) {
tabStrip.getChildAt(i).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
}

How can I disable click on TabLayout in Android

You are accessing tabs before setupWithViewPager, thats why your code is not working. So first set tabs then settouchlistener code.

Try this:

tabLayout.setupWithViewPager(viewPager);
setupTabIcons();

LinearLayout tabStrip = ((LinearLayout)mTabLayout.getChildAt(0));
for(int i = 0; i < tabStrip.getChildCount(); i++) {
tabStrip.getChildAt(i).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
}

How to disable click effect on TabLayout?

Try to change the tab background as transparent app:tabBackground="@android:color/transparent", like below

<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="@dimen/tab_height"
app:tabBackground="@android:color/transparent"
app:tabMode="fixed" />

It does work, I have verified.

Disable Tabs in TabLayout

there are 3 methods implemented by the tab click listener, one of them is onTabSelected() put a boolean condition to check if your fragment is initialised. Then if that condition is satisfied then allow transaction to take place.
Also initialize the tabs after your fragment code

how to disable of select some tab when using TabLayout ?

If you want to handle the onTabSelected for your TabLayout, you can do this and check if they're allowed to show that Fragment.

tab_layout.addOnTabSelectedListener( new TabLayout.OnTabSelectedListener() {
override onTabReselected( TabLayout.Tab tab ) {}
override onTabUnselected( TabLayout.Tab tab ) {}
override onTabSelected( TabLayout.Tab tab ) {
if( ... is not disabled )
pager.currentItem = tab.position
}

})

How to disable tabLayout scroll animation while scrolling it programetically?

Are you referring to the underline scrolling animation of the TabLayout?
you can set

app:tabIndicatorHeight="0dp"

As an example:

<android.support.design.widget.TabLayout
android:id="@+id/comtabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
app:tabIndicatorHeight="0dp"
app:tabPaddingStart="0dp"
app:tabPaddingEnd="0dp"
app:tabMode="fixed"
app:tabGravity="fill"
android:background="@android:color/white" />

But if you referring to the content of the tab that happen because of the viewpager, so when clicking on the tab you need to use this method of the viewpager :

mViewPager.setCurrentItem(position, false)

2nd parameter is smoothScroll if false, you're disabling the scroll animation

Android: how to disable swiping between tablayout control by button click

You can disable swipe feature in your ViewPager. Create custom ViewPager as follow.

public class FCViewPager extends ViewPager {

private boolean enableSwipe;

public FCViewPager(Context context) {
super(context);
init();
}

public FCViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

private void init() {
enableSwipe = true;
}

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
// Never allow swiping to switch between pages
return enableSwipe && super.onInterceptTouchEvent(event);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
// Never allow swiping to switch between pages
return enableSwipe && super.onTouchEvent(event);

}

public void setEnableSwipe(boolean enableSwipe) {
this.enableSwipe = enableSwipe;
}
}

In your MainActivity

//R.id.pager has to be FCViewPager not default ViewPager
viewPager = (FCViewPager) findViewById(R.id.pager);

Your button onClick function

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pager.setEnableSwipe(false);
}
});


Related Topics



Leave a reply



Submit