I am trying to highlight tab icon when viewPager is selected or swiped. For this I am using 'tabLayout.setOnTabSelectedListener()'. But it doesn't highlight when I swipe tab, but when I press tab selected all work perfect and tab gets highlighted. May be it's related with gradle version. I am using 'classpath 'com.android.tools.build:gradle:2.1.0'' . This is activity :
private int[] tabIcons = {
R.drawable.tab_icon_home,
R.drawable.tab_icon_cart2,
R.drawable.tab_icon_aksia,
R.drawable.tab_icon_menu
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_icon_tabs);
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitleTextColor(Color.WHITE);
setSupportActionBar(toolbar);
frameLayout = (FrameLayout) findViewById(R.id.frameLayout);
adapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(pageChangeListener);
viewPager.setOffscreenPageLimit(10);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
}
private OnPageChangeListener pageChangeListener = new OnPageChangeListener() {
int currentPosition = 0;
#Override
public void onPageSelected(int newPosition) {
FragmentLifecycle fragmentToHide = (FragmentLifecycle) adapter.getItem(currentPosition);
fragmentToHide.onPauseFragment();
FragmentLifecycle fragmentToShow = (FragmentLifecycle) adapter.getItem(newPosition);
fragmentToShow.onResumeFragment();
currentPosition = newPosition;
adapter.notifyDataSetChanged();
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
public void onPageScrollStateChanged(int arg0) { }
};
private void setupTabIcons() {
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
tabLayout.getTabAt(3).setIcon(tabIcons[3]);
tabLayout.getTabAt(0).getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);
tabLayout.getTabAt(1).getIcon().setColorFilter(Color.parseColor("#81C784"), PorterDuff.Mode.SRC_IN);
tabLayout.getTabAt(2).getIcon().setColorFilter(Color.parseColor("#81C784"), PorterDuff.Mode.SRC_IN);
tabLayout.getTabAt(3).getIcon().setColorFilter(Color.parseColor("#81C784"), PorterDuff.Mode.SRC_IN);
tabLayout.setOnTabSelectedListener( new TabLayout.ViewPagerOnTabSelectedListener(viewPager) {
#Override
public void onTabSelected(TabLayout.Tab tab) {
super.onTabSelected(tab);
tab.getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
super.onTabUnselected(tab);
tab.getIcon().setColorFilter(Color.parseColor("#81C784"), PorterDuff.Mode.MULTIPLY);
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
super.onTabReselected(tab);
}
});
}
I found solution,in gradle I used
dependencies {
compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.android.support:design:23.0.0' }
And I change from 23.0.0 to 23.0.1 in both appcompat-v7 and design. Now all work for me.
Try this way...
tabLayout = ((TabLayout) findViewById(R.id.tabs));
adapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(
tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
public void onTabReselected(TabLayout.Tab paramAnonymousTab) {
}
public void onTabSelected(TabLayout.Tab paramAnonymousTab) {
viewPager.setCurrentItem(paramAnonymousTab.getPosition());
tab.getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);
}
public void onTabUnselected(TabLayout.Tab paramAnonymousTab) {
tab.getIcon().setColorFilter(Color.parseColor("#81C784"), PorterDuff.Mode.MULTIPLY);
}
});
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
tabLayout.getTabAt(3).setIcon(tabIcons[3]);
tabLayout.getTabAt(0).getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);
tabLayout.getTabAt(1).getIcon().setColorFilter(Color.parseColor("#81C784"), PorterDuff.Mode.SRC_IN);
tabLayout.getTabAt(2).getIcon().setColorFilter(Color.parseColor("#81C784"), PorterDuff.Mode.SRC_IN);
tabLayout.getTabAt(3).getIcon().setColorFilter(Color.parseColor("#81C784"), PorterDuff.Mode.SRC_IN);
viewPager.setOffscreenPageLimit(4);
Happy coding.
When you call setupWithViewPager, this will internally call setOnTabSelectedListener(new ViewPagerOnTabSelectedListener(viewPager));, overriding your OnTabSelectedListener.
you have implemented TabLayout.ViewPagerOnTabSelectedListener, and then overridden onTabSelected() and call setOnTabSelectedListener() after setupWithViewPager():
This is correct and it is working fine when tab clicked,
You are saying that when you scroll the page, it is not working. Have a look at your onPageSelected() of OnPageChangeListener(). Because this method gets called as and when particular Page selected.
Related
i create a viewPager and is work , my problem is for select a page i just can drag page but I want to click on the tabs to select the pages . now i add some new code but i cant find this error
my code
public class MainActivity extends FragmentActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
Pager adapter = new Pager(getSupportFragmentManager());
viewPager.setAdapter(adapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tablayout);
tabLayout.setupWithViewPager(viewPager);
viewPager.addOnAdapterChangeListener( new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
TabLayout.OnTabSelectedListener tabListener = new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
};
You cant use like viewPager.addOnAdapterChangeListener( new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); Because addOnAdapterChangeListener() method expect OnAdapterChangeListener as paramenter. You should use void addOnPageChangeListener (ViewPager.OnPageChangeListener listener) on viewPager instead.It has several callback method, So you can detect the event when page of viewPager is changed.
Reference
I want to start activity with three fragments
Intent intent = new Intent(NewMealActivity.this, MainActivity.class);
intent.putExtra("id", 1);
startActivity(intent);
MainActivity includs three fragments (Frag1, Frag2, Frag3).
I want to open Frag3, but opens Frag1
This is my MainActivity with three tabs
public class MainActivity extends FragmentActivity {
private String txtFragmentMyData;
private String txtFragmentProductList;
private String txtFragmentCalorieCalculator;
private TabLayout tabLayout;
private long tabId;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtFragmentMyData = getString(R.string.txt_fragment_my_data);
txtFragmentProductList = getString(R.string.txt_fragment_product_list);
txtFragmentCalorieCalculator = getString(R.string.txt_fragment_calorie_calculator);
tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText(txtFragmentMyData));
tabLayout.addTab(tabLayout.newTab().setText(txtFragmentProductList));
tabLayout.addTab(tabLayout.newTab().setText(txtFragmentCalorieCalculator));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
final ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
Bundle extras = getIntent().getExtras();
if (extras != null) {
tabId = extras.getLong("id");
}
if (tabId > 0){
viewPager.setCurrentItem(2);
} else {
viewPager.setCurrentItem(0);
}
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
}
I want, that at the first start opens first tab, but if I click on buttom in another activity opens third tab
I don't know why but I'm not able to use addOnTabSelectedListener, though I'm able to use setOnTabSelectedListener() but I don't want to. Here is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
back = (ImageView) findViewById(R.id.back_navigation);
fab = (FloatingActionButton) findViewById(R.id.fab_explore);
viewPager = (CustomViewPager) findViewById(R.id.viewPager_home);
tabLayout = (TabLayout) findViewById(R.id.bottom_navigation2);
navigationView = (NavigationView) findViewById(R.id.nav_view);
DrawerLayout.LayoutParams params = (DrawerLayout.LayoutParams) navigationView.getLayoutParams();
params.width = (int) (width * 0.9);
navigationView.setLayoutParams(params);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
drawer.openDrawer(GravityCompat.START);
}
});
setupViewPager(viewPager);
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
drawer.closeDrawer(GravityCompat.START);
}
});
tabLayout.setupWithViewPager(viewPager);
setupIcon();
tabLayout.getTabAt(0).getIcon().setAlpha(255);
tabLayout.getTabAt(1).getIcon().setAlpha(100);
tabLayout.getTabAt(2).getIcon().setAlpha(100);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
public void setupIcon() {
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new Digest(), "Digest");
adapter.addFragment(new Topics(), "Topics");
adapter.addFragment(new Profile(), "Profile");
viewPager.setAdapter(adapter);
}
I don't know what I'm doing wrong. My compileSdkVersion is 23 and all android support libraries are 23 as well.
Add dependencies inside your build.gradle file
dependencies {
compile 'com.android.support:design:23.1.1'
}
Make sure your build version and design support library versions are same. In your case build tool version and your design support library versions are different, So your are facing that problem
for example in my case:
compileSdkVersion 25
buildToolsVersion '25.0.0'
and in dependencies
compile 'com.android.support:design:25.3.1'
compile 'com.android.support:support-v4:25.3.1'
and that error will be gone for sure.
How to get active tab position in material tabs?
Any one help me.
tabLayout.setOnTabSelectedListener(
new TabLayout.ViewPagerOnTabSelectedListener(viewPager) {
#Override
public void onTabSelected(TabLayout.Tab tab) {
super.onTabSelected(tab);
currentPage = tab.getPosition();
}
});
}
Try this
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.getSelectedTabPosition();
Try this
tabLayout.setOnTabSelectedListener(
new TabLayout.ViewPagerOnTabSelectedListener(viewPager) {
#Override
public void onTabSelected(TabLayout.Tab tab) {
super.onTabSelected(tab);
position = tab.getPosition();
}
});
}
In your Activity implement OnTabChangeListener,
then set the listener for the TabHost tabHost.setOnTabChangedListener(this);
#Override
public void onTabChanged(String id) {
Log.i("INDEX->", "Selected TAB Index - "+ tabHost.getCurrentTab());
}
I am using the android design library TabLayout in that how can I get the current selected item tab position.
ViewPager pager = (ViewPager) view.findViewById(R.id.pager);
MyPagerAdapter adapter = new MyPagerAdapter(getChildFragmentManager());
pager.setAdapter(adapter);
tabLayout.setupWithViewPager(pager);
As of version 22.2.1 of the Support library the TabLayout has a method getSelectedTabPosition.
Source 1 | Source 2
You can call ViewPager's getCurrentItem() to get the index of the currently displayed item.
mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
int position = tab.getPosition();
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
[Updated 29-07-2016]
Refer the accepted answer because below answer is deprecated. If you are using older version then refer it.
You can do it easily ...
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
pos = tab.getPosition();
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});