Search code examples
androidretrofit2oncreateandroid-coordinatorlayout

setContentView in retrofit response success doesn't show all the information in scroll?


I'm using retrofit and coordinatorlayout. All seems that work fine but I'm getting a small problem. That I want to do is that the layout will display in the moment that the response is success. For this reason, I'm calling the setContentView inside the response (I don't know if this is a good practice or not)

My problem is that this works, but the layout don't do the scroll in all the information. He cut the last lines of text. If I call the setContentView in the oncreate method the scroll in the coordinatorlayout is ok (show all the information) but in this scenario I can see the layout design before all the correct images and text when finish the success response.

Somebody know how can I fix this problem??

Thanks in advance

Detail Class:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        apiCall(R.layout.detail);
    }

    private void apiCall(final int layout){
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            id = extras.getString("id");
        }

        ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);

        Call<Hostel> call = apiService.getHostelId(id);
        call.enqueue(new Callback<Hostel>() {
            @Override
            public void onResponse(Call<Hostel> call, Response<Hostel> response) {

                try {
                    if (response.code() == 200) {
                        setContentView(layout);
                        initView(response);
                        setToolbar();
                    } else {
                        Toast.makeText(getApplication(), getResources().getString(R.string.no_conexion), Toast.LENGTH_SHORT).show();
                    }
                }catch (Exception e){
                    Log.e(TAG, e.toString());
                }
            }
            @Override
            public void onFailure(Call<Hostel> call, Throwable t) {
                Log.e(TAG, t.toString());
            }
        });
    }

detail.xml

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/scroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/app_bar"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:paddingTop="24dp">

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="19dp">
                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:padding="5dp">
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical"
                        android:paddingLeft="10dp"
                        android:paddingBottom="10dp"
                        android:paddingTop="5dp">
                        <TextView
                            android:id="@+id/propertyName_detail"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="@string/description"
                            android:textAppearance="@style/TextAppearance.AppCompat.Title" />
                        <TextView
                            android:id="@+id/description_detail"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            />
                    </LinearLayout>
                </LinearLayout>
            </android.support.v7.widget.CardView>

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="19dp"
                android:layout_marginLeft="19dp"
                android:layout_marginRight="19dp">
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
                    <LinearLayout
                        style="@style/Widget.CardContent"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal">
                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="@string/address_1"
                            android:textAppearance="@style/TextAppearance.AppCompat.Title" />
                        <TextView
                            android:id="@+id/addres_one_detail"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"/>
                    </LinearLayout>
                    <LinearLayout
                        style="@style/Widget.CardContent"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal">
                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="@string/address_2"
                            android:textAppearance="@style/TextAppearance.AppCompat.Title" />
                        <TextView
                            android:id="@+id/addres_two_detail"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"/>
                    </LinearLayout>
                    <LinearLayout
                        style="@style/Widget.CardContent"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal">
                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="@string/city"
                            android:textAppearance="@style/TextAppearance.AppCompat.Title" />
                        <TextView
                            android:id="@+id/city_detail"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"/>
                    </LinearLayout>
                    <LinearLayout
                        style="@style/Widget.CardContent"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal">
                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="@string/county"
                            android:textAppearance="@style/TextAppearance.AppCompat.Title" />
                        <TextView
                            android:id="@+id/country_detail"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"/>
                    </LinearLayout>
                </LinearLayout>

            </android.support.v7.widget.CardView>

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="19dp">
                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:padding="5dp">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/directions"
                        android:textAppearance="@style/TextAppearance.AppCompat.Title" />
                    <TextView
                        android:id="@+id/directions_detail"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"/>
                </LinearLayout>
            </android.support.v7.widget.CardView>
        </LinearLayout>

    </android.support.v4.widget.NestedScrollView>

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="256dp"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapser"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/image_detail"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax" />

            <android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto"
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@android:color/transparent"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:theme="@style/CustomActionBar"
                />
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>

Solution

  • Call setContentView in oncreate just try do show layout to user when response is successful. you can manage it using alpha property of a widget

    android:alpha="0.0" to make view invisible

    android:alpha="1.0" to make view visible

    now in oncreate call

    CoordinatorLayout cL=(CoordinatorLayout)findViewById(R.id.coordinator);
    cL.setAlpha(0);
    

    and after getting successfull response make it visible like this :

    cL.setAlpha(1);