Basic Setting
build.gradle (:app)
android {
buildFeatures {
viewBinding true
}
...
}
MainActivity.kt
class MainActivity : AppCompatActivity() {
private val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
}
}
Button Event
Basic Button
class MainActivity : AppCompatActivity() {
private val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
binding.button.setOnClickListener {
binding.textBox.setText("Changed Text")
}
}
}
Radio Button
class MainActivity : AppCompatActivity() {
private val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
binding.radioGroup.setOnCheckedChangeListener { group, checkedId ->
when (checkedId) {
R.id.Btn1 -> Log.d("RadioButton", "Select Button1")
R.id.Btn2 -> Log.d("RadioButton", "Select Button2")
R.id.Btn3 -> Log.d("RadioButton", "Select Button3")
}
}
}
}
Check Box
class MainActivity : AppCompatActivity() {
private val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }
private val listener by lazy {
CompoundButton.OnCheckedChangeListener { buttonView, isChecked ->
when (buttonView.id) {
R.id.checkBox1 -> Log.d("CheckBox", "${if (isChecked) "BOX1 ON" else "BOX1 OFF"}")
R.id.checkBox2 -> Log.d("CheckBox", "${if (isChecked) "BOX2 ON" else "BOX2 OFF"}")
R.id.checkBox3 -> Log.d("CheckBox", "${if (isChecked) "BOX3 ON" else "BOX3 OFF"}")
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
binding.checkBox1.setOnCheckedChangeListener(listener)
binding.checkBox2.setOnCheckedChangeListener(listener)
binding.checkBox3.setOnCheckedChangeListener(listener)
}
}
}
Switch
class MainActivity : AppCompatActivity() {
private val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
binding.switch1.setOnCheckedChangeListener { buttonView, isChecked -> Log.d("SWITCH", "${if (isChecked) "SWITCH1 ON" else "SWITCH1 OFF"}") }
binding.switch2.setOnCheckedChangeListener { buttonView, isChecked -> Log.d("SWITCH", "${if (isChecked) "SWITCH2 ON" else "SWITCH2 OFF"}") }
binding.switch3.setOnCheckedChangeListener { buttonView, isChecked -> Log.d("SWITCH", "${if (isChecked) "SWITCH3 ON" else "SWITCH3 OFF"}") }
}
}
Widgets Event
SeekBar
class MainActivity : AppCompatActivity() {
private val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
binding.seekBar.setOnSeekBarChangeListener(object: SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
binding.seekBarValue.text = "$progress"
}
override fun onStartTrackingTouch(seekBar: SeekBar?) {}
override fun onStopTrackingTouch(seekBar: SeekBar?) {}
})
}
}
Edit Text Event
class MainActivity : AppCompatActivity() {
private val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
binding.inputText.addTextChangedListener {
Log.d("EditText", "Input: ${it.toString()}")
}
}
}