I'm trying to define a background drawable in XML that will make the background have a 1dp grey border on the left. The XML I'm using is:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#00FFFFFF" />
<stroke android:width="1dp" android:color="#CCCCCC" />
<padding android:left="1dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>
In the screenshot below you can see that it's actually putting a 1dp border around the entire view (the "Recent Lessons" area):
Can someone explain to me what I've done wrong here?
I think that you may have confused padding and stroke. The 1dp stroke you are adding is the border you see around the shape - not the padding. Try following these examples. For more about LayerList see the android docs (LayerList section). Basically, it boils down to multiple drawables as one.
Related
I want to make a textview background like in the screenshot. I have a xml file that can make rectangular background like in the screenshot but i am not able to do that below corner styling. I dont know what thing can change this.
Here is my xml file that can give a rectangular background to my textview.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke
android:width="1dp"
android:color="#color/orange" />
<solid android:color="#color/orange" />
<padding
android:left="1dp"
android:right="1dp"
android:top="1dp" />
<corners android:radius="5dp" />
</shape>
Your best bet here would be to make a nine-patch image. Roman Nurik has made a really nice nine-patch generator that you can use. Then you just set the image as the background and it will stretch like you think it should.
I am trying to implement in XML a ShapeDrawable like this, but so far without success.
How do I make the Stroke visible only for two sides?
Is that even possible?
Otherwise what cloud I use (I seed it as background of a TextView).
Here is a solution which is using a LayerListDrawable:
background_white_lateral_border.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="#android:color/white" />
</shape>
</item>
<item>
<inset
android:insetLeft="1dp"
android:insetRight="1dp" >
<shape android:shape="rectangle" >
<solid android:color="#android:color/black" />
</shape>
</inset>
</item>
</layer-list>
Note: This firstly draws a white rectangle and then a black rectangle with a left and right inset of 1dp on top of it, giving the effect of lateral borders. It's just something to keep in mind, in case you worry about performance (which is in my opinion negligible for minor styling like this).
AFAIK, that is not possible with a single ShapeDrawable. A nine-patch PNG, or a LayerListDrawable of two ShapeDrawables (one per line) should work though.
I have created layout similar to this:
<RelativeLayout
android:id="#+id/layout_one"
style="#style/layout_one" >
<RelativeLayout
android:id="#+id/layout_two"
style="#style/layout_two" >
<RelativeLayout
android:id="#+id/layout_three"
style="#style/layout_three" >
</RelativeLayout>
</RelativeLayout>
For layout one I have created a custom drawable with rectangle shape so that the corners would be rounded and blue background color.
But for layout three I need to set white background color but if I do android:background="#FFFFFF" than it changes also the shape and the bottom corners are no longer rounded.
My first thought was to create custom drawable for the layout_three with rounded bottom corners but it wasnt working. Either all the corners were rounded or none.
Need to create something like this in the picture with rounded corners. Any suggestions?
I see that for layout three you use #style/layout_three which is different from #style/layout_one. So why don't you go to your style folder and put item with background white in layout_three style
It should look something like this:
<style name="layout_three">
<item name="android:background">#FFFFFF</item>
<!-- ... other stuff here... -->
</style>
EDIT: Sorry I did not understand your question well, reading that comment now.
There is only one way that comes to my mind right now to help you fix that.
Create an .xml file in your drawable folder and name it layout_three.xml or whatever.
And use this code:
<?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="#FFFFFF" />
<padding
android:bottom="10dp" <!-- you can set padding here
or on your layout layout_three -->
android:left="10dp"
android:right="10dp"
android:top="10dp"
/>
<corners android:radius="2dp" /> <!-- change the radius too -->
</item>
</layer-list>
And then you just use #drawable/layout_three instead of #style/layout_three
EDIT2: You can also use this code for the corners if you want only the bottom one to be rounded
<corners
android:topRightRadius="0dp"
android:topLeftRadius="0dp"
android:bottomLeftRadius="2dp"
android:bottomRightRadius="2dp"/>
You can create a xml drawable with rounded bottom corners for your requirement. Try the below code:
<item>
<shape android:shape="rectangle" >
<solid android:color="#FFFFFF" />
<corners android:radius="7dp"
android:topRightRadius="0dp"
android:topLeftRadius="0dp"
android:bottomLeftRadius="7dp"
android:bottomRightRadius="7dp"/>
</shape>
</item>
I just read that the problem is that the Android layout preview doesn't show the different corner radius so you have to test in on device or emulator to see the difference.
I have a layout with 2-3 childs. Set linear layout backgroud to a following drawable using android:background property.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#373949"/>
<stroke android:width="3dip" android:color="#FFF"/>
<corners android:radius="30dip" />
<padding android:left="10dip" android:top="10dip" android:right="10dip" android:bottom="10dip" />
</shape>
But when set radius to 30dip, rounded corners getting displayed, but back to
layout default gray colored border with rectangular shape is displayed.
Is there any way to get rid of that ?
Thanks in advance
Try this code
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFFFF"/>
<corners android:radius="15px"/>
<padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>
once you change the background of the view, this drawable will no longer be active to draw UI, hence the default layout of view will be applicable, so if you want gray layout to also be rounded, make another drawable, and set that drawable instead of gray color.
That rounded corner border that you see is of the parent of layout that you have changed background.If your cusytomized background belongs to your activity than the grey color you see belongs to system. You can use Hierarchy Viewer. To see it in detail.
you can also refer this for your reference.
Is it possible to draw a left and bottom border for a textview?
You can put the TextView inside of another layout with an XML shape similar to the one below:
<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>
You can create a 9-path to be your TextView's BG. That 9-patch will have only left and bottom border, so it'll suit your needs. You can read more on 9-patches here: http://developer.android.com/guide/developing/tools/draw9patch.html
You can use the draw9patch tool on the SDK do draw your own 9patch, or take en existing 9patch from the Android source code, or even from the other places on the internet, like this blog http://android9patch.blogspot.co.il/ which also have some tutorials on how to create 9 patched.