Gradient usage in android studio - android-studio

I am the newly in android studio.
Just reading and studying in regards to the gradient usage.
Is it possible to use gradient colour for the floating button for it to fill the whole shape?
As well, is it possible to create the text view style using gradient colour?

of course create a background.xml drawable folder and write the code below
<?xml version="1.0" encoding="utf-8"?>
<shape>
<gradient android:startColor="#color/colorAccent"
android:centerColor="#color/colorAccent"
android:endColor="#color/black"
android:angle="90"/>
<corners android:radius="10dp"></corners>
</shape>

Related

Android Studio 4.1 bordered Button loses border setting background color in kotlin code

There are numerous threads for creating a bordered button in this forum.
It works for example perfect with this xml-file
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#FFEB3B" />`<!--Background Color-->`
<stroke android:width="2dip" android:color="#000000"/> `<!--Border-->`
</shape>
where i fail is the following:
i need 2 bordered buttons, one is yellow, the other one blue. Due to program logic I want to switch the colors, the yellow one gets blue, the blue one to yellow.
in the xml-file
<solid android:color="#FFEB3B" />`<!--Background Color-->`
is used.
In Kotlin I only find
button.setBackgroundColor(Color.YELLOW)
But I find nor button.setColor oder button.setdologColor or anything else.
Using setBackgroundColor, the color changes, but the border disappears. Which statement would keep the border?
I solved it with
punktelinks.setBackgroundResource(R.drawable.shapecolory)
in code and with resource
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#FFFF00" />`<!--Background Color-->`
<stroke android:width="2dip" android:color="#000000"/> `<!--Border-->`
</shape>
This works (for me)

default circle ProgressBar thickness etc

I have the default circle ProgressBar:
https://i.stack.imgur.com/DlYL4.png
I would like to change the thickness, color and the corner radius (in an XML) Already, found some examples, but they don't work with the default circle.
Thank you very much!
Create an xml file in drawable folder.
Drawable XML file
progress_bar_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:pivotX="50%"
android:pivotY="50%"
android:fromDegrees="0"
android:toDegrees="360">
<shape
android:shape="ring"
android:thickness="2dp"
android:useLevel="false">
<gradient
android:type="sweep"
android:useLevel="false"
android:startColor="#ff00ff"
android:endColor="#00ffffff"
android:angle="0"/>
</shape>
</rotate>
Start color and end Color of gradient you should change according to your need.
To change the thickness of progress bar you change the thickness using android:thickness attribute of shape.
And add code to your layout xml file.
<ProgressBar
android:id="#+id/progress_bar"
android:layout_width="30dp"
android:layout_height="30dp"
android:indeterminateDrawable="#drawable/progress_bar_bg" />
android:layout_width and android:layout_height should be change according to your need.
I hope its Work for you.

Reusing a border resource with multiple background colors

I currently have the following xml file in my /res/drawable folder:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid
android:color="#color/transparent_white" />
<stroke
android:width="1dip"
android:color="#color/light_gray" />
</shape>
Sometimes I would like the background to be white, gray, blue etc. depending on the item I am trying to border. Is there a way to do this without creating n number of xml files where the only difference is the color of the solid attribute?
you could do this by dynamically declaring the shape and changing the color at runtime.
ShapeDrawable shapeDrawable= new ShapeDrawable();
shapeDrawable.setShape(new RectShape());
shapeDrawable.getPaint().setColor(<your color>);
((TextView) row.findViewById(<your viewid>)).setBackgroundDrawable(shapeDrawable);

Layout like Cards in android

I want to make an android layout like Google Cards,i know there is an Open Source Libraries, however i want only the Layout and the gray hex background style. I could not find information about this, how i can make this possible? im Attaching new Google maps v7 Layout so you can get the idea.
bg_card.xml
<?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="#CCC" />
</shape>
</item>
<item android:bottom="2dp">
<shape android:shape="rectangle">
<solid android:color="#FFF" />
</shape>
</item>
</layer-list>
Or just set the background of the card to #FFF and add at the bottom:
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#CCC" />
NOTE:
For everyone who doesn't care about using a library, you should use
a CardView from the Android support library.
Sample code.
<item name="android:windowBackground">#ffe3e3e3</item>
In your style.xml should make you happy ;-)
And for the layout, copying the card background should be enough isn't it?
We can use the LayerList as above to achieve this (OR) by using the nine patch image get same effect, we need not create nine patch image separately for this puprpose android drawable have one.
if you are using eclipse: android:background="#drawable/abc_menu_dropdown_panel_holo_light"
copy paste the above line as your View backround.
or if you are using android studio copy paste the following line:
android:background="#android:drawable/dialog_holo_light_frame"

Dynamically setting Styles for View : Android

I want to change the look (Style) of various views dynamically in an
Activity. But on going through various blogs, I came to know that
android does not support dynamically setting styles for Views
programmatic / dynamic., though I am not sure.
1) Is there any other ways of implementing apart from making use THEMES or making use layout xml files having styles pre set ?
2) Is it possible to set the different styles for various views (say green colour text for TextView1 and red color text for TextView2) by making use of setTheme() for entire Activity?
Thanks
You can change the text color in your java file like this.
textview1.setTextColor(Color.GREEN);
textview2.setTextColor(Color.RED);
Use this xml file in as resource and add it in drawable folder.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:angle="90"
android:centerColor="#f77b2d"
android:endColor="#f99f66"
android:startColor="#f66a11" />
<stroke
android:width="0.5dp"
android:color="#313437" />
<corners
android:bottomLeftRadius="4dp"
android:bottomRightRadius="4dp"
android:topLeftRadius="4dp"
android:topRightRadius="4dp" />
</shape>

Resources