Reputation: 172
I have a layout created with XML code. which has a scrollview
and in that scroll view there are two relative layouts
here's the layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#151515">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_10sdp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:onClick="monday"
android:id="@+id/monday"
android:layout_width="match_parent"
android:layout_height="@dimen/_145sdp"
app:srcCompat="@drawable/mondaysmolgray" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginStart="@dimen/_70sdp"
android:layout_marginBottom="0dp"
android:orientation="horizontal"
android:weightSum="3">
<TextView
android:id="@+id/mondaydate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/_18sdp"
android:layout_weight="1"
android:gravity="center"
android:text=" No tasks"
android:textColor="#151515"
android:textSize="@dimen/_15sdp"></TextView>
<TextView
android:id="@+id/mondaytasks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/_18sdp"
android:layout_weight="1"
android:gravity="center"
android:text=" No tasks"
android:textColor="#151515"
android:textSize="@dimen/_15sdp"></TextView>
<TextView
android:id="@+id/mondaytodos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/_18sdp"
android:layout_weight="1"
android:gravity="center"
android:text=" No tasks"
android:textColor="#151515"
android:textSize="@dimen/_15sdp"></TextView>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:onClick="tuesday"
android:id="@+id/tuesday"
android:layout_width="match_parent"
android:layout_height="@dimen/_145sdp"
app:srcCompat="@drawable/tuesdaysmolgray" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginStart="@dimen/_70sdp"
android:layout_marginBottom="0dp"
android:orientation="horizontal"
android:weightSum="3">
<TextView
android:id="@+id/tuesdaydate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/_18sdp"
android:layout_weight="1"
android:gravity="center"
android:text=" No tasks"
android:textColor="#151515"
android:textSize="@dimen/_15sdp"></TextView>
<TextView
android:id="@+id/tuesdaytasks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/_18sdp"
android:layout_weight="1"
android:gravity="center"
android:text=" No tasks"
android:textColor="#151515"
android:textSize="@dimen/_15sdp"></TextView>
<TextView
android:id="@+id/tuesdaytodos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/_18sdp"
android:layout_weight="1"
android:gravity="center"
android:text=" No tasks"
android:textColor="#151515"
android:textSize="@dimen/_15sdp"></TextView>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
as you can see there two relative layouts inside a scroll view. I want to change the order of two linear layouts dynamically from without creating the whole thing again in runtime code. how can I do that?
Upvotes: 2
Views: 1014
Reputation: 2496
There is no reorder methods in android layout.
So you can try to remove all views with removeView(view)
.
And then you need to add them with addView(childView, index)
in that order you like.
in your case need to define id to parent LinearLayout and two RelativeLayouts.
<ScrollView>
<LinearLayout android:id="@+id/container">
<RelativeLayout android:id="@+id/first">
<RelativeLayout android:id="@+id/second">
</LinearLayout>
</ScrollView>
In JAVA Code:
ViewGroup container = findViewById(R.id.container);
View first = findViewById(R.id.first);
View second = findViewById(R.id.second);
container.removeView(first);
container.addView(first); // append first view to last.
Upvotes: 3