Thursday, November 18, 2010

Adding xml Layout to an Existing xml Layout Dynamically

So some background information is needed for this tutorial. My project requires many many many screens. It's incredibly difficult to keep track of all these for development purposes, and on top of that it makes load time on the phone mind-bogglingly slow. So I am trying to create generic "blank" screens to use and reuse for my applications. In this particular example, I have created a generic list screen with a TextView "title" and a blank ListView. I already know that I can programically change the row styling, data entries, the text in the title, etc etc. But I needed to add a row of buttons to the bottom. So I created a simple xml layout with some buttons inside a horizontal linear layout. This is where I hit the snag. I assumed it would be simple to just say mainlayout.addView(extrabuttonslayout); 


I was wrong.

So after three or four days of searching, attempting, failing... (Yeah I know I'm slow) I magically discovered four magical lines of text that allow me to make all my wildest dreams come true. At least for now.

So here's how I did it:

main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_height="fill_parent"
android:layout_width="fill_parent" android:id="@+id/LinearLayout_listview">
<TextView android:layout_height="wrap_content"
android:layout_width="fill_parent" android:text="title" android:id="@+id/TextView_Title"
android:padding="5px" android:textSize="20px" />
<ListView android:layout_height="wrap_content" android:id="@android:id/list"
android:layout_width="fill_parent" android:layout_weight="1" />
<FrameLayout android:layout_height="wrap_content"
android:id="@+id/FrameLayout_listview" android:layout_width="fill_parent" />
</LinearLayout>


extrabuttonlayout.xml

<LinearLayout android:layout_height="wrap_content" android:layout_width="fill_parent"
android:gravity="center" android:id="@+id/LinearLayout_er">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Search"
android:minWidth="100px" android:id="@+id/btnEmergencyResponse_Search"></Button>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Exit"
android:minWidth="100px" android:id="@+id/btnEmergencyResponse_Exit"></Button>
</LinearLayout>

and finally the oh so dynamic MainActivity.java (I only included the part where I dynamically add a view, for info on how to dynamically populate a listview, or handle click evens, see my other entries.) Basically here we are setting our layout to be a "view" and then adding the view to the main layout. Silly me, I already thought it was a view...

public class MainActivity extends ListActivity {

LayoutInflater linflater;
LinearLayout llList;
LinearLayout llER;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

//trying to add view to listview.xml
llList = (LinearLayout) findViewById(R.id.LinearLayout_listview);
                linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View myView = linflater.inflate(R.layout.extrabuttonlayout, null);
                llList.addView(myView);


}
}

No comments:

Post a Comment