I have made a sliding tab layout and there is an icon and text on each tab , spanned using "SpannableStringBuilder" so now it shows the icon and title together like one object, Now when a certain tab gets selected i want to replace that icon with another. My code is as follows:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 4;
private String tabTitles[] = new String[]{"Logs", "Rewards", "News", "Rank"};
private int myImageList[] = new int[]{R.drawable.logs, R.drawable.rewards, R.drawable.news, R.drawable.ranking};
private Context context;
ArrayList<Fragment> fragments = new ArrayList<>();
public SectionsPagerAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
this.fragments.add(ActivityList.newInstance(""));
this.fragments.add(OfferedRewardActivity.newInstance(""));
this.fragments.add(NewsActivity.newInstance(""));
this.fragments.add(RankActivity.newInstance(""));
}
@Override
public int getCount() {
return PAGE_COUNT;
}
@Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
public int getDrawableId(int position) {
return myImageList[position];
}
Drawable myDrawable;
String title;
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
myDrawable = getResources().getDrawable(R.drawable.logs);
title = tabTitles[0];
break;
case 1:
myDrawable = getResources().getDrawable(R.drawable.rewards);
title = tabTitles[1];
break;
case 2:
myDrawable = getResources().getDrawable(R.drawable.news);
title = tabTitles[2];
break;
case 3:
myDrawable = getResources().getDrawable(R.drawable.ranking);
title = tabTitles[3];
break;
default:
break;
}
SpannableStringBuilder sb = new SpannableStringBuilder(" " +" \n"+ title); // space added before text for convenience
try {
myDrawable.setBounds(1, 1, myDrawable.getIntrinsicWidth(), myDrawable.getIntrinsicHeight());
ImageSpan span = new ImageSpan(myDrawable, DynamicDrawableSpan.ALIGN_BASELINE);
sb.setSpan(span, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
} catch (Exception e) {
// TODO: handle exception
}
return sb;
}
}
and the code for populatetabscript() is:
private void populateTabStrip() {
final PagerAdapter adapter = mViewPager.getAdapter();
final OnClickListener tabClickListener = new TabClickListener();
for (int i = 0; i < adapter.getCount(); i++) {
View tabView = null;
TextView tabTitleView = null;
if (mTabViewLayoutId != 0) {
// If there is a custom tab view layout id set, try and inflate it
tabView = LayoutInflater.from(getContext()).inflate(mTabViewLayoutId, mTabStrip,
false);
tabTitleView = (TextView) tabView.findViewById(mTabViewTextViewId);
}
if (tabView == null) {
tabView = createDefaultTabView(getContext());
}
if (tabTitleView == null && TextView.class.isInstance(tabView)) {
tabTitleView = (TextView) tabView;
}
if (mDistributeEvenly) {
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) tabView.getLayoutParams();
lp.width = 0;
lp.weight = 1;
}
tabTitleView.setText(adapter.getPageTitle(i));
tabView.setOnClickListener(tabClickListener);
String desc = mContentDescriptions.get(i, null);
if (desc != null) {
tabView.setContentDescription(desc);
}
mTabStrip.addView(tabView);
if (i == mViewPager.getCurrentItem()) {
tabView.setSelected(true);
}
}
}
I have Googled alot but didn't find anything or maybe I left something or misunderstood. What should i do to achieve this?