How to apply stroke to a button specific sides in android - android

How can I apply stroke to a button's only two sides that is LEFT border and RIGHT border? in android.
I have apply the following code but it's not working
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- This is the main color -->
<item>
<shape>
<solid android:color="#EEEEEE" />
</shape>
</item>
<!-- This is the line -->
<item android:right="1dp">
<shape>
<solid android:color="#333333" />
</shape>
</item>
<item android:left="1dp">
<shape >
<solid android:color="#333333"/>
</shape>
</item>
</layer-list>

You were on the right track. This will do the trick:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid android:color="#EEEEEE" />
</shape>
</item>
<!-- This is the line -->
<item android:right="1dp" android:left="1dp">
<shape>
<solid android:color="#333333" />
</shape>
</item>
</layer-list>
And use this as the background for your button. Just tested it - works just fine.

I would suggest using a 9-patch drawable that looks like this:
It has the required black border, 1px on each side of the 0x333 color and the rest is filled with 0xeee. 9-patches scale with the size of the view that they are applied to. So you can use it as a background or src

Related

Progress bar shadow only on filled part

Implementing a progress bar in android, trying to get a drop shadow on the bottom part of the filled section only, like this:
API level is KitKat and up but i wont mind a solution that applies to LOLLIPOP and up only.
Elevation does not seem to work, nor do the card View.
any sugesstions?
Create the following xml shape and place it as your progressBar background.
** The 10dp in the xml assumes the progressBar is 20dp in Height and that you want the shadow to be exactly half of it.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/background">
<layer-list>
<item android:bottom="10dp">
<shape android:shape="rectangle">
<solid android:color="#color/orange" />
</shape>
</item>
</layer-list>
</item>
<item android:id="#android:id/progress">
<clip>
<layer-list>
<item>
<shape android:shape="rectangle">
<solid android:color="#color/green" />
</shape>
</item>
<item android:top="10dp">
<shape android:shape="rectangle">
<gradient
android:angle="90"
android:endColor="#color/black"
android:startColor="#color/white" />
</shape>
</item>
</layer-list>
</clip>
</item>
</layer-list>

Create drawable with colored background and centered icon

I'm trying to create a drawable painted by color with centered icon. I tried to create layer-list but it scales icon to match drawable size:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid android:color="#color/colorImageHolder"/>
</shape>
</item>
<item android:drawable="#drawable/no_photo" android:gravity="center">
</item>
</layer-list>
This is a real device picture:
I want it to look like this:
Is there any way to disable icon scaling ?
UPDATE: My ImageView has scaleType="centerCrop" and I think this is a root of problem :( I tried to place no_photo drawable as background but it scales again.
Try this
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid android:color="#color/colorImageHolder"/>
</shape>
</item>
<item android:drawable="#drawable/no_photo"
android:bottom="20dp"
android:left="20dp"
android:right="20dp"
android:top="20dp"
>
</item>
</layer-list>
Note: This solution only work with square or oval view, not rectangle
inside your second <item> tag add the following <bitmap> tag so it reads:
<item>
<bitmap android:src="#drawable/no_photo" android:gravity="center" />
</item>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<corners android:radius="44dp" />
<solid android:color="#color/white" />
<size
android:width="44dp"
android:height="44dp" />
</shape>
</item>
<item>
<layer-list>
<item android:drawable="#drawable/ic_menu" android:gravity="center"/>
</layer-list>
</item>

custom border for ImageView

I want to remove the peach solid color from interior portion and just want it at the border of top, left and right. The bottom color red is fine. I searched everywhere but they only suggested to use solid to overlap the whole red border.
XML Code
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<!-- Draw a 2dp width border around shape -->
<stroke
android:color="#ff1e0c"
android:width="2dp"
/>
</shape>
</item>
<!-- Overlap the left, top and right border using background color -->
<item
android:bottom="2dp"
>
<shape android:shape="rectangle">
<solid android:color="#fffbce"/>
</shape>
</item>
</layer-list>
try this solution,
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- This is the line -->
<item android:bottom="1dp">
<shape>
<solid android:color="#00000000" />
<stroke
android:width="1dp"
android:color="#FF0000" />
</shape>
</item>
<item
android:bottom="-1dp"
android:left="0dp"
android:top="0dp">
<shape>
<solid android:color="#00000000" />
<stroke
android:width="1dp"
android:color="#000000" />
</shape>
</item>
</layer-list>

EditText with a left and right border of given height

How can i make my edit text have a background with that blue border at the bottom, left and right sides where left and right side border has (let say) a height of 2dp ?
I have tried using layelist but all it's hard to get left and right border like the one at the image
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape >
<solid android:color="#android:color/holo_blue_bright"/>
<size android:height="1dp"/>
</shape>
</item>
<item android:bottom="5dp" android:left="5dp" android:right="5dp">
<shape>
<solid android:color="#android:color/black"/>
</shape>
</item>
This is what i get
Hi in most cases to have this type of background developers use 9.png graphics, you can generate background on this site.
http://android-holo-colors.com/
I tried and this worked for me.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid android:color="#android:color/holo_blue_bright" />
<size android:width="5dp" />
</shape>
</item>
<!-- main color -->
<item
android:bottom="1.5dp"
android:left="1.5dp"
android:right="1.5dp">
<shape>
<solid android:color="#android:color/black" />
</shape>
</item>
<!-- draw another block to cut-off the left and right bars -->
<item android:bottom="5.0dp">
<shape>
<solid android:color="#android:color/black" />
</shape>
</item>
</layer-list>
try this way and set as background
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="-2dp">
<shape android:shape="rectangle" >
<stroke
android:width="0.5dp"
android:color="#android:color/black" />
</shape>
</item>
</layer-list>
set to your Edittext as background
<EditText
android:id="#+id/editTextone"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="#drawable/edtbg"
/>

Drawing top border on a shape on Android

I'm creating a custom progress bar (positioned under a WebView) and what I would like to draw is a 1dp-wide line between the WebView and the ProgressBar. I'm modifying existing drawable, namely progress_horizontal.xml, and tried something like this:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
(...)
<item>
<shape android:shape="line">
<stroke android:width="1dp" android:color="#FF000000" />
</shape>
</item>
</layer-list>
This line however is vertically centered but I want it to be drawn on top of the drawable. The only idea I could come up with is to use this "hacky" gradient below:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
(...)
<item>
<shape>
<gradient
android:startColor="#FF000000"
android:centerColor="#00000000"
android:centerY="0.01"
android:endColor="#00000000"
android:angle="270"
/>
</shape>
</item>
</layer-list>
Do you have better ideas how to draw a single line shape aligned to the top of the drawable defined with layer-list?
I spent a while trying to figure this out as well. Hopefully there is a better way to do it but here is the method I used, which is also kind of hackish, but in case it helps someone, I thought I would share.
The first item in your layer list should be a solid of whatever color you want to be your border. Following that would be the thing(s) you want to have the border, with padding on whatever side you want the border to be on.
For example:
<?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="YOUR BORDER COLOR" />
</shape>
</item>
<item android:top="YOUR TOP BORDER THICKNESS">
THE THING YOU WANT A BORDER ON
</item>
</layer-list>
The idea is that the padding on the item reveals the solid shape behind it, giving the border appearance. You could add padding to any side to add a border. I imagine you could get more complicated and have different colored borders this way as well.
Seems like a hack but it worked for me.
EDIT: I said "padding" but in layer-lists it's more of an offset.
i was going through all related topics but no one could solve my problem. But some of them were very useful to understand how the layer-list or shape parameters work.
My problem was to define a button with a linear gradient and draw a top and a bottom line in different colors. After all I hacked this solution. I saved the file unter res/drawable/blue_btn.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" >
<shape android:shape="rectangle">
<solid android:color="#color/blue_end" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
<stroke
android:width="1dp"
android:color="#color/bottomline_btn" />
</shape>
</item>
<item>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle">
<solid android:color="#color/blue" />
<gradient
android:startColor="#color/blue"
android:endColor="#color/blue_end"
android:angle="270" />
</shape>
</item>
<item android:top="0dp" android:bottom="-1dp" android:left="-1dp" android:right="-1dp">
<shape android:shape="rectangle">
<stroke
android:width="1dp"
android:color="#color/topline_btn" />
<solid android:color="#00000000" />
<corners android:radius="0dp" />
</shape>
</item>
<item android:top="-1dp" android:bottom="0dp" android:left="-1dp" android:right="-1dp">
<shape android:shape="rectangle">
<stroke
android:width="1dp"
android:color="#color/bottomline_btn" />
<solid android:color="#00000000" />
<corners android:radius="0dp" />
</shape>
</item>
</layer-list>
</item>
</selector>
<color name="topline_btn">#31ffffff</color>
<color name="bottomline_btn">#31000000</color>
<color name="blue">#449def</color>
<color name="blue_end">#2f6699</color>
Did you try offsetting it by something like this
http://android.amberfog.com/?p=9
My solution is to add 9-patch as the last layer item :
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape>
<solid android:color="#color/tab_button_active_layer2" />
</shape>
</item>
<item android:bottom="#dimen/tab_button_active_bottom_bar_width">
<shape>
<solid android:color="#color/tab_button_active_layer1" />
</shape>
</item>
<!-- 9-patch drawable -->
<item android:drawable="#drawable/tab_button_border_top"/>
</layer-list>

Categories

Resources