package
com.geeksforgeeks.myfirstkotlinapp
import
androidx.appcompat.app.AppCompatActivity
import
android.os.Bundle
import
android.view.ViewGroup
import
android.widget.CheckedTextView
import
android.widget.RelativeLayout
import
android.widget.Toast
class
MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super
.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val relativeLayout = findViewById<RelativeLayout>(R.id.relativeLayout)
val checkedTextView = CheckedTextView(
this
)
checkedTextView.layoutParams = RelativeLayout.
LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT)
checkedTextView.setText(R.string.app_name)
checkedTextView.isChecked =
false
checkedTextView.setCheckMarkDrawable(android.R.drawable.
checkbox_off_background)
checkedTextView.setOnClickListener {
checkedTextView.isChecked = !checkedTextView.isChecked
checkedTextView.setCheckMarkDrawable(
if
(checkedTextView.isChecked)
android.R.drawable.checkbox_on_background
else
android.R.drawable.checkbox_off_background)
val message = getString(R.string.pre_msg) +
" "
+
if
(checkedTextView.isChecked)
getString(R.string.checked)
else
getString(R.string.unchecked)
Toast.makeText(
this
@MainActivity
, message, Toast.LENGTH_LONG).show()
}
relativeLayout?.addView(checkedTextView)
}
}