Sorry in advance in case this is a stupid question!
I was playing around with a couple of things in Android, and I was experimenting with changing elements at run time which were specified in the xml file.
Button buttonSetup = (Button) findViewById(R.id.buttonSetup);
buttonSetup.setTextSize(40);
However, I noticed that this crashed the application if you make the changes before –
setContentView(R.layout.main);
What is the reason for this? (Sorry if I’m missing something obvious! I think my brain has gone into hibernation for Winter!)
I had thought it would be best practice to alter elements before setContentView, so that if you alter a lot of elements (say half a dozen or so attributes over half a dozen elements) it is not obvious to the user with things changing on screen.
Thanks!
Easy answer:
Your xml-Layout is inflated by calling
setContentView.Before you haven’t inflated it, the Activity doesn’t know about the XML.
You can manually inflate a View with this code:
and later set the View to this inflated layout with