56

I have the following main.xml file with a LinearLayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="1" android:id="@+id/llid">
    <TextView android:text="Client profile"
    android:id="@+id/ProfileName"
    android:layout_width="fill_parent"
    android:textStyle="bold"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal">
    </TextView>    
    <TextView android:text="Specs"
    android:id="@+id/Specs"
    android:layout_width="fill_parent"
    android:textStyle="bold"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal">
    </TextView>
</LinearLayout>

I add an image to the LinearLayout via code at runtime like so

            ImageView image = new ImageView(this);
            image.setImageBitmap(bmp);
            LinearLayout ll = (LinearLayout) findViewById(R.id.llid);
            ll.addView(image);  

However, I want to add the ImageView between the 2 TextViews in my LinearLayout. I can't seem to find a way in the android docs to add a view before another view, or after. How can I do this?

NB I call

setContentView(R.layout.main);

Before I add the ImageView to the LinearLayout.

Joeblackdev
  • 7,217
  • 24
  • 69
  • 106
  • Why do you want to do so? Just declare imageview in layout and set source to it instead of inserting imageview in code. –  Aug 02 '11 at 19:33
  • 2
    This won't work. At runtime I am loading an image dynamically and want to display it in my layout. – Joeblackdev Aug 02 '11 at 19:34
  • You can set source for ImageView dynamically. I really don't see the reason to do in the way you describe. –  Aug 02 '11 at 19:45

6 Answers6

94

When adding a View to a ViewGroup, you can specify an index which sets the position of the view in the parent.

You have two views and so (counting from zero) you would want to add at the 1st position; just call ll.addView(image, 1); to have it placed in between the two TextViews.

antonyt
  • 21,863
  • 9
  • 71
  • 70
  • 1
    I was wondering what if I have two views and I want the first one to take more than half of my layout and the other one only small space of my layout how I can do that ? and can I used this with other layouts like relative layout for example – Lily Nov 20 '13 at 07:10
  • 1
    I came here looking to learn how to add a `View` to a specific location relative to another child `View` to be dynamically determined. I realize the question doesn't directly ask that, so rather than make my own answer, I'll just add to this one how one would do that: first use `ViewGroup.indexOfChild(View view)` to get the index of the other `View`, then use the `addView` method with the index detailed here to insert a new `View` relative to an existing one. – Tim M. Aug 08 '17 at 15:23
  • @TimM. Dude, thanks a lot. I was looking exactly for that. – Big_Chair Aug 29 '20 at 10:13
11

The docs state you can use the index to insert it where you want. I see you are using the signature of the view only, did you try the signature with the index parameter?

public void addView(View child, int index) 
Idistic
  • 6,281
  • 2
  • 28
  • 38
  • I have indexoutofbound error while inserting textview to layout.It only explains index=2,count=0..... – nidhi May 02 '18 at 06:22
3

I faced a similar problem. In my case I wanted to add a LinearLayout at last position of another LinearLayout. To accomplish it, I did:

LinearLayout parentLayout = (LinearLayout) findViewById(R.id.parentLayout);
LinearLayout layout = new LinearLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

// add others views to your linear layout

parentLayout.addView(layout, parentLayout.getChildCount());
Leonardo Costa
  • 994
  • 11
  • 26
2
setContentView(R.layout.main);

ImageView img = (ImageView) findViewById(R.id.imagefield);
img.setImageResource(your_image_here);

and in the xml

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

    <TextView android:text="Client profile"
        android:id="@+id/ProfileName"
        android:layout_width="fill_parent"
        android:textStyle="bold"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal">
    </TextView>    

    <ImageView android:id="@+id/imagefield" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content">
    </ImageView> 

    <TextView android:text="Specs"
        android:id="@+id/Specs"
        android:layout_width="fill_parent"
        android:textStyle="bold"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal">
    </TextView>

</LinearLayout>
JJD
  • 50,076
  • 60
  • 203
  • 339
1

Add an ImageView into the xml, and if its not being used, make it invisible (image.setVisibility(View.INVISIBLE)). It may not show anything anyway when no image is set.

duggu
  • 37,851
  • 12
  • 116
  • 113
Jack
  • 9,156
  • 4
  • 50
  • 75
0

To get position of view in a view group

                val targetPosition = oldLL.indexOfChild(viewToAdd)

To add a view at a position in a view group

                newLL.addView(viewToAdd,targetPosition)
Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154