I have the following drawable which draw a rectangle. Can you please tell me how can I add a 9patch image as the background of this drawable?
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
>
<stroke android:width="1dp" android:color="#FFFFFFFF" />
<solid android:color="#android:color/transparent"/>
</shape>
Thank you.
You can't use a drawable for that, but you can use a
<layer-list>
<item android:drawable="#drawable/mydrawable" />
<item>
<shape>...</shape>
</item>
</layer-list>
Related
Please how can I achieve the image below as background for an image view.
Create my_shape.xml under res/drawable:
<?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="#A5A5A5" />
<corners
android:bottomRightRadius="75dp"
android:topLeftRadius="100dp" />
</shape>
</item>
</layer-list>
Then, you can apply it to your view with:
android:background="#drawable/my_shape"
I want to add border like the one in the image below. I tried achieve this using xml drawables' layer-list component, but I couldn't.
Create an XML file named border.xml in the drawable folder and put the following code in it.
<?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="#FF0000" />
</shape>
</item>
<item android:left="5dp" android:right="5dp" android:top="5dp" >
<shape android:shape="rectangle">
<solid android:color="#000000" />
</shape>
</item>
</layer-list>
Then add a background to your linear layout like this:
android:background="#drawable/border"
Finally its works perfectly with all APIs
Let me know if it was usefull Behzad Fartash
create new drawable resource file into the drawable folder and add below code
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#FFFFFF" />
<corners android:radius="#dimen/_10sdp"
</shape>
and set this drawable as the background of your parent layout
I want to make dotted line vertically with the gradient color effect. But I'm unable to do that. How can I approach. I am using this code on drawable for the dotted line
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:left="3px"
android:right="-5px"
android:top="-5px"
android:bottom="-5px">
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="4px"
android:color="#color/colorPrimary"
android:dashGap="20px"
android:dashWidth="20px" />
</shape>
</item>
</layer-list>
Anyone knows please help
Thanks in advance
create a drawable like below
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="-5px"
android:left="3px"
android:right="-5px"
android:top="-5px">
<shape>
<gradient
android:angle="270"
android:centerColor="#00ff00"
android:endColor="#ff0000"
android:startColor="#0000ff"/>
<stroke
android:width="2dp"
android:color="#fff"
android:dashWidth="8dp"
android:dashGap="16dp"/>
</shape>
</item>
</layer-list>
now in layout you need to use a view and set this shape as background to it.
<View
android:layout_width="4dp"
android:layout_height="match_parent"
android:background="#drawable/your_shape"/>
you might need to change widht settings as per your requirements. Here is what I got
You can use shape Try this...
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<stroke
android:color="#000000"
android:dashWidth="10px"
android:dashGap="10px"
android:width="1dp"/>
</shape>
I am attempting to create a drawable shape that will become the background of a search bar. I know how to create a rectangle with two colors but I do not know how to tilt the color at an angle, like in the second image. Can someone help me with this? Thanks.
Take a Toolbar...code is here...
<android.support.v7.widget.Toolbar
android:layout_width="500dp"
android:layout_height="wrap_content"
android:background="#drawable/custom_background" />
create a drawable res file custom_background.xml and
In that toolbar set drawable like this....code is here
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle">
<size
android:width="500dp"
android:height="50dp" />
<solid android:color="#0072BC" />
<corners android:radius="0dp"/>
</shape>
</item>
<item
android:top="8dp"
android:bottom="0dp"
android:left="-400dp"
android:right="128dp">
<rotate
android:fromDegrees="160">
<shape android:shape="rectangle">
<solid android:color="#FFF" />
</shape>
</rotate>
</item>
I'd like to have a background color of a RelativeLayout similar to that of an image shown in the link below. Ignore the black strip in the bottom half of the image.
I don't want to use the image as a background of the layout. Can you give me an idea of how to proceed?
You can use gradient. Set different gradient in selector as you want. Done!
GradientDrawable.class
This is what you want, set your colors. Enjoy!
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:bottom="30dp">
<shape android:shape="rectangle" >
<corners
android:topRightRadius="8dip"
android:topLeftRadius="8dip" />
<gradient
android:startColor="#030303"
android:endColor="#3B3B3B"
android:angle="270" />
</shape>
</item>
<item android:top="30dp" >
<shape android:shape="rectangle" >
<corners
android:bottomLeftRadius="8dip"
android:bottomRightRadius="8dip" />
<gradient
android:startColor="#4B5057"
android:endColor="#4B5059"
android:angle="270" />
<size android:height="30dp" />
</shape>
</item>
</layer-list>
just try with shape file structure. I think this may give the solution.
//save this below code as gradient and use as background of your layout as
android:background="#drawable/gradient"
//gradient.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#232323" android:endColor="#4B5059"
android:angle="270"/>
</shape>