My TabLayout was perfect, showing with icons, and then I had to add a ViewPagerOnTabSelectedListener, and my icons are missing.
Can someone help me please?
I have tried solutions like adding this in my MainActivity, but nothing changed:
tabLayout.getTabAt(0).setIcon(R.drawable.home);
This is my code:
public class MainActivity extends AppCompatActivity {
private ViewPager pager;
private BottomNavigationView navigation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pager = (ViewPager) findViewById(R.id.pager);
navigation = (BottomNavigationView) findViewById(R.id.navigation);
BottomNavigationViewHelper.disableShiftMode(navigation);
TabLayout tabLayout = new TabLayout(this);
tabLayout = (TabLayout) findViewById(R.id.toolbar);
tabLayout.setupWithViewPager(pager);
tabLayout.addOnTabSelectedListener(
new TabLayout.ViewPagerOnTabSelectedListener(pager) {
@Override
public void onTabSelected(TabLayout.Tab tab) {
super.onTabSelected(tab);
pager.setCurrentItem(tab.getPosition());
Log.i("TAG", "onTabSelected: " + tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
final PagerAdapter mAdapter = new MainPagerAdapter(getSupportFragmentManager());
pager.setAdapter(mAdapter);
navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_adresse:
pager.setCurrentItem(0);
return true;
case R.id.navigation_cognitif:
pager.setCurrentItem(1);
return true;
case R.id.navigation_deplacement:
pager.setCurrentItem(2);
return true;
case R.id.navigation_yeux:
pager.setCurrentItem(3);
return true;
}
return false;
}
});
}
class MainPagerAdapter extends FragmentPagerAdapter {
public MainPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new MeFragment();
case 1:
return new MeFragment();
case 2:
return new MeFragment();
case 3:
default:
return new MeFragment();
}
}
@Override
public int getCount() {
return 4;
}
}
Layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.blabla.bloublou.MainActivity"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TabLayout
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="?attr/colorPrimary">
<android.support.design.widget.TabItem
android:id="@+id/home_button"
android:icon="@drawable/home"
android:title="home"/>
<android.support.design.widget.TabItem
android:id="@+id/me_button"
android:icon="@drawable/me"
android:title="moi" />
<android.support.design.widget.TabItem
android:id="@+id/configuration_button"
android:icon="@drawable/tools"
android:title="configuration"
/>
<android.support.design.widget.TabItem
android:id="@+id/help_button"
android:icon="@drawable/help"
android:title="aide"/>
</android.support.design.widget.TabLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:background="@color/colorPrimary"
app:itemIconTint="@color/icon"
app:itemTextColor="@color/icon"
app:menu="@menu/navigation">
</android.support.design.widget.BottomNavigationView>
<com.umanit.emfsc.NonSwipeableViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/navigation"
android:layout_below="@+id/toolbar"
/>
<include layout="@layout/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/toolbar"
android:layout_above="@id/navigation"
/>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
tabLayout.setupWithViewPager(pager);
tabLayout.addOnTabSelectedListener(
new TabLayout.ViewPagerOnTabSelectedListener(pager) {
@Override
public void onTabSelected(TabLayout.Tab tab) {
super.onTabSelected(tab);
pager.setCurrentItem(tab.getPosition());
Log.i("TAG", "onTabSelected: " + tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
final PagerAdapter mAdapter = new MainPagerAdapter(getSupportFragmentManager());
pager.setAdapter(mAdapter);
tabLayout.setupWithViewPager(pager);
make above changes,the problem is you are setting the viewpager before pager has set the adapter,let me know if this won't work.