Customize new TabLayout in support design library - android

HI i am trying to add image to the new TabLayout introduced in android support design. But it doesn't seems to be working i took some code github and tried to do some little change except for text nothing is workin no icon is showing on tha tab instead of text.
Any help is appreciated.
List<String> titles = new ArrayList<>();
titles.add("Page One");
titles.add("Page Two");
mTabLayout.addTab(mTabLayout.newTab().setIcon(getResources().getDrawable(R.drawable.ic_launcher)));
mTabLayout.addTab(mTabLayout.newTab().setIcon(getResources().getDrawable(R.drawable.ic_launcher)));
List<Fragment> fragments = new ArrayList<>();
fragments.add(new AnimatedFragment());
fragments.add(new AnimatedFragment());
FragmentAdapter adapter =
new FragmentAdapter(getSupportFragmentManager(), fragments, titles);
viewPager.setAdapter(adapter);
viewPager.setOffscreenPageLimit(0);
mTabLayout.setupWithViewPager(viewPager);
mTabLayout.setTabsFromPagerAdapter(adapter);
This is the code where i setup tablayout and viewpager.

You can add icons to tablayout from viewPager's getPageTitle method. Here is a good tutorial tutorial to do so that I tried.

Related

Android TabLayout always shows first tab indicator irrespective of which tab is selected

enter image description hereHere, my first tab selection indicator is always visible
I have added TabLayout inside a fragment. But indicator of first tab is always visible even though I have selected other tabs. Please find the screen shot.
It will be of great help if anybody can suggest on what mistake I am making.
Please find the below code:
ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
Fragment frag1 = new MyFragment1();
adapter.addFragment(frag1, getString(R.string.frag_video));
FragmentMediaPhotos frag2 = new MyFragment2();
adapter.addFragment(frag2, getString(R.string.frag_photo));
FragmentMediaMusic frag3 = new MyFragment3();
adapter.addFragment(frag3, getString(R.string.frag_audios));
mViewPager.setAdapter(adapter);
mTabLayout.setupWithViewPager(mViewPager);
After this setting three layout like this:
TextView tabOne = (TextView) LayoutInflater.from(mContext).inflate(R.layout.layout_tab_text_view, null);
tabOne.setText("Video");
mTabLayout.getTabAt(0).setCustomView(tabOne);
My activity was extended from BaseActivity and I kept BaseActivity as abstract.
Keeping BaseActivity abstract caused the issue.

Dynamic Tab with Fragment

total novice,but bit familiar with static tabs addition to view pager.But here I stuck in dynamic tabs which I am able to show but not the fragments of those dynamically received tabs.How would I add fragments to those dynamically received tabs from server.Pardon me spelling and could be ambiguous question
Follow this link example and change the following method.
Visit https://www.androidhive.info/2015/09/android-material-design-working-with-tabs/
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
for(int i=0;i<NUM_OF_FRAGMENT;i++){
adapter.addFragment(new YourFragment(), "Title"+i);
}
viewPager.setAdapter(adapter);
}
You need only one fragment for creating dynamic tab.

How to create ViewPager tabs in Android (modern way)

I sucessfully created my first ViewPager slider. When I went to the creation of the tabs, I've found that this article iws deprecated. I also found that I can use PagerTitleStrip, which worked but its too ugly. I've found a library for customizing it but I needed to learn how to do it the hard way, because I want to understand how Android works. Could somebody help me understanding how it works?
Here is a guide on Google's suggested usage of tabs in Material Design.
In particular, the TabLayout class from the support design libraries might be what you're after.
Its defaults are very natural, like shown below (taken from the Material Design link above).
try this
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
private void setUpViewPager(ViewPager viewPager)
{
Adapter adapter=new Adapter(getSupportFragmentManager());
adapter.addFragment(new About(),"Article 1");
adapter.addFragment(new About(),"Article 2");
adapter.addFragment(new About(),"Article 3");
viewPager.setAdapter(adapter);
}
Refer here:https://coderzpassion.com/working-appbarlayout-like-whatsapp/
Try this :-
Add the PageTabStrip element as a child of your ViewPager
<android.support.v4.view.ViewPager
android:id="#+id/myViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/navigation_toolbar">
<android.support.v4.view.PagerTabStrip
android:id="#+id/pager_tab"
style="#style/AppToolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"/>
</android.support.v4.view.ViewPager>
Make sure you implement the below method in your Custom Adapter extending FragmentPagerAdapter :
#Override
public CharSequence getPageTitle(int position) {
switch (position){
case 0:
return "Title 1";
case 1:
return "Title 2";
default:
return "null";
}
}

Create Multiple Fragments Dynamically for Dynamic Tabs

I have a requirement and I do not know how to accomplish this. Can you guys help me out to get this done. Below is what the flow is
I have a Main Category list, user can tap to get sub categories (fetch from server) and based on the result I am displaying a list of subcategories with their child category as well. Next I am creating tabs based on the child categories.
Now my problem is creating fragments for that number of tabs. Childs can be from 1 to n number, so I would like to know the best way to create fragments dynamically. Once each fragment I have to display a list of items that would be fetched from server.
Thank you
You can use the following code snap
You will need to create corresponding layout xml
ViewPager pager;
ViewPagerAdapter adapter;
private List<Fragment> fragments;
ArrayList<String> Titles;
#Override
protected void onCreate(Bundle savedInstanceState) {
fragments = new ArrayList<Fragment>();
Titles= new ArrayList<String>();
pager = (ViewPager) findViewById(R.id.pager);
adapter = new ViewPagerAdapter(getSupportFragmentManager(), Titles, fragments);
pager.setAdapter(adapter);
for (int i=0;i<numoffragments;i++){
fragments.add(new DemoFragment());
}
adapter.notifyDataSetChanged();
}
You may need to do some more adjustments to get the numoffragments you will be using and also you have to make a fragment that can be used for all the childs
Hope this wil help.

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.

Categories

Resources