I am trying to have a ToggleButton rectangle that contains an icon, once clicked the background should change color but the icon should remain visible.
So far I'm able to have a rectangle change color on click like such:
<ToggleButton
android:background="#drawable/toggle_selector"
android:id="#+id/toggle"
android:checked="false"
android:textOn=""
android:textOff=""
android:layout_width="60dp"
android:layout_height="60dp" />
toggle_selector.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#drawable/toggle_state_on"
android:state_checked="true" />
<item
android:drawable="#drawable/toggle_state_off"
android:state_checked="false" />
</selector>
toggle_state_on.xml:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<!-- Draw a 5dp width border around shape -->
<stroke
android:color="#4c975d"
android:width="5dp"
/>
</shape>
</item>
<!-- Overlap the left, top and right border using background color -->
<item
android:bottom="5dp"
>
<shape android:shape="rectangle">
<solid android:color="#6dd988"/>
</shape>
</item>
</layer-list>
toggle_state_off being the same as on but with different colors.
I'm not able to find how to put an icon at the center of the button I have created, can that be achieved? And how?
You may need to modify the below as required however this will have a icon the center for both state on and off. Add the line toggle_state_off and toggle_state_on android:drawable="#drawable/ic_attach_money_black_24dp"
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<!-- Draw a 5dp width border around shape -->
<stroke
android:color="#2c125d"
android:width="5dp"
/>
</shape>
</item>
<!-- Overlap the left, top and right border using background color -->
<item
android:bottom="5dp"
android:drawable="#drawable/ic_attach_money_black_24dp">
<shape android:shape="rectangle">
<solid android:color="#2c125d"/>
</shape>
</item>
</layer-list>
Mark it as answered if this helps
Related
I want to have a bottom line in a view. The following drawable somehow adds a bottom border:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape
android:shape="rectangle">
<!-- Set the border color of your layout here -->
<solid android:color="#color/red" />
</shape>
</item>
<!-- This is for border bottom but
you can change this according to your need -->
<item android:bottom="2dp" >
<shape
android:shape="rectangle">
<!-- Set the background color of your layout here -->
<solid android:color="#color/green" />
</shape>
</item>
</layer-list>
The result of this is:
Problem:
1) I don't understand how this works at all. It seems this is some trick using margins to get a red bottom border but I don't really get it.
2) I need to be able to add a bottom border but I don't want to set any specific background color for the whole view. Is that possible?
This is telling the system from where to start this item layout.
Since here we have bottom 2dp so this layout start 2dp from bottom.
Change bottom to end,start or other options for more understanding.
For 2) I need to be able to add a bottom border but I don't want to set any specific background color for the whole view. Is that possible?
replace your drawable with below code:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:left="-2dp"
android:right="-2dp"
android:top="-2dp">
<shape android:shape="rectangle">
<solid android:color="#ffffff" />
<!--Uncomment this if you wnat to set any background color to
your rectangle
<solid android:color="#ffffff" />-->
<stroke
android:width="1dp"
android:color="#color/red" />
</shape>
</item>
How to customize the underline of EditText as shown below?
You can do something like this
<EditText
android:id="#+id/editRemarks"
android:layout_below="#+id/addNoteRemarks"
android:gravity="left"
android:layout_centerHorizontal="true"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#drawable/edit_text_shape"/>
and below is the drawable file "edit_text_shape"
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<!-- Draw a 2dp width border around shape -->
<stroke
android:color="#color/viewfinder_laser"
android:width="2dp"
/>
</shape>
</item>
<!-- Overlap the left, top and right border using background color -->
<item
android:bottom="2dp"
>
<shape android:shape="rectangle">
<solid android:color="#color/white"/>
</shape>
</item>
I want a shadow which must have a GRADIENT shadow as shown in image, zoom image to see actual gradient effect
2nd image is about show effect in zoom
all I can see from the picture - you should use [layer-list drawable with 9-patch shadow] for every [state drawable] of the EditText.
this is my output edittext which is not expected..I expect more accurate
This is finally I got an answer..but Im not satisfied with this answer...so any one have any better solution???
<!-- most important is order of layers -->
<!-- Bottom right side 1dp Shadow -->
<item>
<shape android:shape="rectangle" >
<solid android:color="#color/lighter_gray" />
</shape>
</item>
<!-- Bottom 2dp Shadow -->
<item
android:bottom="1px"
android:right="1px">
<shape android:shape="rectangle" >
<solid android:color="#color/light_gray" />
</shape>
</item>
<!-- Bottom 2dp Shadow -->
<item
android:bottom="3px"
android:right="3px">
<shape android:shape="rectangle" >
<solid android:color="#color/gray" />
</shape>
</item>
<!-- White Top color -->
<item
android:bottom="5px"
android:right="5px">
<shape android:shape="rectangle" >
<solid android:color="#FFFFFF" />
<stroke
android:width="1dp"
android:color="#color/selector_color" />
</shape>
</item>
</layer-list>
EditText
<EditText style="#style/match_width"
android:background="#drawable/border_with_shadow"
android:hint="Enter Order number"
android:padding="5dp"
android:paddingLeft="11dp"
android:singleLine="true"
android:textColor="#000000"
android:textSize="16sp" />
I want to make drawable like below,
I am able to make the Rectangle and Triange shape in two different xml files. But i want to joint them to make the drawable like this.
Use layer-list to make this custom shape drawable.
/res/drawable/custom_shape.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Transparent Rectangle -->
<item>
<shape android:shape="rectangle">
<size
android:width="300dp"
android:height="60dp" />
<solid android:color="#android:color/transparent" />
</shape>
</item>
<!-- Colored Rectangle -->
<item
android:bottom="20dp">
<shape android:shape="rectangle">
<size
android:width="300dp"
android:height="60dp" />
<solid android:color="#9A8585" />
</shape>
</item>
<!-- Bottom Triangle -->
<item
android:left="80dp"
android:right="120dp"
android:top="0dp"
android:bottom="30dp">
<rotate android:fromDegrees="45">
<shape android:shape="rectangle">
<solid android:color="#9A8585" />
</shape>
</rotate>
</item>
<!-- Top Border -->
<item
android:bottom="75dp">
<shape android:shape="rectangle">
<solid android:color="#EFC1B3" />
</shape>
</item>
</layer-list>
USE:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/custom_shape"/>
OUTPUT:
Hope this will help~
Use <layer-list/> for that.
Here is an example for instance.
in Drawable folder of resin project directory,
make xml file and name it as layerlist.xml and paste it below code as your requirement.
you can also add your shape drawable instead of drawable in the <item/> tag in the example. and use this xml as a background of ImageView.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<bitmap android:src="#drawable/android_red"
android:gravity="center" />
</item>
<item android:top="10dp" android:left="10dp">
<bitmap android:src="#drawable/android_green"
android:gravity="center" />
</item>
<item android:top="20dp" android:left="20dp">
<bitmap android:src="#drawable/android_blue"
android:gravity="center" />
</item>
</layer-list>
Here is the result of the use of <layer-list/>.
I hope it helps you...
I expect that you need it for a a Tabular navigator or a ViewPager. My suggestion is that
1)Use the rectangle as your background for all the cases.
2)Create the same triangle drawable but with the background color (White or transparent) for the unselected item.
3)Create a view with the triangle background for all the items
4)Manually setVisibility of the triangle view according to the selection state
The thumb for my Switch-button seems to get skewed(for on&off state). There were similar problems on github, but those were for people making libraries to support Switch button in API 4.0-
main contains the switch-button, and there is a thumb and track drawable applied to it
This is what is happening:
This is how its suppose to loook:
switch_track_on.png
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Switch
android:id="#+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="112dp"
android:thumb="#drawable/switch_thumb_selector"
android:track="#drawable/switch_track_selector"
android:textOn=""
android:textOff=""/>
</RelativeLayout>
switch_thumb_selector.xml
<selector>
<item android:drawable="#drawable/switch_thumb">
</item>
</selector>
switch_track_selector.xml
<selector>
<item android:drawable="#drawable/switch_track_on" android:state_checked="true"/>
<item android:drawable="#drawable/switch_track_off" android:state_checked="false"/>
</selector>
For Custom Switch (Like IOS switch) I tried below Steps:
Create a drawable track_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true">
<shape android:dither="true" android:shape="rectangle" android:useLevel="false" android:visible="true">
<solid android:color="#ca120f" />
<corners android:radius="25dp" />
<size android:width="2dp" android:height="18dp" />
</shape>
</item>
<item android:state_checked="false">
<shape android:dither="true" android:shape="rectangle" android:useLevel="false" android:visible="true">
<solid android:color="#27170432" />
<corners android:radius="25dp" />
<size android:width="2dp" android:height="18dp" />
</shape>
</item>
</selector>
Create a drawable thumb_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false">
<shape android:dither="true" android:shape="rectangle" android:useLevel="false" android:visible="true">
<solid android:color="#ffffff" />
<corners android:radius="100dp" />
<size android:width="24dp" android:height="25dp" />
<stroke android:width="4dp" android:color="#0000ffff" />
</shape>
</item>
</selector>
In your layout :
<Switch
android:id="#+id/checkboxAttendanceSelector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:thumb="#drawable/thumb_selector"
app:track="#drawable/track_selector" />
Its working fine for me.
I had the same requirement. I checked Android code and found that
switch ignores any vertical margin/padding applied to a thumb shape drawable (thus thumb always touch the top and bottom of the track)
the width of the thumb is calculated by taking the horizontal paddings + the max width of the on and off texts.
This makes really hard to make a circle thumb.
But if you specify the thumb drawable as a layer drawable for the sole reason to be able to specify padding for the single layer of the drawable, you can get the desired effect.
Thumb selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<!--
NOTE
We want a thumb with padding around it inside the track.
Sadly, a switch draws its track and thumb with the same height ignoring
any padding of the drawable, so using a shape with padding does not work.
To overcome, we apply a trick. We create layer list because the
LayerListDrawable draws itself with taking the top, left, right, bottom
values into account.
-->
<layer-list>
<item
android:top="#dimen/switch_thumb_padding"
android:left="#dimen/switch_thumb_padding"
android:right="#dimen/switch_thumb_padding"
android:bottom="#dimen/switch_thumb_padding">
<!--
NOTE
No need to specify size because:
- The thumb fills the track in height.
- The thumb width is determined from thumb max(on, off) text +
text padding + drawable padding.
-->
<shape android:shape="oval">
<solid android:color="#color/switch_thumb"/>
<!-- NOTE did not work, had to set Switch's thumbTextPadding to the radius -->
<!--
<padding android:right="#dimen/switch_thumb_radius"
android:left="#dimen/switch_thumb_radius"/>
-->
</shape>
</item>
</layer-list>
</item>
</selector>
I set the on and off text of switch to empty (actually to "" to prevent warning about empty resource).
track:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false">
<shape android:shape="rectangle">
<size android:height="#dimen/switch_track_height"/>
<corners android:radius="#dimen/switch_thumb_radius"/>
<solid android:color="#color/switch_track_off"/>
</shape>
</item>
<item android:state_checked="true">
<shape android:shape="rectangle">
<size android:height="#dimen/switch_track_height"/>
<corners android:radius="#dimen/switch_thumb_radius"/>
<solid android:color="#color/switch_track_on"/>
</shape>
</item>
</selector>
Switch style:
<style name="CustomSwitch">
<!-- NOTE this way the switch will be as width as required minimally -->
<item name="android:switchMinWidth">0dp</item>
<item name="android:track">#drawable/switch_track</item>
<item name="android:thumb">#drawable/switch_thumb</item>
<item name="android:textOff">#string/switch_thumb_off</item>
<item name="android:textOn">#string/switch_thumb_on</item>
<!-- NOTE if set to 0dp, the thumb was not visible even with padding
of the thumb drawable set to -->
<item name="android:thumbTextPadding">#dimen/switch_thumb_radius</item>-->
<!--<item name="android:thumbTextPadding">0dp</item>-->
</style>
And finally, the dimens:
<dimen name="switch_track_height">30dp</dimen>
<dimen name="switch_thumb_radius">15dp</dimen>
<dimen name="switch_thumb_padding">2dp</dimen>
So the only 'tricky' thing is to keep height = radius * 2.
No clue why this happens but solved it by placing the Switch-button within a linearLayout.
There must be some propery(width) of the thumb that has a "match_parent" of some sort that must've been causing it.
Edit: It happens when I remove the default 'ON' and 'OFF' text. So to hide the text i changed its color to white.
How to change textcolor of switch in Android
main.java
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.toggle_button);
Switch sw = (Switch) findViewById(R.id.switch1);
//Both of these need to be used to change the text color to white
sw.setTextColor(Color.WHITE);
sw.setSwitchTextAppearance(this, Color.WHITE);
//Doing this would skew the circle
//sw.setTextOn(" ");
//sw.setTextOff(" ");
}
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp" >
<Switch
android:id="#+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:thumb="#drawable/switch_thumb_selector"
android:track="#drawable/switch_track_selector" />
</LinearLayout>
You can always horizontally stretch the graphic itself (ie, "switch_thumb.png") if you can't use the other answers.
If you use the original software used to design the graphic and re-export it after stretching, it shouldn't cause blurring of the image. You'll have to eye-ball it for your dimensions, but starting at +30% stretching should get you close.