Hi I'm trying to make a drawable which looks like a small vertical line with a circle attached to the bottom of it. I am trying to figure out if creating such a shape in a single xml file is possible (I know I could probably place two shapes on top of each other but I'm trying to avoid doing that)
Here's what I have so far:
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/line">
<shape
android:shape="line"
>
<stroke
android:width="2dp"
android:color="#000000" />
<size android:height="12dp"/>
</shape>
</item>
<item
android:top="5px"
android:id="#+id/circle">
<shape
android:shape="oval"
android:useLevel="false"
android:innerRadius="5dp"
android:thickness="2dp"
>
<solid
android:color="#color/silver_status"
></solid>
<stroke
android:color="#color/silver_status"
android:width="1dp"
/>
</shape>
</item>
</layer-list>
This layers the two images on top of each other (I think) but what I really want is for them to be on top of each other
Anyone know if this is possible/how it can be done?
I ended up solving this simply by using the individual shapes within a linear layout to achieve the affect. Not exactly what I wanted but definitely does the job just fine.
Related
I have tried to search this a lot however I was not able to make the same buttons as in the picture for android.
I have been trying to use the following code but the button is not transparent and it doesn't have that nice rounding on the sides:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#ffffff" />
<corners android:radius="15dp" />
</shape>
Currently it looks like this
I want the EditTexts and the buttons to be transparent with a white border
It would be a great great help if someone could assist me on this.
Defining shape is proper approach. If you want it to be transparent with white border you need to avoid using solid tag and use stroke (that is the border).
So for example you will have this shape (let's call it bg_button.xml under drawable folder):
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:color="#android:color/white" android:width="1dp" />
<corners android:radius="15dp" />
</shape>
Apply then this to your views as background. This is an example:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Facebook"
android:background="#drawable/bg_button"/>
Please try this not just on Android Studio editor but also on your emulator / device. Rendering of rounded corners on Android Studio editor can create problems.
Say you have a xml drawable called rounded_button
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<solid android:color="#6E6E6E"/>
<corners
android:bottomRightRadius="8dp"
android:bottomLeftRadius="8dp"
android:topLeftRadius="8dp"
android:topRightRadius="8dp"/>
</shape>
You then set the background of the button to the #drawable resource you created.
I have developed a listView for my app. My UI is simple flat UI, here is a snapshot:
when I searched internet for better interfaces I found this for example:
As you see in this sample, background colors have shadows at the bottom. Is there a way that I can color my Layout background like this? Thanks in advanced.
In order to achieve a shadow effect I'd guess that it's better to use a layer-list with two items, one with the solid color and the second item would be a gradient coming from transparent to a black color with an alpha value.
Here's how this would look like (needs to be placed in res/drawable/):
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#ff33b5e5" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<gradient
android:angle="-90"
android:endColor="#90000000"
android:startColor="#android:color/transparent" />
</shape>
</item>
</layer-list>
Doing it this way, you won't need to "search" for a darker color of your solid color all the time.
Output would look like this:
Create an XML file and named it as "gradient_effect" in Res -> Drawable folder and put below code in it
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#a0a0a0"
android:endColor="#f5f5f5"
android:angle="0"
/>
Now use this file as a background in any of your layout as you can get is like
android:Background= "#Drawable/gradient_effect"
Set above line as an attribute for any of you view or layout in xml file
This can help too!
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<gradient
android:angle="90"
android:endColor="#A2000000"
android:startColor="#android:color/transparent" />
</shape>
</item>
Is it possible to draw two strokes (one after another) for ListView divider?
I've tried the following drawable, but it only shows the first stroke:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<stroke
android:color="#eeeeee"
/>
<size
android:height="1px"
/>
<stroke
android:color="#c1c1c1"
/>
<size
android:height="1px"
/>
</shape>
Yes, it is possible. If you want to create it with shape drawables, you have to do it differently. A shape drawable can contain just one shape, one line in your case. You can combine two shapes in layer list drawable. Drawable in the layer list are drawn one above another, the last one at the top. To create two lines you just have to set the proper padding for each of the lines, so that both the lines are visible. The resulting drawable will be something like this (I made the lines thicker in the example):
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:bottom="2dp">
<shape android:shape="line">
<stroke
android:color="#eeeeee"
android:width="2dp"
/>
<size
android:height="4dp"
/>
</shape>
</item>
<item android:top="2dp">
<shape android:shape="line">
<stroke
android:color="#c1c1c1"
android:width="2dp"
/>
<size
android:height="4dp"
/>
</shape>
</item>
</layer-list>
If you want the 2 strokes to be really thin, let's say 1px height each, I tried the solution above and I could't make it.
I found it much easier painting a litte image (1x2) with the 2 pixels with the desired colors, and then define the image in the divider doing:
android:divider="#drawable/myTinyDivider"
Hope this helps someone.
In my application, I'm (still) trying to manipulate my Button Views to look the way I want. I'd like to attempt an experiment where I'll draw an Android Rect or RectF in the same location as the (transparent) button to highlight it's appearance.
However, I can't figure out how to get my mitts on that information. The buttons in question are defined in XML, in a Linear Layout, but something inside my Android has to know their sizes and locations. Right?
Any suggestions?
Thanks, R.
You should be able to set a background of the button to a custom xml drawable with the drawable containing only stroke element around the edge.
You will need to create this custom drawable: it would be just a few lines of XML.
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="1dp" android:color="#CCCCCC" />
<corners android:radius="10dp" />
</shape>
Edit: updated to include rounded corners.
This answer is a combination of my poking around and Aleks' suggestion to use a custom drawable. Ultimately, my custom drawable needed more than three lines, but it's still not very big:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:startColor="#android:color/black"
android:endColor="#android:color/black" />
<stroke
android:width="1dp"
android:color="#cccccc" />
<corners
android:radius="5dp" />
<padding
android:left="4dp"
android:top="2dp"
android:right="4dp"
android:bottom="2dp" />
</shape>
</item>
</selector>
I needed to add all of the other detail to get the custom button to work the way I wanted -- and I'm probably not done, but if anybody is interested, this works pretty well...
I am quite new to Android dev, but not Java dev, so doing the logic behind a button is no issue, but styling it is not as easy as css. I have read a couple tutorials on shapes / styles so I kind of know how to do the custom borders and round corners, but I was hoping to see some really good quality examples, like the buttons on the Twitter app http://i.stack.imgur.com/Gip2s.png or the 'debossed' ones on the facebook app.
What I suppose I am really interested in, are using shadows to create effects. Are these done with images, or can you style this in the app?
thanks
for rounded corners create a shape drawable eg. ronded_corner.xml and the angle must be a multiple of 45 degree
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#SomeGradientBeginColor" android:endColor="#SomeGradientEndColor"
android:angle="225"/>
<corners android:bottomRightRadius="7dp" android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp" android:topRightRadius="7dp"/>
</shape>
then set this Background by android:background:#drawable/ronded_corner
Whenever you create the button in the layout just set the background property of the button as XML, Put this XML file in the drawable folder.
sample XML code i will post here. you can modify and learn in that only.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:angle="270"
android:startColor="#B80000"
android:endColor="#900405"
android:type="linear"
/>
<stroke android:width="1dp" android:color="#900405"/>
</shape>
For rounded corner button, define inset as shown below and adjust radius.
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="4dp"
android:insetTop="6dp"
android:insetRight="4dp"
android:insetBottom="6dp">
<ripple android:color="?attr/colorControlHighlight">
<item>
<shape android:shape="rectangle"
android:tint="#0091ea">
<corners android:radius="10dp" />
<solid android:color="#1a237e" />
<padding android:bottom="6dp" />
</shape>
</item>
</ripple>
</inset>
For more information http://www.zoftino.com/android-button