I have been searching for possibilities to define different shapes inside a single shapes.xml and refer to each one on some specific events.
At last I've found a solution to my question. And the answer is using level-list.
<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:maxLevel="0">
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#aaa"
android:endColor="#eee" android:angle="270" />
<corners android:bottomRightRadius="7dp"
android:bottomLeftRadius="7dp" android:topLeftRadius="7dp"
android:topRightRadius="7dp" />
</shape>
</item>
<item android:maxLevel="1">
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<gradient android:startColor="#eee" android:centerColor="#ddd"
android:endColor="#00fff2" android:angle="270" />
<corners android:bottomRightRadius="7dp"
android:bottomLeftRadius="7dp" android:topLeftRadius="7dp"
android:topRightRadius="7dp" />
</shape>
</item>
</level-list>
Apply this to the background attribute in the style. the Interchanging of differents shapes can be achieved by setting the level to that element.
Eg: findViewById(R.id.mybutton).getBackground().setLevel(1);
In the above code I'm setting the second shape to the button with id mybutton.
<?xml version="1.0" encoding="UTF-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="1dip" android:color="#FF8000" />
<solid
android:color="#00FFFFFF"
android:paddingLeft="10dip"
android:paddingTop="10dip"/>
<corners android:radius="10px"/>
<padding
android:left="10dip"
android:top="10dip"
android:right="10dip"
android:bottom="10dip" />
</shape>
you can use this for Boarder and any shape ..its for reference...''
If it is useful so accept the answer and vote up the answer
Related
I want to add inner shadow effect into my drawable file like this.
Below is my drawable file. I want to achieve shadow inside the drawable file as you can see above. What changes should i make to achieve this?
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<item
android:bottom="#dimen/_3sdp"
android:left="#dimen/_8sdp"
android:right="#dimen/_8sdp"
android:top="#dimen/_3sdp">
<shape>
<corners android:radius="#dimen/_75sdp" />
<padding android:bottom="#dimen/_8sdp"
android:top="#dimen/_8sdp" />
<solid android:color="#color/leave_msg_color" />
</shape>
</item>
</layer-list>
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="#dimen/size_point5dp"
android:left="#dimen/size_point5dp"
android:right="#dimen/size_point5dp"
android:bottom="#dimen/size_point5dp">
<shape android:shape="rectangle">
<solid android:color="#color/blue_E7F1FF"/>
<corners android:radius="8dp"/>
</shape>
</item>
<!-- left shadow -->
<item>
<shape android:shape="rectangle">
<gradient
android:angle="180"
android:centerColor="#00FF0000"
android:centerX="0.98"
android:endColor="#5C000000"
android:startColor="#00FF0000" />
<corners android:radius="8dp" />
</shape>
</item>
<!-- right shadow -->
<item>
<shape android:shape="rectangle">
<gradient
android:angle="360"
android:centerColor="#00FF0000"
android:centerX="0.98"
android:endColor="#5C000000"
android:startColor="#00FF0000" />
<corners android:radius="8dp" />
</shape>
</item>
<!-- top shadow -->
<item>
<shape android:shape="rectangle">
<gradient
android:angle="270"
android:centerColor="#00FF0000"
android:centerY="0.1"
android:endColor="#00FF0000"
android:startColor="#6B000000" />
<corners android:radius="8dp" />
</shape>
</item>
<!-- bottom shadow -->
<item>
<shape android:shape="rectangle">
<gradient
android:angle="90"
android:centerColor="#00FF0000"
android:centerY="0.1"
android:endColor="#00FF0000"
android:startColor="#6B000000" />
<corners android:radius="8dp" />
</shape>
</item>
</layer-list>
The image would look like image
You can make many item inside your layer-list to produce shadow effect inside check this one out Add shadow to custom shape on Android.
You just need to change some values to achieve your wanted output. make the radius to get oval form and the values of colors.
I am adding a ViewPager to my app, together with an picture indicator.
I want the shape of the indicator to be a thin line instead of the usual dots.
For that, I have taken a reference from this answer here.
I have tried to modify the XML files given, like this:
default_dot.xml ---> default_line.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<solid android:color="#android:color/darker_gray"/>
<corners
android:bottomRightRadius="7dp"
android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp" />
</shape>
</item>
</layer-list>
selected_dot.xml -----> selected_line.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<solid android:color="#color/colorAccent"/>
<corners
android:bottomRightRadius="7dp"
android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp" />
</shape>
</item>
</layer-list>
And the file tab_selector.xml remains the same, just change the name of the corresponding xml files.
So, well, the result of all this, is that, the selected_line.xml (the pink one) is showed, however, the default_line.xml is not showed, so I can just see the pink like moving to the right/left as I swipe the pictures, but no default grey lines.
Please make changes to your selected_line.xml as follows
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<stroke
android:width="#dimen/activity_horizontal_margin"
android:color="#color/colorAccent" />
<size android:height="1dp" />
<solid android:color="#color/colorAccent" />
<corners
android:bottomLeftRadius="7dp"
android:bottomRightRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp" />
</shape>
</item>
I'm trying to create a complex xml drawable to represent a listbox, setting it as a background of a TextView.
Following there is there a screenshot of what I want to obtain and what I get instead.
As you can see I want to put my arrow in a gradient gray box on the right.
Looking at android documentation, it seems that xml shapes supports the size tag, as reported here http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape , but i can't get it working!
Here there is my XML
<?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="#999999" />
<corners android:radius="5dp" />
<solid android:color="#ffffff" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<stroke android:width="1dp" android:color="#999999" />
<corners android:topRightRadius="5dp" android:bottomRightRadius="5dp" />
<gradient android:startColor="#606060" android:endColor="#303030" android:angle="270" />
<padding android:top="7dp" android:bottom="7dp" android:right="7dp" />
</shape>
</item>
<item>
<bitmap android:src="#drawable/listbox_arrow_icon" android:gravity="right|fill_vertical" android:tileMode="disabled" />
</item>
</layer-list>
I tried specifying android:width in shape tag, as specified here http://developer.android.com/reference/android/graphics/drawable/ShapeDrawable.html#attr_android:width, but it doesn't work too.
I don't want to do it using code, but if it's unavoidable, in the end, I think i'll build a component to use in the place of TextView for this stuff.
Thanks for your help!
#EDIT
Sorry, during copy&paste I dropped the solid tag of the first shape! Fixed!
Try this :
<item><shape>
<solid android:color="#fff" />
<stroke android:width="1px" android:color="#10000000" />
<corners android:radius="6dp" />
<gradient android:angle="270" android:endColor="#10000000" android:startColor="#10FFFFFF" />
</shape></item>
I want to get shape similar to what you can see below. The thing I am missing is the header color (the color behind Anonymous text). I've reproduced what I wanted by simply moving mouse over second textview which is now highlighted and makes this effect :)
Current code:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:width="2dp" android:color="#000000" />
<gradient android:startColor="#898989" android:endColor="#B5B5B5" android:angle="270"/>
<corners android:bottomRightRadius="7dp" android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp" android:topRightRadius="7dp"/>
</shape>
You need to use a Layer List that contains two drawables.
For example, the first will cover the whole shape, and the second will overlay it but with android:top="10dp" set, to create an offset showing the underlying first shape
Edit:
Like so:
<?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="2dp"
android:color="#000000" />
<solid android:color="#00ff00" />
<corners
android:bottomLeftRadius="7dp"
android:bottomRightRadius="7dp" />
</shape>
</item>
<item
android:bottom="2dp"
android:left="2dp"
android:right="2dp"
android:top="20dp">
<shape android:shape="rectangle" >
<gradient
android:angle="270"
android:endColor="#B5B5B5"
android:startColor="#898989" />
<corners
android:bottomLeftRadius="7dp"
android:bottomRightRadius="7dp" />
</shape>
</item>
</layer-list>
Use a LinearLayout and place two TextView with different shapes as background.
The first one having its upper corners rounded and the second one its bottom corners.
this tools might be helpful further in development
http://angrytools.com/
http://android-ui-utils.googlecode.com/hg/asset-studio/dist/index.html
Can anybody tell me how to create an editable textbox with rounded corners in android?
I tried with this code but it's not working:
<?xml version="1.0" encoding="utf-8"?>
<!-- res/drawable/rounded_edittext.xml -->
<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>
Thanks
Hey have a look about the problem in this discussion : How to create EditText with rounded corners? ..I am sure it will surely gonna help you.
Just create an xml file in your drawable folder name as round_corner.xml.And add this following code.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<corners
android:bottomLeftRadius="3dp"
android:bottomRightRadius="3dp"
android:radius="3dp"
/>
<solid
android:color="#android:color/white"/>
</shape>
At Last you add this xml in background property of your Edittext as follows:-
<EditText
android:id="#+id/ed1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/round_corner"
/>
Done..Definitely it works..
Use This Code
<?xml version="1.0" encoding="utf-8"?>
<!-- res/drawable/rounded_edittext.xml -->
<shape xmlns:android="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>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#ECE9E6"
android:endColor="#FFFFFF"
android:angle="270"
/>
<corners
android:radius="15dp" />
<stroke
android:width="1dip"
android:color="#ffffff"
/>
<padding
android:left="4dp"
android:top="4dp"
android:right="4dp"
android:bottom="4dp"
/>
</shape>