Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a view to a LinearLayout at a specified position

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.

like image 640
Joeblackdev Avatar asked Aug 02 '11 19:08

Joeblackdev


People also ask

How do you create a view in linear layout?

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

Which is better LinearLayout or RelativeLayout?

Relativelayout is more effective than Linearlayout. From here: It is a common misconception that using the basic layout structures leads to the most efficient layouts. However, each widget and layout you add to your application requires initialization, layout, and drawing.

How do you align text in LinearLayout?

If in linearlayout your orientation vertical, you can put the textview in the "horizontal center" by android:layout_gravity="center" . For centering textview vertically you need to set layout_height of textview to match_parent and set android:gravity to "center".

What is the different value of the orientation attribute in LinearLayout?

The orientation attribute is used to arrange its children either in horizontal or vertical order. The valid values for this attribute are horizontal and vertical.


3 Answers

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.

like image 104
antonyt Avatar answered Sep 21 '22 09:09

antonyt


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)  
like image 45
Idistic Avatar answered Sep 21 '22 09:09

Idistic


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());
like image 29
Leonardo Costa Avatar answered Sep 19 '22 09:09

Leonardo Costa