Sobes.tech
Junior — Middle

How does setting the user interface in an activity happen using the setContentView method?

sobes.tech AI

Answer from AI

In Android, the setContentView method is used in an Activity to set the user interface. It takes a layout resource identifier (usually an XML file from the res/layout folder), which will be loaded and displayed on the screen. For example:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); // Set the layout activity_main.xml
}

This call links the XML layout with the Activity, after which you can access interface elements via findViewById.