Ramz
Ramz

Reputation: 935

Ripple effect is not working in sliding tab layout

I am trying to obtain ripple effect on clicking the tabs in the sliding tab layout, but it is not working.

custom_tab_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="15dp"
>

<ImageView
    android:id="@+id/tabImage"
    android:layout_width="match_parent"
    android:background="@drawable/tab_bg"
    android:layout_height="wrap_content"
    android:layout_gravity="center" />

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

Below is the drawable xml file to get the ripple effect

v22/tab_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:attr/colorControlHighlight">
<item
    android:id="@+id/mask"
    android:drawable="@color/white"></item>
</ripple>

Below is the MainActivity where i have implemented the colors for background and indicator for the sliding tab.

MainActivty.java

  mTabs.setCustomTabView(R.layout.custom_tab_layout, R.id.tabText);
    mTabs.setDistributeEvenly(true);

    mTabs.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
    mTabs.setSelectedIndicatorColors(getResources().getColor(R.color.accentColor));
    mTabs.setViewPager(mPager);

Any Help is appreciated..!!

Upvotes: 1

Views: 3044

Answers (3)

maros136
maros136

Reputation: 346

Try use TabLayout from Android Design support

compile 'com.android.support:design:23.1.0'

Layout:

xmlns:app="http://schemas.android.com/apk/res-auto"
...
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:tabBackground="?attr/selectableItemBackground"/>

See also tutorial for using custom layout https://guides.codepath.com/android/google-play-style-tabs-using-tablayout

Upvotes: 0

Ramz
Ramz

Reputation: 935

I found a library for getting ripple effect in sliding tab. Let me post both the files here.

  1. In Android Studio, add dependency in build.gradle (compile 'it.neokree:MaterialTabs:0.11')

    Refer this https://github.com/neokree/MaterialTabs

  2. activity_tab_library.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout        xmlns:android="http://schemas.android.com/apk/res/android"
    
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    
    tools:context="com.example.ramz.material_udemy_slidnerd.TabLibrary">
    
    <include
    android:id="@+id/app_bar"
    layout="@layout/app_bar" />
    
    <!-- for Text Tabs -->
    <it.neokree.materialtabs.MaterialTabHost
    android:id="@+id/materialTabHost"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    
    app:accentColor="#FF9800"
    app:primaryColor="#673AB7"
    app:textColor="#FFFFFF" />
    
    <android.support.v4.view.ViewPager
    android:id="@+id/viewPager"
    android:layout_width="match_parent"
    
    android:layout_height="0dp"
    android:layout_weight="1"/>
    
    </LinearLayout>
    

    3.TabLibrary.java

    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentStatePagerAdapter;
    import android.support.v4.view.ViewPager;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.Toast;
    
    import it.neokree.materialtabs.MaterialTab;
    import it.neokree.materialtabs.MaterialTabHost;
    import it.neokree.materialtabs.MaterialTabListener;
    
    
    public class TabLibrary extends AppCompatActivity implements MaterialTabListener {
    
    private MaterialTabHost tabHost;
    private ViewPager viewPager;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tab_library);
    Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    
    tabHost = (MaterialTabHost) findViewById(R.id.materialTabHost);
    viewPager = (ViewPager) findViewById(R.id.viewPager);
    
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    viewPager.setAdapter(adapter);
    //  viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
    viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int posiiton) {
            tabHost.setSelectedNavigationItem(posiiton);
        }
    });
    for (int i = 0; i < adapter.getCount(); i++) {
        tabHost.addTab(tabHost.newTab().setText(adapter.getPageTitle(i)).setTabListener(this));
    }
    
    
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_activity_using_library, menu);
    return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    
    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
    
    
        return true;
    }
    
    return super.onOptionsItemSelected(item);
    }
    
    
    
    @Override
    public void onTabSelected(MaterialTab materialTab) {
    viewPager.setCurrentItem(materialTab.getPosition());
    
    }
    
    @Override
    public void onTabReselected(MaterialTab tab) {
    
    }
    
    @Override
    public void onTabUnselected(MaterialTab tab) {
    
    }
    
    public class ViewPagerAdapter extends FragmentStatePagerAdapter {
    
    public ViewPagerAdapter(FragmentManager fm) {
        super(fm);
    }
    
    @Override
    public Fragment getItem(int num) {
        return MainActivity.Myfragment.getInstance(num);
    }
    
    @Override
    public int getCount() {
        return 3;
    }
    
    @Override
    public CharSequence getPageTitle(int position) {
        return getResources().getStringArray(R.array.tabs)[position];
    
    }
    
    }
    
    }
    

    That's all, we can add any number of tabs here.Thanks

Upvotes: 0

SureshCS50
SureshCS50

Reputation: 3768

In your SlidingTabLayout.java -> createDefaultTabView() you will find

TypedValue outValue = new TypedValue();

just add background of this as selectableItemBackground

getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);

for reference see this SlidingTabLayout (Gist).

Edit :

Fine now i got your issue.. set your custom_tab_layout.xml background as selectableItemBackground

android:background="?attr/selectableItemBackground"

Upvotes: 3

Related Questions