Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 457
midlab Avatar asked Apr 28 '17 19:04

midlab


People also ask

How do I add icons to my browser tab?

To add a favicon to your website, either save your favicon image to the root directory of your webserver, or create a folder in the root directory called images, and save your favicon image in this folder. A common name for a favicon image is "favicon.ico".

What are the icons called in tabs?

A favicon (/ˈfæv. ɪˌkɒn/; short for favorite icon), also known as a shortcut icon, website icon, tab icon, URL icon, or bookmark icon, is a file containing one or more small icons, associated with a particular website or web page.


2 Answers

Try this:

tabLayout.setupWithViewPager(mViewPager);
for (int i = 0; i < mTabLayout.getTabCount(); i++) {
    tabLayout.getTabAt(i).setIcon(R.drawable.your_icon);
}
like image 199
Jonathan Aste Avatar answered Oct 05 '22 08:10

Jonathan Aste


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

like image 25
William Avatar answered Oct 05 '22 09:10

William