Android custom checkbox (or ToggleButton) with numbers inside - android

I'm writing an app that will have 60 checkboxes, numbered 1-60.
My goal is to achieve something like this:
Is there a way to achieve this without having to draw 60 .png files with the number inside? Could I draw only the edges and add the numbers as a text property of the button?
I tried customizing a ToggleButton (got the idea from here)
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/toggle_selector"
android:background="#null"
android:paddingLeft="10dp"
android:layout_centerHorizontal="true"
android:gravity="center"
android:textOn="60"
android:textOff="60" />
toogle_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="true"
android:drawable="#drawable/checked" />
<item
android:drawable="#drawable/not_checked" />
</selector>
but as a result, the text was placed at the right of the button. Is it possible to place the text on top of the image?

You can do it with relative layout with textview on top of Toggle button like this. Replace margins and size as per your need
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/background_floating_material_dark"
tools:context="com.contag.app.fragment.NavDrawerFragment">
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/toggle_selector"
android:background="#android:color/transparent"
android:gravity="center"
android:textOn="#null"
android:textOff="#null"
android:minHeight="0dp"
android:minWidth="0dp"
android:id="#+id/toggleButton"
android:layout_marginTop="26dp"
android:layout_marginStart="156dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="60"
android:textColor="#android:color/holo_green_dark"
android:textSize="15sp"
android:id="#+id/textView"
android:layout_alignLeft="#+id/toggleButton"
android:layout_alignTop="#+id/toggleButton"
android:layout_alignRight="#+id/toggleButton"
android:layout_alignBottom="#+id/toggleButton"
android:gravity="center" />
</RelativeLayout>

Related

Parent Relative Layout Foreground Color affecting Textview Color

I need relative layout background with some transparent for that i used gradient option, But it is affecting text view color also , I gave color of text view is white color but it looks like gray color. Please help me to get out of this issue.
<RelativeLayout
android:id="#+id/topLayout1"
android:layout_width="match_parent"
android:layout_height="210dp"
android:layout_alignParentTop="true"
android:foreground="#drawable/image_overlay"
>
<RelativeLayout
android:id="#+id/topLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/preloader_image"
>
<TextView
android:id="#+id/resName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:text="Green Way"
android:textColor="#color/White"
android:textSize="25sp"
android:backgroundTint="#color/White"
/>
<TextView
android:id="#+id/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/resName"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:text="#string/items"
android:textColor="#color/White"
android:textSize="16sp"
/>
</RelativeLayout>
</RelativeLayout>
and image overlay code,
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#B0000000"
android:centerColor="#A0000000"
android:endColor="#00FFFFFF"
android:angle="270"
/>
</shape>
<color name="White">#FFFFFF</color>
You can use #83000000 color code for setting transparency and that too in android:background instead of android:background property. Alternatively, you can also use "android:aplha="0.x" where 1 < x < 9, for transparency. Your final code will look like this.
<RelativeLayout android:id="#+id/topLayout1"
android:layout_width="match_parent"
android:layout_height="210dp"
android:layout_alignParentTop="true"
android:background="#83000000"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:id="#+id/topLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/resName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:text="Green Way"
android:textColor="#FFFFFF"
android:textSize="25sp"
android:backgroundTint="#FFFFFF" />
<TextView
android:id="#+id/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/resName"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:text="Items"
android:textColor="#FFFFFF"
android:textSize="16sp" />
</RelativeLayout>
You should see this ANSWER to know more about transparency. You can use various hex codes to achieve different level of transparency. Like for 50% transparency 80 code is used and color code would be like #80000000. Similary, you can use various codes in the scheme #xx000000.

Android: Setting View background using itemSelector with complex Views

We have a relatively simply Android item template on a ListView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:background="#android:drawable/list_selector_background">
<!--Encounter Type and Start-->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/mid_grey"
android:paddingTop="12dp"
android:paddingRight="24dp"
android:paddingBottom="12dp"
android:paddingLeft="24dp">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:textStyle="bold"
android:textSize="#dimen/text_size_large"
android:text="Main Item Heading"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10:45"
android:layout_toLeftOf="#+id/startTimeRelative"
android:textColor="#color/grey1"
android:textSize="#dimen/text_size_large" />
<TextView
android:id="#+id/startTimeRelative"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12sp"
android:layout_alignParentRight="true"
android:text="Today"
android:textColor="#color/grey1"
android:textSize="#dimen/text_size_large" />
</RelativeLayout>
<!--Patient Name and Avatar-->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:paddingRight="24dp"
android:paddingBottom="8dp"
android:paddingLeft="24dp">
<ImageView
android:id="#+id/patientAvatar"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:scaleType="fitXY"
android:src="#drawable/patient_avatar" />
<LinearLayout
android:layout_toRightOf="#+id/patientAvatar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/patientname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Subsection Heading"
android:textColor="#000000"
android:textSize="#dimen/text_size_large"
android:textStyle="bold"
android:ellipsize="end"
android:maxLines="1" />
</LinearLayout>
</RelativeLayout>
<!--</LinearLayout>-->
</LinearLayout>
That displays this layout
I want to set the background of the items when they are pressed or selected. I am trying to do this by setting the item's background to a custom item selector
android:background="#android:drawable/list_selector_background"
The is is the selector xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="#color/debug_blue" />
<item android:state_checked="true" android:drawable="#color/debug_green" />
<item android:state_pressed="true" android:drawable="#color/debug_blue" />
</selector>
The problem is that because I am setting the background colour's of Views within the item the Selector does not show through as expected. I have removed one of the inner colours (used in the "Subsection Heading" in the screenshot) and set that as the colour of the Listview. That means the blue shows through when the item is pressed on the lower part of the template, as seen in this screenshot
All the Android examples showing item selectors working with simple item layouts that have one or no background colour set.
How can I set the whole item's background whilst still having one or more background colours on child view's, when not selected?
I have looked at setting drawSelectorOnTop on the ListView but that did not help

How can I place a TextView on top of ImageView

Is there an easy way to place a TextView on top of ImageView?
My code:
<ImageView
android:layout_width="120dp"
android:layout_height="120dp"
android:src="#drawable/finaloval"
android:elevation="10dp"
android:layout_centerHorizontal="true"
android:id="#+id/greencircleId"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="23dp"
android:id="#+id/textView"
android:textSize="40sp"
android:textColor="#color/colorAccent" />
Example Image
The green circle would be my #drawable/finaloval. And the %100 organic would be the TextView.
<TextView android:id="#+id/tv_versionstatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding="5dp"
android:drawableBottom="#drawable/icon_new"
android:text="text"
android:textColor="#363636"
android:textSize="20sp" />
drawableBottom is also a choise for you
Please try to set the background for the layout and place your textviews inside that , i have coded it as per the image you shared in it example
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:background="#drawable/finaloval">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="100% natural"
android:layout_marginTop="23dp"
android:id="#+id/natural"
android:textSize="20sp"
android:textColor="#color/colorAccent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="organic"
android:layout_marginTop="23dp"
android:layout_below="#id/"
android:id="#+id/natural"
android:textSize="40sp"
android:textColor="#color/colorAccent" />
</RelativeLayout>
try this xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="120dp"
android:layout_height="120dp"
android:src="#drawable/finaloval"
android:layout_gravity="center"
android:elevation="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text"
android:textSize="40sp"
android:layout_gravity="center"
android:textColor="#color/text_black" />
</FrameLayout>
Just use a LinearLayout as root and re-sequence your views from top to bottom, meaning in your case should be TextView then ImageView.
Edited based on comment
To do overlap of views, you can use RelativeLayout or FrameLayout as suggested by others.
Try this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text"
android:layout_marginTop="23dp"
android:id="#+id/textView"
android:textSize="40sp"
android:textColor="#color/colorAccent" />
<ImageView
android:layout_width="120dp"
android:layout_height="120dp"
android:src="#drawable/finaloval"
android:elevation="10dp"
android:id="#+id/greencircleId"/>
</RelativeLayout>
Think this is the simplest way of doing. Hope this helps
Try to set the imageview as the background of the textview.
Do you want to put some words upon a image or a sharp drawable?
I'll use a shape drawable to make an example.
Now I have a shape like this:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"
>
<size
android:height="100dp"
android:width="100dp"
/>
<solid
android:color="#66ccff"
/>
</shape>
Then set it as the background of a textview in a layout xml.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/circle"
android:text="Hello World!"
/>
That looks like it. It's a 100dp*100dp one.
100*100
And try to modify the width and height of the textview.
<TextView
android:layout_width="200dp"
android:layout_height="200dp"
android:background="#drawable/circle"
android:text="Hello World!"
/>
Now it looks like this. A Lot Larger.
200*200
So the conclusion is:
Change the size of drawable and use wrap_content in the layout xml.
Change "layout_width" and "layout_height" in the layout xml.
But if you want to use the smaller one or the lager one, maybe you should add some control codes in the control java file.
(Sorry I'm not in a English-spoken country so maybe my words is not clear enough)
I'm sorry that I didn't obverse the rules before answer (cuz it's my first answer).

ActionBar Tab with custom View not centered

I need to use a custom View for my tabs, the problem is that fill_parent doesn't work (as seen here).
So I need to use margin and stuff, but in order to have the view centered inside the tab in all configuration (landscape/portrait, or on a tablet where the height of the tabs will change) it's a bit tricky to do.
I don't know what value to use on each configuration. Plus, I don't find the default layout that the system uses to start with.
I'd recommend that you use the ActionBar Tab styles. This is clean and simple. For example (zeroing in on the TabView):
<style name="MyActionBar" parent="#style/android:Widget.Holo.ActionBar">
<item name="android:titleTextStyle">#style/MyActionBarTitleText</item>
<item name="android:attr/actionBarTabTextStyle">#style/MyActionBarTabText</item>
<item name="android:attr/actionBarTabStyle">#style/MyActionBarTabStyle</item>
</style>
<!-- styling for the tabs -->
<style name="MyActionBarTabStyle" parent="#style/android:Widget.Holo.Light.ActionBar.TabView">
<item name="android:background">#drawable/tab_bar_bg_master</item>
<item name="android:gravity">center</item>
</style>
(I'll also note that the tabs are highly styleable by using custom Drawables. But that's a topic for another day!)
the above reply from #Stephane-Mathis is on the right track, but I couldn't get it to work just right in my code. Here's a simpler version that uses the same concept.
Again, the idea is to force your view to be much taller than the tab itself.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="3dp"
android:paddingBottom="3dp">
<View
android:id="#+id/spacer_1"
android:layout_width="0dp"
android:layout_height="1000dp"
android:layout_weight="1" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="#drawable/ic_tabicon_1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:layout_gravity="center_vertical"
android:textColor="?accentColor"
android:text="My Albums" />
<View
android:id="#+id/spacer_2"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
The spacer_1 and spacer_2 views exist to pad the ImageView and TextView evenly on the left and right side. Furthermore, the height on spacer_1 is set to something absurdly high (1000dp) so force the parent view to be excessively tall. Then the ImageView and the TextView re both set to center_vertical.
So, if you want to do that you will need to do a bit of dirty stuff.
Since fill_parent doesn't work, you need to force the root view to have more height than the tab height and then center what you really need. So my first LinearLayout is useless, and the second one will be in the center of the tab.
Here is what I used. I just wanted one textview with another one at the top right of it.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="invisible">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical">
<TextView
android:id="#+id/tv_tab_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/tv_tab_title"
android:layout_gravity="right"
android:background="#drawable/bg_notification"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:text="1"
android:gravity="center"
android:minWidth="14dp"
android:textStyle="bold"
android:textColor="#color/blanc"
android:textSize="8dp" />
<TextView
android:id="#+id/tv_tab_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="#color/noire"
android:textSize="12dp"
android:textStyle="bold" />
<TextView //this has the same height as the `tv_tab_count` so it remains centered vertically
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/tv_tab_title"
android:layout_gravity="right"
android:background="#0f0"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:text="1"
android:textSize="8dp"
android:visibility="invisible" />
</LinearLayout>
This work for the vertical alignment but the view only has the width of the content I put in my textview. So if you need to use the complete width, you should put a really really long word in one of the invisible textviews.

How to show android checkbox at right side?

By default android checkbox shows
text at right side and checkbox at left
I want to show checkbox at right side with text at left
how do I achieve this?
I think it's too late to answer this question, but actually there is a way to achieve your goal. You just need to add the following line to your checkbox:
android:button="#null"
android:drawableRight="?android:attr/listChoiceIndicatorMultiple"
You can use your customized drawable for checkbox as well.
And for a radioButton:
android:button="#null"
android:drawableRight="#android:drawable/btn_radio"
And if you want to do it programmatically:
Define a layout and name it RightCheckBox and copy the following lines :
<?xml version="1.0" encoding="utf-8"?>
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
android:text="hello"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:button="#null"
android:drawableRight="?android:attr/listChoiceIndicatorMultiple"/>
and when you need to add it programmatically you just need to inflate it to a CheckBox and add it to the root view.
CheckBox cb = (CheckBox)((LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE)).inflate(R.layout.check_right_checkbox,null);
rootView.addView(cb);
You can do
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right|center"//or "center_vertical" for center text
android:layoutDirection="rtl"
android:text="hello" />
Following line is enough
android:layoutDirection="rtl"
You can add android:layoutDirection="rtl" but it's only available with API 17.
I can't think of a way with the styling, but you could just set the text of the checkbox to nothing, and put a TextView to the left of the checkbox with your desired text.
<android.support.v7.widget.AppCompatCheckBox
android:id="#+id/checkBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layoutDirection="rtl"
android:text="text" />`
Just copy this:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your text:"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
/>
</LinearLayout>
Happy codding! :)
Checkbox text may be not aligning to left with
android:button="#null"
android:drawableRight="#android:drawable/btn_radio"
in some device. Can use CheckedTextView as a replacement to avoid the problem,
<CheckedTextView
...
android:checkMark="#android:drawable/btn_radio" />
and this link will be helpful: Align text left, checkbox right
As suggested by #The Berga You can add android:layoutDirection="rtl" but it's only available with API 17.
for dynamic implementation, here it goes
chkBox.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
You can use checkedTextView instead.
http://developer.android.com/reference/android/widget/CheckedTextView.html
furthermore from Hazhir imput, for this issue is necessary inject that property in the checkbox xml configuration android:paddingLeft="0dp", this is for avoid the empty space at the checkbox left side.
Adding another answer to this question that uses CheckedTextView If anyone is trying to do it programatically. It also has the option of using custom images for checkbox. And can be done in a single View
final CheckedTextView checkBox = new CheckedTextView(getApplicationContext());
checkBox.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
checkBox.setId(1);
checkBox.setCheckMarkDrawable(android.R.drawable.checkbox_off_background);
checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (checkBox.isChecked()){
checkBox.setChecked(false);
checkBox.setCheckMarkDrawable(android.R.drawable.checkbox_off_background);
}else{
checkBox.setChecked(true);
checkBox.setCheckMarkDrawable(android.R.drawable.checkbox_on_background);
}
}
});
checkBox.setTextColor(Color.BLACK);
checkBox.setGravity(Gravity.LEFT);
checkBox.setText("hi");
From XML if you want to initiate -
<CheckedTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checkMark="#android:drawable/checkbox_off_background"
android:checked="false"
android:text="Hi from xml"/>
If it is not mandatory to use a CheckBox you could just use a Switch instead. A Switch shows the text on the left side by default.
The following link demonstrates how to render seveveral Standard Android view objects with an animated checkbox on the right by setting the right drawable.
Set the background to get a ripple effect.
[link to website with example checkbox on right and left side.][1]
http://landenlabs.com/android/uicomponents/uicomponents.html#checkbox
<Button
android:id="#+id/p2Button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/transparent_ripple"
android:drawableRight="#drawable/checkline"
android:gravity="left|center_vertical"
android:text="Button"
android:textAllCaps="false"
android:textColor="#android:color/white"
android:textSize="#dimen/buttonTextSize" />
<android.support.v7.widget.AppCompatButton
android:id="#+id/p2Button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/transparent_ripple"
android:drawableRight="#drawable/checkline"
android:gravity="left|center_vertical"
android:text="AppCompatButton"
android:textAllCaps="false"
android:textColor="#android:color/white"
android:textSize="#dimen/buttonTextSize" />
<TextView
android:id="#+id/p2TextView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/transparent_ripple"
android:drawableRight="#drawable/checkline"
android:gravity="left|center_vertical"
android:hapticFeedbackEnabled="true"
android:text="TextView"
android:textColor="#android:color/white"
android:textSize="#dimen/buttonTextSize" />
<android.support.v7.widget.AppCompatTextView
android:id="#+id/p2TextView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/transparent_ripple"
android:drawableRight="#drawable/checkline"
android:gravity="left|center_vertical"
android:hapticFeedbackEnabled="true"
android:text="AppCompatTextView"
android:textColor="#android:color/white"
android:textSize="#dimen/buttonTextSize" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#android:color/white" />
<CheckBox
android:id="#+id/p2Checkbox1"
android:layout_width="match_parent"
android:layout_height="#dimen/buttonHeight"
android:background="#drawable/transparent_ripple"
android:button="#null"
android:checked="true"
android:drawableRight="#drawable/checkline"
android:gravity="left|center_vertical"
android:text="CheckBox"
android:textColor="#android:color/white"
android:textSize="#dimen/buttonTextSize" />
<android.support.v7.widget.AppCompatCheckBox
android:id="#+id/p2Checkbox2"
android:layout_width="match_parent"
android:layout_height="#dimen/buttonHeight"
android:background="#drawable/transparent_ripple"
android:button="#null"
android:checked="true"
android:drawableRight="#drawable/checkline"
android:gravity="left|center_vertical"
android:text="AppCompatCheckBox"
android:textColor="#android:color/white"
android:textSize="#dimen/buttonTextSize" />
<android.support.v7.widget.AppCompatCheckedTextView
android:id="#+id/p2Checkbox3"
android:layout_width="match_parent"
android:layout_height="#dimen/buttonHeight"
android:background="#drawable/transparent_ripple"
android:checkMark="#drawable/checkline"
android:checked="true"
android:gravity="left|center_vertical"
android:text="AppCompatCheckedTextView"
android:textColor="#android:color/white"
android:textSize="#dimen/buttonTextSize" />
<!-- android:checkMark="?android:attr/listChoiceIndicatorMultiple" -->
<CheckedTextView
android:id="#+id/p2Checkbox4"
android:layout_width="match_parent"
android:layout_height="#dimen/buttonHeight"
android:background="#drawable/transparent_ripple"
android:checkMark="#drawable/checkline"
android:checked="true"
android:gravity="left|center_vertical"
android:text="CheckedTextView"
android:textColor="#android:color/white"
android:textSize="#dimen/buttonTextSize" />
<CheckBox
android:id="#+id/p2Checkbox5"
android:layout_width="match_parent"
android:layout_height="#dimen/buttonHeight"
android:background="#drawable/transparent_ripple"
android:checked="true"
android:gravity="center_vertical|end"
android:text="CheckBox"
android:textColor="#android:color/white"
android:textSize="#dimen/buttonTextSize" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#android:color/white" />
<ToggleButton
android:id="#+id/p2ToggleButton1"
android:layout_width="match_parent"
android:layout_height="#dimen/buttonHeight"
android:background="#drawable/transparent_ripple"
android:checked="true"
android:drawableRight="#drawable/checkline"
android:gravity="center_vertical|left"
android:textAllCaps="false"
android:textColor="#android:color/white"
android:textOff="ToggleButtonOff"
android:textOn="ToggleButtonOn"
android:textSize="#dimen/buttonTextSize" />
<ToggleButton
android:id="#+id/p2ToggleButton2"
android:layout_width="match_parent"
android:layout_height="#dimen/buttonHeight"
android:background="#drawable/transparent_ripple"
android:checked="true"
android:drawableRight="#drawable/btn_check_material_anim"
android:gravity="center_vertical|left"
android:textAllCaps="false"
android:textColor="#android:color/white"
android:textOff="ToggleBtnnAnimOff"
android:textOn="ToggleBtnnAnimOn"
android:textSize="#dimen/buttonTextSize" />
Sample checkline.xml (in drawable, see link for animated version in drawable-v21)
Sample transparent_ripple.xml (in drawable-v21)
<!-- Limit ripple to view object, can also use shape such as oval -->
<item android:id="#android:id/mask" android:drawable="#android:color/white" />
<item>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:enterFadeDuration="200"
android:exitFadeDuration="200">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="#80c0c000" />
</shape>
</item>
</selector>
</item>
Sample transparent_ripple.xml (in drawable, highlight only no ripple available
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="#80c0c000" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="#android:color/transparent" />
</shape>
</item>
The simplest solution that I use:
checkBox.setLayoutDirection(getResources().getConfiguration().getLayoutDirection() == View.LAYOUT_DIRECTION_LTR ? View.LAYOUT_DIRECTION_RTL : View.LAYOUT_DIRECTION_LTR);
Tip:
If you want to make CheckBox on the right side so it is better for the text to be from left to right such as English, Spanish, and Italian.
Also If you want to make CheckBox on the left side so it is better for the text to be from right to left such as Arabic, Hebrew, and Farsi.
You can use this also,
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layoutDirection="rtl"
android:drawablePadding="#dimen/padding_5"
android:drawableEnd="#drawable/ic_english"
android:button="#drawable/language_selector"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/location_permissions"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
android:textColor="#android:color/black" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:id="#+id/location_permission_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="8dp"
android:onClick="onLocationPermissionClicked" />
</RelativeLayout>
</LinearLayout>

Categories

Resources