theapache64
theapache64

Reputation: 11754

How can i set ViewPager's height programatically more than one time?

I can set ViewPager's height programatically by using vpTabs.getLayoutParams().height = DESIRED_SIZE; but it works only once at run time. now the question is how to set the height more than one time at runtime ?

activity_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rlActivityProfile"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.my.package.ProfileActivity">

    <!--MainContainer-->
    <com.my.package.widget.ScrollViewX
        android:id="@+id/svxUserProfile"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <!--Main Container-->
        <RelativeLayout

            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <!--Header-->
            <RelativeLayout
                android:id="@+id/rlProfileBanner"
                android:layout_width="match_parent"
                android:layout_height="300dp"
                android:background="@color/listile_green">

                    <!-- SOME OTHER VIEW-->

            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/rlTabs"
                android:layout_below="@id/rlProfileBanner"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <!--Slider-->
                <android.support.v4.view.ViewPager
                    android:id="@+id/vpTabs"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_below="@id/stlControl" />

            </RelativeLayout>

        </RelativeLayout>


    </com.my.package.widget.ScrollViewX>


    <!--Transparent toolbar-->
    <include
        android:id="@+id/iAppBar"
        layout="@layout/app_bar" />

</RelativeLayout>

The viewPager is initialized in onCreate() of MainActivity class

ViewPager vpTabs = (ViewPager) findViewById(R.id.vpTabs);

I am changing the viewPager's height from fragment using an interface like this

//Calling from fragment
int totalHeight = titles.length * EditProfileAdapter.ROW_HEIGHT;
vpListener.onViewPagerHeightChange(totalHeight);

in MainActivity

@Override
    public void onViewPagerHeightChange(int height) {
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.height = height; //left, top, right, bottom
        vpTabs.setLayoutParams(params);
    }

NOTE: The custom widget ScrollViewX is the same ScrollView but with a Custom ScrollListener.

Upvotes: 6

Views: 13170

Answers (3)

Hasan Mhd Amin
Hasan Mhd Amin

Reputation: 417

first, get the LayoutParams for your ViewPager then assign the height for this LayoutParams

ViewGroup.LayoutParams lp = viewPager.getLayoutParams();
lp.height = ScreenUtils.convertDpToPixel(300, getContext());

Upvotes: 0

Ashish John
Ashish John

Reputation: 1905

ViewGroup.LayoutParams params= tabPager.getLayoutParams();
params.height = 500;    //500px
params.width = 500;    //500px
viewPager.requestLayout();

Upvotes: 0

Ahmad Al-Sanie
Ahmad Al-Sanie

Reputation: 3785

define new layoutParams and set your viewPager to it then change the layoutParams's height this will do the trick, don't do it directly to its original params or changes won't effect the layout more than once, you also have to make sure that the condition you are using is correct, try to use this:

LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

//setting margins around imageimageview
params.height=yourHeight; //left, top, right, bottom

//adding attributes to the imageview
viewPager.setLayoutParams(params);

instead of setting it directly

Upvotes: 10

Related Questions