Android tablayout selecting the last tab instead of first - android

I need your help. I have a tablayout with tintable imageview and textview. I want to highlight the default selected tab which is the first one.
This is my sample code:
TabLayout.Tab tab;
mTabs = (TabLayout) findViewById(R.id.tabs);
mViewpager = (ViewPager) findViewById(R.id.viewpager);
MyPagerAdapter pagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
mViewpager.setAdapter(pagerAdapter);
mViewpager.addOnPageChangeListener(new MyPageScrollListener(mTabs));
mTabs.setupWithViewPager(mViewpager);
// Iterate over all tabs and set the custom view
for (int i = 0; i < mTabs.getTabCount(); i++) {
tab = mTabs.getTabAt(i);
tab.setCustomView(pagerAdapter.getTabView(i));
}
mTabs.setOnTabSelectedListener(new MyOnTabSelectedListener());
tab.select();
But it's selecting the last tab. Anyone here knows how to fix this? I'd appreciate any kind of help. Thanks!

Finally, I found a solution here: link
I added:
mTabs.getTabAt(1).select();
mTabs.getTabAt(0).select();
And it works!

I found a better solution. Instead if selecting the tab from the TabLayout you can do this with the ViewPager like so:
ViewPager viewPager = (ViewPager) findViewById(R.id.restaurant_menu_viewPager);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
// ViewPager mit TabLayout verknüpfen
TabLayout tabLayout = (TabLayout) findViewById(R.id.restaurant_menu_tabLayout);
tabLayout.setupWithViewPager(viewPager);
viewPager.setCurrentItem(1);
See the post here:
https://stackoverflow.com/a/38762670/7318519

You selected the last tab.
Take a look at your source code :)
TabLayout.Tab tab;
...
for (int i = 0; i < mTabs.getTabCount(); i++) {
tab = mTabs.getTabAt(i);
}
...
tab.select();
tab is the last tab you will get with getTabAt(int position).
So when you want to select the first tab this will help
maTabs.getTabAt(0).select();
Another (maybe better) solution is to turn the for loop into this
for (int i = mTabs.getTabCount() - 1; i >= 0; i--) {
tab = mTabs.getTabAt(i);
}
Then your "last" tab you are getting in this loop is the first tab in your TabLayout.
An working full example code looks like this gist.

Related

Setting View's atribute that is in child fragment

I want to access Views that are in Fragment's children that are put in ViewPager. The idea is to toggle (disable/enable) spinners when button is pressed. Spinners are placed in separate fragments that are displayed in ViewPager controlled by TabLayout:
// Snippet of code from Fragment A
final TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tabLayout);
final ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewPager);
Fragment childFragment = new Fragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.viewPager, childFragment).commit();
ViewPagerAdapter adapter = new ViewPagerAdapter(childFragment.getFragmentManager());
adapter.AddFragment(new fragment_unositelj(), "FragmentB");
adapter.AddFragment(new fragment_voditelj(), "FragmentC");
adapter.AddFragment(new fragment_pratitelj(), "FragmentD");
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
FragmentB = viewPager.findViewById(R.id.FragmentB);
FragmentC = viewPager.findViewById(R.id.FragmentC);
FragmentD = viewPager.findViewById(R.id.FragmentD);
I don't know how to achieve to set Spinner disabled / enabled in all fragment children's spinner. I tried to set public variable in MainActivity and to control Spinners attribute based on that, but I am not sure in which lifecycle part to place it because fragment B is inflated initialy, and others are when TabItem clicked. I hope I could describe my problem properly. If anything more is needed please let me know. Thanks!
I found workaround for this problem, I am not sure if it is correct way, but for now it works for me. In FragmentB,FragmentC,FragmentD i added
#Nullable
#Override
public View getView() {
if (((MainActivity) getActivity()).getStatus() == 1) {
mySpinner.setEnabled( true );
} else
mySpinner.setEnabled( false );
return super.getView();
}
//(MainActivity) getActivity()).getStatus() - method that returns state if it shold be enabled

android disable / enable tab in tabbedActivity

I can not disable a tab in android tabbed activity.
The tabbed activity have 3 tabs and I want to disable the tab in the middle.
I tried the following code in my Fragment but the variable middleTabView is always null!
TabLayout tabhostNew = (TabLayout) getActivity().findViewById(R.id.tabs);
TabLayout.Tab middleTabView = tabhostNew.getTabAt(1).getCustomView();
middleTabView.setEnabled(false); //does not work, because middleTabView is null
The following code should work, but i am not able to get the variable tabwidget.
tabHost.getTabWidget().getChildTabViewAt(your_index).setEnabled(false);
Can you please help me?
Thank you in advance!
The method you are trying to call getTabWidget() is implemented in the TabHost class and not in TabLayout (which you are using).
Check out this answer:
TabLayout tabLayout = (TabLayout) getActivity().findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager); // if you are using a view pager
LinearLayout tabStrip = ((LinearLayout)mTabLayout.getChildAt(0));
for(int i = 0; i < tabStrip.getChildCount(); i++) {
tabStrip.getChildAt(i).setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
}

How to use public method removeTab(TabLayout.Tab tab)?

Checking [https://developer.android.com/reference/android/support/design/widget/TabLayout.html][1] I created my tabs
tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Tab 1 Item").setIcon(android.R.drawable.ic_dialog_email).setTag("tt"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 2 Item").setIcon(android.R.drawable.ic_btn_speak_now));
tabLayout.addTab(tabLayout.newTab().setText("Tab 3 Item").setIcon(android.R.drawable.ic_lock_idle_low_battery));
tabLayout.addTab(tabLayout.newTab().setText("Tab 4 Item").setIcon(android.R.drawable.ic_dialog_alert));
but then when I try to remove the tab I am getting error. My simple question is how can I use the method removeTab(TabLayout.Tab tab) ?
for example:
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
TabLayout.Tab tab1 = tabLayout.newTab().setText("Tab 1 Item").setIcon(android.R.drawable.ic_dialog_email).setTag("tt");
tabLayout.addTab(tab1);
TabLayout.Tab tab2 = tabLayout.newTab().setText("Tab 2 Item").setIcon(android.R.drawable.ic_btn_speak_now);
tabLayout.addTab(tab2);
tabLayout.removeTab(tab1); // remove first tab
Create your tab outside the function and then add and remove them as you wish.
Something like :
TabLayout t = new TabLayout();
t.setText("test");
...
add(t);
remove(t);

View Pager page not refreshing?

I have used SlidingTabLayout and Viewpager to make sliding Tab in fragement. And i have implemented it successfully.
My code for adding the fragment under view pager
void setUpFragementPager(View view) {
List<Fragment> fragments = new Vector<Fragment>();
MyFragmentAdapter mPagerAdapter;
//for each fragment you want to add to the pager
Bundle page = new Bundle();
page.putString("url", "d");
fragments.add(Fragment.instantiate(getActivity(),
AppFragement.class.getName(), page));
fragments.add(Fragment.instantiate(getActivity(),
CatFragement.class.getName(), page));
fragments.add(Fragment.instantiate(getActivity()
,FavFragement.class.getName(),page));
//after adding all the fragments write the below lines
mViewPager=(ViewPager)view.findViewById(R.id.viewpager);
mPagerAdapter = new
MyFragmentAdapter(super.getFragmentManager(), fragments);
mViewPager.setAdapter(mPagerAdapter);
mSlidingTabLayout = (SlidingTabLayout)
view.findViewById(R.id.sliding_tabs);
mSlidingTabLayout.setViewPager(mViewPager);
if (mParam1.toString().equals("1")){
mViewPager.setCurrentItem(1);
mPagerAdapter.notifyDataSetChanged();
}
else
mViewPager.setCurrentItem(0);
}
Now when i click on MyLibrary button (Image 2)
under drawer the Tab should change to CATEGORY but what happen is currently now is that , white strip is going under the category tab but the CatFragement is not loading under the same tab(category tab). You can see it in the snapshot (image 3)
I used
mViewPager.setCurrentItem(1);
mPagerAdapter.notifyDataSetChanged();
but it leading me to the my desired result . The Desired Result should be look like image4 which you can see in the snapshot too.
Please help me where i am doing wrong.

changing action bar title for every swipe api level < 11

I couldn't manage to change the action bar title for every swipe action, there's always a problem. Either they get mixed or just some of them does not show up. Here are my tab codes:
actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
SectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
ViewPager = (ViewPager) findViewById(R.id.viewpager);
ViewPager.setAdapter(mSectionsPagerAdapter);
ViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
Tab tab = actionBar.newTab()
.setIcon(R.drawable.home)
.setTabListener(this);
.
.
.
see answer below before going further into the question.
You can try using this in your Fragment's onResume method :
if(getUserVisibleHint()){
getActionBar().setTitle(title);
}
defining String title as a variable inside your fragment.
You should read the documentation on it: http://developer.android.com/training/implementing-navigation/lateral.html#horizontal-paging
and
http://developer.android.com/training/implementing-navigation/lateral.html#swipe-tabs
Download the source code and use it. It worked for me. I had the same problem.

Categories

Resources