ripple effect for tabs

1.7k Views Asked by At

I am not able to use ripple effect on pressing on a tab please help I have tried a lot and struggling with it whole day.How can I use ripple effect on both pre-lollipop and lollipop devices any suggestion?

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mPager=(ViewPager)findViewById(R.id.pager);
    mPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager(),getApplicationContext()));
    mTabs=(SlidingTabLayout)findViewById(R.id.tabs);
    mTabs.setDistributeEvenly(true);
    mTabs.setCustomTabView(R.layout.customtablayout,R.id.textTab);
    mTabs.setBackgroundColor(getResources().getColor(R.color.colorAccent));
    mTabs.setSelectedIndicatorColors(getResources().getColor(R.color.colorPrimary));

    mTabs.setViewPager(mPager);

}

activitymain.xml

<sarveshchavan777.inrerface2.SlidingTabLayout
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" />

customtablayout.xml

<ImageView
    android:id="@+id/imageTab"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center" />

<TextView
    android:id="@+id/textTab"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="bottom|center" />
2

There are 2 best solutions below

0
mohamad ali gharat On

use this code for any item you want to give it ripple effect.

copy this code into a new XML file in the drawable folder. and set it as the background of any item you want ripple effect for.

<?xml version="1.0" encoding="utf-8"?>
   <ripple xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:color="@color/icons"
   tools:targetApi="lollipop">
   <item android:drawable="?attr/colorPrimaryDark" />
</ripple
0
McDerek On

This should work

mTabs.setTabRippleColor(getResources().getColorStateList(R.color.colorPrimaryDark));