Jeremy
Jeremy

Reputation: 2536

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;
}

Upvotes: 10

Views: 20167

Answers (2)

saa
saa

Reputation: 1568

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.

Upvotes: 17

CoolMonster
CoolMonster

Reputation: 2266

http://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html

Refer the above documentation for more details about how to set the Layout Parameter for specific component.

LinearLayout.LayoutParams layoutParam = new LinearLayout.LayoutParams(
                    R.id.componentid;

btn.leftMargin = x;
btn.topMargin = y;
btn.width = width;
btn.height = height;

imageview.setLayoutParams(layoutParam);

above code will show how to use layoutparam.

Upvotes: 0

Related Questions