How to decrease the size of thumb in switch java/kotlin? - android-layout

Hye there, I have created 4 SVG files 2 of them represent the thumb and 2 others represent the track as ON/OFF, and when I implemented them on the Switch button, I had no problem with the track but the thumb cirlce is forcing the track to take it size, for example if the size of the thumb = 20 then the track high should = 20 as well, and that make 2 problems, one, it distruct the shape of the svg, two, it is not showing as I want it to be, I want the thumb to be smaller and to fit inside the track like that:the shape I want to show
and here is my result:
this is what it show
this is the switch code:
<Switch
android:id="#+id/nightLight"
android:layout_centerHorizontal="true"
android:track="#drawable/night_light_track"
android:thumb="#drawable/night_light_thumb"
android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
this is the drawables:
night_light_thumb:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false">
<shape android:shape="oval">
<solid android:color="#color/C"/>
<size android:height="32dp" android:width="32dp"/>
</shape>
</item>
<item android:state_checked="true">
<shape android:shape="oval">
<solid android:color="#4E505F"/>
<size android:height="32dp" android:width="32dp"/>
</shape>
</item>
</selector>
night_light_track:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false"
android:drawable="#drawable/light_track"/>
<item android:state_checked="true"
android:drawable="#drawable/night_track"/>
</selector>
night_track:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="72dp"
android:height="32dp"
android:viewportWidth="72"
android:viewportHeight="32">
<group>
<clip-path
android:pathData="M56,0H16C7.2,0 0,7.2 0,16l0,0c0,8.8 7.2,16 16,16h40c8.8,0 16,-7.2 16,-16l0,0C72,7.2 64.8,0 56,0z"/>
<path
android:pathData="M56,0H16C7.2,0 0,7.2 0,16l0,0c0,8.8 7.2,16 16,16h40c8.8,0 16,-7.2 16,-16l0,0C72,7.2 64.8,0 56,0z"
android:fillColor="#2E2F38"/>
</group>
<group>
<clip-path
android:pathData="M56,0H16C7.2,0 0,7.2 0,16l0,0c0,8.8 7.2,16 16,16h40c8.8,0 16,-7.2 16,-16l0,0C72,7.2 64.8,0 56,0z"/>
<path
android:pathData="M16.5,10.2c-1.8,1.3 -3,3.4 -3,5.8c0,3.9 3.1,7 7,7c2.4,0 4.5,-1.2 5.8,-3c-0.3,0 -0.5,0 -0.8,0c-5,0 -9,-4 -9,-9C16.5,10.7 16.5,10.5 16.5,10.2zM11.5,16c0,-4 2.6,-7.3 6.1,-8.5l1.3,1.3c-0.2,0.7 -0.4,1.5 -0.4,2.3c0,3.9 3.1,7 7,7c0.8,0 1.5,-0.1 2.3,-0.4l1.3,1.3c-1.2,3.5 -4.6,6.1 -8.5,6.1C15.5,25 11.5,21 11.5,16z"
android:strokeAlpha="0.7"
android:fillColor="#4B5EFC"
android:fillType="evenOdd"
android:fillAlpha="0.7"/>
</group>
</vector>
light_track:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="72dp"
android:height="32dp"
android:viewportWidth="72"
android:viewportHeight="32">
<group>
<clip-path
android:pathData="M56,0H16C7.2,0 0,7.2 0,16l0,0c0,8.8 7.2,16 16,16h40c8.8,0 16,-7.2 16,-16l0,0C72,7.2 64.8,0 56,0z"/>
<path
android:pathData="M56,0H16C7.2,0 0,7.2 0,16l0,0c0,8.8 7.2,16 16,16h40c8.8,0 16,-7.2 16,-16l0,0C72,7.2 64.8,0 56,0z"
android:fillColor="#FFFFFF"/>
</group>
<group>
<clip-path
android:pathData="M56,0H16C7.2,0 0,7.2 0,16l0,0c0,8.8 7.2,16 16,16h40c8.8,0 16,-7.2 16,-16l0,0C72,7.2 64.8,0 56,0z"/>
<path
android:pathData="M51,6v1v2v1h2V9V7V6H51zM62,15h-1h-2h-1v2h1h2h1V15zM45,15h1v2h-1h-2h-1v-2h1H45zM51,22v1v2v1h2v-1v-2v-1H51zM46.3,23.1l-0.7,0.7l-1.4,-1.4l0.7,-0.7l1.4,-1.4l0.7,-0.7l1.4,1.4l-0.7,0.7L46.3,23.1zM56.9,12.5l0.7,-0.7l1.4,-1.4l0.7,-0.7l-1.4,-1.4l-0.7,0.7l-1.4,1.4l-0.7,0.7L56.9,12.5zM44.9,10.3l-0.7,-0.7l1.4,-1.4l0.7,0.7l1.4,1.4l0.7,0.7l-1.4,1.4l-0.7,-0.7L44.9,10.3zM55.5,20.9l0.7,0.7l1.4,1.4l0.7,0.7l1.4,-1.4l-0.7,-0.7l-1.4,-1.4l-0.7,-0.7L55.5,20.9zM54,16c0,-1.1 -0.9,-2 -2,-2s-2,0.9 -2,2s0.9,2 2,2S54,17.1 54,16zM52,12c2.2,0 4,1.8 4,4s-1.8,4 -4,4s-4,-1.8 -4,-4S49.8,12 52,12z"
android:strokeAlpha="0.7"
android:fillColor="#4B5EFC"
android:fillType="evenOdd"
android:fillAlpha="0.7"/>
</group>
</vector>
What I did try is to change the size of the thumb same as the size of the track so it don't distruct the shape, so we don't stay having 2 problems, but still couldn't change the size of the thumb inside the track.

The switch size adjust as you have stated. To get around this, make a thumb that has two concentric circles inside a Layer List. The outer circle will be the size you want the track to be with a transparent color. The inner circle will be the same as the outer circle but will change color of the inner circle if that's what you want. This way, the thumb will appear to be inset inside the track but in actuality it will just have a transparent annular ring.
night_light_thumb.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false">
<layer-list>
<item>
<shape android:shape="oval">
<solid android:color="#android:color/transparent" />
<size android:width="32dp" android:height="32dp" />
</shape>
</item>
<item android:bottom="4dp" android:left="4dp" android:right="4dp" android:top="4dp">
<shape android:shape="oval">
<solid android:color="#color/C" />
<size android:width="24dp" android:height="24dp" />
</shape>
</item>
</layer-list>
</item>
<item android:state_checked="true">
<layer-list>
<item>
<shape android:shape="oval">
<solid android:color="#android:color/transparent" />
<size android:width="32dp" android:height="32dp" />
</shape>
</item>
<item android:bottom="4dp" android:left="4dp" android:right="4dp" android:top="4dp">
<shape android:shape="oval">
<solid android:color="#4E505F" />
<size android:width="24dp" android:height="24dp" />
</shape>
</item>
</layer-list>
</item>
</selector>
There are probably other ways to do this, but the idea is to build a transparent area around the visible thumb.
Update
Here is another way to do it using inset of the item attribute of layer list. This way involve less drawing and is shorter.
<selector>
<item android:state_checked="false">
<layer-list>
<item android:bottom="4dp" android:left="4dp" android:right="4dp" android:top="4dp">
<shape android:shape="oval">
<solid android:color="#android:color/holo_red_light" />
<size android:width="34dp" android:height="34dp" />
</shape>
</item>
</layer-list>
</item>
<item android:state_checked="true">
<layer-list>
<item android:bottom="4dp" android:left="4dp" android:right="4dp" android:top="4dp">
<shape android:shape="oval">
<solid android:color="#4E505F" />
<size android:width="34dp" android:height="34dp" />
</shape>
</item>
</layer-list>
</item>
</selector>

Related

How to change textView shape and color in Android Studio

I have looked everywhere to change the textView shape AND color and the only solution I can find is to make a drawable resource file. Which I did:
rounded_blue.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#00BCD4" />
<stroke
android:width="3dp"
android:color="#ffffff" />
<corners android:radius="30dp" />
<padding
android:bottom="0dp"
android:left="0dp"
android:right="0dp"
android:top="0dp" />
</shape>
But when I apply it to my textView I get the shape but not the color. The color goes to default.
<Button
android:id="#+id/loginButton"
android:layout_width="249dp"
android:layout_height="45dp"
android:text="Login"
android:background="#drawable/rounded_blue"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#+id/button_standard"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="#+id/button_standard"
app:layout_constraintTop_toBottomOf="#+id/edittext_password"
app:layout_constraintVertical_bias="0.086" />```
[This is what I get ][1]
[1]: https://i.stack.imgur.com/f5P8Y.png
The edges are rounded as I wanted, but I want to change the button color. Why won't the color translate over as well?T

XLSX XML cell formatting works in LibreOffice but not MS Excel

I modified data tables xlsx export to generate tables with my custom styles. Primarily for the background colors. Mine is a mess, but it works. It generates the xlsx file and in LibreOffice it looks exactly like it should. But in Excel, the cells with Style #3 (FFAAAA) are not filled with solid yellow background but with a dotted gray background.
The ones with red or white background just work fine everywhere.
The whole xml was reverse engineered from other exports.
Any idea what Excel expects to be different?
<?xml version="1.0" encoding="UTF-8"?>
<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x14ac" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac">
<numFmts count="7">
<numFmt numFmtId="0" formatCode=""/>
<numFmt numFmtId="1" formatCode="#,##0.00_-\ [$$-45C]"/>
<numFmt numFmtId="2" formatCode=""£"#,##0.00"/>
<numFmt numFmtId="3" formatCode="[$€-2]\ #,##0.00"/>
<numFmt numFmtId="4" formatCode="0.0%"/>
<numFmt numFmtId="5" formatCode="#,##0;(#,##0)"/>
<numFmt numFmtId="6" formatCode="#,##0.00;(#,##0.00)"/>
</numFmts>
<fonts count="2" x14ac:knownFonts="1">
<font>
<sz val="11" />
<name val="undefined" />
<color rgb="FF000000" />
</font>
<font>
<sz val="11" />
<name val="Calibri" />
<color rgb="FF000000" />
<b />
</font>
</fonts>
<fills count="4">
<fill>
<patternFill patternType="none" />
</fill>
<fill>
<patternFill patternType="solid">
<fgColor rgb="FFffeeaa" />
<bgColor indexed="64" />
</patternFill>
</fill>
<fill>
<patternFill patternType="solid">
<fgColor rgb="FFffaaaa" />
<bgColor indexed="65" />
</patternFill>
</fill>
<fill>
<patternFill patternType="solid">
<fgColor rgb="FFffffff" />
<bgColor indexed="66" />
</patternFill>
</fill>
</fills>
<borders count="2">
<border> <left /> <right /> <top /> <bottom /> <diagonal /> </border>
<border diagonalUp="false" diagonalDown="false"> <left style="thin"> <color auto="1" /> </left> <right style="thin"> <color auto="1" /> </right> <top style="thin"> <color auto="1" /> </top> <bottom style="thin"> <color auto="1" /> </bottom> <diagonal /> </border>
</borders>
<cellStyleXfs count="1">
<xf numFmtId="0" fontId="0" fillId="0" borderId="0" />
</cellStyleXfs>
<cellXfs count="5">
<xf numFmtId="0" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>
<xf numFmtId="0" fontId="1" fillId="0" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>
<xf numFmtId="0" fontId="1" fillId="1" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>
<xf numFmtId="0" fontId="1" fillId="2" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>
<xf numFmtId="0" fontId="1" fillId="3" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>
</cellXfs>
<cellStyles count="1">
<cellStyle name="Normal" xfId="0" builtinId="0" />
</cellStyles>
<dxfs count="0" />
<tableStyles count="0" defaultTableStyle="TableStyleMedium9" defaultPivotStyle="PivotStyleMedium4" />
</styleSheet>
It seems excel always overwrites the second with patternType="gray125".
I just keep
<fill>
<patternFill patternType="gray125">
<fgColor rgb="FFffffff" />
<bgColor rgb="FFffffff" />
</patternFill>
</fill>
as second , regardless if i actually use it in any style and add the i need after this. Now it works in Libre Office Calc and MS Excel.
I hope that helps others aswell.

how to highlight selected item in recyclerview?

I am new to Material Card view and I want to change the card Foreground color programmatically when it is long pressed.
But the parameter to the setCardForegroundColor() method requires an instance of ColorStateList to be passed and I dont know how to create ColorStateList instance for my needs.
So can anyone please tell me how to use the ColorStateList to highlight cardview when it is long pressed ?
Instead of using the ColorStateList, to change colors of my RecyclerView items when they are pressed I use a drawable item in the background of the list item. Here's how I implement it:
List item example:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/list_item_selector"
android:padding="8dp">
<!-- Above: android:background defines the list item Touch Selector to highlight row -->
<TextView
android:id="#+id/rating_name_textview"
style="#style/EditorFieldStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textColor="#color/colorStatListItemText"
android:textSize="18sp" />
<TextView
android:id="#+id/rating_score_textview"
style="#style/EditorFieldStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="end"
android:textColor="#color/colorStatListItemText"
android:textSize="18sp"
tools:text="100" />
</LinearLayout>
List item selector example:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Adding Touch Selectors for when items in the RecyclerView are touched -->
<!-- Three states: pressed, activated, and selected -->
<item android:drawable="#color/colorPrimaryLight" android:state_pressed="true" />
<item android:drawable="#color/colorPrimaryLight" android:state_activated="true" />
<item android:drawable="#color/colorPrimaryLight" android:state_selected="true" />
<!-- Define background color for Touch Selectors when item is not selected (light gray) -->
<item android:drawable="#color/colorBackgroundLight" />
</selector>
The list item references the list item selector and highlights the RecyclerView item appropriately, based on the colors I've defined in the color value file.

Increase text size in android custom keyboard's key

I am making a custom keyboard in android. I created it successfully.
<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyHeight="7%p"
android:keyWidth="33.33%p">
<Row
android:keyHeight="5%p"
android:keyWidth="25%p" >
<Key
android:codes="-3"
android:keyEdgeFlags="right"
android:keyLabel="Done" />
</Row>
<Row
android:horizontalGap="100px">
<Key
android:codes="49"
android:keyEdgeFlags="left"
android:keyLabel="1" />
<Key
android:codes="50"
android:keyLabel="2" />
<Key
android:codes="51"
android:keyEdgeFlags="right"
android:keyLabel="3" />
</Row>
<Row>
<Key
android:codes="52"
android:keyEdgeFlags="left"
android:keyLabel="4" />
<Key
android:codes="53"
android:keyLabel="5" />
<Key
android:codes="54"
android:keyEdgeFlags="right"
android:keyLabel="6" />
</Row>
<Row>
<Key
android:codes="55"
android:keyEdgeFlags="left"
android:keyLabel="7" />
<Key
android:codes="56"
android:keyLabel="8" />
<Key
android:codes="57"
android:keyEdgeFlags="right"
android:keyLabel="9" />
</Row>
<Row>
<Key
android:codes="46"
android:keyEdgeFlags="left"
android:keyLabel="." />
<Key
android:codes="48"
android:keyLabel="0" />
<Key
android:codes="-5"
android:isRepeatable="true"
android:keyEdgeFlags="right"
android:keyIcon="#drawable/sym_keyboard_delete" />
</Row>
I want to change the text size and give a trailing space (space to the right) of key.
How can I do this?
I want to create keyboard exactly like this image.
For the textsize using - android:keyTextSize works
http://developer.android.com/reference/android/inputmethodservice/KeyboardView.html#attr_android:keyTextSize
Not sure what you mean by space to the right of they key - if you can clarify, maybe I can help.
use labelTextSize to change the label size. e.g.
<android.inputmethodservice.KeyboardView
.......
android:labelTextSize="15sp"/>
You can use
android:horizontalGap="0px"
android:verticalGap="0px"
for horizontal or vertical gap between keys.
Also for spacing between keys use some less value for android:keyWidth as right now you are using 33.33% that is for three will be something equivalent to 100%.
for changing Text size of your keylabel this link will help you.

how to fill color in sld with the feature's attribute

for example:
here is a grass feature like this:
id,area, level
1,geometry, 24
2,geometry, 56
...
i wount to write a sld file that display color according to the attribute level, like this :
<?xml version="1.0" encoding="UTF-8"?>
<StyledLayerDescriptor version="1.0.0" xmlns="http://www.opengis.net/sld" xmlns:ogc="http://www.opengis.net/ogc"
xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.opengis.net/sld http://schemas.opengis.net/sld/1.0.0/StyledLayerDescriptor.xsd">
<NamedLayer>
<Name>grass</Name>
<UserStyle>
<Name>grass</Name>
<Title>grass polygon</Title>
<Abstract>grass fill with level</Abstract>
<FeatureTypeStyle>
<Rule>
<PolygonSymbolizer>
<Fill>
<CssParameter name="fill">rgb(**level**, 0, 255)</CssParameter>
</Fill>
<Stroke />
</PolygonSymbolizer>
</Rule>
</FeatureTypeStyle>
</UserStyle>
</NamedLayer>
</StyledLayerDescriptor>
Have a look at the categorize and interpolate functions:
http://docs.geoserver.org/latest/en/user/styling/sld-tipstricks/transformation-func.html

Resources