How to alter LayerDrawable within ImageView? - android

I'm adding a couple of ImageView to a LinearLayout programmatically, those ImageView have its src set to R.drawable.rectangle.
I'm trying to create a rectangle with solid color that has a border only to the left.
R.drawable.rectangle looks like this:
<?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="rectangle">
<solid android:color="#000000" />
<stroke android:width="1dp" android:color="#999" />
</shape>
</item>
<item
android:top="0dp" android:left="1dp" android:bottom="0dp" android:right="0dp">
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="#283BCB"/>
<size android:width="100dp" android:height="8dp" />
</shape>
</item>
</layer-list>
In my activity, I'm inflating the layout that contains the LinearLayout and I'm adding ImageViews to it programatically, that is all working. The problem is that I don't manage to manipulate the rectangle width. If I do:
imageView.getLayoutParams().width = 10;
Rectangles need to be different sizes and colors. However if I do it this way, rectangles get deformed weirdly, this doesn't happen If I don't use a layer-list, but only a shape (but that way I cannot add a left border).
So I'm guessing I need to get LayerDrawable, then get GradientDrawable and change it. However I'm not having any luck achieving this.
LayerDrawable layer = (LayerDrawable)imageView.getDrawable();
GradientDrawable rectangle = (GradientDrawable)layer.getDrawable(1);
// rectangle.setBounds
// rectangle.setColor NOT working
Any hints? thanks

Related

How to force android:shape="oval" to remain perfect circle?

I am trying to define a circular background to be used as a background for buttons.
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="#dimen/abc_button_inset_horizontal_material"
android:insetTop="#dimen/abc_button_inset_vertical_material"
android:insetRight="#dimen/abc_button_inset_horizontal_material"
android:insetBottom="#dimen/abc_button_inset_vertical_material">
<shape android:shape="oval">
<solid android:color="#color/material_orange_200" />
<padding android:left="#dimen/abc_button_padding_horizontal_material"
android:top="#dimen/abc_button_padding_vertical_material"
android:right="#dimen/abc_button_padding_horizontal_material"
android:bottom="#dimen/abc_button_padding_vertical_material" />
</shape>
</inset>
The problem is that if the button is not completely square the shape get stretched into oval (elipsoid) which is undesirable. I could use <size> tag and set width and height to the same arbitrary value. But this would defeat the purpose to use this background as universal background for different size buttons.
Is there a way to force (in xml) oval shape to remain a circle?
create a drawable file like this
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#22C2FE" />
<stroke
android:width="0dp"
android:color="#2B8DC6"></stroke>
</shape>
and use the same height and width on which view are using the drawable.

Android Drawable Cascade Rectangle and add Bitmap

I am new to use Android XML drawables. I am trying to define a simple drawable layerlist that has a gray stroke outer rectangle with a white stroke inner rectangle. The two rectangles should all have stroke width = 1 and the inner rectangle should be just 1px smaller than the outer one. Here's my code:
<?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="rectangle" >
<stroke
android:width="1dp"
android:color="#android:color/darker_gray"/>
</shape>
</item>
<item android:bottom="1dp" android:top="1dp" android:right="1dp" android:left="1dp">
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke
android:width="1dp"
android:color="#android:color/white"/>
</shape>
</item>
</layer-list>
But this doesn't work. I got a solid gray box as result... How should I change the code to make it like I described?
Also, I am trying to put a drawable #drawable/image to the center of the rectangle with a margin 50dp to the borders. How should I do that?
Thank you!

android remove default border for a view with rounded corners

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.

Android draw circle around Text

I have a textView with a single character in it (all single digit, numbers 0-9). I'd like to draw a circle or a square around the number. I saw a thread mention using a nine-patch to style around it, but I'm unsure of how to do this (or if it is the best way to do it). How can I have a circle around the number?
Thanks
Just need to create a round drawable like this
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<corners android:radius="10dip"/>
<stroke android:color="#color/red" android:width="2dip"/>
<solid android:color="#android:color/transparent"/>
</shape>
And set this drawable as your TextView background.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke android:color="#color/teal_200" android:width="2dip"/>
<solid android:color="#android:color/transparent"/>
<size
android:width="10dp"
android:height="10dp"
/>
</shape>
You should be able to just give that TextView a Shape drawable that is a circle as a background.
http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape

Android shape within a shape

I want to have a shape element with a two color border outline. I can do a single color outline using the solid element, but this only allows me to draw a single line. I tried using two stroke elements within my shape, but that didn't work either.
Is there a way to either draw a shape within a shape or draw two lines around my shape (which has rounded corners, BTW).
I found that the <layer-list> is the best approach. Like this:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:left="6dp"
android:right="6dp">
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="3dp"
android:color="#000000" />
</shape>
</item>
<item
android:bottom="1dp"
android:left="8dp"
android:right="8dp"
android:top="1dp">
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:bottomLeftRadius="2dp"
android:bottomRightRadius="2dp"
android:topLeftRadius="2dp"
android:topRightRadius="2dp" />
<solid android:color="#android:color/white" />
<stroke
android:width="1dp"
android:color="#BDBDBD" />
</shape>
</item>
</layer-list>
You then need to put the proper margins on your listView row layout, but it works quite nicely.
so i have a work around but its ugly. the work around is to wrap my element inside another container element. i.e.
<RelativeLayout ... android:background="#drawable/outer">
<ListView ... android:background="#drawable/inner" />
</RelativeLayout>

Categories

Resources