I have written activities which used layouts which are entirely defined by xml, e.g:
setContentView(R.layout.my_layout_defined_by_xml);
I have also written activities which used layouts which are created entirely programatically, e.g.:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
game_frame_layout = new FrameLayout(getApplicationContext());
game_frame_layout.addView(some_view_I_made);
setContentView(game_frame_layout);
But what I have never done, and don’t know how to do, is combine the two (can this even be done?) For example, say I want to have the whole screen filled with buttons and views defined in xml, but wish to programmatically add an extra button somewhere. Or conversely I may wish to have a programatically created layout include a sub-layout defined by xml.
I suspect it may actually be quite trivial, but one problem is that I don’t know how to translate a layoutResID, (like R.id.my_layout_defined_by_xml) into a layout.
you can create a View object from an xml file like that:
and the other way around:
you can create a class that extends View, for example MyTextView, and when you want to include it in your xml files you do it like that: