Summery of widgets- AGN HUB
In Kotlin, widgets typically refer to UI components used in Android development with the Android SDK. These components are part of the Android Views system and are used to build graphical user interfaces for Android apps. Below is a summary of commonly used widgets in Android development with Kotlin:
Common Android Widgets in Kotlin
1. TextView
Displays text to the user.
Often used for static or dynamic content.
val textView: TextView = findViewById(R.id.textView)
textView.text = "Hello, Kotlin!"
2. EditText
Input field where the user can enter text.
val editText: EditText = findViewById(R.id.editText)
val userInput = editText.text.toString()
3. Button
Interactive element that triggers actions when clicked.
val button: Button = findViewById(R.id.button)
button.setOnClickListener {
Toast.makeText(this, "Button clicked!", Toast.LENGTH_SHORT).show()
}
4. ImageView
Used to display images.
val imageView: ImageView = findViewById(R.id.imageView)
imageView.setImageResource(R.drawable.image)
5. CheckBox
Toggle option that can be checked or unchecked.
val checkBox: CheckBox = findViewById(R.id.checkBox)
if (checkBox.isChecked) {
Toast.makeText(this, "Checked!", Toast.LENGTH_SHORT).show()
}
6. RadioButton & RadioGroup
Single-selection radio buttons.
val radioGroup: RadioGroup = findViewById(R.id.radioGroup)
val selectedId = radioGroup.checkedRadioButtonId
val selectedRadioButton: RadioButton = findViewById(selectedId)
Toast.makeText(this, "Selected: ${selectedRadioButton.text}", Toast.LENGTH_SHORT).show()
7. Spinner
Drop-down menu for selecting an item from a list.
val spinner: Spinner = findViewById(R.id.spinner)
val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, listOf("Option 1", "Option 2"))
spinner.adapter = adapter
8. Switch
Toggle between on and off states.
val switch: Switch = findViewById(R.id.switch)
switch.setOnCheckedChangeListener { _, isChecked ->
Toast.makeText(this, "Switch is $isChecked", Toast.LENGTH_SHORT).show()
}
9. RecyclerView
Displays a list or grid of items efficiently.
Requires an adapter and layout manager.
val recyclerView: RecyclerView = findViewById(R.id.recyclerView)
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.adapter = MyAdapter(myDataList)
10. ProgressBar
Shows loading progress.
val progressBar: ProgressBar = findViewById(R.id.progressBar)
progressBar.visibility = View.VISIBLE
Custom Widgets
You can create your custom widgets by extending existing ones or combining multiple widgets into a ViewGroup.
class CustomWidget(context: Context, attrs: AttributeSet) : LinearLayout(context, attrs) {
init {
LayoutInflater.from(context).inflate(R.layout.custom_widget, this, true)
}
}
Jetpack Compose (Modern Alternative)
Jetpack Compose is the modern UI toolkit in Android development. Widgets are replaced by Composable functions in Compose.
@Composable
fun MyButton() {
Button(onClick = { /* Do something */ }) {
Text("Click Me")
}
}
Would you like examples of a specific widget or guidance on using Jetpack Compose?
Comments
Post a Comment