October 2018
Intermediate to advanced
464 pages
15h 17m
English
What if we wanted to create the entire layout from code? Though it may not be considered best practice, there are times when it is certainly easier (and less complex) to create the entire layout from code. Let's see how this example would look if we didn't use the layout from activity_main.xml. Here's how onCreate() would look:
@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ConstraintLayout layout = new ConstraintLayout(this); DatePicker datePicker = new DatePicker(this); layout.addView(datePicker); setContentView(layout);}
In this example, it's really not that different. If you create a view in code and want to reference it later, you either need to keep a reference to the ...