Removing the 'box' from the checkbox in Android - android

I am an entry level software developer, and have found tons of great answers from this site, but I can't seem to find how to hide the 'box' of a checkbox in Android. I just want the check mark to show, when a user selects an option. Here are some of the most recent things I have tried.
chkSortOrder.setBackgroundResource(android.R.color.transparent);
chkSortOrder.setBackgroundResource(android.R.drawable.checkbox_off_background);
Both of these still show the box.

put the following attribute in your checkbox tag in XML.
android:button="#android:color/transparent"

It's quite late but in any case, if it helps anyone. If you wanna set custom background to RadioButton programmatically, then you can set it like this,
checkbox.setButtonDrawable(null);
checkbox.setBackgroundResource(R.drawable.your_custom_drawable);

It's 2020! and many things have changed. But if you are struggling with this like me and tried everything here (and other solutions) with no luck, then try this one too. What I've got with other solutions came to this (Emulator, API 16):
Weird behavior! Two drawables, right is my custom box and default one on the left. And then accidentally i added this:
app:buttonCompat="#null"
and the second one is gone finally!
Here is the complete definition:
<CheckBox
android:id="#+id/cb_fs_ExactSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#null"
android:gravity="center_vertical"
android:padding="5dp"
android:text="#string/fs_options_exact"
android:textColor="#color/textPrimary"
android:textSize="#dimen/textSmall"
app:buttonCompat="#null"
app:drawableRightCompat="#drawable/drw_checkbox" />
You need to set both button and buttonCompat to null.

I found this error when updating from androidx.appcompat:appcompat:1.0.0 to 1.1.0.
None of the other answers were useful to me as setting button attribute to #null or #android:color/transparent doesn't work on Android API Level 20 or lower.
What I did is to change my CheckBox to ToggleButton and set textOn and textOff attributes to an empty string
<ToggleButton
...
android:background="#drawable/custom_selector"
android:textOff=""
android:textOn="" />
This way I could update to androidx appCompat 1.1.0

You can also toggle between a CheckBox and a TextView. Just make one view visible and another one hidden. This also makes sense because the CheckBox contains paddings around the check mark.
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<CheckBox
android:id="#+id/checkbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checkMark="#null"
android:checked="true"
android:gravity="center_vertical"
android:paddingStart="3dp"
android:paddingLeft="3dp"
android:text="My text"
android:textColor="#color/black"
android:textSize="12sp"
/>
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My text"
android:textColor="#color/black"
android:textSize="12sp"
/>
</FrameLayout>

Related

AppCompatButton does not show on Android Marshmallow

I put two android.support.v7.widget.AppCompatButton in a linear layout. They show and work fine in Android versions 4.x and 5.x, but do not show up on Version 6 (The phone is S7 Edge).
<android.support.v7.widget.AppCompatButton
android:id="#+id/btnAsk"
android:textSize="16sp"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="#string/btn_ask"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:textStyle="bold"
android:background="#drawable/round_shape_btn"
android:textColor="#color/white"/>
<android.support.v7.widget.AppCompatButton
android:id="#+id/btnBuy"
android:layout_width="0dip"
android:layout_weight="1"
android:textSize="16sp"
android:layout_height="wrap_content"
android:text="#string/btn_buy"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
android:textStyle="bold"
android:background="#drawable/round_shape_btn"
android:textColor="#color/white" />
</LinearLayout>
I googled it and checked SOF for a possible solution but could not find anything working.
Any help is appreciated!
You can simply use Button in your layout instead of specifying AppCompatButton as according to the docs for AppCompatButton:
[AppCompatButton] will automatically be used when you use Button in your layouts. You should only need to manually use this class when writing custom views.
I don`t know exactly how that happened but I changed the layout from
LinearLayout to RelativeLayout
and the buttons showed up. Weird!
I believe your problem is that the Orientation property from the LinearLayout was not set, the default is horizontal which causes the views not to show specially if there's a first view with MATCH_PARENT width.

How can I perfectly mimic ActionButton style?

I'm using the support Toolbar and I've added some Buttons to it that I'd like to have mimic the ActionButton style. I got pretty close using style="#android:style/Widget.ActionButton", but the font size or style seems to be slightly off. Adding android:textStyle="bold" got me closer still.
<Button
style="#android:style/Widget.ActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEST"
android:id="#+id/removeButton"
android:layout_gravity="center" />
I've also tried style="#android:style/Widget.Holo.ActionButton" and some others in the button which seems to change nothing
I'm not totally sure this is the right way, but I kept digging and this appears correct when adding android:textAppearance="#style/TextAppearance.AppCompat.Widget.ActionBar.Menu" to my Button like so:
<Button
style="#android:style/Widget.ActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEST"
android:textAppearance="#style/TextAppearance.AppCompat.Widget.ActionBar.Menu"
android:id="#+id/removeButton"
android:layout_gravity="center" />
Hopefully it will help someone, as I couldn't find this anywhere. I apparently can't accept my own answer for a couple days.

Why switch's textOn and textOff are not displaying?

I am trying to make a listview with an on/off switch. I found this simple code but the problem is it is not working for me.
<Switch
android:id="#+id/itemListSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="Vibrate on"
android:textOff="Vibrate off"/>
Yes, it displays a switch but the text (Vibrate on/off) is not. Only a circle that can be switched is displayed. I want to post a screenshot but i am not allowed to because i lack reputation. Can anyone tell me why because i tried to find answers but a simple code like the one above is working for them. If it helps, my Android phone version is 5.0.2. Thanks in advance!
First of all you should use SwitchCompat in order to make it compatible with all android versions and have the nice look of material design of the switch.
Back to your problem, you are missing an attribute for showing the text -app:showText -, here is an example:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
...>
<android.support.v7.widget.SwitchCompat
android:id="#+id/switch_compat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="50dp"
android:layout_marginStart="50dp"
android:layout_marginTop="25dp"
android:checked="false"
android:textOff="OFF"
android:textOn="ON"
app:showText="true"/>
</RelativeLayout>

Correct way to remove the box of checkbox completely in API 16 and lower

I've got a problem with hiding the box of my CheckBox. My CheckBox looks like this at the moment:
<CheckBox
android:id="#+id/favoritesBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:button="#null"
android:tag="1"
android:textStyle="italic"
android:text="#string/details_tags_favs" />
The only problem here is, the button=#null is removing its style and its not completely gone. Because of that, my text is not centered but moved to the right (by box size).
Is there a smooth way to get rid of it completely in API 16 and lower?
With a little help from from Andrew T. with my RubberDuck debugging I found solution for this problem.
What you have to do is set background="#android:color/transparent on CheckBox and the invisible box will be gone from your View.
So my new CheckBox looks like this:
<CheckBox
android:id="#+id/favoritesBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:button="#null"
android:background="#android:color/transparent"
android:tag="1"
android:textStyle="italic"
android:text="#string/details_tags_favs" />
Following property work for me.
android:button="#null"

Is that is a bug with CheckBox in android?

I am using 2.1 android version. I was creating a check box and I saw something Strange with checkbox. When I put android:padding="5dp" the check box shown as
But the text should be shown next to checkbox. When I remove padding Its looks fine. Is that mean it is a bug or I am taking it in a wrong sense?
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginLeft="10dp"
android:padding="5dp"
android:text="Select the checkbox"
android:textColor="#color/black" />
It's more of a bad implementation on Google's part. The answer to this question will have your solution: Android - Spacing between CheckBox and text
Instead of
android:layout_gravity="left"
you can try
android:gravity="center"
It did the work for me.

Categories

Resources