Android: background of listview corners - android

I have listview with rounded corners. How can I set the background of these corners? I need to set them grey, like main background but they are white.
My listview
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/cats_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#000"
android:background="#drawable/round_corners"
/>
round_corners.xml :
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<corners android:bottomRightRadius="7dp" android:bottomLeftRadius="7dp"
android:topLeftRadius="17dp" android:topRightRadius="17dp"/>
<gradient
android:angle="180"
android:endColor="#ff0000"
android:startColor="#ff0000"
android:type="linear" />
</shape>

Would it be possible to set the background of your xml shape to transparent?
You can use the system default
#android:color/transparent

Try followin trick of adding a view befor adding ListView.
That View have background color as gray.
<View
android:background="#android:color/darker_gray"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/cats_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#000"
android:background="#drawable/round_corners"
/>
I checked it, and it works.

Related

Add gradient to imageview

I want to add a gradient on the bottom of my image . Something like this :
I tried something like this but I only get the gradient no image..
<ImageView
android:id="#+id/trendingImageView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/trend_donald_sterling"
android:src="#drawable/trending_gradient_shape"
/>
trending_gradient_shape:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="90"
android:endColor="#android:color/darker_gray"
android:startColor="#android:color/darker_gray" />
<corners android:radius="0dp" />
</shape>
You need two layers: An ImageView, and a View on top of that with your gradient as android:background. Put these two Views in a FrameLayout:
<FrameLayout
... >
<ImageView
...
android:src="#drawable/trend_donald_sterling" />
<View
...
android:background="#drawable/trending_gradient_shape"/>
</FrameLayout>
Simply set the alpha value in your gardient.xml:
Your imageView:
android:background="#drawable/trend_donald_sterling"
android:src="#drawable/trending_gradient_shape"
Your gradient xml file:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="90"
android:endColor="#00ffffff"
android:startColor="#aa000000"
android:centerColor="#00ffffff" />
<corners android:radius="0dp" />
</shape>
In the color value, the first two places after # correspond to the alpha value, while the rest are the actual color value in R G B format, two for each.
try using the "foreground" attribute in your imageview
<ImageView
...
android:src="#drawable/trend_donald_sterling"
android:foreground="#drawable/trending_gradient_shape" />
it worked for me.
Use android:foreground="..." instead of android:background="..."
Now you won't need to put ImageView and View inside a FrameLayout!
So your final code will be:
ImageView
<ImageView
...
android:foreground="#drawable/trend_donald_sterling"/>
Drawable
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="90"
android:endColor="#00ffffff"
android:startColor="#aa000000"
android:centerColor="#00ffffff" />
<corners android:radius="0dp" />
</shape>
this is how im gonna do,
i used relative layout as my parent layout, use the following code
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/img_sample"/>
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradiant"/>
<LinearLayout
android:layout_marginLeft="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.55"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="0.25"
android:text="Events"
android:gravity="bottom"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="#ffffff"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="0.25"
android:text="Some description about the events goes here"
android:textSize="14sp"
android:textColor="#ffffff"/>
</LinearLayout>
</RelativeLayout>
hope you can figure out, here i attach my gradiant code below.use it inside the drawable folder....
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="90"
android:endColor="#00ffffff"
android:startColor="#aa000000"
android:centerColor="#00ffffff" />
<corners android:radius="0dp" />
</shape>
This is an easy way that creates a similar effect yet doesn't actually have the image disappear. Sometimes using the foreground attribute is not the best for the gradient, especially if using a motionlayout or you have nested scrollviews. Create an entirely new imageview and set the background to the gradient.
XML With Both Imageviews
<ImageView
android:id="#+id/main_imageView"
android:layout_width="0dp"
android:layout_height="0dp"
android:scaleType="centerCrop"
android:src="#drawable/peakpx__1_"
ads:layout_constraintHeight_percent=".55"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/main_imageView_gradient"
android:layout_width="0dp"
android:layout_height="0dp"
ads:layout_constraintHeight_percent=".55"
android:background="#drawable/gradient_theme_background"
app:layout_constraintEnd_toEndOf="#id/main_imageView"
app:layout_constraintBottom_toBottomOf="#id/main_imageView"
app:layout_constraintStart_toStartOf="#id/main_imageView" />
Then for the gradient, I use black #000000 for darker themes, and white #ffffff for lighter ones. A lot of answers I see on this are not adding the center color. This is important if you want to have the gradient start closer to the edge of the image.
gradient_background
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="270"
android:type="linear"
android:endColor="#ff000000"
android:centerColor="#00000000"
android:startColor="#00000000"/>
</shape>
**1> Create file black_shadow.xml**
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:angle="270"
android:startColor="#00000000"
android:centerColor="#9c000000"
android:endColor="#000000"
android:type="linear" />
</shape>
</item>bla
</selector>
**2> Just add below line in Imageview.**
android:foreground="#drawable/black_shadow"

Android shape background

Is it possible to draw a shape in xml, and use a png as the background to that shape? I already have the shape (it's a square with rounded corners), and i would like to put a background to that square.
Yes you can use any shape file as background for any view. This sample create rounded background with white color and black border around the shape.
Sample :
rounded_corner.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners
android:bottomLeftRadius="10dp"
android:bottomRightRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp" />
<stroke
android:width="0.5dp"
android:color="#color/color_grey" />
<solid android:color="#color/color_white" />
</shape>
u can use this as,
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal"
android:background="#drawable/rounded_corner"
android:orientation="vertical" >
//try this way this will help you
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/rounded_corner"
android:padding="2dp"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/yourdrawable />
</LinearLayout>

ListView dividers don't appear with cells background

I have a custom ListView and I want to display thin gradient dividers between my cells. Here is the XML of my ListView
<ListView
android:id="#+id/fil"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#drawable/list_divider"
android:dividerHeight="1dp"
android:clickable="true">
</ListView>
In my list_divider.xml, I have a gradient defined like this :
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle">
<gradient
android:angle="0"
android:centerColor="#999999"
android:endColor="#00000000"
android:startColor="#00000000" />
</shape>
</item>
Normally, this would display a gradient divider, but it doesn't work. Apparently, the problem is located in my row layout with a RelativeLayout root :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#ffffff"
android:padding="5dip" >
When I put no background, it's working. But when I want to add a white background for example, it displays a full black divider. What's the problem here ?
Thanks

Gradient shape background on bottom layout

here is some UI behavior I don't understand...
What I want:
What I got:
My code:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/black_to_white" />
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/blue_to_red" />
</LinearLayout>
backgrounds code: (same code for both, except the colors)
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#000000"
android:endColor="#ffffff"
android:angle="270" />
</shape>
How can I make my 2nd gradient start blue at the middle of the screen?
(I noticed the gradient works fine on android 3.0 view mode, but not on other versions)
I am using the following code and it is working fine as you need.
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#151B8D"
android:endColor="#ff0000"
android:angle="270" />
</shape>
The problem is you lack the alpha in your color code:
#[alpha][red][green][blue]

Background properties on ImageView ignored when placed over ViewFlipper

I have an Activity that contains a FrameLayout and two views. The first is a ViewFlipper and the second is an ImageView. If I comment out the ViewFlipper, the activity renders the ImageView and its background correctly, but when the ViewFlipper is visible, the ImageView's background is completely ignored.
Is this a "feature" of ViewFlipper? Is there a way I can work around this?
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/background_light"
>
<ViewFlipper android:id="#+id/flipper"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:flipInterval="5000"
>
<include layout="#layout/cell_1" android:id="#+id/cell_1"/>
<include layout="#layout/cell_2" android:id="#+id/cell_2"/>
<include layout="#layout/cell_3" android:id="#+id/cell_3"/>
</ViewFlipper>
<ImageView
android:background="#drawable/alpha_gradient_background"
android:id="#+id/statusIcon"
android:src="#drawable/status_icon"
android:padding="5dp"
android:layout_gravity="right"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:top="10dp"
android:right="10dp"
/>
</FrameLayout>
And my background drawable looks like this:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:width="3dp" android:color="#ffff0000" />
<gradient
android:startColor="#66660000"
android:centerColor="#66006600"
android:endColor="#66000066"
android:angle="270"
/>
</shape>
It appears that Buttons are able to render their background when placed over a ViewFlipper.
I have therefore got around this by creating a partially transparent PNG and setting that as the background image on the button.

Categories

Resources