I want to create a listView of cards, but after reading this blog post goolge-plus-layout, I'm cards are a viable solution for a list of anything. The animation part seems too memory intensive to load say a listview with more than 40 elements simultaneously.
Is there a better way to achieve a cards UI in listView?
You can create a custom drawable, and apply that to each of your listview elements.
I use this one for cards in my UI. I don't think there is significant performance issues with this approach.
The drawable code (in drawable\big_card.xml) looks like this:
<?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="#color/second_grey" />
</shape>
</item>
<item
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp">
<shape android:shape="rectangle" >
<corners android:radius="3dp" />
<solid android:color="#color/card_shadow" />
</shape>
</item>
<item
android:bottom="12dp"
android:left="10dp"
android:right="10dp"
android:top="10dp">
<shape android:shape="rectangle" >
<corners android:radius="3dp" />
<solid android:color="#color/card_white" />
</shape>
</item>
</layer-list>
I apply the background to my listview elements like this:
<ListView
android:id="#+id/apps_fragment_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:listSelector="#drawable/big_card" />
If you want to add this as a background to any View (not just a list), you just make the custom drawable that View's background element:
<TextView
android:id="#+id/any_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/big_card" />
EDIT: Google provides a nice class called CardView. I didn't check it but it looks promising.
Here's the previous way, which also works fine (that's what I wrote before the edit) :
There is a nice tutorial here and a nice sample of it here .
in short , these are the files you can create:
listView definition:
android:divider="#null"
android:dividerHeight="10dp"
android:listSelector="#android:color/transparent"
android:cacheColorHint="#android:color/transparent"
android:headerDividersEnabled="true"
android:footerDividersEnabled="true"
also this:
m_list.addHeaderView(new View(this));
m_list.addFooterView(new View(this));
res/drawable/selector_card_background.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="#drawable/layer_card_background_selected" />
<item android:drawable="#drawable/layer_card_background" />
</selector>
listView item :
res/layout/list_item_card.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:descendantFocusability="beforeDescendants">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="15dp"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:paddingRight="15dp"
android:background="#drawable/selector_card_background"
android:descendantFocusability="afterDescendants">
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</FrameLayout>
res/drawable/layer_card_background_selected.xml
<?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="#CABBBBBB"/>
<corners android:radius="2dp" />
</shape>
</item>
<item
android:left="0dp"
android:right="0dp"
android:top="0dp"
android:bottom="2dp">
<shape android:shape="rectangle">
<solid android:color="#CCCCCC"/>
<corners android:radius="2dp" />
</shape>
</item>
</layer-list>
res/drawable/layer_card_background.xml
<?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="#CABBBBBB"/>
<corners android:radius="2dp" />
</shape>
</item>
<item
android:left="0dp"
android:right="0dp"
android:top="0dp"
android:bottom="2dp">
<shape android:shape="rectangle">
<solid android:color="#android:color/white"/>
<corners android:radius="2dp" />
</shape>
</item>
</layer-list>
If all you want is a ListView that simulates the cards-look you can use a 9-patch as background for your listitems to make them look like cards. You can find a 9-patch and some more tips and explanation here: http://www.tiemenschut.com/simply-get-cards-ui-look/
Add divider for the Listview item and padding :
android:divider="#android:color/transparent"
android:dividerHeight="1dip"
Add RelativeLayout into your LinearLayout for ListItem with some desired padding :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:orientation="vertical"
android:padding="3dp" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/rowshadow" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
...
Add Background to the listview item , like :
<?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="#android:color/darker_gray" />
<corners android:radius="0dp"/>
</shape>
</item>
<item android:right="1dp" android:left="1dp" android:bottom="2dp">
<shape
android:shape="rectangle">
<solid android:color="#android:color/white"/>
<corners android:radius="0dp"/>
</shape>
</item>
</layer-list>
Use https://github.com/elcrion/demo.cardlistview as an example. It is somehow close to google style
As "android developer" briefly mentions in his answer, the CardView class can be used to easily create card views.
Just wrap you UI widgets in a CardView element and you are ready to go. See the short introduction to the CardView widget at https://developer.android.com/training/material/lists-cards.html#CardView.
The CardView class requires a v7 support library, remember to add the dependencies to your .gradle file!
compile 'com.android.support:cardview-v7:21.0.+'
Related
This is Textview :
<TextView
android:id="#+id/text_naer_me"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text=“Hello”
android:background="#drawable/rounded_white_border"
/>
This background resource
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="1dp"
android:color="#color/light_gray" />
<solid android:color="#color/white" />
<corners android:radius="2dp" />
</shape>
current view is below
enter image description here
expected view is below
enter image description here
please suggest me how to achieve this i want make textview as card view you can see my given screen
There are lots of ways and here they are Use that which one suits you
Create your own drawable
border.xml
<?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="#android:color/darker_gray" />
<corners android:radius="5dp"/>
</shape>
</item>
<item android:right="1dp" android:left="1dp" android:bottom="2dp">
<shape
android:shape="rectangle">
<solid android:color="#android:color/white"/>
<corners android:radius="5dp"/>
</shape>
</item>
</layer-list>
and your_layout.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="10dp"
android:background="#drawable/border"
/>
You can also use use a drawable from android
android:background="#android:drawable/toast_frame"
or:
android:background="#android:drawable/dialog_frame"
or:
android:background="#android:drawable/dialog_holo_light_frame"
Use a 9-patch image with a shadow and set it as the background to your
Linear layout
Use this website to create 9 patch with shadow
http://inloop.github.io/shadow4android/
Use android:elevation="20dp" and background same effect
like
cardview without wrap cardview
<TextView
android:layout_margin="10dp"
android:elevation="20dp"
android:padding="20dp"
android:background="#drawable/background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
/>
What I'm trying to do is to have a relativelayout with a gradient on it and a gray bottom border, I'm using the below custom view in my application:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list_item_container"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/list_item_image"
android:layout_width="match_parent"
android:layout_height="#dimen/SIZE_LIST_ITEM_LARGE"
android:scaleType="centerCrop"/>
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/shape_list_item_gradient" />
<com.rahil.widgets.CustomTextView
android:id="#+id/list_item_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:gravity="bottom"
android:textColor="#color/COLOR_BLACK"/>
</RelativeLayout>
and this is shape_list_item_gradient:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<gradient
android:startColor="#FFFFFFFF"
android:endColor="#00FFFFFF"
android:angle="90" />
</shape>
</item>
<item android:bottom="2dp">
<shape android:shape="rectangle">
<stroke android:width="2dp" android:color="#color/COLOR_GRAY" />
</shape>
</item>
</layer-list>
but the above code applies border to all borders not just bottom border. I added the separate shape xml to the relativelayout but it didn't work which I assume the view with gradient covers it maybe?? I'm quiet lost. thanks for your help.
The android:bottom attribute is actually the "bottom offset in pixels" as noted here about layer-list drawables. There is no explicit way to create a rectangle that is stroked only on one side.
However you can use the same trick that Krylez suggested here: https://stackoverflow.com/a/19239478/1538674
So something like this:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<gradient
android:startColor="#FFFFFFFF"
android:endColor="#00FFFFFF"
android:angle="90" />
</shape>
</item>
<item android:top="-3dp" android:right="-3dp" android:left="-3dp">
<shape>
<solid android:color="#android:color/transparent" />
<stroke android:width="2dp" android:color="#color/COLOR_GRAY" />
</shape>
</item>
</layer-list>
I want to create the shape of autocomplete textview shown in the figure and text should come in center of it.
Currently I am trying to implement it as
<item>
<shape android:shape="rectangle" >
<corners android:radius="15dip" />
<solid android:color="#color/blue_text" />
<padding
android:bottom="5dip"
android:left="15dip"
android:right="15dip"
android:top="5dip" />
</shape>
</item>
<AutoCompleteTextView
android:id="#+id/autoComp"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:background="#drawable/bg_autocompletetext"
android:textColor="#color/white"
</AutoCompleteTextView>
But it would just make the corners of the textview round. It does make it completely round. Please specify any solution for it.
try this code of shape
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners
android:radius="100dp"/>
<solid
android:color="#2137FF"/>
<size
android:width="250dp"
android:height="60dp"/>
</shape>
try this tool
Its better to use 9 patch image as a background of your TextView.
With using 9 patch image you can set proper resolution for different resolution device.And it will give you better result .
You write a seperate xml layout(shape.xml) having code:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#DCDCDC" />
<stroke android:width="2.5dp"
android:color="#DCDCDC" />
<corners
android:radius="10dp"/>
</shape>
Then in your main layout add:
<AutoCompleteTextView
android:id="#+id/autoComp"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:background="#drawable/shape"
android:textColor="#color/white"
</AutoCompleteTextView>
Hope this help.
Try this, working for rounded text view
<?xml version="1.0" encoding="utf-8"?>
<!-- res/drawable/rounded_textview.xml -->
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#284A89" />
<corners
android:bottomLeftRadius="30dp"
android:bottomRightRadius="30dp"
android:topLeftRadius="30dp"
android:topRightRadius="30dp" />
</shape>
Add Following file in drawables "searchtextbox_border.xml"
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners
android:radius="100dp"/>
<stroke android:color="#color/light_grey"
android:width="1dp">
</stroke>
</shape>
Then Add it as background in your AutocompleteTextview as follows:
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="32dp"
android:padding="#dimen/padding_10dp"
android:background="#drawable/searchtextbox_border"
android:hint="Search" />
I want to know how to add a shadow layer to any general View in android. for eg: suppose i have a layout xml, showing something like this..
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
<Button....
...
</LinearLayout>
Now when it is displayed I want to have a shadow around it.
The best way to create a shadow is to use a 9patch image as the
background of the view (or a ViewGroup that wraps the view).
The first step is to create a png image with a shadow around it. I
used photoshop to create such an image. Its really simple.
Create a new image with Photoshop.
Add a layer and create a black square of 4x4.
Create a shadow on the layer by selecting the layer in layer
explorer and clicking on a button titled fx and choosing drop shadow.
Export the image as png.
The next step is to create 9-patch drawables from this image.
Open draw9patch from android-sdk/tools
Open the image in draw9patch
Create 4 black lines on the four sides of the square like the
following and then save the image as shadow.9.png.
Now you can add this shadow as the background of the views you want to
add the shadow to. Add shadow.9.png to res/drawables. Now add it
as a background:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/shadow"
android:paddingBottom="5px"
android:paddingLeft="6px"
android:paddingRight="5px"
android:paddingTop="5px"
>
I recently wrote a blog post that explains this in detail and
includes the 9patch image that I use for creating the shadow.
Assuming u would use a linear layout(i have considered a vertical linear layout)..and have a view just below your linear layout.Now for this view provide a start colour and end colour..
I also wanted to get this thing,its working for me..If you need a even better effect,then just work around the start and end colour.
activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/vertical"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#drawable/layout_back_bgn"
android:orientation="vertical" >
</LinearLayout>
<View
android:layout_below="#+id/vertical"
android:layout_width="match_parent"
android:layout_height="10dp"
android:background="#drawable/shadow"
>
</View>
</LinearLayout>
layout_back_bgn.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="#FF4500" />
</shape>
shadow.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient
android:startColor="#4D4D4D"
android:endColor="#E6E6E6"
android:angle="270"
>
</gradient>
</shape>
I tried to post an image which i have it after using the above code,but stackoverflow doesnot allow me coz i dont have reputation..Sorry about that.
You can use elevation, available since API level 21
The elevation of a view, represented by the Z property, determines the
visual appearance of its shadow: views with higher Z values cast
larger, softer shadows. Views with higher Z values occlude views with
lower Z values; however, the Z value of a view does not affect the
view's size. To set the elevation of a view:
in a layout definition, use the
android:elevation
attribute. To set the elevation of a view in the code of an activity,
use the
View.setElevation()
method.
Source
Here's my cheesy version of the solution...This is the modification of the solution found
here
I didn't like how the corners look so I faded all of them...
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!--Layer 0-->
<!--Layer 1-->
<!--Layer 2-->
<!--Layer 3-->
<!--Layer 4 (content background)-->
<!-- dropshadow -->
<item>
<shape>
<gradient
android:startColor="#color/white"
android:endColor="#color/white"
android:centerColor="#10CCCCCC"
android:angle="180"/>
<padding android:top="0dp" android:right="0dp" android:bottom="2dp" android:left="0dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#color/white"
android:endColor="#color/white"
android:centerColor="#20CCCCCC"
android:angle="180"/>
<padding android:top="0dp" android:right="0dp" android:bottom="2dp" android:left="0dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#color/white"
android:endColor="#color/white"
android:centerColor="#30CCCCCC"
android:angle="180"/>
<padding android:top="0dp" android:right="0dp" android:bottom="2dp" android:left="0dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#color/white"
android:endColor="#color/white"
android:centerColor="#40CCCCCC"
android:angle="180"/>
<padding android:top="0dp" android:right="0dp" android:bottom="2dp" android:left="0dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#color/white"
android:endColor="#color/white"
android:centerColor="#50CCCCCC"
android:angle="180"/>
<padding android:top="0dp" android:right="0dp" android:bottom="2dp" android:left="0dp" />
</shape>
</item>
<!-- content background -->
<item>
<shape>
<solid android:color="#color/PostIt_yellow" />
</shape>
</item>
There are a simple trick, using two views that form the shadow.
<?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:padding="10dp"
android:background="#CC55CC">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="0">
<TableRow>
<LinearLayout
android:id="#+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:text="#string/hello" />
</LinearLayout>
<View
android:layout_width="5dp"
android:layout_height="fill_parent"
android:layout_marginTop="5dp"
android:background="#55000000"/>
</TableRow>
</TableLayout>
<View
android:layout_width="fill_parent"
android:layout_height="5dp"
android:layout_marginLeft="5dp"
android:background="#55000000"/>
</LinearLayout>
</FrameLayout>
Hope this help.
Create card_background.xml in the res/drawable folder with the following code:
<?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="#BDBDBD"/>
<corners android:radius="5dp"/>
</shape>
</item>
<item
android:left="0dp"
android:right="0dp"
android:top="0dp"
android:bottom="2dp">
<shape android:shape="rectangle">
<solid android:color="#ffffff"/>
<corners android:radius="5dp"/>
</shape>
</item>
</layer-list>
Then add the following code to the element to which you want the card layout
android:background="#drawable/card_background"
the following line defines the color of the shadow for the card
<solid android:color="#BDBDBD"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#color/dropShadow" />
Use Just Below the LinearLayout
Another Method
create "rounded_corner_bg.xml" in /drawable folder
<?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="#color/primaryColor" />
<corners android:radius="4dp" />
</shape>
</item>
<item
android:bottom="2dp"
android:left="0dp"
android:right="0dp"
android:top="0dp">
<shape android:shape="rectangle">
<solid android:color="#F7F7F7" />
<corners android:radius="4dp" />
</shape>
</item>
</layer-list>
To use this Layout android:background="#drawable/rounded_corner_bg"
Given I have a background drawable to create bulletpoints for TextViews like this:
Then my XML code looks like this:
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:right="235dp">
<shape android:shape="oval">
<padding android:left="10dp" />
<size android:height="5dp" android:width="5dp"/>
<solid android:color="#color/my_pink"/>
</shape>
</item>
<item android:left="10dp">
<shape android:shape="rectangle">
<solid android:color="#ffffff"/>
<padding android:left="10dp" />
</shape>
</item>
</layer-list>
But once I use the code shown above, my bullet points look like this:
It seems the <size> tag is ignored completely.
How would you solve this problem? Using a 9patch, yes I know.. perhaps that's the easiest to do.. but in fact I was hoping to find a XML solution as it's more flexible in the future.
Custom drawing is also out of the question.
<size> tag certainly works in layer-list
and to show the bullet points for textviews you can use the xml attribute android:drawableLeft
-> link
With this approach, there is no need of 9-patch and custom drawing.
Posting code & screenshot here for the reference.
res/drawable/bullet_point_layer.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:right="3dp">
<shape android:shape="oval">
<padding android:left="10dp" />
<size android:height="10dp" android:width="10dp"/>
<solid android:color="#ff0080"/>
</shape>
</item>
res/layout/main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView one"
android:textColor="#android:color/black"
android:drawableLeft="#drawable/bullet_point_layer"
android:background="#android:color/white" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView Two"
android:textColor="#android:color/black"
android:drawableLeft="#drawable/bullet_point_layer"
android:background="#android:color/white" />
</LinearLayout>
How it looks
This is an old post but I thought I would add something very important which is missing on this post.
From my experience the last item of the <layer-list> will be the item which the size value is taken from.
So in this example I create a two line divider line. I specify an empty item at the end to specify the divider drawable height.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle">
<solid android:color="#535353" />
</shape>
</item>
<item android:top="#dimen/common_divider_line_half_size">
<shape android:shape="rectangle">
<solid android:color="#737373" />
</shape>
</item>
<!-- Last item empty to specify divider height -->
<item>
<shape android:shape="rectangle">
<size android:height="#dimen/common_divider_line_size"/>
<solid android:color="#android:color/transparent" />
</shape>
</item>
</layer-list>