I am trying to implement SwitchCompat from AppCompat but it looks different on different version devices.
On Lollipop & Froyo it looks good but on Gingerbread to KitKat it doesn't look like a switch.
Code:
<android.support.v7.widget.SwitchCompat
android:id="#+id/label_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="No"
android:textOn="Yes"
android:checked="false" />
Can I make these switches look the same across all versions or at least make them look like a switch?
Min sdk of my application was GingerBread and I had the same problem, finally I found the solution. In order to make SwitchCompat consistent in all android versions I used two drawable at res/drawable folders, one for thumb and one for track. and assign them to SwitchCompat in java code not in xml. Here is the code you should use.
SwitchCopmat widget:
<android.support.v7.widget.SwitchCompat
android:id="#+id/label_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
drawable for thumb, switch_compat_thumb.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="#dimen/switch_compat_thumb_margin"
android:left="#dimen/switch_compat_thumb_margin"
android:right="#dimen/switch_compat_thumb_margin"
android:top="#dimen/switch_compat_thumb_margin">
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true">
<shape android:shape="oval">
<size
android:width="#dimen/switch_compat_thumb_size"
android:height="#dimen/switch_compat_thumb_size"/>
<solid android:color="#android:color/red"/>
</shape>
</item>
<item>
<shape android:shape="oval">
<size
android:width="#dimen/switch_compat_thumb_size"
android:height="#dimen/switch_compat_thumb_size"/>
<stroke
android:width="#dimen/switch_compat_thumb_stroke_width"
android:color="#android:color/red"/>
<solid android:color="#android:color/transparent" />
</shape>
</item>
</selector>
</item>
drawable for track, switch_compat_track.xml
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="#dimen/switch_compat_track_radius"/>
<stroke
android:width="#dimen/switch_compat_track_stroke_width"
android:color="#android:color/red"/>
<solid android:color="#android:color/transparent" />
and then, after finding it in java, assign thumb and track to SwitchCompat in java code:
final SwitchCopmat switchCompat = (SwitchCopmat) findViewById(R.id.label_switch);
//add thumb and track drawable in java since it doesn't work on xml for gingerbread
switchCompat.setThumbDrawable(getResources().getDrawable(R.drawable.switch_compat_thumb));
switchCompat.setTrackDrawable(getResources().getDrawable(R.drawable.switch_compat_track));
Related
Why are the gradients looking different in Android Studio preview and in an actual Android device? Here are the screenshots:
In Android Studio
In my Android phone
This is the drawable/btn_background.xml file:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false">
<shape android:shape="rectangle">
<gradient
android:startColor="#eee"
android:endColor="#aaa"/>
</shape>
</item>
<item android:state_pressed="true">
<shape android:shape="rectangle">
<gradient
android:startColor="#aaa"
android:endColor="#eee"/>
</shape>
</item>
</selector>
This is the XML code for the two buttons:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="display"
android:text="#string/btn_txt"
android:textAllCaps="false"
android:background="#drawable/btn_background"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/btn_background"
android:scaleType="fitCenter"
android:src="#drawable/ic_fullscreen_black" />
Is it a bug or something?
I have got a solution to my problem. I have just added the android:angle property in the gradient drawable:
<item android:state_pressed="false">
<shape android:shape="rectangle">
<gradient
android:startColor="#eee"
android:endColor="#aaa"
android:angle="0"/>
</shape>
</item>
Without the android:angle property, Android Studio and Android phones were not agreeing with each other. So, defining the angle solved the issue.
I am new to Android programming. I want to add ripple effect to cardview. Actually I found the solution, however, after touching cardview element, it seems that the effect doesn't cover entire cardview. I mean that the effect fades out suddenly and doesn't reach the edges of the cardview.
The result which I'm getting is as follows:
While I want something like this one posted in this blog post.
This is the approach I have tried:
cardview_item.xml
<android.support.v7.widget.CardView
android:layout_width="match_parent"
style="#style/MyCardViewStyle"
android:layout_height="280dp"
android:id="#+id/appletCard"
app:cardBackgroundColor="#color/cardDefaultBg"
android:clickable="true"
android:foreground="#drawable/ripple_effect"
>
//Some TextViews here ...
</android.support.v7.widget.CardView>
And this is the ripple_effect.xml in the drawable-v21 package:
<ripple
xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:attr/colorControlHighlight">
<item
android:id="#android:id/mask"
android:drawable="#android:color/white"/>
</ripple>
If it is important, I use support library.
You should apply mask to your ripple. Try this:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:attr/colorControlHighlight">
<item android:id="#android:id/mask">
<shape android:shape="rectangle">
<solid android:color="#000000" />
<corners android:radius="#dimen/card_corner_radius" />
</shape>
</item>
</ripple>
You can use android default ripple effect with "?attr/selectableItemBackground"
<android.support.v7.widget.CardView
android:id="#+id/appletCard"
android:layout_width="match_parent"
android:layout_height="280dp"
style="#style/MyCardViewStyle"
android:clickable="true"
android:foreground="?attr/selectableItemBackground"
app:cardBackgroundColor="#color/cardDefaultBg">
</android.support.v7.widget.CardView>
Or create custom
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true">
<shape
android:shape="rectangle">
<solid android:color="#color/RippleColor" />
</shape>
</item>
<item android:state_pressed="true">
<shape
android:shape="rectangle">
<solid android:color="#color/RippleColor" />
</shape>
</item>
<item>
<shape
android:shape="rectangle">
<solid android:color="#color/backColor" />
</shape>
</item>
</selector>
If you want that effect reach the edges, use this library that provides you OnRippleCompleteListener, so you can wait until it finish effect and reach the edges
After checking the similar applications which are using ripple effect for their ui elements, I found out that the effect has changed over different versions of Android. The case I'm looking for is used in Lollipop Android devices. So as I ran the emulator on API 25 (Nogut), I didn't get the desired result.
Thank you all for ur responses.
I am facing the issue in creating the checkbox in circular shape in android. I tried many methods but my problem is not solved.I created the shapes and applied to the checkbox then also problem is not solved.Please help me how to create the check box in circle shape .
How to create the circular checkbox like shown image.
After spending some time, i have created this template, which you can use. You may need to modify as required.
In activity.xml
<CheckBox
android:id="#+id/checkb"
android:layout_width="115dp"
android:layout_height="50dp"
android:button="#drawable/custom_checkbox"
android:scaleX="3"
android:scaleY="3"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="15dp"
android:layout_marginEnd="15dp" />
create a new xml in drawable folder called custom_checkbox.xml
<?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/checked" />
<item android:state_pressed="true"
android:drawable="#drawable/checked" />
<item android:state_pressed="false"
android:drawable="#drawable/unchecked" />
</selector>
create a new xml in drawable folder called checked.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true">
<layer-list>
<item>
<shape android:shape="oval">
<corners android:radius="1dp" />
<stroke
android:width="1dp"
android:color="#777" />
<gradient
android:startColor="#990000"
android:centerColor="#990000"
android:endColor="#990000"
android:angle="270" />
<size
android:width="30dp"
android:height="30dp" />
</shape>
</item>
<item
android:width="8dp"
android:height="2dp"
android:top="20dp"
android:left="6dp">
<rotate
android:fromDegrees="45">
<shape android:shape="rectangle">
<solid android:color="#fff"/>
</shape>
</rotate>
</item>
<item
android:width="19dp"
android:height="2dp"
android:top="16dp"
android:left="9dp">
<rotate
android:fromDegrees="-45">
<shape android:shape="rectangle">
<solid android:color="#fff"/>
</shape>
</rotate>
</item>
</layer-list>
</item>
</selector>
create a new xml in drawable folder called unchecked.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
<corners android:radius="1dp" />
<stroke
android:width="1dp"
android:color="#777" />
<gradient
android:startColor="#990000"
android:centerColor="#990000"
android:endColor="#990000"
android:angle="270" />
<size
android:width="30dp"
android:height="30dp" />
</shape>
When unchecked it looks as below. (you can add the code between from checked.xml and modify the top and left to give X when checkbox is not checked)
When checked it will look as below
If this works mark it as answer.
many of the previous answers are well accepted with chirag90's answer being the best but also need a lot of coding, in my answer i will retake his concept but will show you how you can create your own circular checkbox drawables with all the style you want without any coding needed, you will then use this drawables to defind the states of the checkbox like in chirag90's answer
first go to freelogomaker.com and create your drawables, it is very easy to use and you can create any thing, on the website go to the shape's search bar and type "round checkbox" then click the search button, a list of mutiple drawables will be presented to you so you can choose
above are the drawables i selected, customise as you wish and save then go to appiconmaker.co and use the drawables you created to generate the various design sizes of the drawables like mdpi,hdpi,xhdpi and xxhdpi, below are the drawables i made
unchecked checkbox
checked checkbox
once that is done you can then add the drawables to your project with the coresponding names checked and unchecked in your drawables folder,once all that done now create custom_checkbox.xml like in chirag90's answer
<?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/checked" />
<item android:state_pressed="true"
android:drawable="#drawable/checked" />
<item android:state_pressed="false"
android:drawable="#drawable/unchecked" />
</selector>
now in your layout create your checkbox as follows
<CheckBox
android:id="#+id/checkman"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="15dp"
android:layout_marginEnd="15dp"
android:button="#drawable/custom_checkbox"
android:scaleX="0.8"
android:scaleY="0.8" />
you my then modify the android:scaleX="0.8" and android:scaleY="0.8" to fit your layout
this is the result in my project
unchecked
checked
hope this helps many out there
Solution accepted is correct, but following the same flow you can change the shapes to use a drawable on the "checked.xml" this solution should work with android devices before API 21 because there is no width or height on shapes on xml drawables.
Unchecked XML:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
<solid android:color = "#color/lightgray" />
</shape>
Checked XML:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android = "http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="#color/colorPrimary" />
</shape>
</item>
<item
android:drawable="#drawable/check_arrow_png" />
</layer-list>
All the answers above require creating custom drawables while we can achieve this by using built-in vector drawables.
Have a look at this post it explains this beautifully.
In addition to answer of Rodolfo, I can add, that you can use inset attributes if your drawable is too big.
Note: the solution of #chirag90 works only for API 23 and higher.
Note: vector drawable can be rendered badly.
Checked.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid
android:color="#color/col_chat_selected"/>
<size
android:width="35dp"
android:height="35dp"/>
</shape>
</item>
<item>
<inset
android:drawable="#drawable/shape"
android:insetBottom="10dp"
android:insetLeft="10dp"
android:insetRight="10dp"
android:insetTop="10dp"/>
</item>
</layer-list>
All the proposed options are sheer nonsense, since it is difficult and not beautiful, my solution: learn vector animation and make everything beautiful with ShapeShifter.design
Create TWO animations Start_to_End and End_to_Start;
Export the created animation in the format AnimationVectorDrawable;
Move your animation to the resources folder drawable;
Place ImageView in XML;
For convenience, create CustomImageView { Do not worry below will be an example };
#RequiresApi(Build.VERSION_CODES.M)
class CustomCheckBox(
context: Context,
attrs: AttributeSet
) : AppCompatImageView(context, attrs) {
private val TAG = this::class.simpleName
private val check =
resources.getDrawable(R.drawable.avd_anim_start_to_end)!!.toAnimationVectorDrawable()
private val uncheck =
resources.getDrawable(R.drawable.avd_anim_end_to_start)!!.toAnimationVectorDrawable()
init {
setImageDrawable(check)
setOnClickListener { animateCheckOrUncheck() }
}
private fun animateCheckOrUncheck() {
check.registerAnimationCallback(object : Animatable2.AnimationCallback() {
override fun onAnimationEnd(drawable: Drawable?) {
Log.i(TAG, "onAnimationEnd: check")
check.reset()
setImageDrawable(uncheck)
}
})
uncheck.registerAnimationCallback(object : Animatable2.AnimationCallback() {
override fun onAnimationEnd(drawable: Drawable?) {
Log.i(TAG, "onAnimationEnd: uncheck")
uncheck.reset()
setImageDrawable(check)
}
})
(drawable as AnimatedVectorDrawable).start()
}
}
#RequiresApi(Build.VERSION_CODES.LOLLIPOP)
private fun Drawable.toAnimationVectorDrawable(): AnimatedVectorDrawable {
return (this as AnimatedVectorDrawable)
}
On devices running Android API <= 22, my custom SeekBar looks as follows:
On devices running Android API >= 23, my custom SeekBar looks as follows:
Why does my SeekBar drawable look incorrect on devices API >= 23? Here is my XML
seekbar_progress.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#android:id/background"
android:drawable="#drawable/seekbar_progress_bg" />
<item android:id="#android:id/progress">
<scale
android:drawable="#drawable/seekbar_progress_fill"
android:scaleWidth="100%" />
</item>
</layer-list>
seekbar_thumb.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<size
android:width="#dimen/seekbar_thumb"
android:height="#dimen/seekbar_thumb" />
<solid android:color="#color/white" />
<stroke
android:width="#dimen/seekbar_thumb_border"
android:color="#color/gray" />
</shape>
</item>
</layer-list>
layout file
<SeekBar
android:thumb="#drawable/seekbar_thumb"
android:progressDrawable="#drawable/seekbar_progress"
android:splitTrack="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:progress="50" />
For reasons unknown, by playing with minHeight and maxHeight I was able to get the desired behavior
I know this question is very brief but how can I create this shape in XML for an Android Studio project?
It seems like I can create a rectangle and then remove a semi circle portion from it but achieving that effect in XML seems very difficult. Has anyone done this before?
Create such background with pure xml drawables is always tricky, but possible.
I've created button you need in xml. Add this files to your drawable folder:
circle.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<!-- inset - moves circle to the left-->
<inset android:insetLeft="-150dp">
<shape android:shape="ring"
android:thicknessRatio="7"
android:innerRadius="0dp"
android:useLevel="false"
>
<stroke android:width="2dp" android:color="#000000"/>
<solid android:color="#ffffff"/>
</shape>
</inset>
</item>
</selector>
custom_button_default.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#00ff00"/>
<stroke
android:color="#000000"
android:width="2dp"/>
</shape>
</item>
<item android:drawable="#drawable/circle" />
</layer-list>
custom_button_pressed.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#dddddd"/>
<stroke
android:color="#000000"
android:width="2dp"/>
</shape>
</item>
<item android:drawable="#drawable/circle" />
</layer-list>
custom_button_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/custom_button_pressed" />
<item android:drawable="#drawable/custom_button_default"/>
</selector>
Set custom_button_selector drawable as a background of your view in layout:
<Button
android:layout_width="150dp"
android:layout_height="100dp"
android:id="#+id/button"
android:background="#drawable/custom_button_selector"
/>
You probably will have to adjust circle insetLeft value and button width, height in layout. Would be better to have this variables defined in values/dimens.xml.
Default state:
Pressed state:
I've tested it on Nexus 5 and Nexus 7 - button background looks the same.
Hi you ca use convert online tools http://www.online-convert.com/ after convert svg to xml android file this site http://inloop.github.io/svg2android/