set contentDescription for tabLyaout - android

I want to set contentDescription for tabs of tabLayout but talkBack doesn't read the content description of the tab that is selected and it just say 'selected'. How do I have to set contentDescription for tabLayout?
I update content description in viewPager.addOnPageChangeListener for each tabs and also I tried to set content description in tabLayout.addOnTabSelectedListener for each tabs but none of them worked!
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
updateTabContentDescription();
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
...
private void updateTabContentDescription() {
for (int i = 0; i < adapter.getCount(); i++) {
TabLayout.Tab tab = tabLayout.getTabAt(i);
if (tab != null) {
tab.setContentDescription(adapter.getPageTitle(i));
}
}
}
and I test it for viewPager:
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
updateTabContentDescription();
}
#Override
public void onPageScrollStateChanged(int state) {
}
});

Related

ViewPager.setCurrentItem Return Wrong Position in PagerAdapter

Here's my tabLayoutCode
mTabLayoutBerita.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
Log.i("onTabSelected", String.valueOf(tab.getPosition()));
mViewPagerBerita.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
And this is in my PagerAdapter
public class PagerBerita extends FragmentPagerAdapter {
private int mNumOfTabs;
private FragmentBeritaByKategori fragmentBeritaByKategori = null;
private List<KategoriItem> mDataListKategoriBerita;
public PagerBerita(FragmentManager fm, int NumOfTabs, List<KategoriItem> mDataListKategoriBerita) {
super(fm);
this.mNumOfTabs = NumOfTabs;
this.mDataListKategoriBerita = mDataListKategoriBerita;
}
#Override
public Fragment getItem(int position) {
fragmentBeritaByKategori = new FragmentBeritaByKategori(mDataListKategoriBerita.get(position).getId());
return fragmentBeritaByKategori;
}
#Override
public int getCount() {
return mDataListKategoriBerita.size();
}
}
I'm trying to create dynamic tab layout, but when the tab already showed up, i get wrong data because the position i use in PagerAdapter is wrong. I've already use setOffscreenPageLimit and not working, i'm so confuse. Any solution for this case ? Thanks a lot.
You are forgot to add ViewPager to tab layout.
TabLayout tabsLayout = (TabLayout) findViewById(R.id.tabsLayout);
ViewPager viewpager = (ViewPager) findViewById(R.id.viewpager);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
viewpager.setAdapter(adapter);
//Add ViewPager to tab layout.
tabsLayout.setupWithViewPager(viewpager);
viewpager.setOffscreenPageLimit(8);
tabsLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewpager.setCurrentItem(tab.getPosition());
Log.i("TabLayout Position: " + tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
It working fine.
I do like this, is it wrong ?
final CustomViewPager viewPager = findViewById(R.id.pager);
final PagerAdapterBottomNavigation pagerAdapterBottomNavigation = new PagerAdapterBottomNavigation(getSupportFragmentManager(), tab_layout.getTabCount());
viewPager.setAdapter(pagerAdapterBottomNavigation);
viewPager.setCurrentItem(2);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tab_layout));
tab_layout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
tab_layout.getTabAt(2).getIcon().setColorFilter(getResources().getColor(R.color.secondaryTextColor), PorterDuff.Mode.SRC_IN);
tab.getIcon().setColorFilter(getResources().getColor(R.color.secondaryLightColor), PorterDuff.Mode.SRC_IN);
if(tab.getPosition() == 0) {
actionBar.setTitle("Menu");
} else if(tab.getPosition() == 1) {
actionBar.setTitle("News");
} else if(tab.getPosition() == 2) {
actionBar.setTitle("Event");
} else if(tab.getPosition() == 3) {
actionBar.setTitle("Promo");
} else if(tab.getPosition() == 4) {
actionBar.setTitle("Setting");
}
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
tab.getIcon().setColorFilter(getResources().getColor(R.color.secondaryTextColor), PorterDuff.Mode.SRC_IN);
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
My PagerAdapter
public class PagerAdapterBottomNavigation extends FragmentPagerAdapter {
int mNumOfTabs;
private FragmentAllMenu fragmentAllMenu;
private FragmentBerita fragmentBerita;
private FragmentEvent fragmentEvent;
private FragmentSetting fragmentSetting;
private FragmentPromo fragmentPromo;
public PagerAdapterBottomNavigation(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
fragmentAllMenu = new FragmentAllMenu();
return fragmentAllMenu;
case 1:
fragmentBerita = new FragmentBerita();
return fragmentBerita;
case 2:
fragmentEvent = new FragmentEvent();
return fragmentEvent;
case 3:
fragmentPromo = new FragmentPromo();
return fragmentPromo;
case 4:
fragmentSetting = new FragmentSetting();
return fragmentSetting;
default:
return null;
}
}
#Override
public int getCount() {
return mNumOfTabs;
}
}
But i check on PagerAdapter getItem(int position), the position wrong but show correct fragment.

how can i solve issue with icons in tabbed view

i created tabbed view in android studio but i got an issue i put icons above the text title home,category and favorite when i slide from home to category or favorite the icons goes disappear the problem come with me in this section when i comment this section it work properly but when i call this method then the icons shows only one time when i open app and slide to another class they goes disappear
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
viewPager.getAdapter().notifyDataSetChanged();
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
and this is the method where i called icons
setupTabIcons();
check out whole section and help me out please how i can i do it
viewPager = (ViewPager) findViewById(R.id.container);
setupViewPager(viewPager);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
viewPager.getAdapter().notifyDataSetChanged();
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
tabLayout.getSelectedTabPosition();
if (tab.getText().equals("Categories")) {
currentFragment = "Categories";
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(MainActivity.this);
Intent i = new Intent("TAG_REFRESH");
lbm.sendBroadcast(i);
} else {
currentFragment = "check_internet_connection";
}
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
setupTabIcons();
}
private void setupTabIcons() {
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
}
private void setupViewPager(ViewPager viewPager) {
adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new HomeFragment(), "Home");
adapter.addFragment(new Category(), "Categories");
adapter.addFragment(new Favorite(), "Favorite");
viewPager.setAdapter(adapter);
}

How to disable/ lock one page in viewpager?

I want lock / disable one particular page(fragment) in viewpager. According to scenario for some people only able to access that page. How restrict swiping to that page.
I disabled tab click for Events page by using following code :
LinearLayout tabStrip = ((LinearLayout)tabLayout.getChildAt(0));
if(!isAccess){
tabStrip.getChildAt(3).setClickable(false);
tabStrip.getChildAt(3).setEnabled(false);
}
But now I need to restrict swiping for that page in view pager. How to do this?
I written this in pager adapter it works.
#Override
public int getCount() {
if(!isAccess) {
return 3;
} else {
return 4;
}
}
Use this
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
public void onPageScrollStateChanged(int state) {}
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
public void onPageSelected(int position) {
if (position == 3)
{
// show dialog that you are not allowed
viewPager.setSelected(2);
}
}
});
Hope this helped you
You can also use
viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
// optional
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { }
// optional
#Override
public void onPageSelected(int position) { }
// optional
#Override
public void onPageScrollStateChanged(int state) { }
});
This is also the solution without using the deprecated method (setOnPageChangeListener).
viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
super.onPageScrolled(position, positionOffset, positionOffsetPixels);
}
#Override
public void onPageSelected(int position) {
super.onPageSelected(position);
if(position == 3) {
//To disable specific tab and set the previuos tab
viewPager.setCurrentItem(2); //We cannot provide the position in setSelected(boolean) now. This was missed in above solution
}
}
#Override
public void onPageScrollStateChanged(int state) {
super.onPageScrollStateChanged(state);
}
});
Initialize the first tab position
int currentTab = 0;
then make listener for viewPager
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
//4 is the position of the tab we want to disable
if (position == 4) {
if (currentTab < 4) {
// swipe from left to right
viewPager.setCurrentItem(5);
currentTab = 5;
} else {
// swipe from right to left
viewPager.setCurrentItem(3);
currentTab = 3;
}
} else {
currentTab = position;
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});

Get Current item ViewPageAdapter Android

Im searching in all internet but I dont find any REAL solution about my problem.
I have a class with a Viewpager. (Class DescripcionRutinas)
private void setupViewPager(ViewPager viewPager) {
adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addNumDias(numDias);
adapter.setDias(dias);
viewPager.setAdapter(adapter);
viewPager.setOffscreenPageLimit(5);
}
And I have a ViewPagerAdapter
class ViewPagerAdapter extends FragmentPagerAdapter {
private int numDias;
private int count = 0;
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
ArrayList<Dia> dias;
#Override
public Fragment getItem(int position) {
OneFragmentSeries fragmento = new OneFragmentSeries();
Dia ex = dias.get(**I NEED CURRENT ITEM HERE**);
nomDia = ex.getNombre();
fragmento.setSeries((ArrayList<Serie>)((ArrayList<Dia>)WorkoutYourself.dameWorkoutYourself().dameRutina(nomRutina).getDias()).get(position).getSeries());
return fragmento;
}
I need the current item position here.
NOTE: ViewPagerAdapter is INSIDE DescripcionRutinas.
Thanks guys!
Try to use yourViewPager.getCurrentItem() it should work to get your visible pager position.
Update
onPageSelected(int currentPage) should do the stuff
dia.setOnPageChangeListener(new OnPageChangeListener() {
public void onPageScrollStateChanged(int arg0) {
}
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
public void onPageSelected(int currentPage) {
//here you get your current position
}
});
Read this documentation
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int positionCurrentPage) {
if(positionCurrentPage == 1){ // if you want the second page, for example
//Your code here
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});

why viewPager addOnPageChangeListener works with tab clicking and break on scrolling pages?

I have a viewPager of four different tabs, i'm using addOnPageChangeListener to manage page scrolling, the following code is trying to get a view from the first fragment when clicking on a button on the fourth fragment, this works fine when i click on tabs and break when i scroll the pages, which i couldn't find a reason why, the exception i get is a java.lang.NullPointerException for the EditTest view error
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float
positionOffset, int positionOffsetPixels) {
if(position == 3){
EditText title = (EditText) findViewById(R.id.title);
Log.d("Hello", title.getText().toString());
}
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
link to gif showing the problem here
i have tried to move this code to onPageSelected and it didn't work, i also tried to get the fragment view using the following code and no luck:
public View getFragmentView(int position) {
String fragmentD = "android:switcher:" + viewPager.getId() + ":" + position;
android.support.v4.app.Fragment fragment =
getSupportFragmentManager().findFragmentByTag(fragmentD);
return fragment.getView();
}
question is why this works with tab clicking and doesn't with scrolling?
You must call it in OnCreate
public class MainActivity extends Activity {
EditText title;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
title = (EditText) findViewById(R.id.title);
}
}
And at public void onPageSelected(int position)
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float
positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
if(position == 3){
Log.d("Hello", title.getText().toString());
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});

Categories

Resources