Frage

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

War es hilfreich?

Lösung

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.

Andere Tipps

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);             
    } 
});
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top