How can I programmatically create a shape in Android - android-layout

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

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

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"
/>

Create gradient shape in Android

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)

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"

Resources