Android - highlight icon with SlidingTabLayout

330 views Asked by At

Currently I'm trying to implement the SlidingTab with icon as tabtitle, then the icon should be highlighted when the corresponding tab is selected.

I use Google's SlidingTabLayout in following tutorial, and have made the icon instead of the tabtitle as well. https://www.youtube.com/watch?v=WSaSNX5QI-E

I know where I can get the current tab position by implementing OnPageChangeListener, but I don't what should I add in public void onPageSelected(int position) {} here is my current status and code, please kindly advise, it will be appreciate if you keep the instruction as detail as possible, since I am a very beginner as a android developer.

thanks

MainActivity.java

public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {
// Declaring Your View and Variables
private Toolbar toolbar;
private ViewPager mPager;
private SlidingTabLayout mTabs;
private ViewPagerAdapter mAdapter;
private static Context context;
int brightIcons[]={R.drawable.abc_btn_switch_to_on_mtrl_00012,R.drawable.abc_btn_switch_to_on_mtrl_00012,R.drawable.abc_btn_switch_to_on_mtrl_00012,R.drawable.abc_btn_switch_to_on_mtrl_00012,R.drawable.abc_btn_switch_to_on_mtrl_00012,};



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mPager = (ViewPager) findViewById(R.id.pager);
    mAdapter = new ViewPagerAdapter(getSupportFragmentManager(), getApplicationContext());
    mPager.setAdapter(mAdapter);
    mTabs = (SlidingTabLayout) findViewById(R.id.tabs);
    mTabs.setOnPageChangeListener(this);
    mTabs.setCustomTabView(R.layout.custom_tab_view, R.id.tabText);

    mTabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
        @Override
        public int getIndicatorColor(int position) {
            return getResources().getColor(R.color.tabsScrollColor);
        }
    });

    mTabs.setViewPager(mPager);

}


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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

}

@Override
public void onPageSelected(int position) {
    Toast.makeText(this, Integer.toString(position), Toast.LENGTH_LONG).show();
}

@Override
public void onPageScrollStateChanged(int state) {

}

}

ViewPagerAdapter.java

public class ViewPagerAdapter extends FragmentPagerAdapter {
int NumbOfTabs = 4; // Store the number of tabs, this will also be passed when the ViewPagerAdapter is created
String[]tabs;
CharSequence Titles[]={"P","M","C","S","I"};
int icons[]={R.drawable.profile,R.drawable.match,R.drawable.chat,R.drawable.ic_search,R.drawable.info};
Context context;


public ViewPagerAdapter(FragmentManager fm, Context mContext) {
    super(fm);
    this.context= mContext;
    tabs = context.getResources().getStringArray(R.array.tabs);

}


@Override
public Fragment getItem(int position) {

    switch (position){

        case 0:
            Profile Profile = new Profile();
            return Profile;
        case 1:
            MatchPage MatchPage = new MatchPage();
            return MatchPage;
        case 2:
            Chat Chat = new Chat();
            return Chat;
        case 3:
            SearchPreference SearchPreference = new SearchPreference();
            return SearchPreference;
        case 4:
            Info Info = new Info();
            return Info;
    }
    return null;
}

@Override
public CharSequence getPageTitle(int position) {
    Drawable drawable=context.getResources().getDrawable(icons[position]);
    drawable.setBounds(0, 0,100,100);
    ImageSpan imageSpan = new ImageSpan(drawable);
    SpannableString spannableString =new SpannableString(" ");
    spannableString.setSpan(imageSpan,0,spannableString.length(),spannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
    return spannableString;

    //return Titles[position];

}


@Override
public int getCount() {
    return NumbOfTabs;
}

}

0

There are 0 answers