Junior
What needs to be done in an Android project to start drawing a user interface on the screen?
sobes.tech AI
Answer from AI
It is necessary to:
- Create an XML layout file in the
res/layoutdirectory. - Add user interface elements (widgets) to this file.
- Assign this layout file to an activity or fragment using the
setContentView()method.
Example in an activity:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main) // Specify the layout file
// Further UI setup
}
}
Example in a fragment:
class MyFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout
return inflater.inflate(R.layout.fragment_my, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// Further UI setup after view creation
}
}