Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 937
theapache64 Avatar asked May 05 '15 05:05

theapache64


1 Answers

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

like image 115
Ahmad Sanie Avatar answered Nov 10 '22 13:11

Ahmad Sanie