android how to set border in listview - android

This is my code which work fine, I just want to add border at top and bottom of listview. So, My question is how to add border at top and bottom of listview. Note: I just want to add only border and where I can place border? Any help will be appreciated. Thanks
<ListView android:id="#+id/listCategory"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fadeScrollbars="true"
android:fastScrollEnabled="true"
android:listSelector="#drawable/listview_selector"
android:dividerHeight="1dp"
android:layout_below="#+id/gridlayout"
android:visibility="gone"/>
listview_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true" android:drawable="#drawable/listview_selector_focussed" />
<item android:state_pressed="true" android:drawable="#drawable/listview_selector_pressed" />
</selector>
listview_selector_focussed.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient android:startColor="#0a89f1" android:endColor="#56768d" android:angle="90" />
</shape>
listview_selector_pressed.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient android:startColor="#0a89f1" android:endColor="#56768d" android:angle="90" />
</shape>

Add a header and footer, they will serve you as a border
In your Activity where you will declare your ListView;
View v=new View(this);
v.setLayoutParams(new LayoutParams(<width>,>height>));
v.setBackgroundColor(<your color>);
ListView list=(ListView)findViewById(R.id.listCategory);
list.addHeader(v);
list.addFooter(v);
In your layout file at your parent layout element
<YourParentLayoutElement
...
android:paddingTop="<your_value>"
android:paddingBottom="<your_value>"
>

Related

Selector drawable strange behavior: why does it get stuck in the middle of the screen?

I've tried several combinations of selectors, but the end result is the same. Even though the selector is displayed correctly, if I go up or down the selector stays in the middle of the screen. It's exactly this issue, but I couldn't make it work with this or any other solutions I've found.
Here I am selecting a row:
And then scrolling down:
My ListView:
<ListView
android:id="#+id/cardListView"
android:background="#android:color/black"
android:listSelector="#drawable/list_view_selector"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
My list_view_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="true"
android:drawable="#drawable/background_selected" />
<item
android:drawable="#drawable/background_selected" />
</selector>
And my background_selected
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="90"
android:centerColor="#ffffff"
android:endColor="#ffffff"
android:startColor="#ffffff" />
</shape>
Thanks in advance.

Selector for list view

I have a selector for different states of the list view. I have selected a grey colour for the background but its not working ,pressed state is working fine but my list view background is still white, and when I select the list item its colour changes to grey which I have chosen for the background and the background is still white.
Please help ,Sorry for the silly question as I am new to android.
My list view is:
<?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="match_parent"
android:background="?android:attr/activatedBackgroundIndicator">
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector">
</ListView>
</FrameLayout>
Selector xml is:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="false"
android:state_pressed="false"
android:drawable="#drawable/gradient_bg" />
<item android:state_pressed="true"
android:drawable="#drawable/gradient_bg_hover" />
<item android:state_selected="true"
android:state_pressed="false"
android:drawable="#drawable/gradient_bg_hover" />
</selector>
pressed_state xml is:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#78DDFF"
android:centerColor="#16cedb"
android:endColor="#09adb9"
android:angle="270" />
</shape>
background xml is:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#D5DDE0"
android:centerColor="#e7e7e8"
android:endColor="#CFCFCF"
android:angle="270" />
</shape>
Apply your selector to the ListView item, I mean the viewgroup of the row.xml or whatever name you have defined for the listview item.

Can't see the ListView divider

I added a divider to my listview but there is nothing there. The ListView stays comepletely the same as if there wasn't any divider set.
This is ListView XML:
<ListView
android:id="#+id/listview_language"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#drawable/shadow" />
#drawavble/shadow XML:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
>
<shape
>
<gradient android:startColor="#color/shadow_start"
android:endColor="#color/shadow_end"
android:angle="90"/>
<stroke
android:height="1dp"/>
</shape>
</item>
</selector>
Why won't the divider appear on ListView?
you should also add the android:dividerHeight property to your layout
Try to replace selector root element with layer-list in your shadow xml file.

Can't get ListView background gradient to show

I have a listview and I want to have a background gradient that covers the entire listview. I don't want a gradient for each row. All I get is a white background.
Code for gradient in mybg.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:startColor="#e6e6e6"
android:endColor="#ffffff"
android:angle="45" />
</shape>
</item>
</selector>
My ListView:
<ListView
android:id="#+id/lvEvents"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#drawable/mybg"
android:cacheColorHint="#00000000"
android:divider="#ffc0c0c0"
android:dividerHeight="1dp"
android:scrollingCache="true" />
Try set the row view background to be transparent by using #null.
You missed the shape , try this
<?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:centerColor="#000000"
android:endColor="#404040"
android:startColor="#404040" />
</shape>

Android gridview divider drawable between images

I have a gridview of images, i need to add one drawable image as a separator between images in gridview.
anyone help me how to add.?
try this hope help
list_item.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:orientation="horizontal"
android:background="#drawable/list_selector">
<!-- Cell contents -->
</LinearLayout>
list_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="true"
android:drawable="#drawable/item_border_selected"
/>
<item
android:state_pressed="true"
android:drawable="#drawable/item_border_selected"
/>
<item
android:drawable="#drawable/item_border"
/>
</selector>
item_border.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="#android:color/transparent"
/>
<stroke
android:width="1px"
android:color="#color/list_divider"
/>
</shape>
item_border_selected.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="#color/list_select"
/>
<stroke
android:width="1px"
android:color="#color/list_divider"
/>
</shape>
items_view.xml
<?xml version="1.0" encoding="utf-8"?>
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="-1px"
android:layout_marginRight="-1px"
android:listSelector="#android:color/transparent"
/>
Since all lines double in size as they join their neighboring cells, I made the divider size 1px instead of 1dp so it doesn't appear too large on some screens. Also, I made the grid view have negative margins to hide the lines on either side. I hope this helps someone.from here

Categories

Resources