Background properties on ImageView ignored when placed over ViewFlipper - android

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.

Related

How to round corners of list item at the top and bottom of the list view? (not only for top and bottom items)

I have a listview with rounded corner at the top and the bottom. Look like this image:
But when I click on the top and the bottom of ListView, the List Item's background is rectangle not rounded as background of ListView at top and bottom. Like this image :
How to resolve this issue?
this is my code :
1/ list_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#cacaca">
<ListView
android:id="#+id/listView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#drawable/list_border"
android:listSelector="#drawable/list_selector"/>
</LinearLayout>
2/ list_border.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#fff"/>
<corners android:bottomRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp"/>
</shape>
3/ list_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#color/colorPrimary" />
</selector>
4/ ListActivity.java
ListView listView = (ListView)findViewById(R.id.listView3);
ArrayList<String> listItems = new ArrayList<>();
for(int i=0;i<20;i++){
listItems.add(""+i);
}
ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_selectable_list_item, listItems);
listView.setAdapter(adapter);
This solution is not pretty but it does what you want. The idea is to invert the corner and draw them as foreground :
the ListView is wrapped in a FrameLayout :
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="10dp"
android:foreground="#drawable/inverted_corners"
android:layout_weight="1">
<ListView
android:id="#+id/listView3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:listSelector="#drawable/list_selector" />
</FrameLayout>
which foreground (drawable/inverted_corners.xml) is set to a rounded hole, drawn with the background color. The trick is to draw a line outside the shape, instead of a filled shape :
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="-11dp"
android:left="-11dp"
android:right="-11dp"
android:top="-11dp">
<shape android:shape="rectangle">
<stroke
android:width="10dp"
android:color="#cacaca" />
<corners android:radius="22dp" />
</shape>
</item>
</layer-list>
This garanties that the rounded corners are on top of the selector, and the overscroll effect.
I suggest you should set the layout with corners to your ListView's first and last element, but not to the whole ListView.
You can create two extra .xml files in addition to your list_selector.xml: one for first element with the round corners at the top (lets call it list_selector_top.xml) and another one for the last element with the round corners at the bottom (list_selector_bottom.xml).
Then you can create your own MyAdapter extends ArrayAdapter and somewhere in getView() method set the appropriate background to your element's View (if position==0 set list_selector_top.xml, if position==getCount()-1 set list_selector_bottom.xml, by default set list_selector.xml).
It might be not the simpliest approach, but it works.
Set your corner radius to list view selector also using this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/list"
android:state_pressed="true"
>
<shape>
<solid android:color="#color/colorPrimary"/>
<corners
android:radius="10dp"/>
</shape>
</item>
</selector>
so your selection will not be cross boundry of list view.
cons - you will not able to define which radius you have to set as per top and bottom part that is why have to set radius to all sides.
hope this should help or at least give you some direction to resolve your issue.
If you can use FrameLayout as parent to the ListView, then set the cornered shape as foreground to the parent FrameLayout.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:foreground="#drawable/list_border">
<ListView
android:id="#+id/listView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:listSelector="#drawable/list_selector"/>
</FrameLayout>
If you want to use LinearLayout as parent of ListView, then surround your ListView with ForegroundLinearLayout and set the cornered shape as foreground to the ForegroundLinearLayout.
What is ForegroundLinearLayout? It is LinearLayout with Foreground property like FrameLayout. Get the code from Chris Banes. https://gist.github.com/chrisbanes/9091754
Your code should be:
<?xml version="1.0" encoding="utf-8"?>
<ForegroundLinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:foreground="#drawable/list_border">
<ListView
android:id="#+id/listView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:listSelector="#drawable/list_selector"/>
</ForegroundLinearLayout>
please add padding in your style.i updated style here.
2 list_border.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#fff"/>
<corners android:bottomRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp"/>
<padding android:left="10dp"
android:right="10dp"
android:top="10dp"
android:bottom="10dp"
/>
</shape>
This can be done easily by using Android's support library's CardView. Just wrap your ListView in a CardView layout and set a cardCornerRadius to the CardView, like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#cacaca">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_height="wrap_content"
android:layout_width="match_parent"
card_view:cardCornerRadius="10dp"
>
<ListView
android:id="#+id/listView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:listSelector="#drawable/list_selector"/>
</android.support.v7.widget.CardView>
</LinearLayout>
Dont forgot to import the CardView's support library:
compile 'com.android.support:cardview-v7:23.1.1'

Android: background of listview corners

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.

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 Background that doesn't scale

I need to create a gradient that is 230dp high (i.e., not the whole screen), with the rest of the screen a solid color. I can't figure out how to do this -- everything I try ends up with the gradient expanding to fill the whole screen. My current XML looks like this:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:top="230dp">
<shape android:shape="rectangle" >
<color android:color="#333333" />
</shape>
</item>
<item>
<shape android:shape="rectangle" >
<gradient
android:angle="270"
android:endColor="#333333"
android:startColor="#D9D9D9"
android:type="linear" />
<size android:height="230dp" />
</shape>
</item>
</layer-list>
I then apply that drawable as the background to my LinearLayout in XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#333333" >
<LinearLayout
android:id="#+id/container"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="#drawable/grey_gradient_bg" >
<!-- a bunch of content, buttons, etc. -->
</LinearLayout>
</LinearLayout>
But the gradient expands to fill the whole screen.
I have tried using a PNG for the background, but I get the banding problems described here, and the fixes haven't helped me yet.
Use a RelativeLayout instead of a LinearLayout
Instead of using your gradient as a background of the main container of the layout, add a child View, set the height explicitly to the size you want (230dp), and set the background of that view to your gradient.
If your project doesn't require API < 21 you may set the size of your gradient directly in your drawable, like that:
<item android:height="230dp">
<shape android:shape="rectangle" >
<gradient
android:angle="270"
android:endColor="#333333"
android:startColor="#D9D9D9"
android:type="linear" />
</shape>
</item>
Anyway, to support API < 21 without this behavior, you may add this drawable and then, another drawable with the same name in the drawable-v21, but it without android:height="230dp".
Setting height in gradient tag or size really doesn't work.
Something like this might help you :)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="fill_parent"
android:layout_height="320dip"
android:orientation="vertical"
android:background="#drawable/grey_gradient_bg">
</LinearLayout>
The problem you encountered was because your layout had fill_parent on height.
Hope this will help you.
Good luck,
Arkde
Here's my solution using the other answers:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#333333" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="230dp"
android:background="#drawable/grey_gradient_bg"
android:layout_alignParentTop="true" />
<LinearLayout
android:id="#+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_alignParentTop="true" >
<!-- all the content -->
</RelativeLayout>
</RelativeLayout>
grey_gradient_bg is now simple:
<?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:endColor="#333333"
android:startColor="#D9D9D9"
android:type="linear" />
</shape>

Adding rounded corners to a mapview

I'm trying to code a simple MapActivity which uses the following layout (only contains the mapview for the moment, as I plan to add share the screen with a ListView in the future) :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.android.maps.MapView
android:id="#+id/mapview"
android:layout_width="fill_parent"
android:layout_height="200dip"
android:enabled="true"
android:clickable="true"
android:apiKey="mykey"
android:layout_alignParentBottom="true" />
</RelativeLayout>
I have applied a simple shape to the mapview in order to have round corners and a stroke :
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:bottomRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp"/>
<stroke android:width="3dp"
android:color="#color/bordure_tables" />
</shape>
I use the following to apply the shape :
mapView.setBackgroundResource(R.layout.map);
Problem is that it does not have any effect and I can't see my rounded corners, nor the stroke.
Just wrote a tutorial to have rounded corners on a MapView it's done programmatically so should scale well to all devices:
http://www.anddev.org/novice-tutorials-f8/rounded-corners-map-view-t56908.html
Once you have added the custom view class you can just instantiate a rounded mapview like so:
<com.blundell.tut.ui.widget.RoundedMapView
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
Have you tried assigning it a padding?
<padding
android:left="2dp"
android:top="2dp"
android:right="2dp"
android:bottom="2dp" />
So your strokes and radius should be visible.
So I've achieved this affect by doing following: I've created a 9-patch drawable called round_corners and placed it in my drawable folder, then I use following xml to plot the map:
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">/
<com.google.android.maps.MapView
android:id="#+id/center_map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="your_key"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/round_corners"
></LinearLayout>
</FrameLayout>
I was able to achieve this creating a rounded drawable like this:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#android:color/transparent" />
<stroke android:width="10dp" android:color="#color/color_gray5_eeeeee" />
<corners android:radius="10px" />
</shape>
then using a linear layout and a margin of the same size of the drawable stroke width, like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background_gray_rectangle_rounded_corners"
android:orientation="vertical">
<com.google.android.gms.maps.MapView
android:id="#+id/map_details_map_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:clickable="true"
android:enabled="true" />
</LinearLayout>
Just put the Mapview into a CardView
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="100dp"
android:elevation="0dp"
app:cardCornerRadius="5dp">
<com.google.android.gms.maps.MapView
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v7.widget.CardView>

Categories

Resources