Custom listview selector / drawable dimensions? - android

Is there any way I can set my listview selector to a custom drawable that only occupies a portion of the item view? I have made a drawable rectangle and set it as the selector, but I'm having a difficult time getting it to size properly. The picture shows what I have now (1) and the desired effect (2).

Got it - initially decided to just set the selector as a drawable that only shows a border on one side, and messed around with insets to no avail (for some reason the top and bottom were not working properly). So I just made a layer list and offset the top layer to the right by a few dp.
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="0dp" android:left="0dp" android:bottom="0dp" android:right="0dp">
<shape android:shape="rectangle">
<solid android:color="#color/colorAccent"/>
</shape>
</item>
<item android:top="0dp" android:left="5dp" android:bottom="0dp" android:right="0dp">
<shape android:shape="rectangle">
<solid android:color="#FFFFFF"/>
</shape>
</item>
</layer-list>

Related

Custom drawable speech bubble

I need to create a speech bubble background for an icons container like the white one in this image:
Required Appearance
I decided to use a drawable xml rather than a 9 patch image, so I can control of the rounded corner radii in my code. So, with the help of this answer to a related question, I have created the following xml:
bg_icons_container.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:left="0dp"
android:top="27dp"> <!-- How to make this 50% of height? -->
<rotate
android:fromDegrees="-45"
android:toDegrees="0"
android:pivotX="0%"
android:pivotY="0%"> <!-- How to make the triangle more acute? -->
<shape
android:shape="rectangle" >
<solid
android:color="#color/white"/>
</shape>
</rotate>
</item>
<item
android:left="10dp">
<shape
android:shape="rectangle">
<solid
android:color="#color/white"/>
<corners
android:radius="#dimen/border_radius_small"/>
</shape>
</item>
</layer-list>
This results in:
Actual Apperance (enlarged)
My problem is that the triangular part of the drawable is too wide. How can I make it more acute? (My minSdkVersion is 16, so I cannot use android_width on the first item to try and stretch it.)
Also, I am having to hard code android:top of the first item to 27dp. Is it possible to make this 50%, so it will correctly adjust when the height of the drawable changes?

Drawable with 2 solid colours

I'm trying to create a drawable to act as the background for one of my layouts, and can't get it to display two solid colours divided in the middle.
Currently I'm attempting to use a layer-list with the background being solid, and an overlay of a semi transparent white rectangle to tint the original solid background colour along the top half of the rectangle. It doesn't need to be done like this though.
Gradient will not work unless you can find a way to make the transition immediate.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<size android:height="#dimen/basic_runners_height"></size>
<solid android:color="#color/brb_orange"
android:gravity="bottom"></solid>
</shape>
</item>
<item>
<shape android:shape="rectangle">
<size android:height="#dimen/basic_runners_height_halved"></size>
<solid android:color="#color/md_white_1000"
android:alpha="127"
android:gravity="top"></solid>
</shape>
</item>
So, my understanding of this is that there should be two shapes, one half the size of the other. The first shape covers the whole drawable in orange and the other is white, and ~50% transparent (alpha = 127), takes up half the drawable and gravitates toward the top.
The preview of the drawable is fully white, without a hint of orange. What am I doing wrong?
Edit:
Right, to make this even simpler, I've added the tinted colour to my #color so that I don't even need to bother with transparency. The XML is as shown below:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:top="#dimen/basic_runners_height_halved">
<shape android:shape="rectangle" >
<size android:height="#dimen/basic_runners_height_halved" />
<solid android:color="#color/brb_orange_tint" />
</shape>
</item>
<item android:top="#dimen/basic_runners_height">
<shape android:shape="rectangle" >
<size android:height="#dimen/basic_runners_height" />
<solid android:color="#color/brb_orange" />
</shape>
</item>
But this doesn't even do the trick... runners_height_halved is half the dp of runners_height but this doesn't split it down the middle. Maybe 10% of the drawable is the tinted colour, and for some reason they are reversed so that the tinted colour is at the bottom. This should be the most simple thing...
Try to use padding(top,left, right, bottom) in the <item> tag. Hope it helps
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<size android:height="#dimen/basic_runners_height"></size>
<solid android:color="#color/brb_orange"
android:gravity="bottom"></solid>
</shape>
</item>
<item android:top="10dp"
android:left="10dp"
android:right="10dp"
android:bottom="10dp">
<shape android:shape="rectangle">
<size android:height="#dimen/basic_runners_height_halved"></size>
<solid android:color="#color/md_white_1000"
android:alpha="127"
android:gravity="top"></solid>
</shape>
</item>

Get a shadow below transparent oval Shape in android xml

I want a shadow beneath an oval shape (see image below), but I can't figure out how to do it because the shape is a transparent color.
For this reason I can't use a layerlist, than you see the shape through the button and not the background.
I also tried a half transparent shadow, this works but doesn't look good, I would rather have the shadow black.
This is how I would want it
Thank you in advance
Set below file into your view background for shadow
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="#color/grey"/>
<!--shadow Color-->
</shape>
</item>
<item
android:left="0dp"
android:right="0dp"
android:top="0dp"
android:bottom="3dp">
<shape android:shape="oval">
<solid android:color="#color/white"/>
</shape>
</item>

How to alter LayerDrawable within ImageView?

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

Have bottom border for TableLayout which is used as row in ListView

According to Open-sided Android stroke?, there are basically 2 ways to implement borders for TableLayout.
Use Stroke
<?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="#ffffff"/>
</shape>
However, this is not something which I want, as I only want bottom border. Hence, I try another alternative.
Use 2 layers
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- This is the line -->
<item>
<shape>
<solid android:color="#ffffff" />
</shape>
</item>
<!-- This is the main color -->
<item android:bottom="1dp">
<shape>
<solid android:color="#000000" />
</shape>
</item>
</layer-list>
However, during list view selection, the TableRow will not be highlighted with ListView selection color, as its background already take over by "#000000" layer.
Is there any better way I can have bottom border only, yet it will obey ListView selection highlight color?
In your shape file use <solid android:color="#android:color/transparent"/> instead <solid android:color="#00ffffff"/>

Categories

Resources