Why switch's textOn and textOff are not displaying? - android

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>

Related

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.

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"

Removing the 'box' from the checkbox in 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>

Item list in Android like GooglePlay

I'm a beginner in Android development. I'm creating an application where I need to show products as a list with:
Thumbnail
Name
Price
Rating
I want to make it somewhat like the Apps list in Google play. Can someone direct me to a starting point in making that kind of UI?
I think its something related to GridView but, if yes, not sure how to proceed.
Thanks.
EDIT: I think people misunderstood my GooglePlay reference. Im pasting an image to show how I want my UI to be.
http://i49.tinypic.com/2cqjqx0.png
(I'm not allowed to post images..)
I already have the tab swipes and everything. Only want to know how this type of list could be created.
Thanks again!
I think this and this is what you need.
check out the links. They will be useful to you. Its called customize Listview
What you need to do is to create a custom view called layout for a list Item .
Following code will create a list item of your choice. Modify code as per your requirement. But you can idea from following code.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/txtItemName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/imageView1"
android:text="Item Name"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/txtItemPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView1"
android:layout_alignLeft="#+id/txtItemName"
android:text="Item Price" />
<TextView
android:id="#+id/txtRating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView1"
android:layout_alignBottom="#+id/textView1"
android:layout_alignParentRight="true"
android:text="Rating"
android:textAppearance="?android:attr/textAppearanceSmall" />
Just copy and paste above code in your layout and switch to Graphical Layout. You can see outcome.
Following links helps you how to use customized layout in your list view.
Custom List1 and Custom List2
This two way Android GridView that can be configured to scroll horizontally or vertically.
This gridview is what you are looking for.
Hope it helps :)

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