Change Order of Views in Linear Layout Android

change order of views in linear layout android

I would try to remove all views with removeView(view) and add them with addView(childView, index) in that order you like.

how to change order of statically created layouts in a linear layout dynamically? (Android Studio)

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.

How do I change the order of the textviews and buttons in a linerLayout or RelativeLayout?

  1. Yes it is possible to change the order of views dynamically, even though the Views are added in XML.
  2. Yes it's possible to move sub-views around from one layout to another layout.

Check out this example code:

public class MainActivity extends Activity {
private Button button1, button2, button3;
private CheckBox checkBox1;
private LinearLayout ll1, ll2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ll1 = (LinearLayout) findViewById(R.id.ll1);
ll2 = (LinearLayout) findViewById(R.id.ll2);

button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// Move views around.
float button2x = button2.getX();
float button2y = button2.getY();

float checkbox1x = checkBox1.getX();
float checkbox1y = checkBox1.getY();

button2.setX(checkbox1x);
button2.setY(checkbox1y);

checkBox1.setX(button2x);
checkBox1.setY(button2y);
}
});

button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Move a view from one layout to another.
ll1.removeView(button2);
ll2.addView(button2);
}
});

button2 = (Button) findViewById(R.id.button2);

checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
}
}

=== activity_main.xml ===

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" android:id="@+id/ll1">

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="42dp"
android:layout_marginTop="22dp"
android:text="Button" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2" />

<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="99dp"
android:text="CheckBox" />

<LinearLayout android:id="@+id/ll2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button3" />

</LinearLayout>

</LinearLayout>

How to order elements inside a Linear Layout?

set an id to your LinearLayout:

android:id="@+id/myLinearLayout

and in Java:

public void reorder() {

LinearLayout myLinearLayout = (LinearLayout) findViewById(R.id.myLinearLayout);
// get number of children
int childcount = myLinearLayout.getChildCount();
// create array
View[] children = new View[childcount];

// get children of linearlayout
for (int i=0; i < childcount; i++){
children[i] = myLinearLayout.getChildAt(i);
}

//now remove all children
myLinearLayout.removeAllViews();

//and resort, first position
myLinearLayout.addView(children[2]);
//second position
myLinearLayout.addView(children[0]);
//etc.
}

look at Droid: How to reorder a linearlayouts contents programmatically? and Get all child views inside LinearLayout at once

Droid: How to reorder a linearlayouts contents programmatically?

Just find your views then removethen from linearlayout and add by new order.

ln1= (LinearLayout)findViewById(R.id.ln1);

btn1= (Button)findViewById(R.id.button1);
btn2= (Button)findViewById(R.id.button2);
btn3= (Button)findViewById(R.id.button3);
btn4= (Button)findViewById(R.id.button4);
btn5= (Button)findViewById(R.id.button5);
btn6= (Button)findViewById(R.id.button6);

ln1.removeAllViews();

ln1.addView(btn6);
ln1.addView(btn1);
ln1.addView(btn2);
ln1.addView(btn3);
ln1.addView(btn4);
ln1.addView(btn5);

How do I shift a View under another View in LinearLayout?

You would need to work with the children of the linear layout. Something like this:

public void reorderButtons() {
LinearLayout layout = (LinearLayout) findViewById(R.id.main_layout);

View button1 = layout.getChildAt(0);
View button2 = layout.getChildAt(1);

// Remove both buttons
layout.removeAllViews();

// Add the buttons in the order you want
layout.addView(button2);
layout.addView(button1);
}

Android Linear Layout keeps reordering children whenever I change it's properties

This is a known issue with android studio from Android Studio 3.5 Canary 8 and has been fixed in Android studio version 3.5.2 (have a look at issue #129457736 for more details). So all you need to do is update your Android Studio version.

When editing XML code, the IDE might apply an incorrect code style
when you select Code > Reformat Code from the menu bar. To fix this
issue, reset the appropriate Android code style as follows.

In case you want to keep your Android Studio version, do as the following:

  1. Open the Settings window by clicking File > Settings (on macOS, Android Studio > Preferences).
  2. In the left panel, click Editor > Code Style > XML.
  3. Near the top-right corner of the right panel, select Set from > Predefined Style > Android
  4. Click OK (you may need to restart Android Studio)


Related Topics



Leave a reply



Submit