Switch (Thumb and Track) Android - java

guys! I have a question :) trying to customize a switch a little bit, such as the outline of the switch will have a border, and the inside part of it, the one you click to activate the event, also with a border, but in a way that it won't overlap the outline border of the switch. Since I don't have enough rep, I can't actually post the picture, therefore I hope it is clear enough :) what I got so far is:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<size android:height="50dp"/>
<corners
android:radius="3dp"/>
<stroke
android:width="2dp"
android:color="#color/wampRed"/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#color/wampRed"/>
<corners
android:radius="2dp"/>
</shape>
<
Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/switchButton"
android:layout_marginBottom="40dp"
android:textOn=""
android:thumb="#drawable/shape_track"
android:track="#drawable/shape_thumb"
android:layout_marginRight="5dp"
android:textOff=" "
android:layout_alignParentBottom="true"
android:layout_toRightOf="#+id/dateOutput"
/>

Related

How to change width of track for horizontal scrollbar?

I have got a horizontal recyclerView which use horizontal custom scrollbars but i have an issue, thumb is over than the track, how can i specify the width of track for scrollbar?
here is my RecyclerView:
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/itemRecyclerView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:scrollbars="horizontal"
app:layoutManager=".LinearLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/skinName2"
android:scrollbarAlwaysDrawHorizontalTrack="true"
android:scrollbarThumbHorizontal="#drawable/scrollbar_horizontal_thumb"
android:scrollbarTrackHorizontal="#drawable/scrollbar_horizontal_track"
tools:itemCount="15"
tools:listitem="#layout/item_item" />
Here is my custom thumb:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:gravity="center_vertical"
android:height="1dp">
<shape
android:shape="rectangle">
<solid android:color="#FFFFFF" />
</shape>
</item>
</layer-list>```
and here is my track
```<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:gravity="center_vertical"
android:height="1dp">
<shape
android:shape="rectangle">
<solid android:color="#FFFFFF" />
</shape>
</item>
</layer-list>
issue is here
Any ideas?

LinearLayout with rounded corners and background color

CompileSdkVersion 26, if it's important. I want to create LinearLayout with rounded corners, and background color.
I created 2 LinearLayouts, one with corners, and one with color, but their shapes do not match my expectations.
Code from activity
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/whitegrey"
android:layout_marginTop="340dp"
android:id="#+id/layout"
android:layout_marginStart="10dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/custom_border"
android:orientation="vertical"
android:padding="6dp"
android:textColor="#color/black">
...
</LinearLayout>
</LinearLayout>
custom_border.xml:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="20dp"/>
<padding android:left="8dp" android:right="8dp" android:top="8dp" android:bottom="8dp"/>
<stroke android:width="2dp" android:color="#444444" />
</shape>
Actually, there is rounded border, and behind, there is rectangular layout with target color. I want to fill with target color only what is inside borders.
Is there any way to set the fill color, preferably in custom_border.xml?
Here, what I have at the moment:
Here, what I want to achieve:
View from phone, the letters on the left are cut off:
From the code samples you provide us, it doesn't seem necessary to me to have two LinearLayout. Only the layout with android:background="#drawable/custom_border" is required.
To achieve the expected result, just add the property <solid android:color="#color/whitegrey" /> to your custom_border.xml :
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="20dp"/>
<padding android:left="8dp" android:right="8dp" android:top="8dp" android:bottom="8dp"/>
<stroke android:width="2dp" android:color="#444444" />
<solid android:color="#color/whitegrey" />
</shape>
I wish I was able to help you!

How to change height of "progress line" in Progress Bar?

Can I change the height of "progress line" in ProgressBar in Android, so it's larger than the "background line" to achieve this effect?
I have tried setting size on background and progress items on my custom progressBarDrawable, but doesn't seem to work.
<ProgressBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="false"
android:max="100"
android:progress="50"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:progressDrawable="#drawable/xml_progress_bar_line"
style="#style/Widget.AppCompat.ProgressBar.Horizontal"/>
Here is the xml_progress_bar_line.xml file.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/background">
<shape>
<size
android:height="5dp"/>
<solid
android:color="#color/grey" />
</shape>
</item>
<item
android:id="#android:id/progress">
<clip>
<shape>
<size
android:height="10dp"/>
<solid
android:color="#color/green" />
<corners
android:radius="10dp"/>
</shape>
</clip>
</item>
</layer-list>
Yes you can do it with add android:top & android:bottom like this:
<?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:top=2dp android:bottom=2dp>
<shape>
<size
android:height="5dp"/>
<solid
android:color="#color/grey" />
</shape>
</item>
<item
android:id="#android:id/progress">
<clip>
<shape>
<size
android:height="10dp"/>
<solid
android:color="#color/green" />
<corners
android:radius="10dp"/>
</shape>
</clip>
</item>
Change the maxHeight and minHeight to equal each other, then set the layout_heightexplicitly.
Also try using #android:style/Widget.ProgressBar.Horizontal
You can also set vertical scale - mProgressBar.setScaleY(3f);
Here's an example I took from: https://xinyustudio.wordpress.com/2014/08/19/android-change-progressbar-height/
I ended up not using the progress bar at all!
I just used 2 lines in relative layout. Here is the corresponding code!
Here is the progress bar background line xml (xml_custom_progress_bar_bg.xml):
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners
android:radius="4dp"/>
<solid
android:color="#color/light_grey"/>
</shape>
Here is the progress bar "progress line" xml (xml_custom_progress_bar_progress.xml):
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners
android:radius="4dp"/>
<gradient
android:angle="270"
android:startColor="#color/green"
android:endColor="#color/green"/>
</shape>
And here is the progress bar container:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:layout_width="match_parent"
android:layout_height="4dp"
android:layout_centerVertical="true"
android:background="#drawable/xml_custom_progress_bar_bg"/>
<View
android:layout_width="0dp"
android:layout_height="8dp"
android:layout_centerVertical="true"
android:background="#drawable/xml_custom_progress_bar_progress"/>
</RelativeLayout>
Then, I calculate the progress manually and set the width of "progress line" programmatically.

White Border along with transparency in "LinearLayout"

I wanted to add a linear layout, having a transparent background as well as with white borders. The problem is: as far as I have googled, I can achieve only one out of both.
Here's what I did:
Saved the following as border.xml in drawable
<?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="#FFFFFF" />
</shape>
</item>
<item android:left="5dp" android:right="5dp" android:top="5dp" android:bottom="5dp" >
<shape android:shape="rectangle">
</shape>
</item>
</layer-list>
my existing page layout
<LinearLayout
android:id="#+id/quiz"
android:layout_width="150dp"
android:layout_height="120dp"
android:background="#66041414" <-------- replaced it with android:background="#drawable/border"
android:orientation="vertical"
android:layout_marginLeft="5dp" >
......
</LinearLayout>
I am getting opaque background, when the border was included.
I wanted a final result to be like:
totally stuck with it. Just wanted to find a way out to achieve it. Any suggestions would be quite helpful.
Your drawable for background of layout:
You can change radius for corner shape if you want. But stroke will create a border and solid part is the background which we are making transparent.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="2dp"
android:topRightRadius="0dp"
android:bottomRightRadius="0dp"
android:bottomLeftRadius="0dp" />
<stroke
android:width="1dp"
android:color="#android:color/white" />
<solid android:color="#android:color/transparent"/>
</shape>
and my test layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ll1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/ll2"
android:layout_height="50dp"
android:layout_width="50dp"
android:background="#drawable/my_transparent_linear_layout"></LinearLayout>
</LinearLayout>
It works, Below is the proof:
For this you can use two layout aligned one top of the other then set the background transparent for the top view and set the white border as background for the bottom view. You can do this thing inside relative layouts.
Xml Drawable for background:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="30dp" />
<stroke android:width="5dp" android:color="#ffffffff"/>
<solid android:color="#66000000"/>
</shape>
Adjust radius, width and dark color transparency ( #ff and #66 parts) as you please.
Indeed well suggestion by #Ali Imran, check below way, hope it will help.
back.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<stroke android:width="1dp" android:color="#dd7b7a"/>
<corners android:bottomRightRadius="10dp" android:bottomLeftRadius="10dp"
android:topLeftRadius="10dp" android:topRightRadius="10dp"/>
<solid android:color="#dd7b7a"/>
</shape>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
>
<LinearLayout
android:padding="4dip"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/back"
android:gravity="center_horizontal"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/tile_mode" // your transparent image
/>
</LinearLayout>
</LinearLayout>
also go through below links in that, way of using xml will works for you.
Bitmap image with rounded corners with stroke

Android - Can't create simple rectangle shape... UnsupportedOperationException?

I'm having trouble creating a simple rounded rectangle using XML. Every time I try to add the "corners" element to the custom shape I get:
java.lang.UnsupportedOperationException
at
android.graphics.Path.addRoundRect(Path.java:514)
at
android.graphics.drawable.GradientDrawable.draw(GradientDrawable.java:314)
at
android.view.View.draw(View.java:6520)
...
res/dawable/rounded_rectangle.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#ffffff"/>
<stroke android:width="3dp"
android:color="#ff000000"/>
<padding android:left="1dp"
android:top="1dp"
android:right="1dp"
android:bottom="1dp"/>
<corners android:bottomRightRadius="7dp" android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp" android:topRightRadius="7dp"/>
</shape>
simple layout.xml using the above shape:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<View android:id="#+id/View01"
android:background="#drawable/rounded_rectangle"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</View>
</RelativeLayout>
Fyi, I'm trying to compile for Android 2.1 and I have all the latest updates installed to Eclipse and the Android SDK. This shape is a direct copy of something I saw on another website, but for some reason It doesn't want to work for me.
Thanks.
So, I was just playing around with this a bit and I changed a couple of lines in the rounded_rectangle.xml to get it working. See below:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#ffffff"/>
<stroke android:width="3dp"
android:color="#ff000000"/>
<padding android:left="1dp"
android:top="1dp"
android:right="1dp"
android:bottom="1dp"/>
<corners android:radius="30dp"/>
</shape>
I only wish Google would put out a proper reference doc for creating XML-based shapes. After hours (4+) of hunting down examples on the Web, I feel like it's still a guessing game as to what elements/attributes are supported in these types of XML documents. Sorry for the mini-rant.
I hope this helps someone else.

Categories

Resources