Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set icon size in TabLayout?

Here is my code:

private TabLayout tabLayout;
private int[] tabIcons = {
        R.mipmap.ic_compass,
        R.mipmap.ic_place,
        R.mipmap.ic_passport,
        R.mipmap.ic_setting
};


...
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
tabLayout.getTabAt(3).setIcon(tabIcons[3]);

The size of icon is according to image size. How could I resize it?

like image 653
Junior Frogie Avatar asked Jan 08 '16 04:01

Junior Frogie


People also ask

How do I change the icon size on my Android tablet?

Change icon size on Android devices by going to Settings > Wallpaper & style > App grid and changing it to a grid with fewer or more columns and rows.

How to add tab in tabLayout android?

Tabs are created using the newTab() method of TabLayout class. The title and icon of Tabs are set through setText(int) and setIcon(int) methods of TabListener interface respectively. Tabs of layout are attached over TabLayout using the method addTab(Tab) method.


2 Answers

set icons padding

    for (int i = 0; i < tablayout.getTabWidget().getChildCount(); i++)
    {
        tablayout.getTabWidget().getChildAt(i).setPadding(10,10,10,10);
    }
like image 139
Rooban Avatar answered Oct 15 '22 02:10

Rooban


If you don't want to use xml for the custom view. Simply create imageview on runtime and add that in tablayout in a particular tab.

  ImageView imgView= new ImageView(MainActivity.this);
  imgView.setImageResource(drawableImage);
  imgView.setPadding(10,10,10,10)
  tabLayout.getTabAt(1).setCustomView(imgView);

It will look like this.

enter image description here

like image 23
Deepak Sachdeva Avatar answered Oct 15 '22 03:10

Deepak Sachdeva