Create gradient shape in Android - android-layout

How can I create circle shape with gradient line as a drawable resource, something like this:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadiusRatio="3"
android:shape="ring"
android:thicknessRatio="8"
android:useLevel="false">
<gradient
android:angle="270"
android:centerX="50%"
android:centerY="50%"
android:endColor="#FF4AA7D0"
android:gradientRadius="100"
android:startColor="#FFE66433"
android:type="linear"
android:useLevel="false" />
</shape>

This is a simple circle as a drawable in Android.
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#666666"/>
<size
android:width="120dp"
android:height="120dp"/>
</shape>
and you can apply a color filter from Java code (see Drawable class)

Related

Shape with an alternated bi-color border in android studio

I would like to create a shape to use as a background for an element, since the app has red and orange as main colors I would like to make a cool border, however I have only managed to find how to put a single color and don't really know how to do a sort of a design.
I would like to do something like this (I made this quickly in paint just a demonstration of my idea):
Shape with cool alternated border
Ok I worked on it a bit and came to a solution that isn't exactly what I was trying to do but it will be good enough I guess. This is the code to put in the xml file in the drawable folder:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="rectangle"
android:padding="10dp">
<solid android:color="#FFFFFF"/>
<corners
android:bottomRightRadius="20dp"
android:bottomLeftRadius="20dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp"/>
<stroke
android:width="10dp"
android:color="#color/colorAccent"/>
</shape>
</item>
<item>
<shape
android:shape="rectangle"
android:padding="10dp">
<corners
android:bottomRightRadius="20dp"
android:bottomLeftRadius="20dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp"/>
<stroke
android:width="8dp"
android:color="#color/colorPrimary"
android:dashGap="10dp"
android:dashWidth="8dp" />
</shape>
</item>
</layer-list>
this code will produce this result:
shape with bicolor border
Hope this helps anybody

Cannot set background color of the selected tab programmatically in ViewPager

I want to set BackgroundColor of the selected TAB by code in ViewPager.
For far what I have done is:-
tabs.setSelectedTabIndicatorColor(Color.parseColor("#92278F"));
tabs.setSelectedTabIndicatorHeight(50);
tabs.setTabTextColors(Color.parseColor("#FFFFFF"), Color.parseColor("#FFFFFF"));
But I cannot achieve it.
If the selected Color is RED then the TAB should reflect as RED in COLOR for the selected TAB and white as selected TEXT COLOR.
I can do it by XML code but I am not getting any idea how to achieve it by CODE(dynamically)
Please help me.
This is how it will look like-
Image1
Image2
Here is the code:
Make 3 drawable files for background, selected and unselected states-
tab_background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/tab_background_selected" android:state_selected="true" />
<item android:drawable="#drawable/tab_background_unselected" android:state_selected="false" android:state_focused="false" android:state_pressed="false" />
</selector>
tab_background_selected.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#87CEFA" />
</shape>
tab_background_unselected.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#3F51B5" />
</shape>
Then finally add tablayout styles in styles.xml
<style name="Base.Widget.Design.TabLayout" parent="android:Widget">
<item name="tabBackground">#drawable/tab_background</item>
<item name="tabIndicatorColor">#ff00ff</item>
<item name="tabIndicatorHeight">2dp</item>
</style>

EditText Line appearance

I am using edittext and it's showing a line where I can enter text but I have seen examples of the line having upturned edges (see below). Can't figure out how to do it.
Edit text looks like this:
_____________.
but I want it to look more like this:
|__________________|
Thanks for help.
you need to use layer-list and add custom background to your edit text.
here's a sample.
create a xml file in your drawable folder-> custombg.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Border -->
<item>
<shape>
<solid android:color="#f000"></solid>
</shape>
</item>
<!-- Body -->
<item android:left="2dip"
android:bottom="2dp"
android:right="2dp">
<shape>
<solid android:color="#ffafafaf"></solid>
</shape>
</item>
</layer-list>
in you're edit text do this.
<EditText
android:id="#+id/edit_text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/border_line_withradius"
/>

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);

How can I programmatically create a shape in Android

I have following myRec.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape>
<corners android:radius="1dp"
android:bottomRightRadius="0dp"
android:bottomLeftRadius="4dp"
android:topLeftRadius="4dp"
android:topRightRadius="0dp"/>
<stroke
android:color="#color/my_button_border"
android:width="1dp" />
<solid android:color="#android:color/transparent" />
</shape>
</item>
</selector>
.... and would like to create the same shape programmatically so that I can freely change color in runtime. How can I achieve it? Much thanks in advance
Take a look at the android.graphics.drawable.ShapeDrawable class and the Shape class subclasses, suchs as:
PathShape
RectShape
ArcShape
OvalShape
RoundRectShape
ShapeDrawable receives a shape in the constructor, and then you have a lot of function to modify the shape. Also, you can retrieve the Paint asociated to the ShapeDrawable so you can alter things like the color and so.
Also, take a look at this small example:
http://www.edumobile.org/android/android-tutorial/shape-drawing-example-in-android/
where you can see some diferent shapes, and some efects suchs as the CornerPathEffect,
Use shapedrawable, gradientdrawable with
http://developer.android.com/reference/android/graphics/CornerPathEffect.html

Resources