Android & Kotlin Examples code
Android kotlin tutorials and examples code for android apps developers.
How to change the shape stroke color android programmatically
To set the shape stroke color programmatically this example uses GradientDrawable method setStroke(int width, int color) which sets the stroke width and change shape’s color. Here default orange color of the shape drawable is changes programetically usingb GradientDrawable method setStroke(int width, int color).
GradientDrawable
A GradientDrawable is Drawable with a color gradient for buttons, backgrounds, etc.
It can be defined in an XML file with the element. For more information, see GradientDrawable.
xmlns:app=»http://schemas.android.com/apk/res-auto»
xmlns:tools=»http://schemas.android.com/tools»
android:layout_width=»match_parent»
android:layout_height=»match_parent»
android:orientation=»vertical»
android:gravity=»center_horizontal»
tools:context=»quizcounter.compete.geeks.vectordrawablecolorchange.MainActivity»>
android:id=»@+id/text»
android:layout_width=»wrap_content»
android:layout_height=»wrap_content»
android:text=»TextView »
android:padding=»10dp»
android:textColor=»@android:color/black»
android:background=»@drawable/stroke_background»
/>
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.support.graphics.drawable.VectorDrawableCompat;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity <
Источник
Android change stroke color
ColorWheel is a library for Android which contains helpful views which can be used to pick an ARGB color.
From version 1.1.13 the library will be hosted on Maven Central Repository because JCenter announced its deprecation in the future. This migration lead to change of the group id which is now com.github.antonpopoff .
The minimal required Android version is 16 (Android 4.1).
In case of any problems make sure that jCenter or Maven Central repositories are specified in your build.gradle file:
For version 1.1.13 and above:
For version 1.1.12 and below:
To set/get a RGB color you can use ColorWheel.rgb property:
You can set a listener via ColorWheel.colorChangeListener property:
Important note: keep in mind that ColorWheel is a two-dimensional implementation of HSV color model. Two-dimensional in that case means that its brightness (value) component of HSV is always set to 1 . Because of that, ColorWheel can’t display every single color supported by HSV.
Therefore if you set a color which can’t be displayed it will be transformed to the closest HSV color which can be displayed by ColorWheel . In order to pick colors which can’t be displayed by ColorWheel you can additionally use GradientSeekBar .
GradientSeekBar view draws a vertical or horizontal bar filled with a linear gradient of two colors. You can use the view to pick an intermediate color in-between.
To pick an intermediate ARGB color you can use GradientSeekBar.argb property:
To set/get start/end colors you can use the following properties and methods:
If you want to change an intermediate color programmatically you can use GradientSeekBar.offset property. Its value lies within the range from 0f to 1f and reflects how close the intermediate color to startColor or endColor properties.
You can set a listener via GradientSeekBar.colorChangeListener property:
It’s impossible to pick all different colors from ColorWheel because it’s only two-dimensional and the brightness (value) component of HSV is always set to 1 .
To compensate that you can use GradientSeekBar and its setBlackToColor(color) extension. This method sets black color as startColor and supplied color as endColor . This gives you a possibility to pick additional shades of a color picked from ColorWheel .
If you want to use GradientSeekBar to pick the alpha value of a color in range from 0 to 255 you can use the following extension:
GradientSeekBar.setTransparentToColor(color: Int, respectAlpha: Boolean = true)
This method takes an ARGB color and sets the transparent version of it as startColor and an opaque version as endColor . respectAlpha parameter determines whether or not the thumb’s position will be adjusted based on the alpha value of the color you supplied.
To get the alpha value of the current color you can use GradientSeekBar.currentColorAlpha extension property.
You can use GradientSeekBar.setAlphaChangeListener extenstion to set a listener that has additional alpha parameter which lies withing the range from 0 to 255 .
You can use the following XML attributes to additionally customize ColorWheel and GradientSeekBar .
XML Attribute | Property | Description |
---|---|---|
tb_thumbRadius | thumbRadius | Sets ColorWheel ‘s thumb radius |
tb_thumbColor | thumbColor | Sets ColorWheel ‘s thumb color |
tb_thumbStrokeColor | thumbStrokeColor | Sets ColorWheel ‘s thumb stroke color |
tb_thumbColorCirlceScale | thumbColorCircleScale | Sets ColorWheel ‘s thumb color circle size which is relative to thumbRadius . This value is in range from 0 to 1 . If it set to 1 it will have the same size as thumbRadius |
For version 1.1.13 and above:
XML Attribute | Property | Description |
---|---|---|
tb_thumbRadius | thumbRadius | Sets GradientSeekBar ‘s thumb radius |
tb_thumbColor | thumbColor | Sets GradientSeekBar ‘s thumb color |
tb_thumbStrokeColor | thumbStrokeColor | Sets GradientSeekBar ‘s thumb stroke color |
tb_thumbColorCirlceScale | thumbColorCircleScale | Sets GradientSeekBar ‘s thumb color circle size which is relative to thumbRadius . This value is in range from 0 to 1 . If it set to 1 it will have the same size as thumbRadius |
gsb_barSize | barSize | Sets width(vertical)/height(horizontal) of GradientSeekBar ‘s gradient bar depending on it’s orientation |
gsb_barCornersRadius | cornerRadius | Sets GradientSeekBar ‘s gradient bar corners radius |
gsb_startColor | startColor | Sets GradientSeekBar ‘s startColor |
gsb_endColor | endColor | Sets GradientSeekBar ‘s endColor |
gsb_offset | offset | Sets GradientSeekBar ‘s offset |
gsb_orientation | orientation | Sets GradientSeekBar ‘s orientation. Possible values: vertical / horizontal |
For version 1.1.12 and below:
Источник
Android change stroke color
ColorWheel is a library for Android which contains helpful views which can be used to pick an ARGB color.
From version 1.1.13 the library will be hosted on Maven Central Repository because JCenter announced its deprecation in the future. This migration lead to change of the group id which is now com.github.antonpopoff .
The minimal required Android version is 16 (Android 4.1).
In case of any problems make sure that jCenter or Maven Central repositories are specified in your build.gradle file:
For version 1.1.13 and above:
For version 1.1.12 and below:
To set/get a RGB color you can use ColorWheel.rgb property:
You can set a listener via ColorWheel.colorChangeListener property:
Important note: keep in mind that ColorWheel is a two-dimensional implementation of HSV color model. Two-dimensional in that case means that its brightness (value) component of HSV is always set to 1 . Because of that, ColorWheel can’t display every single color supported by HSV.
Therefore if you set a color which can’t be displayed it will be transformed to the closest HSV color which can be displayed by ColorWheel . In order to pick colors which can’t be displayed by ColorWheel you can additionally use GradientSeekBar .
GradientSeekBar view draws a vertical or horizontal bar filled with a linear gradient of two colors. You can use the view to pick an intermediate color in-between.
To pick an intermediate ARGB color you can use GradientSeekBar.argb property:
To set/get start/end colors you can use the following properties and methods:
If you want to change an intermediate color programmatically you can use GradientSeekBar.offset property. Its value lies within the range from 0f to 1f and reflects how close the intermediate color to startColor or endColor properties.
You can set a listener via GradientSeekBar.colorChangeListener property:
It’s impossible to pick all different colors from ColorWheel because it’s only two-dimensional and the brightness (value) component of HSV is always set to 1 .
To compensate that you can use GradientSeekBar and its setBlackToColor(color) extension. This method sets black color as startColor and supplied color as endColor . This gives you a possibility to pick additional shades of a color picked from ColorWheel .
If you want to use GradientSeekBar to pick the alpha value of a color in range from 0 to 255 you can use the following extension:
GradientSeekBar.setTransparentToColor(color: Int, respectAlpha: Boolean = true)
This method takes an ARGB color and sets the transparent version of it as startColor and an opaque version as endColor . respectAlpha parameter determines whether or not the thumb’s position will be adjusted based on the alpha value of the color you supplied.
To get the alpha value of the current color you can use GradientSeekBar.currentColorAlpha extension property.
You can use GradientSeekBar.setAlphaChangeListener extenstion to set a listener that has additional alpha parameter which lies withing the range from 0 to 255 .
You can use the following XML attributes to additionally customize ColorWheel and GradientSeekBar .
XML Attribute | Property | Description |
---|---|---|
tb_thumbRadius | thumbRadius | Sets ColorWheel ‘s thumb radius |
tb_thumbColor | thumbColor | Sets ColorWheel ‘s thumb color |
tb_thumbStrokeColor | thumbStrokeColor | Sets ColorWheel ‘s thumb stroke color |
tb_thumbColorCirlceScale | thumbColorCircleScale | Sets ColorWheel ‘s thumb color circle size which is relative to thumbRadius . This value is in range from 0 to 1 . If it set to 1 it will have the same size as thumbRadius |
For version 1.1.13 and above:
XML Attribute | Property | Description |
---|---|---|
tb_thumbRadius | thumbRadius | Sets GradientSeekBar ‘s thumb radius |
tb_thumbColor | thumbColor | Sets GradientSeekBar ‘s thumb color |
tb_thumbStrokeColor | thumbStrokeColor | Sets GradientSeekBar ‘s thumb stroke color |
tb_thumbColorCirlceScale | thumbColorCircleScale | Sets GradientSeekBar ‘s thumb color circle size which is relative to thumbRadius . This value is in range from 0 to 1 . If it set to 1 it will have the same size as thumbRadius |
gsb_barSize | barSize | Sets width(vertical)/height(horizontal) of GradientSeekBar ‘s gradient bar depending on it’s orientation |
gsb_barCornersRadius | cornerRadius | Sets GradientSeekBar ‘s gradient bar corners radius |
gsb_startColor | startColor | Sets GradientSeekBar ‘s startColor |
gsb_endColor | endColor | Sets GradientSeekBar ‘s endColor |
gsb_offset | offset | Sets GradientSeekBar ‘s offset |
gsb_orientation | orientation | Sets GradientSeekBar ‘s orientation. Possible values: vertical / horizontal |
For version 1.1.12 and below:
Источник
Android & Kotlin Examples code
Android kotlin tutorials and examples code for android apps developers.
Android change shape solid color programmatically
Change Shape Solid Color Programmatically in Android Kotlin.
In the shape drawable xml-file you can set custom solid color with the attribute but to change the shape drawable solid color in runtime/dynamically. There is need to Change solid color of the shape drawable in android programmatically, you can edit Solid Color of a shape drawable xml-file in Android programmatically using GradientDrawable.setColor(int) method which change tint of solid color of shape drawable.
Here, TextView is set with shape drawable background. This example changes android TextView background — shape drawable solid color to grey from purple color.
android:layout_below=»@+id/button»
android:id=»@+id/textView»
android:layout_width=»wrap_content»
android:layout_height=»wrap_content»
android:text=»TextView »
android:padding=»10dp»
android:textColor=»@android:color/black»
android:background=»@drawable/rounded_border»
/>
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() <
override fun onCreate(savedInstanceState: Bundle?) <
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button.setOnClickListener <
//change the shape solid color in xml programmatically Kotlin
val gradientDrawable = (textView.getBackground() as GradientDrawable).mutate()
(gradientDrawable as GradientDrawable).setColor(Color.LTGRAY)
>
>
>
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.support.graphics.drawable.VectorDrawableCompat;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity <
Источник