Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving layout position programmatically in linear layout

I have the following xml:

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

    <LinearLayout
        android:id="@+id/wrapper"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:layout_margin="10dip"
        android:weightSum="1"
        android:background="#000"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/textMessage"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight = ".9"
            android:layout_gravity="center"
            android:layout_margin="5dip"
            android:paddingLeft="10dip"
            android:background="#FFF"
            android:text="TEXT NOT SET"
            android:textColor="@android:color/white" />
        <ImageView
            android:id="@+id/thumbPhoto"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight = ".1"
            android:src="@drawable/ic_contact_default"/>
    </LinearLayout>
</LinearLayout>

What i would like to do is to have the position of my ImageView change to the left side of my textMessage programmatically.

This could be done through xml by placing my ImageView above the TextView. However I would like to achieve the same result but programmatically.

Suppose I have this in my Adapter:

public View getView(int position, View convertView, ViewGroup parent) {

    View row = convertView;
    if (row == null) {
            LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.chat_item, parent, false);
    }

    wrapper = (LinearLayout) row.findViewById(R.id.wrapper);

    // How should I change the position of my ImageView ??
    wrapper.setGravity([some_condition] ? Gravity.LEFT : Gravity.RIGHT);

    return row;
}
like image 648
Jeremy Avatar asked Jan 07 '14 11:01

Jeremy


People also ask

How do you move a button in linear layout?

You can set the android:layout_weight='1' and both buttons will share the screen equally(side by side) or if you want the extra space between them, you can place a button each in a linear layout and set the android:layout_gravity to left and right for each.

How can I dynamically set the position of view in android?

This example demonstrates how do I in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2− Add the following code to res/layout/activity_main.

What is layout explain linear layout with an example?

LinearLayout is a view group that aligns all children in a single direction, vertically or horizontally. You can specify the layout direction with the android:orientation attribute. Note: For better performance and tooling support, you should instead build your layout with ConstraintLayout.

How do you align views in linear layout?

To center align LinearLayout, assign android:gravity attribute of this LinearLayout with the value “center”. Let us create an Android application with LinearLayout containing two Button widgets as children. We shall center align these children using gravity attribute.

What is vertical linear layout in Android?

Vertical linear layout gives top to bottom approach in android application means widgets can automatically added one by one into vertically form. So here is the complete step by step tutorial for Android create vertical linearlayout programmatically. Android create vertical linearlayout programmatically.

How to add a view to a linear layout programmatically?

Add View to Linear Layout at a Specific Index Programmatically If you want to add a view at a specific index (or position) inside a linear layout you can do it by just writing below the line of code. linear_layout.addView (your_view, 2)

How do I change the orientation of a linearlayout?

You can specify the layout direction with the android:orientation attribute. Note: For better performance and tooling support, you should instead build your layout with ConstraintLayout. All children of a LinearLayout are stacked one after the other, so a vertical list will only have one child per row, no matter how wide they are,...

What is the use of linear layout?

LinearLayout is a view group that aligns all children in a single direction, vertically or horizontally. You can specify the layout direction with the android:orientation attribute.


1 Answers

try like this....

ImageView imageView= (ImageView)yourLinearLayout.findViewById(R.id.thumbPhoto);
yourLinearLayout.removeViewAt(1);
yourLinearLayout.addView(imageView, 0);

By using this line

// How should i change the position of my ImageView ??
        wrapper.setGravity([some_condition] ? Gravity.LEFT : Gravity.RIGHT);

It shows no effect. Because here you are applying gravity to linearlayout, this gravity aligns its whole content either in left or right.

like image 193
saa Avatar answered Sep 29 '22 14:09

saa