Tabhost- set the badge position in tabs Android
Asked Answered
L

2

6

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();

enter image description here

when i add one more tab here it will look like this

enter image description here

i have already use these badge properties

    badge1.setPadding(left, top, right, bottom);
    badge1.setTextSize(15);
    badge1.setBadgeMargin(5,5);
    badge1.setWidth(10);
Lombardo answered 29/9, 2014 at 11:44 Comment(1)
kevinpelgrims.com/blog/2014/06/24/…Albuminous
A
1

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: screen

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.

Amato answered 30/9, 2014 at 12:14 Comment(1)
this is perfectly answer and work like a charm @Amato thank u so muchLombardo
C
1

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();

            }
        }
Camerlengo answered 26/10, 2014 at 21:48 Comment(1)
#26403248 have a look at this question @CamerlengoLombardo

© 2022 - 2024 — McMap. All rights reserved.