-5

I have an xml like below.

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">

<LinearLayout
    android:id="@+id/etMsisdn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <LinearLayout
        android:id="@+id/allView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <EditText
            android:id="@+id/msisdn"
            android:layout_marginRight="4dp"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:backgroundTint="@color/colorPrimaryDark"
            android:hint="MSISDN"
            android:inputType="numberDecimal"
            />
        <ImageView
            android:layout_width="60px"
            android:layout_height="match_parent"
            android:src="@drawable/scan"/>
    </LinearLayout>
</LinearLayout>
............
Another View
............
</LinearLayout>

How do I add EditText and ImageView programatically inside the horizontal LinearLayout (allView) and add the allView inside Vertical LinearLayout(etMsisdn) while keeping the same attribute as in xml.

The EditText and ImageView r supposed to below the msisdn edittext

The EditText and ImageView r supposed to below the msisdn edittext

BrenDonie
  • 85
  • 1
  • 10

4 Answers4

0

You need to get a reference to the outermost LinearLayout (the first one in your layout), so the best idea is to give it an Id:

<LinearLayout
    android:id="@+id/myContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp">

Then, you need to get the reference to this layout and add the children views, like so:

LinearLayout containerLayout = (LinearLayout)findViewById(R.id.myContainer);
containerLayout.addView(yourView1);
containerLayout.addView(yourView2);

To set the desired layout alignment, you can either manually set the required LayoutParams (see this answer in SO) or you could inflate a layout and add it to your current layout, instead of two individual views (EditText and ImageView) (see this answer in SO).

Xavier Rubio Jansana
  • 6,388
  • 1
  • 27
  • 50
  • Did this already,,,but couldnt get edittext and imageview horizontally aligned – BrenDonie Aug 23 '17 at 10:41
  • That's a different thing then. You can manually add the required LayoutParams (see [this answer in SO](https://stackoverflow.com/questions/5715612/how-to-set-setlayoutparams-for-linear-layout-elements)) or you could inflate a layout (see [this answer in SO](https://stackoverflow.com/questions/5342121/inflate-a-view-layout-into-another-layout#5343733)). – Xavier Rubio Jansana Aug 23 '17 at 10:44
  • I've updated the answer with the previous information. – Xavier Rubio Jansana Aug 23 '17 at 10:49
0

here is your solution

LinearLayout allview=(LinearLayout)findViewById(R.id.allView);

EditText edt=new EditText(this);
ImageView img=new ImageView(this);
allview.addView(edt);
allview.addView(img);

put this in your activity

jigar savaliya
  • 474
  • 1
  • 8
  • 21
0

You have to use the addView method on the layout object. But, if you want to add more times the same "sub layout" in the main layout it's better to create an xml layout with the "sub part" and add it programmatically. Let me know if the second case is what you need to provide the code.

anemomylos
  • 546
  • 1
  • 6
  • 14
  • I want to add linear layout (sub) inside linear layout (main). But inside the sub linear layout contains another view like edittext and imageview – BrenDonie Aug 24 '17 at 03:15
0

Find LinerLayout and add views:

LinearLayout root = (LinearLayout)findViewById(R.id.allView);
root.addView(someView);

and add the attributes like this on your view:

How to programmatically set textview-s and their properties?