Android Add Layout View to another Layout programatically

Filip Luchianenco

I have a LinearLayout with Vertical orientation and I would like to add another horizontal LinearLayout with two childs.

Here is my xml layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/Gray_background"
android:orientation="vertical" >

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_marginTop="10dp"
    android:background="@color/GrayStroke" />

<LinearLayout
    android:id="@+id/contacts_tab_menulist"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/White"
    android:orientation="vertical" >
</LinearLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@color/GrayStroke" /> </LinearLayout>

Here is the code which should do the job:

View rootView = inflater.inflate(R.layout.contacts_tab,
            container, false);      

        contacts_tab_menulist = (LinearLayout) getActivity().findViewById(R.id.contacts_tab_menulist);
    //      add LayoutParams
    LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 35);
    LinearLayout listItem = new LinearLayout(getActivity());
    listItem.setLayoutParams(lparams);
    listItem.setOrientation(LinearLayout.HORIZONTAL);

    AspectRatioImageView icon = new AspectRatioImageView(getActivity());
    LinearLayout.LayoutParams ic_params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 25);
    icon.setImageResource(R.drawable.contacts_add_contact);
    icon.setLayoutParams(ic_params);
    listItem.addView(icon);

    TextView title = new TextView(getActivity());
    LinearLayout.LayoutParams t_params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    title.setText("Add Contact");
    title.setId(0);
    title.setLayoutParams(t_params);
    listItem.addView(title);

    contacts_tab_menulist.addView(listItem);
return rootView;

I get a nullPointerException on contacts_tab_menulist.addView(listItem). What am I doing wrong?

mstrthealias

Get the view from your inflated view, since inflating the View does not mean its the current activities view:

    contacts_tab_menulist = (LinearLayout) rootView.findViewById(R.id.contacts_tab_menulist);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Android Add Layout View to another Layout programatically

From Dev

Android programatically add layout resource to view

From Dev

Adding Rule programatically to a View that is already in the layout - Android

From Dev

Adding Rule programatically to a View that is already in the layout - Android

From Dev

Swift: programatically copy auto layout constraints from one view to another

From Dev

view viewpager in another layout android

From Dev

How to copy layout of View which add child programatically?

From Dev

How to programatically add LinearLayout with background color, and weight to another layout

From Dev

How to programatically add LinearLayout with background color, and weight to another layout

From Dev

Android layout_centerInParent property not working after adding a view programatically

From Dev

Android: How to cover the whole layout by a view programatically while the layout contains buttons already?

From Dev

Dynamically add layout to another layout

From Dev

Android relative layout center horizontally with another view

From Dev

Android relative layout center horizontally with another view

From Dev

layout_weight changes when I add a view into the layout (Android)

From Dev

layout_weight changes when I add a view into the layout (Android)

From Dev

Adding one layout on top of another programatically

From Dev

Android - Add a custom view to a component in a xml layout

From Dev

How to add or replace page in android layout view

From Dev

Android Fragment top level Layout is not programatically accessible

From Dev

Android table layout how to set border programatically?

From Dev

Add another layout in Lubuntu

From Dev

Add another layout in Lubuntu

From Dev

Trying to remove view from one layout and add to another

From Dev

add view from MainActivity to another xml file layout

From Dev

Android View/Layout Masking

From Dev

swiping view or layout in android

From Dev

ANDROID View alignment on layout

From Dev

IllegalStateException when add a layout into another layout with LayoutInflater

Related Related

  1. 1

    Android Add Layout View to another Layout programatically

  2. 2

    Android programatically add layout resource to view

  3. 3

    Adding Rule programatically to a View that is already in the layout - Android

  4. 4

    Adding Rule programatically to a View that is already in the layout - Android

  5. 5

    Swift: programatically copy auto layout constraints from one view to another

  6. 6

    view viewpager in another layout android

  7. 7

    How to copy layout of View which add child programatically?

  8. 8

    How to programatically add LinearLayout with background color, and weight to another layout

  9. 9

    How to programatically add LinearLayout with background color, and weight to another layout

  10. 10

    Android layout_centerInParent property not working after adding a view programatically

  11. 11

    Android: How to cover the whole layout by a view programatically while the layout contains buttons already?

  12. 12

    Dynamically add layout to another layout

  13. 13

    Android relative layout center horizontally with another view

  14. 14

    Android relative layout center horizontally with another view

  15. 15

    layout_weight changes when I add a view into the layout (Android)

  16. 16

    layout_weight changes when I add a view into the layout (Android)

  17. 17

    Adding one layout on top of another programatically

  18. 18

    Android - Add a custom view to a component in a xml layout

  19. 19

    How to add or replace page in android layout view

  20. 20

    Android Fragment top level Layout is not programatically accessible

  21. 21

    Android table layout how to set border programatically?

  22. 22

    Add another layout in Lubuntu

  23. 23

    Add another layout in Lubuntu

  24. 24

    Trying to remove view from one layout and add to another

  25. 25

    add view from MainActivity to another xml file layout

  26. 26

    Android View/Layout Masking

  27. 27

    swiping view or layout in android

  28. 28

    ANDROID View alignment on layout

  29. 29

    IllegalStateException when add a layout into another layout with LayoutInflater

HotTag

Archive