Mohd Ali
Mohd Ali

Reputation: 311

Why Android layout inflater is inflating blank space?

I'm trying to inflate a layout on onPostExectue method of AsyncTask, but nothing gets displayed, I think my view gets inflated because my parent ScrollView's size increases. But why nothing is visible?

Here is what I'm doing in onPostExecute method:

LinearLayout rootView = (LinearLayout) getActivity().findViewById(R.id.root_view);
View child = getActivity()
                 .getLayoutInflater()
                 .inflate(R.layout.layout_child, rootView, false);
rootView.addView(child);

Child layout to be inflated:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="horizontal">

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:background="@null"
        android:src="@drawable/ic_play_arrow_black_24dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="T 1"
        android:textSize="30sp" />

</LinearLayout>

Parent layout where I'm inflating:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.abc.DetailFragment"
    tools:showIn="@layout/activity_a">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="@+id/root_view">

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

            <ImageView
                android:id="@+id/poster"
                android:layout_width="139dp"
                android:layout_height="209dp"
                android:layout_margin="10dp" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:orientation="vertical"
                android:padding="5dp">

                .
                .
                .

            </LinearLayout>

        </LinearLayout>

        <TextView
            android:id="@+id/overview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:layout_margin="8dp" />

    </LinearLayout>
</ScrollView>

I get no error but just some blank space.

Upvotes: 1

Views: 634

Answers (1)

Vedran Kopanja
Vedran Kopanja

Reputation: 1299

First of all, I think you should probably delegate inflating of the views to activity but for the sake of how you're doing things at the moment, try inflating and adding a child in the runOnUiThread.

runOnUiThread(new Runnable() {
        public void run() {
        LinearLayout rootView = (LinearLayout) getActivity().findViewById(R.id.root_view);
        View child = getActivity()
                         .getLayoutInflater()
                         .inflate(R.layout.layout_child, rootView, false);
        rootView.addView(child);
    }
});

Also, if you know your root view is LinearLayout, why not just use that instead of View?

Upvotes: 2

Related Questions