How can I customize my layout.xml from code before calling the setContentView (View) function?

advertisements

I would like to customize my layout definition (declared in my_layout.xml file) from code.

Unfortunately I can use findViewById() function to find the specific views (defined in my_layout.xml file) and customize them from code only after I called setContentView(R.layout.my_layout).

But what if I want to customize my layout first, before calling setContentView()? How can I access the specific views before calling setContentView()?


LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout myLayout = (RelativeLayout)inflater.inflate(R.layout.my_layout, null);

Now you can use the views/resources defined in R.layout.my_layout and add to your custom layout.