Rick Joe Rick Joe - 9 months ago 30
Android Question

where can I add include in my xml file?

I'm trying to add a toolbar in my xml files.

So I made toolbar.xml and I need to include it in upload.xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_upload"
android:layout_width="match_parent"
android:padding="10dp"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.sk.mf.Upload"
android:background="#fff">

<LinearLayout

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

<ImageView
android:id="@+id/imageToUpload"
android:layout_gravity="center_horizontal"
android:layout_width="150dp"
android:layout_height="150dp" />

<EditText
android:id="@+id/etUploadName"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<Button
android:id="@+id/bUploadImage"
android:text="Upload Image"
android:layout_width="match_parent"
android:layout_height="wrap_content" />


</LinearLayout>


</ScrollView>


the problem is, anywhere I place it I had error like:
scrollview can host only one direct child (inside scrollview)
multiple root tags (after scrollview)
...

my question here is, where and how can I put the code below in the xml above?

<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">
<include android:id="@+id/toolbar" layout="@layout/toolbar"></include>
</RelativeLayout>

Answer Source

try this : wrap your layout in LinearLayout and the toolbar tag

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical">

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


    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_upload"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#fff"
        android:padding="10dp"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.sk.mf.Upload">

        <LinearLayout

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

            <ImageView
                android:id="@+id/imageToUpload"
                android:layout_width="150dp"
                android:layout_height="150dp"
                android:layout_gravity="center_horizontal" />

            <EditText
                android:id="@+id/etUploadName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <Button
                android:id="@+id/bUploadImage"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Upload Image" />

        </LinearLayout>

    </ScrollView>
</LinearLayout>