Adding a Linear Layout in XML dynamically to another Linear Layout

Tags: , , , ,



Basically i created a seperate xml containing a linear layout which contains a image and a textview, and i need this linear layout inside of another linear layout, where i need to add my first linear layout dynamically to the second linear layout.

First linear layout, a seperate xml file :

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

    <LinearLayout
        android:id="@+id/storeLocation"
        android:layout_width="@dimen/settings_item_width"
        android:layout_height="@dimen/settings_item_height"
        android:layout_marginBottom="10dp"
        android:visibility="@integer/sales_order_settings_visibility"
        style="@style/dashboard_btn"
        android:onClick="onStoreLocationClick"
        android:clickable="true"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="@dimen/settings_item_image_container_width"
            android:layout_height="match_parent"
            android:gravity="center">

            <ImageView
                android:layout_width="@dimen/setting_item_image_size"
                android:layout_height="@dimen/setting_item_image_size"
                android:tint="@color/cherry_red_without_opacity"
                android:src="@drawable/ic_location_icon" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingRight="40dp"
            android:gravity="center|left">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Store Location"
                android:textColor="@color/white"
                android:textSize="@dimen/top_section_purchase_id_font" />

        </LinearLayout>
    </LinearLayout>


</LinearLayout>

Second linear layout, seperate xml:

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

Android Studio code:

linearLayout = (LinearLayout) findViewById(R.id.settingButtonHolder);
            linearLayout2 = (LinearLayout) findViewById(R.id.storeLocation);
            linearLayout.addView(linearLayout2);

This doesnt work, i am getting a null pointer exception :

12-16 16:48:52.402 21571-21571/io.apptizer.business.clover E/AndroidRuntime: FATAL EXCEPTION: main
    Process: io.apptizer.business.clover, PID: 21571
    java.lang.RuntimeException: Unable to start activity ComponentInfo{io.apptizer.business.clover/io.apptizer.pos.activity.ApplicationSettingsActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
        at android.app.ActivityThread.access$800(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5017)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
        at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
        at android.view.ViewGroup.addView(ViewGroup.java:3353)
        at android.view.ViewGroup.addView(ViewGroup.java:3336)
        at io.apptizer.pos.activity.applicationSetting.ApplicationSettingsActivityCommon.onCreate(ApplicationSettingsActivityCommon.java:151)
        at io.apptizer.pos.activity.ApplicationSettingsActivity.onCreate(ApplicationSettingsActivity.java:78)
        at android.app.Activity.performCreate(Activity.java:5231)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)

Can somone pls help me with this, will be helpful.

Answer

One of the layouts must not exist as a view yet. If your activity or fragment is being built with settingButtonHolder layout then you need to inflate the other layout using a LayoutInflater.

Once you’ve used the LayoutInflater you will receive a view instance which you can use to add to your linearlayout in the place of linearLayout2. Until you’ve inflated your new layout, you will keep seeing NPEs because the views in that layout will not exist. You will have to use your inflated view instance (linearLayout2) to access the views, preferably through linearLayout2.findViewById(R.id.X).

Try this out and let me know if it has helped you at all, Panos.



Source: stackoverflow