user1823974
user1823974

Reputation: 49

How to switch xml layout ordering

I was wondering how to change the the layout of an xml file based on a conditional. So lets say we have a layout as such..

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

>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:background="@drawable/app_background"
 android:padding="5dip"
 >

<ListView android:id="@+id/xlist"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:cacheColorHint="#00000000"

            android:divider="@drawable/listdivider"
            android:dividerHeight="19dp"

           />
  <TextView 
              android:layout_width="fill_parent"
              android:background="@drawable/listdivider"
              android:layout_height="19dp"
              android:visibility="gone"
             android:id="@+id/dividerline"
              />
  <ListView android:id="@+id/ylist"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:cacheColorHint="#00000000"
             android:divider="@drawable/listdivider"
            android:dividerHeight="19dp"

           />
</LinearLayout>
</LinearLayout>

So you set two Variables as listviews, and based on the xml the "xlist" will appear before the "ylist". But for my code I would like to switch the ordering of this view if a certain condition is met. So how would I go about switching the order so that if a certain condition is met, the "ylist" will appear above the "xlist"?

Upvotes: 0

Views: 155

Answers (2)

Mr.Me
Mr.Me

Reputation: 9296

An easy way to do that is to put each view in its own xml file. Then at runtime attach them to the linearLayout in the desired order.

Say your layout file is: main.xml and you have list_a.xml, list_b.xml, and textview.xml

in your activity:

@Override

public void onCreate(Bundle bundle) {
 Super.onCreate(bundle);
 setContentView(R.layout.main);
 LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
 LayoutInflater inflater = getLayoutInflater();
 if (condition) {
 inflater.inflate(R.layout.list_a, layout);
 inflater.inflate(R.layout.textview, layout);
 inflater.inflate(R.layout.list_b, layout);


} else { 
     inflater.inflate(R.layout.list_b, layout);
 inflater.inflate(R.layout.textview, layout);
 inflater.inflate(R.layout.list_a, layout);
 }
 }

Upvotes: 3

m0skit0
m0skit0

Reputation: 25874

Never did that but this should work:

public void switchViewOrder(final ViewGroup parent, final int view1Id, final int view2Id) {

    View view1 = null;
    int view1pos = -1;
    View view2 = null;
    int view2pos = -1;

    int count = parent.getChildCount();
    for(int i = 0; i < count; i++) {
        View cur = parent.getChildAt(i);
        if (view1Id == cur.getId()) {
            view1 = cur;
        } else if (view2Id == cur.getId()) {
            view2 = cur;
        }
    }

    parent.removeViewAt(view1pos);
    parent.removeViewAt(view2pos);

    parent.addView(view1, view2pos);
    parent.addView(view2, view1pos);
}

I leave to you to add proper null-and-the-like checks.

Upvotes: 0

Related Questions