I want create one shape such as below image, but I want create this with XML codes (in drawable) and I do not want create this with 9.patch images!
How can I create this shape with xml code?
Here is a work- around
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<solid android:color="#FFFFFF"/>
<corners
android:bottomRightRadius="0dp"
android:bottomLeftRadius="0dp"
android:topLeftRadius="15dp"
android:topRightRadius="15dp"/>
</shape>
Tweak the corner radius to get your upper part curved!
or overlap this oval shape to get the desired output
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<!--circle filling color-->
<solid android:color="#ffffff" />
<stroke
<!--radious can define in here-->
android:width="1dp"
android:color="#ffffff" />
<corners
android:bottomLeftRadius="2dp"
android:bottomRightRadius="2dp"
android:topLeftRadius="2dp"
android:topRightRadius="2dp" />
</shape>
But basically, this is not the correct way to do this! Use another way around, use 9 patch
Related
How can we display the colors as shown in the image where we can select more than one color ? The colored boxes must be checkboxes according to me but I am not able customize them
You need to define an xml resource in the drawable folder and use it in an imageView for instance for your question above it may look like this. Where by you use a shape called rectangle, giving it a solid colour as below and then also making the corner radius circular with 8dp. Then to display it you use an imageView and set it as a src. I have also added a stroke. Remove it if you dont need it!
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#DFDFE0" />
<size
android:height="8dp"
android:width="16dp" />
<corners
android:bottomLeftRadius="8dp"
android:bottomRightRadius="8dp"
android:topLeftRadius="8dp"
android:topRightRadius="8dp" />
<stroke
android:width="3dp"
android:color="#2E3135" />
<gradient
android:type="linear"
android:angle="0"
android:startColor="#f6ee19"
android:endColor="#115ede" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="5dp"/>
<gradient
android:startColor="#2E3135"
android:centerColor="#000000"
android:endColor="#ffffff"
android:angle="180"/>
</shape>
Change the colour as your need.
I want to put rounded corners and border to an ImageView, with custom border color. Also, I would like to achieve a glowing effect with the color of the border. Attached sample image. Note that the source image has square borders.
I suppose this is achievable only by using Canvas? Any ideas and sample code?
this is what i have done for my ImageView to make same changes as you want.
made one image_shape.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="1dp"
/>
<corners
android:radius="50dp" />
<padding
android:left="10dp"
android:right="10dp"
android:top="10dp"
android:bottom="10dp"/>
<solid android:color="#10151D"/>
</shape>
Now, put this xml as the background of your imageView then you will get effect as you want.
Right now i have set my own color. you can put your desire color and get effect as rounded glow border effect.
try using the following code in a new xml file named roundCorners in your drawable folder:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#000000" />
<stroke
android:width="1dp"
android:color="#DDDDDD" />
<corners
android:bottomLeftRadius="2dp"
android:bottomRightRadius="2dp"
android:topLeftRadius="2dp"
android:topRightRadius="2dp" />
</shape>
And then set it as a background to your ImageView in the layout file by:
android:background="#drawable/roundCorners"
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:thickness="0dp" >
<stroke android:color="#ff5febe7" android:width="2dp" />
<solid android:color="#601E3232"/>
<corners android:radius="5dp" />
</shape>
I need to create a xml shape drawable that draws a rectangle without the top-line (a "u-form"). What I am able to do is draw a rectangle, like so:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#color/detailrow_bg_normal" />
<corners
android:bottomLeftRadius="0dp"
android:bottomRightRadius="0dp"
android:radius="1dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp" />
<padding
android:bottom="2dip"
android:left="1.5dip"
android:right="1.5dip"
android:top="8dip" />
<stroke
android:width="1dip"
android:color="#color/detailtable_border" />
But how - if possible, can I define the same shape without the top (or bottom) line?
You could try using a layer-list. See if something like this would work:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle">
<solid android:color="#color/detailtable_border" />
</shape>
</item>
<item android:left="1.5dp" android:right="1.5dp" android:bottom="2dp">
<shape android:shape="rectangle">
<solid android:color="#color/detailrow_bg_normal" />
</shape>
</item>
</layer-list>
This (should) fill the rectangle with the border color, then overlay it with the default background color, leaving the appropriate amount of the right/left/bottom border color showing.
try this code and also refer this link:::
I achieved a good solution with this one:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- This is the line -->
<item android:top="-1dp" android:right="-1dp" android:left="-1dp">
<shape>
<solid android:color="#android:color/transparent" />
<stroke android:width="1dp" android:color="#ffffff" />
</shape>
</item>
</layer-list>
This works well in case you need a transparent color but still an open stroke color (In my case i only needed a bottom line). If you need a background color you can add a solid shape color as in above answer.
You might be able to achieve this using two shapes and a LayerList, but I think it is both a better solution and easier to use a NinePatch-drawable.
In My android xml layout i am applying the border by using the borderframe.xml as a background.
borderframe.xml file is looks like below:
<?xml version="1.0" encoding="UTF-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="1dip"
android:color="#ffffff"/>
<solid
android:color="#95865F"/>
<corners
android:radius="10px"/>
<padding
android:left="1dp"
android:right="1dp"
android:top="1dp"
android:bottom="1dp"/>
</shape>
Now, While there is a android:radius="10px" then it is works but while i am going to give round shape to specific corner only then it not works.
There is no any erro message in log cat but i found error in eclipse like:
The graphics preview in the layout editor may not be accurate:
* Different corner sizes are not supported in Path.addRoundRect.
Even if there is no padding in that xml file then also i am not able to see any border.
Now, what should i have to do for it ?
and whats the sollution of it if i want to create the rounded border for only topLeftcorner and bottomLeftCorner. ?
Thanks.
I am also facing the same problem. But for that I use layer-list. I post my answer here which may help you. Please check output screen
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape
android:shape="rectangle">
<stroke android:width="1dp" android:color="#c1c1c1" />
<solid android:color="#c1c1c1" />
<corners android:radius="20dp"/>
</shape>
</item>
<item android:right="20dp"
>
<shape
android:shape="rectangle">
<stroke android:width="1dp" android:color="#c1c1c1" />
<solid android:color="#c1c1c1" />
</shape>
</item>
</layer-list>
You'll have to do that, assuming you only want a rounded top left corner:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="20sp"
android:topRightRadius="0dp"
android:bottomRightRadius="0dp"
android:bottomLeftRadius="0dp" />
<gradient
android:startColor="#color/logo_blue"
android:endColor="#color/blue"
android:angle="0"/>
</shape>
Explanation: Every corner must (initially) be provided a corner radius greater than 1, or else no corners are rounded. If you want specific corners to not be rounded, a work-around is to use android:radius to set a default corner radius greater than 1, but then override each and every corner with the values you really want, providing zero ("0dp") where you don't want rounded corners. [source]
As a consequence, you need to define your drawable as:
<?xml version="1.0" encoding="UTF-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="1dip"
android:color="#ffffff"/>
<solid
android:color="#95865F"/>
<corners
android:radius="10px"
android:topRightRadius="0dp"
android:bottomRightRadius="0dp" />
<padding
android:left="1dp"
android:right="1dp"
android:top="1dp"
android:bottom="1dp"/>
</shape>
Update
From The Shape Drawable Resource Android Documentation:
android:radius
Dimension. The radius for all corners, as a dimension
value or dimension resource. This is overridden for each corner by the
following attributes.
overridden is the keyword for your problem…
I'm using SDK tools 19, platform-tools 11 and running the app in Android 4.0.3. Using the following XML works for me:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#dd000000"/>
<padding
android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp" />
<corners
android:bottomLeftRadius="25dp"
android:bottomRightRadius="25dp"
android:topLeftRadius="0dp"
android:topRightRadius="0dp" />
</shape>
The Eclipse warning is about not being able to show the preview. In het app it shows up correct.
I got the solution on one of the forums. I solved it by commenting each corner & adding to the xml in drawable. I have retained commented code below only for understanding.
XML Code-
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="#fd2149" />
<stroke android:width="1dip" android:color="#ffd102"/>
<!-- <corners
android:bottomLeftRadius="8dp"
android:bottomRightRadius="8dp"
android:topLeftRadius="8dp"
android:topRightRadius="8dp" />
-->
<corners android:radius="8dp" />
</shape>
---Edited-
The above code is my back.xml in drawable folder and if required you can add padding to the same back.xml. For those who are new developers, the back.xml is referenced from your layout file as follows
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="20sp"
android:scrollbars="vertical"
android:background="#drawable/back">
Hope this helps.
I am trying to make a drawable to use for a button. I would like it to have this coloring, with a 2px border around it.
Everything works just fine except I cannot get the border to show up...
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient android:startColor="#color/bar_clicked_dark"
android:endColor="#color/bar_clicked_light"
android:angle="90"/>
<corners android:bottomLeftRadius="0dp"
android:topLeftRadius="15dp"
android:bottomRightRadius="15dp"
android:topRightRadius="0dp" />
<stroke android:width="2dp"
color="#ff00ffff" />
</shape>
It looks like you forgot the prefix on the color attribute. Try
<stroke android:width="2dp" android:color="#ff00ffff"/>
If you want make a border in a shape xml. You need to use:
For the external border,you need to use:
<stroke/>
For the internal background,you need to use:
<solid/>
If you want to set corners,you need to use:
<corners/>
If you want a padding betwen border and the internal elements,you need to use:
<padding/>
Here is a shape xml example using the above items. It works for me
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="2dp" android:color="#D0CFCC" />
<solid android:color="#F8F7F5" />
<corners android:radius="10dp" />
<padding android:left="2dp" android:top="2dp" android:right="2dp" android:bottom="2dp" />
</shape>
We can add drawable .xml like below
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="1dp"
android:color="#color/color_C4CDD5"/>
<corners android:radius="8dp"/>
<solid
android:color="#color/color_white"/>
</shape>