I am using google's SlidingTabLayout in my view, but i want to add icons to the tabs. I'm using this http://developer.android.com/samples/SlidingTabsBasic/src/com.example.android.common/view/SlidingTabLayout.html Can anyone please help?
void setUpPager(View view){
mViewPager = (ViewPager) view.findViewById(R.id.viewpager);
mViewPager.setAdapter(new TabsPagerAdapter(getActivity()));
mSlidingTabLayout = (SlidingTabLayout) view.findViewById(R.id.sliding_tabs);
mSlidingTabLayout.setViewPager(mViewPager);
}
Here is my xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.common.view.SlidingTabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:background="@android:color/white"/>
</LinearLayout>
To customize SlidingTabLayout the way you want, you only need to modify the method populateTabStrip(). With this approach, you do not need care about any TextView.
Your layout could be something like that:
The way you implement the getResourceArray() method is up to you. Here is how I did:
In the activity:
Be aware that in order to have access to R.layout.tab_layout*, you have to import yourpackage.R instead of android.R as it is by default in SlidingTabStrip.
Use
mSlidingTabLayout.setCustomTabView(int layoutResId, int textViewId)
to inflate a custom layout for theSlidingTabLayout
tab views.When
SlidingTabLayout
tries to populate the tab strips, initially looks for any specified layout resource to inflate. Otherwise, it inflates default tab view.Just want to give example for the Correct and accepted answer. :)
Firstly, add the tabs and set their custom view when you are adding the adapter to your viewpager. For example see following lines:
Where
sliding_tabs
are the tabs_titles to be added and are defined inxml
file of theviewpager
like:and where
custom_tab_title
is a separatexml
file in yourlayout
folder.For example:and you can set the image for a particular tab by:
Notice that I have added an additional space to the
viewpager_tab_title
and am setting up the image to that position, so that the actual string is displayed completely.