android activity containing viewpager opening slowly

2.1k views Asked by At

I am trying to navigate from one activity to another. The second activity has a viewpager having three fragments. Two of the fragments have lots of views on it and one of it has less views. I tried keeping only one fragment in the viewpager that has lot of views. It took some time to load. I tried another one with lot of views. It also took time to load. But when i kept the fragment that had less views it took less time. If i load all at once it again takes lot of time to load. I first had lot of view hierarchies on both the fragment, but i removed them all ultimately using table layout. The activity is rendering slow. fragment 1:

    <ScrollView
        android:id="@+id/scroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TableLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TableRow style="@style/TableRowStyle">

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1" />

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1" />

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1" />

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1" />

                </TableRow>

                <TableRow style="@style/TableRowStyle">

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                </TableRow>

                <TableRow
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/white">

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:background="@color/white" />

                </TableRow>

                <TableRow style="@style/TableRowStyle">

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1.5"/>

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1" />

                </TableRow>


                <TableRow style="@style/TableRowStyle">

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1.5" />

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                </TableRow>

                <TableRow
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/white">

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:background="@color/white" />

                </TableRow>

                <TableRow
                    android:id="@+id/ll_nfo1"
                    style="@style/TableRowStyle"
                    android:visibility="visible">

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1" />

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1" />

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                </TableRow>

                <TableRow android:id="@+id/ll_nfo2"
                    style="@style/TableRowStyle"
                    android:visibility="visible">

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                </TableRow>

                <TableRow style="@style/BidOfferTableRowStyle">

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1" />

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1" />

                </TableRow>

                <TableRow style="@style/BidOfferTableRowStyle">

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1" />

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1" />

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1" />

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                </TableRow>

                <TableRow style="@style/BidOfferTableRowStyle">

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1" />

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1" />

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1" />

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                </TableRow>

                <TableRow style="@style/BidOfferTableRowStyle">

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1" />

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1" />

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                </TableRow>

                <TableRow style="@style/BidOfferTableRowStyle">

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1" />

                </TableRow>

            </TableLayout>


        </LinearLayout>

    </ScrollView>

    <LinearLayout
        android:id="@+id/ll_bottom"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dimen_40">

        <com.intmilli.imobile.Views.CustomTextView
            android:id="@+id/tv_h_first"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"/>

        <View
            android:id="@+id/view_line3_left"
            android:layout_height="match_parent"
            android:layout_toRightOf="@id/tv_h_first"
            android:background="@color/mainbg_lightblue" />

        <com.intmilli.imobile.Views.CustomTextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    </LinearLayout>

</RelativeLayout>

fragment 3:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <com.intmilli.imobile.Views.CustomTextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                />

            <com.intmilli.imobile.Views.CustomTextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" />

            <com.intmilli.imobile.Views.CustomTextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" />

            <com.intmilli.imobile.Views.CustomTextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" />

            <com.intmilli.imobile.Views.CustomTextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>

            <com.intmilli.imobile.Views.CustomTextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>

        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="4dp"
            android:paddingBottom="4dp">

            <com.intmilli.imobile.Views.CustomTextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" />

            <com.intmilli.imobile.Views.CustomTextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>

            <com.intmilli.imobile.Views.CustomTextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" />

            <com.intmilli.imobile.Views.CustomTextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" />

            <com.intmilli.imobile.Views.CustomTextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" />

            <com.intmilli.imobile.Views.CustomTextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" />

        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <Spinner
                android:id="@+id/day_spinner"
                android:layout_width="0dp"
                android:layout_height="34dp" />

            <Spinner
                android:id="@+id/limit_spinner"
                android:layout_width="0dp"
                android:layout_height="34dp" />

            <Spinner
                android:id="@+id/buy_spinner"
                android:layout_width="0dp"
                android:layout_height="34dp" />

        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp">

            <com.intmilli.imobile.Views.CustomTextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>

            <ImageButton
                android:id="@+id/sub_qty"
                android:layout_width="0dp"
                android:layout_height="36dp"
                android:layout_weight="0.3" />

            <EditText
                android:id="@+id/et_qty"
                android:layout_width="0dp"
                android:layout_height="36dp"/>

            <ImageButton
                android:id="@+id/add_qty"
                android:layout_width="0dp"
                android:layout_height="36dp"
                android:layout_weight="0.3"/>

        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp">

            <com.intmilli.imobile.Views.CustomTextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" />

            <ImageButton
                android:id="@+id/sub_price"
                android:layout_width="0dp"
                android:layout_height="36dp"
                android:layout_weight="0.3" />

            <EditText
                android:id="@+id/et_pricevalue"
                android:layout_width="0dp"
                android:layout_height="36dp" />

            <ImageButton
                android:id="@+id/add_price"
                android:layout_width="0dp"
                android:layout_height="36dp"
                android:layout_weight="0.3" />

        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp">

            <com.intmilli.imobile.Views.CustomTextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>

            <ImageButton
                android:id="@+id/sub_dis_qty"
                android:layout_width="0dp"
                android:layout_height="36dp"
                android:layout_weight="0.3"/>

            <EditText
                android:id="@+id/et_dis_qty"
                android:layout_width="0dp"
                android:layout_height="36dp" />

            <ImageButton
                android:id="@+id/add_dis_qty"
                android:layout_width="0dp"
                android:layout_height="36dp"
                android:layout_weight="0.3"/>


        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp">

            <com.intmilli.imobile.Views.CustomTextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>

            <ImageButton
                android:id="@+id/sub_tri_price"
                android:layout_width="0dp"
                android:layout_height="36dp"
                android:layout_weight="0.3"/>

            <EditText
                android:id="@+id/et_triggerPrice"
                android:layout_width="0dp"
                android:layout_height="36dp" />

            <ImageButton
                android:id="@+id/add_tri_price"
                android:layout_width="0dp"
                android:layout_height="36dp"
                android:layout_weight="0.3" />

        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="6dp">

            <CheckBox
                android:id="@+id/ch_stopLoss"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <CheckBox
                android:id="@+id/ch_IOC"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center">

            <com.intmilli.imobile.Views.CustomTextView
                android:id="@+id/tv_placeOrderBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

        </TableRow>

    </TableLayout>

</ScrollView>

As i was saying fragment 1 and 3 are taking lots of time. Can someone help to solve this issue thanks!

1

There are 1 answers

15
Khemraj Sharma On
  • Weights are not good for much use. I seen you have used too much android:layout_weight in your layout. That cause slow loading layout. Because it needs adjust views according to device screen.

    So don't use too much weights and go with RelativeLayout or ConstraintLayout.

  • Don't write any logics, data fetching, views management inside onCreate() or onCreateView().

  • Do all findViewById() in onViewCreated() of Fragments. Also read Fragment Lifecycle for better understanding.

  • If you are calling API then call it with some delay in onViewCreated(). It will let view created first then set data received from API.

  • Note that you are not doing any long operation or logics in UI or Main Thread. That should be done in Worker Thread like AsyncTask or new Thread.