1

I have this layout for my listview, and I want to change the order of TextView to be before and after LinearLayout programmatically. Here's what it looks like,

<LinearLayout
    android:id="@+id/content"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:orientation="horizontal">

    <LinearLayout
        android:id="@+id/contentWithBackground"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/out_message_bg"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tvMessage"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:textColor="@android:color/black"
            android:text="TEST TEST"
            android:padding="@dimen/default_layout_padding"
            android:maxWidth="260dp" />

    </LinearLayout>

    <TextView
        android:id="@+id/tvInfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:textSize="12sp"
        android:text="23:21"
        android:textColor="@android:color/darker_gray" />

</LinearLayout>

And I want to change this layout to,

<LinearLayout
    android:id="@+id/content"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/tvInfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:textSize="12sp"
        android:text="23:21"
        android:textColor="@android:color/darker_gray" />

    <LinearLayout
        android:id="@+id/contentWithBackground"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/out_message_bg"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tvMessage"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:textColor="@android:color/black"
            android:text="TEST TEST"
            android:padding="@dimen/default_layout_padding"
            android:maxWidth="260dp" />

    </LinearLayout>

</LinearLayout>

Is it possible to do this programmatically alone?

Dicky Bullin
  • 259
  • 1
  • 2
  • 10
  • @SelçukCihan I saw that post before posting here. But if there's no other solution other than removing and adding the view back, I'll just mark this as duplicated. – Dicky Bullin Apr 04 '16 at 21:23

1 Answers1

1

Very simple. Just create two empty LinearLayouts and add the View dynamically to those.

<LinearLayout
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:orientation="horizontal">

<LinearLayout
    android:id="@+id/layout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"/>

<LinearLayout
    android:id="@+id/contentWithBackground"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/out_message_bg"
    android:orientation="vertical">

    <LinearLayout
    android:id="@+id/layout2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tvMessage"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:textColor="@android:color/black"
        android:text="TEST TEST"
        android:padding="@dimen/default_layout_padding"
        android:maxWidth="260dp" />
    </LinearLayout>
</LinearLayout>

<TextView
    android:id="@+id/tvInfo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="left"
    android:textSize="12sp"
    android:text="23:21"
    android:textColor="@android:color/darker_gray" />

</LinearLayout>

Now simply get your TextView, call removeAllViews in layout2 and addView(textView) on layout1.

SoroushA
  • 2,043
  • 1
  • 13
  • 29
  • Thanks for your solution. But I'd rather go with the removing and adding the view back by index to the linear layout. – Dicky Bullin Apr 04 '16 at 21:25