dannyBoy
dannyBoy

Reputation: 67

How to inflate a child layout

I'm starting to learn Android Development so this question may sound a bit noobish. I tried to inflate a child layout inside a parent layout with a template I created, but it throws the error: The specified child already has a parent. You must call removeView() on the child's parent first Here are my codes:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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.codingrabbit.spandan.rabbit101.MainActivity">

<include
    layout="@layout/toolbar"
    android:id="@+id/toolbar">
</include>

<LinearLayout
    android:id="@+id/contents"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/toolbar"
    android:layout_centerHorizontal="true">
</LinearLayout>

</RelativeLayout>

special_buttons.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/special_button"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Subject"
    android:textSize="20sp"
    android:textColor="@color/white"
    android:textAlignment="center"
    android:background="@color/blue"
    android:id="@+id/subjectText"
    android:layout_gravity="center_horizontal" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="70dp"
    android:text="Details"
    android:textSize="18sp"
    android:textAlignment="center"
    android:textColor="@color/white"
    android:background="@color/green"
    android:paddingRight="16dp"
    android:paddingLeft="16dp"
    android:id="@+id/detailsText"
    android:layout_gravity="center_horizontal" />
</LinearLayout>

toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/toolbar"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
android:background="@color/colorPrimary"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize">

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

MainActivity.java

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

private Toolbar toolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Toolbar Setup:
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitle("Main Menu");
    toolbar.setTitleTextColor(getResources().getColor(R.color.white));

    //Adding special buttons:
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    LinearLayout parent = (LinearLayout) findViewById(R.id.contents);
    for(int i=0; i<3; i++){
        View custom = inflater.inflate(R.layout.special_buttons, parent, true);
        TextView subText = (TextView) custom.findViewById(R.id.subjectText);
        TextView detailsText = (TextView) custom.findViewById(R.id.detailsText);
        subText.setText("Subject: "+(i+1));
        detailsText.setText("Details of subject " + (i + 1));
        parent.addView(custom);    //this is where the error is diplayed
    }
    setContentView(parent);
}
}

Upvotes: 2

Views: 1688

Answers (2)

inflater.inflate(R.layout.special_buttons, parent, false);

Upvotes: 0

Blackbelt
Blackbelt

Reputation: 157487

if you provide true as third parameter for inflate, the inflated view is automatically added to parent (the second parameter) and the parent itself is returned. So, in your case you are trying to adding twice the same view to parent, which, as the error points out, is not possible.

Change

inflater.inflate(R.layout.special_buttons, parent, true);

with

inflater.inflate(R.layout.special_buttons, parent, false);

and you will get the behaviour you expect

Upvotes: 2

Related Questions