A little bit about layout inflater. loading layouts dynamically.

satya - Monday, August 15, 2011 1:43:10 PM

satya komatineni layout inflater

satya komatineni layout inflater

Search for: satya komatineni layout inflater

satya - Monday, August 15, 2011 2:20:45 PM

you can load a layout multiple times


public class Uncscramble1Activity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setup1();
    }
    private void setup1()
    {
    	LinearLayout buttonParentLayoutView
    	= (LinearLayout)this.findViewById(R.id.buttonParentLayoutView);
    	
    	LayoutInflater li = this.getLayoutInflater();
    	Button b1 =
    	(Button)li.inflate(R.layout.button_layout, null);
    	b1.setText("A");

    	Button b2 =
        	(Button)li.inflate(R.layout.button_layout, null);
        	b2.setText("B");
    	
    	buttonParentLayoutView.addView(b1);
    	buttonParentLayoutView.addView(b2);
    }
}

satya - Monday, August 15, 2011 2:22:12 PM

here is the layout that was loaded multiple times in its own layout file


<?xml version="1.0" encoding="utf-8"?>
<Button 
xmlns:android="http://schemas.android.com/apk/res/android">
android:text="Button" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
</Button>

satya - Monday, August 15, 2011 2:23:10 PM

The id won't make sense in this scenario

you will need to keep track of the loaded views through pointers. Any time you hold stuff in pointers, beware of device rotation.