문제

I have tabbar with 5 tabs and i have added badge on my 4th tab. i want to update my badge value after some operation perform with the server. but don't know how can do this. Also i want update badge value from different activities. snippet of code adding badge on Tabbar

TabWidget tabs = (TabWidget) findViewById(android.R.id.tabs);
badge = new BadgeView(context, tabs, 3);
badge.setTextSize(12);
badge.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);
badge.setText(pref.getString("balance", "0"));
badge.toggle();

Thank you in Advance

도움이 되었습니까?

해결책

create static TabWidget object tab in TabActivity like this,

public static TabWidget tabs;

Access the tabs object from any activity after your operations performs, update your balance in SharedPreferences. and use following code snippet.

In your other Activity.

TabWidget tabs = TabActivity.tabs;
badge = new BadgeView(context, tabs, 3);
badge.setTextSize(12);
badge.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);
badge.setText(pref.getString("balance", "0"));
badge.toggle();

Hope this will help you.

다른 팁

To save the shared preference you have to do it like this. Sort of like how you loaded it. You need to use the same preferences.

        SharedPreferences prefs = mContext.getSharedPreferences("PREF_KEY", 0);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString("balance", balance_value);
        editor.commit();

Share some of my try

If your view is layout like this

android:layout_width="0dp"<br>
android:layout_height="wrap_content"<br>
android:layout_weight="1"<br>

BadgeView wil blank your view.

How to slove? patch

android.view.ViewTreeObserver.OnGlobalLayoutListener

like this :-

ViewTreeObserver vto = container.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout() { 
        container.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
        int width  = container.getMeasuredWidth();
        int height = container.getMeasuredHeight(); 

        LayoutParams params = target.getLayoutParams();
        params.width = width;
        params.height = height - BadgeView.this.getHeight();
        target.setLayoutParams(params);             
    } 
});
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top