Reputation: 111
I am using a custom background color that changes depending on the state of the checkbox so my background property is already used. Most advices tell me to set the background to @null. I just want to keep the text and remove the box on the left.
I can already change the color of the background and the color of the text. I just want to remove the box.
<CheckBox
android:text="CheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/checkbox_selector"
android:textColor="@color/checkbox_text"/>
Upvotes: 3
Views: 2008
Reputation: 382
You can do that by the button
property of the checkbox to change the check drawable
like done below as per your requirement:
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:background="@drawable/checkbox_selector"
android:button="@drawable/cb_show_hide_selector"
android:textColor="@color/checkbox_text"
android:text="this is a checkbox"/>
Create the file in drawable folder
with name "cb_show_hide_selector.xml"
and add below following code:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="true"
android:drawable="@drawable/ic_input_check" /> // for checked state
<item android:state_pressed="true"
android:drawable="@drawable/ic_input_check" /> // for checked state
<item android:state_pressed="false"
android:drawable="@android:color/transparent" /> // for unchecked state
</selector>
Upvotes: 6
Reputation: 11
Example code checkbox dynamic no square button.
private fun initCustomCheckbox(list : ArrayList<String>){
for (i in list.indices) {
val checkBox = AppCompatCheckBox(this)
val params = FlowLayout.LayoutParams(
FlowLayout.LayoutParams.WRAP_CONTENT,
FlowLayout.LayoutParams.WRAP_CONTENT
)
params.setMargins(0, 0, 24, 24)
checkBox.layoutParams = params
checkBox.setPadding(24, 0, 24, 0)
checkBox.id = i
checkBox.tag = list[i]
checkBox.text = list[i]
checkBox.buttonDrawable = null
checkBox.background = ContextCompat.getDrawable(this, R.drawable.selector_check_box_empty_corner)
checkBox.setTextColor(ContextCompat.getColorStateList(this, R.drawable.selector_text_color_primary))
checkBox.gravity = Gravity.CENTER
checkBox.isClickable = true
checkBox.setTextSize(TypedValue.COMPLEX_UNIT_PX, 40f)
checkBox.setOnCheckedChangeListener { buttonView, isChecked ->
if (isChecked) {
buttonView.setPadding(0, 0, 24, 0)
buttonView.buttonDrawable = ContextCompat.getDrawable(this, R.drawable.ic_check)
} else {
checkBox.setPadding(24, 0, 24, 0)
buttonView.buttonDrawable = null
}
buttonView.layoutParams = params
}
flowLayout.addView(checkBox)
}
}
I hope this code it help you ^_^
Upvotes: 1
Reputation: 226
I offer you two different options:
1) You can set the checkbox to android: clickable="false", in the java code you call the instance checkbox.set clickable = false
2) Make a textView with set Visibility Gone, and when you click the checkbox, make the checkBox.set visibility("GONE") and the textView.set visibility("VISIBLE") that can fix your issue :D
I hope this can help you
Upvotes: 0