Custom switch in Android with text in track - java

I have a custom switch. I want the text (on/off) to be in the track and not in the thumb.
I was thinking of setting a selector with text in drawable and setting it as track of the switch. But I cannot set text in a shape. Any ideas how I can do this?
I also want to add some padding to the track, I dont want the thumb to touch the track.
This is kind of how I want it to look:
This is the shape of my track:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:visible="true"
android:useLevel="false">
<corners
android:radius="20dp" />
<size
android:width="48dp"
android:height="21dp" />
<stroke
android:color="#bfbfbf"
android:width="6dp"/>

Switch off screenshot
Switch on screenshot
Switch on image
Switch off image
Thumb image
switch_on_off.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/on_switch"/>
<item android:state_checked="false" android:drawable="#drawable/off_switch"/>
<item android:drawable="#drawable/off_switch"/>
</selector>
xml code
<android.support.v7.widget.SwitchCompat
android:id="#+id/switch_compat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
app:track="#drawable/switch_on_off"
android:thumb="#drawable/on_switch_active"/>
Try above code and let me know if you find any error.....

Related

How to make a fade black background in Android Studio

I would like to put a fade black covering my image, just like this one, with a text, a fade background and then my image. I just want to know how to implement this fade. Thank you!
Just create a drawable file, I'll name it gradient_background (you can set any name)
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="90"
android:startColor="#000000"
android:centerColor="#00000000"
android:endColor="#00000000"/>
</shape>
then inside your target xml, create a View like this one, look that inside it, we're setting the background as your drawable file. In this case is gradient_background
<View
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#drawable/gradient_background"
app:layout_constraintBottom_toBottomOf="post_image"
app:layout_constraintEnd_toEndOf="#+id/post_image"
app:layout_constraintStart_toStartOf="post_image"
app:layout_constraintTop_toTopOf="#+id/post_image" />
So it should look like this:
:)
First, Make bottom_transparent_gradient.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient android:angle="270"
android:endColor="#64000000"
android:startColor="#android:color/transparent" />
</shape>
</item>
</selector>
Then, add that drawable as your view background in which you want to show the fade background effect.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bottom_transparent_gradient"/>
And that's it! You can see the result you wanted. :)

How do you change the size of an item in a layer-list?

Here is a layer-list:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#color/blue"/>
<item
android:drawable="#mipmap/ic_launcher_foreground"
android:gravity="center"/>
</layer-list>
How do I set the size of the second item? In some devices, it just stretches across the entire screen. I can't use android:width and android:height cause they're for API 23 minimum. I tried adding a <shape> item above it like in this answer here: https://stackoverflow.com/a/46115699/14536109 but it still stretched across the entire screen like before.
How about adding pixels to the item?
<item
android:drawable="#mipmap/ic_launcher_foreground"
android:gravity="center"
android:top="dimension"
android:right="dimension"
android:bottom="dimension"
android:left="dimension"/>
Try this maybe it will work.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:left="16dp"
android:right="0dp"
android:top="0dp"
android:bottom="16dp"
/>
</layer-list>

my application ignores any changes in background color of buttons

i'm trying to customize my alert dialog. but i can not change the background color of buttons.
i tried drawable button designing, style, setting direct color in xml and also setting color by java codes in the activity but i could not change it.
the shape of the button changes but the color only can be either the default color or transparent color.
the xml coding of button itself:
<Button
android:id="#+id/btnDenied"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="35dp"
android:layout_marginBottom="15dp"
android:background="#drawable/button_selected" //// or #drawable/button_costume
android:text="#string/button_no" />
the selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed = "true">
<shape>
<solid android:color="#FFEB3B"/>
</shape>
</item>
<item android:state_pressed = "false">
<shape>
<solid android:color="#FFC107"/>
</shape>
</item>
</selector>
and another drawable file:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="50dp"/>
<solid
android:color="#color/colorHeader"/>
</shape>
and i also tried this style:
<style name="Button_Style"
parent="#android:style/Widget.Button">
<item name="android:textSize">14sp</item>
<item name="android:background">#color/colorHeader</item>
<item name="android:textColor">#1B1B22</item>
<item name="android:textStyle">bold</item>
<item name="android:layout_margin">2dp</item>
</style>
but non of them could change the color of the button.
The code that I have written is 100 percent correct, but sometimes Android Studio does some wrong tasks, so solve these issues either by running the program again, or doing the following steps:
Clean Project , From the build menu .
Rebuild Project .
And I got this problem with me not to show errors in xml file,
I hope this thing helped you.

How to set thickness of circular indeterminate progress bar android?

I have two nested circular indeterminate progress bars but I cannot set their thickness to an equal size. I have tried to customize it using shapes. However, that removes the default behaviour of the circular indeterminate progress.
I customize like this and it made thickness equal but not getting default behaviour.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/progress">
<rotate android:toDegrees="360">
<shape
android:shape="ring"
android:thickness="4dp">
<solid android:color="#color/white" />
</shape>
</rotate>
</item>
</layer-list>
<ProgressBar
android:id="#+id/download_progress4"
android:layout_width="85dp"
android:layout_height="85dp"
android:layout_gravity="center"
android:indeterminate="true"
android:indeterminateDrawable="#style/ProgressThem" />
I want like this with default behaviour of circular indeterminate progress bar
Try this
USE
android:innerRadiusRatio="3"
android:thicknessRatio="20.0
SAMPLE CODE
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/progress">
<rotate android:toDegrees="360">
<shape
android:shape="ring"
android:thickness="4dp"
android:innerRadiusRatio="3"
android:thicknessRatio="20.0">
<solid android:color="#color/white" />
</shape>
</rotate>
</item>
</layer-list>
If you want the modern animation rather than just rotation, you can copy and alter https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/drawable/progress_indeterminate_anim_medium_material.xml using it as the indeterminateDrawable.
<ProgressBar
android:indeterminateDrawable="#drawable/new_progress_indeterminate_anim_medium_material"
To change the width in vector_drawable_progress_bar_medium.xml android:strokeWidth="1" for example.
It's a bit painful to do this as you also need to copy over the animation, interpolator and animator xml files to your project as they are private in the Android SDK.
There is actually an even easier way to do that with the material components:
<com.google.android.material.progressindicator.CircularProgressIndicator
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true"
app:trackThickness="3dp" />
More details in material docs -> https://material.io/components/progress-indicators/android#circular-progress-indicators

Android Button vs TextView - Hyperlink like behavior

On an Android View, I want to have a some clickable text that works similar to a webpage hyperlink.
It looks just like normal text (no button border), but when touched, I want the text color to change and I may also want its background to turn a reverse color.
Is it better to use a Button with a transparent background or a TextView. When should I choose one over the other?
Thanks much
you can use Button and make selector for background and text both.
textselector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#0000ff"/> <!-- pressed -->
<item android:state_focused="true" android:color="#android:color/white"/> <!-- focused -->
<item android:color="#android:color/white"/> <!-- default -->
</selector>
buttonselector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#android:color/white"/> <!-- pressed -->
<item android:state_focused="true" android:color="#0000ff"/> <!-- focused -->
<item android:color="#0000ff"/> <!-- default -->
</selector>
In your layout xml
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttonselector"
android:text="some text"
android:textColor="#drawable/textselector" />
hotveryspicy gave a great answer and really pointed me in the right direction. Thank you, but I had a few issues with the response.
What I want is a button that has Black text and a Gray background in its normal state and a Red background with White text in its selected state. Like this:
Using hotveryspicy's suggestions I had the following issues:
I get an error when trying to assign a drawable to textColor. Funny but I see other references to doing this sort of thing. Maybe the XML validation is stricter now?
I also got an error if I didn't define a android:drawable in the buttonselector.xml. It seems you can set the color of a drawable in a selector eiter, you need to actually create a drawable.
Anyhow this is what I did:
I created a Color State Resource that looks like this:
File: res/colors/link_button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#ffffff"/>
<item android:color="#000000"/>
</selector>
Then the following 3 drawables:
My button background in its normal state using a Shape Drawable:
File: res/drawable/link_button.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#cccccc"/>
</shape>
My button background in its selected state, also a Shape Drawable:
File: res/drawable/link_button_selected.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#ff0000"/>
</shape>
Then I created a Drawable Selector that chooses between the 2 shapes.
File: res/drawable/link_button_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#drawable/link_button_selected"
android:state_pressed="true" />
<item
android:drawable="#drawable/link_button" />
</selector>
Then in my layout my button looks like this:
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Button"
android:textColor="#color/link_button"
android:background="#drawable/link_button_selector"
/>
Thanks for the help everyone!
Add String like
<string name="link">'Google'</string>
In your layout
<TextView
android:id="#+id/link"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="all"
android:linksClickable="true"
android:text="#string/link" />
You can linkify your text using the linkify class.
Have a look at this and this with few examples
For Google's sake:
Rather than make a button look like a link why not make a TextView clickable? This was effectively what I wanted and worked for my use. I wanted text to be clickable to spawn an intent.
TextView.setOnClickListener(new View.OnClickListener()

Categories