I have a Tabhost in an activity that hosts fragments in each tab. The fragments have a list View that is populated with some elements.
Every Tab has a badge which reflects the no of items in the list in the fragment. I am using Badge from the library Badge Library . Now, the items in the list may change upon an action (for eg delete or add ).
I want the badge for each tab to be updated with every insertion or deletion in their lists. I tried adding a new Badge each time there is an updation in list using following code:-
TabWidget tabs = (TabWidget) getActivity().findViewById(android.R.id.tabs);
BadgeView allBadge = new BadgeView(getActivity(), tabs, 2);
BadgeView learntBadge = new BadgeView(getActivity(), tabs, 1);
BadgeView newBadge = new BadgeView(getActivity(), tabs, 0);
allBadge.setText(String.valueOf(allWords.getCount()));
learntBadge.setText(String.valueOf(learntWords.getCount()));
newBadge.setText(String.valueOf(newWords.getCount()));
allBadge.toggle();
learntBadge.toggle();
newBadge.toggle();
But this did not work. Any guesses what am I doing wrong here ?
The Badge Views that are being displayed on TabHost tabs can be made class members of Activity to which Tab Host belongs. The Activity class can have a update member function that takes as input the text to be shown by Badge Views and updates the text being displayed by these BadgeViews with the function input. At the Fragment side, the fragments can get a handle to the parent activity through getActivity and call the update member function of activity while passing the new values to be displayed on Badge View.
Adding BadgeViews to activity
The function to update BadgeViews in Activity
The call to Update BadgeViews from Fragment