Junior — Middle
What steps are necessary to display a user interface in your project?
sobes.tech AI
Answer from AI
The following steps are usually performed to display a user interface in an Android project:
-
Creating a layout — describing the UI structure in an XML file (e.g.,
activity_main.xml). -
Defining an activity or fragment — a Java/Kotlin class that is linked to the layout.
-
Setting the layout in the activity — calling
setContentView(R.layout.activity_main)in theonCreate()method. -
Initializing and configuring UI elements — obtaining references to widgets via
findViewByIdand setting event listeners. -
Handling user interactions — implementing logic for button presses, input, and other events.
Example code for an activity:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button = findViewById<Button>(R.id.myButton)
button.setOnClickListener {
Toast.makeText(this, "Button pressed", Toast.LENGTH_SHORT).show()
}
}
}