How to add a tab to SlidingTabLayout? - android

I am using google's SlidingTabLayout in my view, but i want to add dynamically new tabs to it.
I'm using this http://developer.android.com/samples/SlidingTabsBasic/src/com.example.android.common/view/SlidingTabLayout.html
This is my code which initialize it:
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
//this ^ is a FragmentStatePagerAdapter
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
// Assiging the Sliding Tab Layout View
tabs = (SlidingTabLayout) findViewById(R.id.tabs);
// Setting the ViewPager For the SlidingTabsLayout
tabs.setViewPager(mViewPager);
I have absolutely no idea how to add a Tab.
I thought about changing the ViewPager but in the SlidingTabLayout there is a comment:
/**
* Sets the associated view pager. Note that the assumption here is that the pager content
* (number of tabs and tab titles) does not change after this call has been made.
*/
public void setViewPager(ViewPager viewPager) {

There's probably a nicer, more efficient way, but simply calling setViewPager() every time you add a page to your ViewPager will work.

Here is a nice tutorial. http://www.android4devs.com/2015/01/how-to-make-material-design-sliding-tabs.html
In short, you can supply them with your Pager Adapter. Override the getPageTitle method in order to supply something custom. E.g.
#Override
public CharSequence getPageTitle(int position) {
return _items[position].getDisplayName();
}

Related

Multiline PageTitle with android.support.design.widget.TabLayout

in an android app, I use a TabLayout to display multiple Fragments in a single activity.
I want the tabs to be labeled with icons and a two line heading.
In the Activity's onCreate I initialise the SectionsPageAdapter and the TabLayout like:
private SectionsPagerAdapter mSectionsPagerAdapter;
protected void onCreate(Bundle savedInstanceState) {
...
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new
SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
// add a PageChangeListener to be able to detect which tab is activated
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener(){
...
});
...
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
tabLayout.setTabGravity(TabLayout.GRAVITY_CENTER);
//Set icons for tabs
tabLayout.getTabAt(0).setIcon(R.drawable.ic0);
tabLayout.getTabAt(1).setIcon(R.drawable.ic1);
tabLayout.getTabAt(2).setIcon(R.drawable.ic2);
}
And in the SectionpagerAdapter I define the PageTitles using:
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "First \nTab";
case 1:
return "Second \nTab";
case 2:
return "Third \nTab";
}
return null;
}
But when i run the app, the PageTitles aren't broken in two lines, but shortened with ... to fit the screen.
How to enable multiline PageTitles for the Tabs?
tabLayout.getTabAt(i).setCustomView(yuorTabView);
And you can customize your own TabView

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.

How to style Android default ViewPager's bar, created from Android Studio template

I am trying to create a tabbed activity, and I'm using ViewPager for that (actually, Android Studio created it from template, I didn't even know what ViewPager was). I want to style the tab-bar below (indicated with red "circle"):
I've tried setting styles.xml tried almost everything, but still, nothing applies to that. Here is the code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set up the action bar.
final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
// When swiping between different sections, select the corresponding
// tab. We can also use ActionBar.Tab#select() to do this if we have
// a reference to the Tab.
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by
// the adapter. Also specify this Activity object, which implements
// the TabListener interface, as the callback (listener) for when
// this tab is selected.
actionBar.addTab(
actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
Ironically, the generated code (by Android Studio's latest version) has many deprecated method calls, but I don't want to touch them unless I know what exactly I'm doing. Here is the layout file (activity_main.xml), again, generated by Android Studio:
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="#+id/pager"
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context="MY_APP_IDENTIFIER"/>
How can I change the colors of the tab bar?
Try using onPageSelected it's probably the simplest way I think. Here is a link.

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.

How to change ViewPager's page?

I'm using ViewPager in my app and define it in the main Activity. Inside onCreate method I load some number of pages from SharedPreferences and then pass it to PagerAdapter:
#Override
public int getCount() {
return numberOfPages;
}
The problem is that if I would change this number in Preferences (or another Activity) to some other less then page index I viewed before, my app crashes because this index is out of bounds when I return to the activity with this ViewPager. It can be fixed simply by changing active ViewPager's page. Is there any way to do it?
I'm not sure that I fully understand the question, but from the title of your question, I'm guessing that what you're looking for is pager.setCurrentItem( num ). That allows you to programatically switch to another page within the ViewPager.
I'd need to see a stack trace from logcat to be more specific if this is not the problem.
slide to right
viewPager.arrowScroll(View.FOCUS_RIGHT);
slide to left
viewPager.arrowScroll(View.FOCUS_LEFT);
Without checking your code, I think what you are describing is that your pages are out of sync and you have stale data.
You say you are changing the number of pages, then crashing because you are accessing the old set of pages. This sounds to me like you are not calling pageAdapter.notifyDataSetChanged() after changing your data.
When your viewPager is showing page 3 of a set of 10 pages, and you change to a set with only 5, then call notifyDataSetChanged(), what you'll find is you are now viewing page 3 of the new set. If you were previously viewing page 8 of the old set, after putting in the new set and calling notifyDataSetChanged() you will find you are now viewing the last page of the new set without crashing.
If you simply change your current page, you may just be masking the problem.
for switch to another page, try with this code:
viewPager.postDelayed(new Runnable()
{
#Override
public void run()
{
viewPager.setCurrentItem(num, true);
}
}, 100);
Supplemental answer
I was originally having trouble getting a reference to the ViewPager from other class methods because the addOnTabSelectedListener made an anonymous inner class, which in turn required the ViewPager variable to be declared final. The solution was to use a class member variable and not use the anonymous inner class.
public class MainActivity extends AppCompatActivity {
TabLayout tabLayout;
ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 3"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
// don't use an anonymous inner class here
tabLayout.addOnTabSelectedListener(tabListener);
}
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) {
}
};
// The view pager can now be accessed here, too.
public void someMethod() {
viewPager.setCurrentItem(0);
}
}

Categories

Resources