How to set icon size in TabLayout? - android

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?

set icons padding
for (int i = 0; i < tablayout.getTabWidget().getChildCount(); i++)
{
tablayout.getTabWidget().getChildAt(i).setPadding(10,10,10,10);
}

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.

change of Icon size android.support.design.widget.TabLayout
for (int i = 0; i < view_bottom_tabLayout.getTabCount(); i++)
{
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(60, 60); //set new width & Height
params.gravity = Gravity.CENTER; //set gravity back to center
view_bottom_tabLayout.getChildAt(i).setLayoutParams(params);//set ur new params
}

To get total control of size I recommend using a custom view to render the tab.
First create a new layout - named "my_custom_tab" for this example
<?xml version="1.0" encoding="utf-8"?>
<!--Change the width, height, scaleType to whatever you like-->
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:id="#+id/icon"
android:layout_gravity="center_horizontal"
android:src="#mipmap/ic_launcher"/>
Now in your code set the custom image
private void setUpTabs (ViewPager viewPager) {
TabLayout tabs = (TabLayout) findViewById(R.id.tabs);
if (tabs != null) {
tabs.setupWithViewPager(viewPager);
int tabCount = tabAdapter.getCount(); //Assuming you have already somewhere set the adapter for the ViewPager
for (int i = 0; i < tabCount; i++) {
TabLayout.Tab tab = tabs.getTabAt(i);
if (tab != null){
ImageView myCustomIcon = (ImageView) LayoutInflater.from(tabs.getContext()).inflate(R.layout.my_custom_tab, null);
/*Here is where to set image if doing it dynamically
myCustomIcon.setImageBitmap(bitmap);
*/
tab.setCustomView(myCustomIcon);
}
}
}
}

you can do this:
LinearLayout ll = (LinearLayout) tabLayout.getChildAt(0);
for (int i = 0; i < ll.getChildCount(); i++) {
LinearLayout tabView = (LinearLayout) ll.getChildAt(i);
for (int j = 0; j < tabView.getChildCount(); j++) {
if (tabView.getChildAt(j) instanceof TextView) {
TextView textView = (TextView) tabView.getChildAt(j);
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) textView.getLayoutParams();
layoutParams.topMargin = 0;
textView.setLayoutParams(layoutParams);
} else if (tabView.getChildAt(j) instanceof ImageView) {
ImageView imageView = (ImageView) tabView.getChildAt(j);
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) imageView.getLayoutParams();
layoutParams.bottomMargin = 0;
imageView.setLayoutParams(layoutParams);
}
}
}

I decide to close this question. Since we actually cannot set specific size for icon in TabLayout but padding to it.
Ref:
https://developer.android.com/guide/practices/ui_guidelines/icon_design_tab

one code worked for me, the answer above of Deepak Sachdeva
just i edited it a litlle, try it 100% working
from first i add the tab with custom image
ImageView imgView= new ImageView(getApplicationContext());
imgView.setImageResource(R.drawable.icon);
imgView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imgView.setLayoutParams(new LinearLayout.LayoutParams(100, 100));
tablayout1.addTab(tablayout1.newTab().setCustomView(imgView));
and do that to all your tabs, add them by this way.

Related

Android Create multiple ImageViews in a loop ... strange behavior

I am creating multiple ImageViews dynamiclly in a loop.
Every ImageView has a different position and a different image.
Finally i add them to a FrameLayout.
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(container.getWidth() / 3, container.getHeight() / 3);
ImageView imageView;
for(int i=0; i<cloths.size(); i++) {
imageView = new ImageView(getActivity(), null);
imageView.setAdjustViewBounds(true);
Glide.with(getActivity()).load(cloths.get(i).getImage()).into(imageView);
imageView.setOnTouchListener(touchListener);
params.leftMargin = (int) cloths.get(i).getxPos(container.getWidth());
params.topMargin = (int) cloths.get(i).getyPos(container.getHeight());
params.rightMargin = 0;
params.bottomMargin = 0;
imageView.setScaleY(cloths.get(i).getScale());
imageView.setScaleX(cloths.get(i).getScale());
container.addView(imageView, params);
}
Instead of positioning all imageviews correctly, they are all laying on top of each other at on the position of the last imageview.
Any ideas how to fix it ?
What am i doing wrong ?
Assuming you know the desired position and size (getyPos() suggest you do), you can try aggregating all the sizes so the images will go one over the other. If you don't know the sizes, you can use Glide to help you find them (or just measure the views after the image arrives).
public void putImages(FrameLayout container){
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(container.getWidth() / 3, container.getHeight() / 3);
int topMargin = 0;
for(int i=0; i<cloths.size(); i++) {
ImageView imageView = new ImageView(getActivity(), null);
imageView.setAdjustViewBounds(true);
Glide.with(getActivity()).load(cloths.get(i).getImage()).into(imageView);
imageView.setOnTouchListener(touchListener);
params.leftMargin = (int) cloths.get(i).getxPos(container.getWidth());
params.topMargin = topMargin;
params.rightMargin = 0;
params.bottomMargin = 0;
topMargin += (int) cloths.get(i).getyPos(container.getHeight());
imageView.setScaleY(cloths.get(i).getScale());
imageView.setScaleX(cloths.get(i).getScale());
container.addView(imageView, params);
}
}

android correct padding between views programmatically

I programmatically created four Textviews and added it in my Linearlayout.my linearlayout has 216dp width and TextView which I created pro-grammatically has 48 dp width and height.
I want to add padding between TextView. I wrote some code but I received this result
this is a my code
for (int i = 0; i < mDigits; i++) {
DigitView digitView = new DigitView(getContext());
digitView.setWidth(valueInPixels);//48dp
digitView.setHeight(valueInPixels);//48dp
digitView.setTextColor(Color.WHITE);
digitView.setTextSize(mDigitTextSize);
digitView.setGravity(Gravity.CENTER);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
digitView.setElevation(mDigitElevation);
}
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
if (i > 0) {
lp.leftMargin = 10;//problem is this line
}
childView.addView(digitView, lp);//childview 216dp
}
how i can to create correct padding between views programmatically?
if anyone has solution please help me
thanks.
First add appropriate android:paddingLeft to your childview I added as below:
<LinearLayout
android:id="#+id/childView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:background="#000"
android:orientation="horizontal">
Then edit you code as below
for (int i = 0; i < 4; i++) {
DigitView digitView = new DigitView(this);
digitView.setWidth(valueInPixels);//48dp
digitView.setHeight(valueInPixels);//48dp
digitView.setTextColor(Color.WHITE);
digitView.setBackgroundColor(Color.YELLOW);
digitView.setTextSize(mDigitTextSize);
digitView.setGravity(Gravity.CENTER);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
digitView.setElevation(mDigitElevation);
}
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.rightMargin = 10;//problem is this line
childView.addView(digitView, lp);//childview 216dp
}
Add padding on view programmatically
Here you have the solution to create a padding in java code.
Try this,
public void setPadding (int left, int top, int right, int bottom)
view.setPadding(0,padding,0,0);

Tabhost- set the badge position in tabs Android

I am new to badge's concept. In my application i want to show the badges on one tab. For that i used the android-viewbadger.jar file Android ViewBadger
it is working fine with 4 tabs,
TabWidget tabs = (TabWidget) findViewById(android.R.id.tabs);
badge1 = new BadgeView(this, tabs, 1);
badge1.setText("155");
badge1.setBadgePosition(BadgeView.POSITION_BOTTOM_RIGHT);
badge1.toggle();
when i add one more tab here it will look like this
i have already use these badge properties
badge1.setPadding(left, top, right, bottom);
badge1.setTextSize(15);
badge1.setBadgeMargin(5,5);
badge1.setWidth(10);
Setting badge on TabWidget will only show the badge in the space available between the drawable and the boundary of TabWidget, so adding more tabs will compress the badge. Instead using setIndicator(String,Drawable), try this:
ImageView iv = new ImageView(this);
iv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
iv.setImageResource(R.drawable.whoseinterested);
whosespec.setIndicator(iv);
Intent whoseIntent = new Intent(this, BlankActivity.class);
whosespec.setContent(whoseIntent);
tabHost.addTab(whosespec);
badge1 = new BadgeView(this, iv);
badge1.setText("155");
badge1.setTextSize(15);
badge1.setBadgeBackgroundColor(Color.BLACK);
badge1.setTextColor(Color.WHITE);
badge1.toggle();
Screenshot:
You can clearly see in your screenshot that the image in the tab with badge is shifted towards the center. If there is no space between image and its tab boundary, it will not be displayed properly.
Source : Tested myself.
Hi set badge in child of view of tab in imageview
try this
TabWidget tabsw = (TabWidget) rootView.findViewById(android.R.id.tabs);
ViewGroup viewgroup = (ViewGroup) tabsw.getChildAt(0);
viewgroup.getChildCount();
for (int i = 0; i < viewgroup.getChildCount(); i++) {
if (viewgroup.getChildAt(i) instanceof ImageView) {
ImageView new_name = (ImageView) viewgroup.getChildAt(i);
badge7 = new BadgeView(getActivity(),new_name);
badge7.setText("9");
badge7.setTextSize(9);
badge7.setBadgeMargin(0,0);
badge7.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);
badge7.toggle();
}
}

Dynamically Adding LinearLayouts in Android

I'm trying to add LinearLayouts dynamically but it's just not working. I think I just need another set of eyes to look it over. Can anyone help me?
LinearLayout parentLayout = (LinearLayout)findViewById(R.id.parentLayout);
lLayout = new LinearLayout[8];
for(int i = 0; i < lLayout.length; i++) {
lLayout[i] = new LinearLayout(this);
lLayout[i].setId(i);
lLayout[i].setOrientation(LinearLayout.HORIZONTAL);
if(i%2 == 0) {
lLayout[i].setBackgroundColor(Color.GREEN);
} else {
lLayout[i].setBackgroundColor(Color.MAGENTA);
}
parentLayout.addView(lLayout[i]);
}
You need to set LayoutParams, try adding this:
lLayout[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
It looks like you are trying to make a listview yourself using linearlayouts rather than actually using the ListView already supported.
If you are actually trying to do that, you should first give the layouts inside the view a width and height. I'd also put the list in a scrollview in case it overflows the outer layout.
Maybe try this
LinearLayout parentLayout = (LinearLayout)findViewById(R.id.parentLayout);
lLayout = new LinearLayout[8];
for(int i = 0; i < lLayout.length; i++) {
lLayout[i] = new LinearLayout(this);
lLayout[i].setId(i);
lLayout[i].setOrientation(LinearLayout.HORIZONTAL);
if(i%2 == 0) {
lLayout[i].setBackgroundColor(Color.GREEN);
} else {
lLayout[i].setBackgroundColor(Color.MAGENTA);
}
LinearLayout.LayoutParams myLayoutParams = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
myLayoutParams.leftMargin = 0;
myLayoutParams.topMargin = 50 * i;
myLayoutParams.width = myScreenSize; //e.g. 480
myLayoutParams.height = 50;
lLayout[i].setLayoutParams(myLayoutParams);
parentLayout.addView(lLayout[i]);
}

Android: get height of tab bar?

Is there any way I can get the height of the tab bar in Android? Its getHeight() method seems to only return 0.
Thanks!
Try this in your code .
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
RelativeLayout relativeLayout =(RelativeLayout)tabHost.getTabWidget().getChildAt(i);
relativeLayout.getChildCount();
TextView tabhostText = (TextView) relativeLayout.getChildAt(1);
tabhostText.setTextSize(20.0f); // you can set the TextSize of your tab also
tabHost.getTabWidget().getChildAt(i).getLayoutParams().height = 30;
}
It will work .

Categories

Resources