super1ha1
super1ha1

Reputation: 629

How to implement custom tabview in Android ViewPager?

Can I ask how could we implement the following layout using Viewpager in Android:

1) Desired View

enter image description here


2)Techniques/Methods tried:

There is an Activity with Viewpager and a FragmentPagerAdapter to return the appropriate Fragment for each tab. I have tried the following answers/library:

Custom viewpager tabs with custom view, multiple fragment and layouts

Viewpager in Android with fixed tabs at bottom with Icon and Text in each tab

http://viewpagerindicator.com/

I also try to set the ActionbarStyle in app Theme, but it all the rules apply to the ActionBar, not the tab view of view pager.

<style name="MyTheme" parent="android:Theme.Holo.Light">
        <item name="android:actionBarStyle">@style/MyCustomActionBar</item>
        <item name="android:actionBarTabStyle">@style/ActionBarTabStyle</item>
        <item name="android:actionBarSize">100dp</item>
    </style>

    <style name="ActionBarTabStyle" parent="@android:style/Widget.Holo.ActionBar.TabView">
        <item name="android:minHeight">200dp</item>
    </style>


    <style name="MyCustomActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">@color/white</item>
        <item name="android:height">100dp</item>
    </style>


3) Current View:

This is the current tabview of the viewpager, which is not good looking:

enter image description here

4) XML layout:

Activity layout:

<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   />

Curent Fragment layout:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/new_deliver_scroll"
    >
<LinearLayout  android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"

    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:background="@color/background_material_light"
    android:id="@+id/newDeliverMain"
    >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/new_deliver_main_linear"
    >
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:orientation="horizontal"
        android:id="@+id/relative_layout_item_info"
        >

        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Item_desc"
            android:id="@+id/new_deliver_item_desc_text"
            android:textColor="@color/app_blue"

            />

...........

5) Code to create custom tabview in Activity:

viewPagerAdapter = new SenderViewPagerAdapter( getSupportFragmentManager());
        viewPager = (ViewPager) findViewById(R.id.pager);

        viewPager.setAdapter( viewPagerAdapter);

        actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        viewPager.setOnPageChangeListener(
                new ViewPager.SimpleOnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {
                        actionBar.setSelectedNavigationItem(position);
                    }
                    @Override
                    public void onPageScrolled(int arg0, float arg1, int arg2) {
                    }
                    @Override
                    public void onPageScrollStateChanged(int arg0) {
                    }
                });
        for (int i = 0; i < viewPagerAdapter.getCount(); i++) {

            ActionBar.Tab newTab = actionBar.newTab();
            newTab.setCustomView(R.layout.custom_actionbar);
            newTab.setTabListener(this);

            TextView title = (TextView) newTab.getCustomView().findViewById(R.id.tab_title);
            ImageView icon = (ImageView)newTab.getCustomView().findViewById(R.id.tab_icon);
            title.setText(viewPagerAdapter.getPageTitle(i));
            icon.setImageResource(ICONS[i]);
            actionBar.addTab(newTab);
       }


After several trials, I temporarily stuck here. Any ideas, suggestions how to implement the desired view would be very appreciated. Thank you very much.

6) Update

Not work with:
icon.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));

@refer to @karaokyo answer: I switch to use Radio Button instead of action bar, it works ok, just few more customisations.

Change image when check:

Drawable top = getResources().getDrawable(R.drawable.image);
button.setCompoundDrawablesWithIntrinsicBounds(null, top , null, null);

For Radio Button, it very hard to change the size of the top image programatically, better to resize the image itself > take me 2 hours figure out.

Upvotes: 0

Views: 3333

Answers (1)

tachyonflux
tachyonflux

Reputation: 20221

There's not really a reason to use the action bar tabs if you need so much customization. It's also deprecated, so there's that. If I were doing this, I would probably use a RadioGroup for the tabs:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/tabs">
        <RadioButton
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawableTop="@drawable/new_delivery"
            android:button="@null"
            android:text="New Delivery"
            android:id="@+id/new_delivery"
            android:background="@drawable/checked_background_indicator"
            android:checked="true"
            style="?android:attr/borderlessButtonStyle"/>
        <RadioButton
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawableTop="@drawable/job_history"
            android:button="@null"
            android:text="Job History"
            android:id="@+id/job_history"
            android:background="@drawable/checked_background_indicator"
            style="?android:attr/borderlessButtonStyle"/>
        <RadioButton
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawableTop="@drawable/track_delivery"
            android:button="@null"
            android:text="Track Delivery"
            android:id="@+id/track_delivery"
            android:background="@drawable/checked_background_indicator"
            style="?android:attr/borderlessButtonStyle"/>
    </RadioGroup>
    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/tabs"
        android:layout_alignParentBottom="true"/>
</RelativeLayout>

checked_background_indicator.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:drawable="@color/accent" />
</selector>

And then just hook them together with listeners:

mTabs.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.new_delivery:
                mViewPager.setCurrentItem(0);
                break;
            case R.id.job_history:
                mViewPager.setCurrentItem(1);
                break;
            case R.id.track_delivery:
                mViewPager.setCurrentItem(2);
        }
    }
});

mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        switch(position){
            case 0:
                mTabs.check(R.id.new_delivery);
                break;
            case 1:
                mTabs.check(R.id.job_history);
                break;
            case 2:
                mTabs.check(R.id.track_delivery);
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
});

Upvotes: 2

Related Questions