Android : how to change the color of icon on tabs using action bar tabs when selected like Facebook

on Tuesday, July 29, 2014


I have add icons on tabs but not changing the color of icon when selecting . I want tabs like Facebook when i selected the the particular tab the icon color should changed. i did not find any solution on web Here is my code



@Override
public void onPageSelected(int position) {

// TODO Auto-generated method stub
actionBar.setSelectedNavigationItem(position);
int resIdLenght = resId.length;
if (position < 0 || position >= resIdLenght)
return;
int drawableId = resId[position];
/*actionBar.setIcon(drawableId);*/
actionBar.setTitle(drawableId);
}


@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub




}

@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub

}
});
for (int i = 0; i < adapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by
// the adapter. Also specify this Activity object, which implements
// the TabListener interface, as the callback (listener) for when
// this tab is selected.
actionBar.addTab(
actionBar.newTab().setIcon(getIcon(i))

.setTabListener(this));


}


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.option_menu, menu);
return true;
}

@Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub

}

@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction arg1) {
// TODO Auto-generated method stub
viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub


}
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
}
return null;
}

public int getIcon(int position)
{
switch (position) {
case 0:
return R.drawable.ic_action_download;

case 1:
return R.drawable.ic_action_search_light;

case 2:

return R.drawable.ic_action_settings;

case 3:
return R.drawable.ic_action_good;

}
return R.drawable.ic_action_search_light;
}

0 comments:

Post a Comment