I’m relatively new to Android development and Java (learning both simultaneously sort of…).
My question is: if I set up a button in main.xml, I still need to actually create the button in my Activity, right? Like the XML just controls the look of the button, but you need to actually create a button by doing something like
private Button myButton;
Just wanted to make sure I had this clear conceptually. You create an object in your class and then just tell Android to do something like
myButton = (Button) findViewById(R.id.my_button);
Just wanted to make sure I’m clear on this.
Yes, that is exactly what you would need to do.
Update:
Just to help you further conceptualize the process, Android uses the
main.xmlfor your widgets (such as yourButton) and different layouts. This is where you would need to define your widget.Now inside of your project, you would need to create your widget variable,
Once your
myButtonobject is created, you can then start adding functionality to the button, such as what it should do when clicked, (although that can be defined in yourmain.xmlfile withandroid:onClickattribute)I hope this clarifies things a bit, if you need me to elaborate at any points please let me know.