Android How to add Tabs dynamically from json using viewpager - android

How to add Tabs dynamically from json using viewpager at load page on Mainactivity and show corresponding data on gridview.
I already try this ,just static method.
can i add dynamically tabs from json? and it's data corresponding.
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
if (viewPager != null) {
setupViewPager(viewPager);
}
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
ViewPager viewPager1 = (ViewPager) findViewById(R.id.viewpager1);
if (viewPager1 != null) {
setupViewPager1(viewPager1);
}
TabLayout tabLayout1 = (TabLayout) findViewById(R.id.tabs1);
tabLayout1.setupWithViewPager(viewPager1);
}
public void setTabFragmentTwo(String t){
TabFragmentTwo = t;
}
public String getTabFragmentTwo(){
return TabFragmentTwo;
}
private void setupViewPager(ViewPager viewPager) {
Adapter adapter = new Adapter(getSupportFragmentManager());
adapter.addFragment(new OneFragment(), "Category 1");
adapter.addFragment(new TwoFragment(), "Category 2");
adapter.addFragment(new ThreeFragment(), "Category 3");
adapter.addFragment(new ThreeFragment(), "Category 3");
adapter.addFragment(new ThreeFragment(), "Category 3");
adapter.addFragment(new ThreeFragment(), "Category 3");
viewPager.setAdapter(adapter);
}
static class Adapter extends FragmentPagerAdapter {
private final List<Fragment> mFragments = new ArrayList<>();
private final List<String> mFragmentTitles = new ArrayList<>();
public Adapter(FragmentManager fm) {
super(fm);
}
public void addFragment(Fragment fragment, String title) {
mFragments.add(fragment);
mFragmentTitles.add(title);
}
#Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
#Override
public int getCount() {
return mFragments.size();
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitles.get(position);
}
}
private void setupViewPager1(ViewPager viewPager) {
Adapter1 adapter1 = new Adapter1(getSupportFragmentManager());
adapter1.addFragment(new Fragment_quick_bill(), "Quick Bill");
adapter1.addFragment(new Fragment_addvnced_bill(), "Addvanc-Bil");
viewPager.setAdapter(adapter1);
}
static class Adapter1 extends FragmentPagerAdapter {
private final List<Fragment> mFragments = new ArrayList<>();
private final List<String> mFragmentTitles = new ArrayList<>();
public Adapter1(FragmentManager fm) {
super(fm);
}
public void addFragment(Fragment fragment, String title) {
mFragments.add(fragment);
mFragmentTitles.add(title);
}
#Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
#Override
public int getCount() {
return mFragments.size();
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitles.get(position);
}
}

You can easily add tabs dynamically:
private void addTab(String title) {
mTabLayout.addTab(mTabLayout.newTab().setText(title));
mPagerAdapter.addTabPage(title);
}
and addTabPage method to your PagerAdapter(tabItems are item list).
public void addTabPage(String title) {
tabItems.add(title);
notifyDataSetChanged();
}
// complete class(check how using for loop I am able to add 3 tabs dynamically now you can call above mentioned method wherever you need like getting category list from api or using button click etc.)
public class MainActivity extends AppCompatActivity {
TabLayout mTabLayout;
ViewPager mViewPager;
Adapter mPagerAdapter;
List<String> tabItems = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager = (ViewPager) findViewById(R.id.viewpager);
mTabLayout = (TabLayout) findViewById(R.id.tabs);
mPagerAdapter = new Adapter(getSupportFragmentManager());
mViewPager.setAdapter(mPagerAdapter);
for(int i=1; i<=3; i++){
addTab("Tab"+ i);
}
mTabLayout.setupWithViewPager(mViewPager);
}
private void addTab(String title) {
mTabLayout.addTab(mTabLayout.newTab().setText(title));
addTabPage(title);
}
public void addTabPage(String title) {
tabItems.add(title);
mPagerAdapter.notifyDataSetChanged();
}
class Adapter extends FragmentPagerAdapter {
public Adapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return OneFragment.newInstance("param1","param2");
}
#Override
public int getCount() {
return tabItems.size();
}
#Override
public CharSequence getPageTitle(int position) {
return tabItems.get(position);
}
}
}

Related

Content of tabs not displaying

After failing to stop reloading the fragments on tab change by using fragment manager, I implemented it using viewpager.
But the content doesn't display in the tabs. Everything seems to be correct. I have been trying for hours, and unable to figure out what the problem is.
This is the code for my activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.audit_form_activity);
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
button = (Button) findViewById(R.id.button);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setOffscreenPageLimit(no_of_categories);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setOnTabSelectedListener(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){
}
});
}
private void setupViewPager(ViewPager viewPager) {
// Outside Loop
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
// Get the JSON Data from DataSource
try {
JSONObject pageObj = new JSONObject(page);
JSONArray auditQuestionsArray = pageObj.getJSONArray("AuditQuestions");
int auditQuestionsArrayLength = auditQuestionsArray.length();
no_of_categories = auditQuestionsArrayLength;
// Outer-Loop Through the CategoryNames to Create Fragments for each CategoryName.
for (int i = 0; i < no_of_categories; i++) {
JSONObject auditQuestionsArrayObject = auditQuestionsArray.getJSONObject(i);
category = auditQuestionsArrayObject.getString("categoryName");
//JSONArray questionListArray = auditQuestionsArrayObject.getJSONArray("QuestionList");
Tab1 myFrag = Tab1.newInstance(category);
adapter.addFrag(myFrag, category);
}
} catch (JSONException e) {
e.printStackTrace();
}
viewPager.setAdapter(adapter);
}
public void bringBackBoolean(Boolean booleanReceived) {
//bList.add(booleanReceived);
}
This is the code for my pager adapter which is the inner class in the earlier activity:
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
Please help me figure out the problem.

how make Sliding tabs make clickable in Android Development?

I have made a sliding tabs feature which is currently working. I also want to make it clickable
public class MainActivity extends AppCompatActivity {
public List<String> datafragmnt = new ArrayList<String>();
public ViewPager viewPager;
public pagerfragmetAdapter myAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
datafragmnt.add("Frgmnt 1 ");
datafragmnt.add("Frgmnt 2 ");
datafragmnt.add("Frgmnt 3 ");
datafragmnt.add("Frgmnt 4 ");
myAdapter = new pagerfragmetAdapter(getSupportFragmentManager());
myAdapter.setMydata(datafragmnt);
myAdapter.setFragmntList(new fragment1());
myAdapter.setFragmntList(new fragment2());
myAdapter.setFragmntList(new fragment3());
myAdapter.setFragmntList(new fragment4());
viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPager.setAdapter(myAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
}
This is what I have tried so far. Any help is welcome!
Please help me!
You Need to create an Adapter for that view page,something like this:
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
And in the SetUpViewPager you add your fragments like this:
private void setupViewPager(ViewPager viewPager) {
Main.ViewPagerAdapter adapter = new Main.ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new fragment1(), "FRAGN+MENT UNO");

I have TabLayout in Fragment and then used FragmentTransaction to intent from Main Activity to Fragment

I have TabLayout in Fragment and then used FragmentTransaction to intent from Main Activity to Fragment, But When Transaction from Activity to Fragment Show this Problem.
toolbar = (Toolbar) view.findViewById(R.id.Toolbar);
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
// setSupportActionBar(toolbar);
// getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager)view. findViewById(R.id.viewpager1);
setupViewPager(viewPager);
tabLayout = (TabLayout) view.findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
// setupTabIcons();
return view;
}
// Add Fragments to Tabs
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
// ViewPagerAdapter adapter = new ViewPagerAdapter(getActivity().getFragmentManager());
adapter.addFrag(new AllFragment() ,"All");
adapter.addFrag(new ManFragment(),"Man");
adapter.addFrag(new WomenFragment(),"Women");
adapter.addFrag( new KidsFragment(),"Kids");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public android.support.v4.app.Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
MainActivivty :

How do I add icons to my tab layout activity

How do i add icons to the side of my page title in tab layout? This is a new created tab activity, so the code is already generated by android studio. I just don't know how to add icons beside my title. Thanks
Here is the code of my main activity.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1);
}
#Override
public int getCount() {
return 4;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Schedule";
case 1:
return "News";
case 2:
return "Games";
case 3:
return "Standings";
}
return null;
You can add icons to tab layout by:
tabLayout.addTab(tabLayout.newTab().setText("Tab1").setIcon(R.mipmap.ic_launcher));
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
private int[] tabIcons = {
R.drawable.ic_tab_favourite,
R.drawable.ic_tab_call,
R.drawable.ic_tab_contacts
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
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) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new OneFragment(), "ONE");
adapter.addFrag(new TwoFragment(), "TWO");
adapter.addFrag(new ThreeFragment(), "THREE");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}

How to do tabs with icons

I'm trying to do an activity with tabs and pagerview
I found this code in a blog but it does not come like making the tabs have icons instead of text ... I put the code:
ViewPagerAdapter.java
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
Tablayout_in_Android.java
public class Tablayout_in_Android extends Fragment {
public Tablayout_in_Android() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_tablayout_in__android, container, false);
}
}
Method onCreate main class
viewPager = (ViewPager) findViewById(R.id.viewpager);
addTabs(viewPager);
tabLayout = (TabLayout) findViewById(R.id.pestaƱas);
tabLayout.setupWithViewPager(viewPager);
}
private void addTabs(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new Tablayout_in_Android(), "ONE");
adapter.addFrag(new Tablayout_in_Android(), "TWO");
adapter.addFrag(new Tablayout_in_Android(), "THREE");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
how can i change text for icons? Thanks
Try this:
tabLayout.setupWithViewPager(mViewPager);
for (int i = 0; i < mTabLayout.getTabCount(); i++) {
tabLayout.getTabAt(i).setIcon(R.drawable.your_icon);
}
To expand on Jonathan's answer, you can also have a list of icons to show in the tabs. Ensure the icons match the number of tabs you have.
tabLayout.setupWithViewPager(mViewPager);
int[] tabIcons = {
R.drawable.ic_icon_1,
R.drawable.ic_icon_2,
R.drawable.ic_icon_3
};
for(int i=0; i<tabLayout.getTabCount(); i++){
if(tabLayout.getTabAt(i) != null){
tabLayout.getTabAt(i).setIcon(tabIcons[i]);
}
}
Also, you may want to have the icon and tab label to be in a horizontal orientation, the use app:tabInlineLabel="true" in the TabLayout's xml layout

Categories

Resources