Android/Spinner: Remove padding to the right of the arrow - android

I'm using a spinner in a relative layout with a couple of other layout elements (layout-v21):
<Spinner
android:id="#+id/spinner"
android:layout_width="155dp"
android:layout_height="34dp"
android:layout_alignBottom="#+id/textList"
android:layout_alignTop="#+id/textList"
android:layout_alignParentEnd="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="0dp"
android:layout_toEndOf="#+id/textList"
android:paddingRight="1dp"/>
This sets the distance to where the parent layout ends:
android:layout_marginRight="0dp"
android:layout_alignParentEnd="true"
This sets how close the choice items inside the spinner can get to the left of the arrow (right padding of the text):
android:paddingRight="1dp"
But is there a way to get rid of (or at least make smaller) the unnecessary space/padding that is to the right of the arrow but still within the spinner layout element (so I can then use more space on the left without changing the size of the spinner)?
This is what I'm talking about:
Edit: Here's the code for the RelativeLayout:
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.mycompany.myapp.MainActivity"
android:backgroundTint="#000000"
android:focusableInTouchMode="true">

It work for me :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:backgroundTint="#000000"
android:focusableInTouchMode="true">
<Spinner
android:id="#+id/spinner"
android:layout_width="155dp"
android:layout_height="34dp"
style="#style/spinner_style"
android:layout_alignParentEnd="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="0dp"
android:paddingRight="1dp"/>
styles.xml
<style name="spinner_style">
<item name="android:layout_height">35dp</item>
<item name="android:background">#drawable/spinner_bg</item>
</style>
spinner_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<layer-list>
<item>
<shape>
<gradient android:angle="90" android:endColor="#android:color/white" android:startColor="#android:color/white" android:type="linear" />
<stroke android:width="1dp" android:color="#color/colorAccent" />
<corners android:radius="0dp" />
<padding android:bottom="6dp" android:left="6dp" android:right="6dp" android:top="6dp" />
</shape>
</item>
<item>
<bitmap android:gravity="center|right" android:src="#drawable/ic_spin_down_arrow" />
</item>
</layer-list>
</item>
</selector>

Related

Rounding top and bottom corners of a listview

This is my issue I can't seem to round the edges on the bottom of my listview.
I've tried a couple of methods where you create a shape for the backgroup resource, tried it programmatic ally and in the layout xml's but it does not do anything at all.
Like I either did it wrong which I doubt since I am familiar with shapes, or something else interferes with my attempts.
Either way I need help is what I'm getting at.
Here's some of the xml's that make up what you're looking at.
This makes up the actual dialog. (settings_dialog.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#BB000000"
android:gravity="center">
<ImageView
style="#style/CloseButtonStyle"
android:id="#+id/ivCancel_SD"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_above="#+id/rlContainer_SD"
android:layout_toRightOf="#+id/rlContainer_SD"
android:layout_toEndOf="#+id/rlContainer_SD"/>
<RelativeLayout
style="#style/SettingsDialogStyle"
android:id="#+id/rlContainer_SD"
android:layout_width="800dp"
android:layout_height="600dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TextView
style="#style/SettingsHeaderStyle"
android:id="#+id/tvSettingsLabel_SD"
android:layout_width="330dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="#string/sd_header_label"/>
<ListView
style="#style/SettingsListStyle"
android:id="#+id/lvOptions_SD"
android:background="#drawable/bg_listview_corners"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tvSettingsLabel_SD"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
</RelativeLayout>
</RelativeLayout>
This is located in my styles.xml with a bunch of other stuff that has no relation to this.
<style name="SettingsListStyle">
<item name="android:divider">#color/table_divider_color</item>
<item name="android:dividerHeight">1dp</item>
<item name="android:paddingTop">40dp</item>
<item name="android:paddingLeft">6dp</item>
<item name="android:paddingRight">6dp</item>
<item name="android:paddingBottom">2dp</item>
</style>
And this makes up a list_item. (settings_list_item.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="80dp"
android:padding="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
style="#style/SettingsOptionNameStyle"
android:id="#+id/tvOptionName_SLI"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
style="#style/SettingsOptionDescStyle"
android:id="#+id/tvOptionDesc_SLI"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
</RelativeLayout>
And here is the shape I used to try and round the edges.
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/bg_listview_corners" android:shape="rectangle">
<corners android:radius="30dp" />
</shape>
I've also tried this piece for the shape.
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/bg_listview_corners" android:shape="rectangle">
<corners
android:bottomRightRadius="30dp"
android:bottomLeftRadius="30dp" />
</shape>
This is all the information I think you'll need to help me on my way.
In case I missed some information or you just have questions don't hesitate to ask.
Update 1:
I figured out it somehow only shapes the container of the listview but not the listview itself.
Any ideas?
you can increase the padding (paddingRight, paddingLeft, and paddingBottom)
<style name="SettingsListStyle">
<item name="android:divider">#color/table_divider_color</item>
<item name="android:dividerHeight">1dp</item>
<item name="android:paddingTop">40dp</item>
<item name="android:paddingLeft">7dp</item>
<item name="android:paddingRight">7dp</item>
<item name="android:paddingBottom">3dp</item>
or use mask background over the listview (like this Post which use 9-Patch as mask, or you can use your "bg_listview_corners.xml" background for the mask)
You can create a custom background drawable selector file called custom_list.xml in your drawable folder with this code in it
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/list_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/list_focused" android:state_focused="true"/>
<item android:drawable="#drawable/list_default"/>
</selector>
then create another file list_default.xml in your drawable folder with this code in it
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="270"
android:endColor="#1c95a0"
android:startColor="#1c95a0" />
<corners android:radius="5dip" />
</shape>
finally set the background property of the list view to
android:background="#drawable/custom_list"
Ended up using a mask as suggested by #Sally here is how I changed it.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#BB000000"
android:gravity="center">
<ImageView
style="#style/CloseButtonStyle"
android:id="#+id/ivCancel_SD"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_above="#+id/rlContainer_SD"
android:layout_toRightOf="#+id/rlContainer_SD"
android:layout_toEndOf="#+id/rlContainer_SD"/>
<RelativeLayout
style="#style/SettingsDialogStyle"
android:id="#+id/rlContainer_SD"
android:layout_width="800dp"
android:layout_height="600dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TextView
style="#style/SettingsHeaderStyle"
android:id="#+id/tvSettingsLabel_SD"
android:layout_width="330dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="#string/sd_header_label"/>
<ListView
style="#style/SettingsListStyle"
android:id="#+id/lvOptions_SD"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tvSettingsLabel_SD"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
</RelativeLayout>
<ImageView
android:layout_width="800dp"
android:layout_height="600dp"
android:layout_centerVertical="true"
android:background="#drawable/bg_listview_corners"/>
</RelativeLayout>
I added an ImageView on top of the relative layout matching the container.
This is the drawable I used for masking the layout.(bg_listview_corners.xml)
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" >
<stroke
android:width="12dp"
android:color="#color/app_night_sky_color" />
<padding android:left="6px"
android:top="6px"
android:right="6px"
android:bottom="6px"/>
<corners android:radius="30dp" />
</shape>
</item>
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke
android:width="6dp"
android:color="#color/popup_bg_color" />
<corners android:radius="30dp" />
</shape>
</item>
</layer-list>

Removing the left padding on an Android EditText

The default EditText background seems to put ~ 4dp of padding on the left. This causes misalignment with other widgets.
I made a simple app to demonstrate. Screenshot:
Layout:
<LinearLayout 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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:background="#00ff00"
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="HINT"
android:text="TEXT"
android:singleLine="true"/>
<TextView
android:background="#00ff00"
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Is there anyway to stop it from doing this?
One workaround is to use negative margin on the sides (-4dp)
This will only work when the EditText background matches the Layout background, and you don't have anything on the sides in the Layout that would be covered by the EditText
Did you try adding android:background="#android:color/transparent"?
I did the same example with this result
<LinearLayout 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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:background="#00ff00"
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="HINT"
android:text="TEXT"
android:background="#android:color/transparent"
android:singleLine="true"/>
<TextView
android:background="#00ff00"
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Second way with lines
Add an xml file in drawable 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="#android:color/transparent" /> <!--background color of box-->
</shape>
</item>
<item
android:top="-2dp"
android:right="-2dp"
android:left="-2dp">
<shape>
<solid android:color="#android:color/transparent" />
<stroke
android:width="1dp"
android:color="#000000" /> <!-- color of stroke -->
</shape>
</item>
</layer-list>
then in your layout add this in your EditText android:background="#drawable/edittext_background"
<LinearLayout 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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:background="#00ff00"
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="HINT"
android:text="TEXT"
android:background="#drawable/edittext_background"
android:singleLine="true"
android:layout_marginBottom="3dp"/>
<TextView
android:background="#00ff00"
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
and you will have this
Use android:layout_marginLeft="-4dp" in edit text field as follows.
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="HINT"
android:text="TEXT"
android:layout_marginLeft="-4dp"
android:singleLine="true"/>
This issue comes from the default value of ?android:attr/editTextBackground.
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetBottom="#dimen/abc_edit_text_inset_bottom_material"
android:insetLeft="#dimen/abc_edit_text_inset_horizontal_material"
android:insetRight="#dimen/abc_edit_text_inset_horizontal_material"
android:insetTop="#dimen/abc_edit_text_inset_top_material">
<selector>
<item android:state_enabled="false">
<nine-patch
android:alpha="?android:attr/disabledAlpha"
android:src="#drawable/abc_textfield_default_mtrl_alpha"
android:tint="?attr/colorControlNormal" />
</item>
<item
android:state_focused="false"
android:state_pressed="false">
<nine-patch
android:src="#drawable/abc_textfield_default_mtrl_alpha"
android:tint="?attr/colorControlNormal" />
</item>
<item>
<nine-patch
android:src="#drawable/abc_textfield_activated_mtrl_alpha"
android:tint="?attr/colorControlActivated" />
</item>
</selector>
</inset>
As you can see the inset value for all edges of EditText is abc_edit_text_inset_top_material = 4dp. That makes EditText has small padding around its content.
To remove that padding you should create a new EditText style with modified editTextBackground attribute. For example:
<style name="Widget.App.TextField" parent="#style/Widget.AppCompat.EditText">
<item name="colorControlActivated">#303E44</item>
<item name="colorControlHighlight">#E5E9EC</item>
<item name="colorControlNormal">#E5E9EC</item>
<item name="editTextBackground">#drawable/background_new_edit_text</item>
<item name="android:editTextBackground">#drawable/background_new_edit_text</item>
</style>
New background_edit_text.xml (Remember to put this file into correct drawable directory so it will not be overried such as drawable-v21...)
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:inset="#dimen/spacing_0dp">
<selector>
<item android:state_enabled="false">
<nine-patch
android:alpha="?android:attr/disabledAlpha"
android:src="#drawable/abc_textfield_default_mtrl_alpha"
android:tint="?attr/colorControlNormal" />
</item>
<item
android:state_focused="false"
android:state_pressed="false">
<nine-patch
android:src="#drawable/abc_textfield_default_mtrl_alpha"
android:tint="?attr/colorControlNormal" />
</item>
<item>
<nine-patch
android:src="#drawable/abc_textfield_activated_mtrl_alpha"
android:tint="?attr/colorControlActivated" />
</item>
</selector>
</inset>
Apply new style to your EditText:
<EditText
android:id="#+id/edt_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/Widget.App.TextField"
/>
Change background of EditText to custom, it have nine-patch padding
If you want to align only the text and not the line underneath, you can overwrite the padding programmatically.
So let's say this layout is for an activity. Once the activity has been created you can do something like:
findViewById(R.id.edit_text).setPaddding(
0,
old_top_paddding,
old_right_paddding,
old_bottom_padding);
The key is to re-set the padding once the background has been applied. You might need to watch for cases where the background is set again, so maybe one idea is to override the setBackground*() methods and re-set the padding there.

set space between rows in listview Android

I have a listView with custom rows and I need to set a custom divider between each row.
This is the code of the listView:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="#dimen/menu_padding"
android:background="#color/customBlue"
android:divider="#drawable/list_divider"
android:dividerHeight="2px" />
This is the code of the rows:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
tools:ignore="contentDescription"
android:background="#color/strongBlue" >
<ImageView
android:id="#+id/row_icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="10dp"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/row_title"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:padding="10dp"
android:textColor="#color/white"
android:textAppearance="#android:style/TextAppearance.Medium" />
</LinearLayout>
And this is the code for the 2 pixel divider:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:bottom="1px">
<shape android:shape="line">
<stroke
android:color="#0C0832"
android:width="2dp"
/>
<size
android:height="1px"
/>
</shape>
</item>
<item android:top="1px">
<shape android:shape="line">
<stroke
android:color="#2D2E57"
android:width="2dp"
/>
<size
android:height="1px"
/>
</shape>
</item>
</layer-list>
The problem is that I have a random space between each row that has the same color as the listView backgroundColor and if I set the dividerHeight to 4px I can see my divider. How can I remove the space that gets created automatically and only have my custom divider?
Use the following code in rows in linear layout
android:layout_marginTop="15dp"
Instead of pixel you can provide dp in xml
android:dividerHeight="10dp"
android:divider="#ffffff"
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="schemas.android.com/apk/res/android">;
<gradient
android:type="radial"
android:startColor="#343030"
android:endColor="#151515"
android:gradientRadius="300"
android:angle="270"
android:centerY="0.3"/>
</shape>
Instead of making custom drawable for divider you can directly give color to divider.

how to make card list in android

I was looking for some ListView, styling techniques and I found this one Post-How to make card List, I wanted the ListItems to be just like this :
list_item_background.xml is :
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle"
android:dither="true">
<corners android:radius="2dp"/>
<solid android:color="#ccc" />
</shape>
</item>
<item android:bottom="2dp">
<shape android:shape="rectangle"
android:dither="true">
<corners android:radius="2dp" />
<solid android:color="#android:color/white" />
<padding android:bottom="8dp"
android:left="8dp"
android:right="8dp"
android:top="8dp" />
</shape>
</item>
I have implemented that in my RowOfListView.xml like :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/ten"
android:background="#drawable/list_item_background" >
<ImageView
android:id="#+id/imageView_in_row"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/row_member_name"
android:layout_marginBottom="#dimen/ten"
android:layout_marginLeft="#dimen/five"
android:background="#drawable/rectangle"
android:contentDescription="#string/image"
android:src="#drawable/member" />
<TextView
android:id="#+id/row_member_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/ten"
android:layout_marginRight="#dimen/ten"
android:layout_marginTop="#dimen/ten"
android:layout_toRightOf="#+id/imageView_in_row"
android:ellipsize="end"
android:text="Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
.
.
.
</RelativeLayout>
But I'm getting this one :
unable to get that margin between Items of ListView as in prior picture, where I should set that margin.
You can try like this for listview row xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/outer"
style="#style/CardOuterStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/dialog_white"
android:orientation="vertical" >
//your fields here
</LinearLayout>
</FrameLayout>
and CardOuterStyle
<style name="CardOuterStyle">
<item name="android:paddingLeft">5dp</item>
<item name="android:paddingRight">5dp</item>
<item name="android:paddingTop">5dp</item>
</style>
You should set the dividerHeight of the ListView as :
<ListView android:id="#+id/MyListView"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:divider="#android:color/transparent"
android:dividerHeight="#dimen/ten"/>
Alternatively, you may also give padding in your relative layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/ten"
android:background="#drawable/list_item_background"
android:paddingTop="#dimen/five"
android:paddingBottom="#dimen/five" >

How to assign padding to Listview item divider line

How can i give padding to list item as i show in image.
I want to make the divider in the layout as shown in the image.
this is my list fragment code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp">
<ListView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="#+id/listV_main"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"/>
This is my List section code
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp">
<include
android:id="#+id/list_item_section_text"
layout="#android:layout/preference_category"
/>
This is my list item code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:adjustViewBounds="true"
android:paddingRight="?android:attr/scrollbarSize"
>
<ImageView
android:id="#+id/showlist_item_entry_drawable"
android:layout_width="82dp"
android:adjustViewBounds="true"
android:layout_height="68dp"
android:scaleType="fitXY"
android:paddingLeft="9dp"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginRight="6dip"
android:layout_marginTop="6dip"
android:layout_marginBottom="6dip"
android:layout_weight="1"
>
<TextView android:id="#+id/list_item_entry_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:ellipsize="marquee"
android:fadingEdge="horizontal" />
<TextView android:id="#+id/list_item_entry_summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/list_item_entry_title"
android:layout_alignLeft="#id/list_item_entry_title"
android:textAppearance="?android:attr/textAppearanceSmall"
android:singleLine="true"
android:textColor="?android:attr/textColorSecondary" />
</RelativeLayout>
Use 'inset'.....
(list_divider.xml)
<?xml version="1.0" encoding="UTF-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="50dp"
android:insetRight="50dp" >
<shape>
<solid android:color="#color/orange" />
<corners android:radius="2.0dip" />
</shape>
</inset>
and in your list view add like this...
<ListView
android:dividerHeight="2dp"
android:divider="#drawable/list_divider"
...
/>
you can set the inset value as desired...
UPDATE
As pointed out by #Giulio Piancastelli , If the background of list container is different from background of list item then you may use 'layer-list'...
(list_divider.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="#color/list_background" />
</shape>
</item>
<item
android:left="10dp"
android:right="10dp">
<shape android:shape="rectangle" >
<solid android:color="#color/divider_color"/>
</shape>
</item>
</layer-list>
and in your list view add like this...
<ListView
android:dividerHeight="2dp"
android:divider="#drawable/list_divider"
...
/>
You need padding for dividers? Create your custom drawable-shape as:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:left="10dp"
android:right="10dp">
<shape android:shape="rectangle" >
<solid android:color="#android:color/black" />
</shape>
</item>
</layer-list>
And set as divider for your ListView in xml:
<ListView
android:dividerHeight="2dp"
android:divider="#drawable/custom_divider"
...
/>
UPD
I just have ListView in xml:
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="2dp"
android:divider="#drawable/line"
/>
Divider as line.xml in drawable:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:left="10dp"
android:right="10dp">
<shape android:shape="rectangle" >
<solid android:color="#android:color/black" />
</shape>
</item>
</layer-list>
Do not make any modifications on the ListView in code. You can try to use Clean if resources are wrong builded.
As #Giulio Piancastelli mentioned under #ASP answer, the <inset> fails when the background colour of the list container is not the same as the rows inside the list. One solution is to use <layer-list> like below:
//This item is to overlay the container colour with the row background colour
<item
android:left="0dp"
android:right="0dp">
<shape android:shape="rectangle" >
<solid android:color="#color/row_background" />
</shape>
</item>
//This item is the divider, put it at bottom so it will overlay the background colour
<item
android:left="92dp"
android:right="0dp">
<shape android:shape="rectangle" >
<solid android:color="#color/divider
</shape>
</item>
Then you can set it as divider as most answers suggest:
<ListView
android:dividerHeight="2dp"
android:divider="#drawable/layer_list"
...
/>
In your listitem give the paddingtop to the main layout as
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:padding="10dp"
....// Remaining fields
>
... //Remaining items
</LinearLayout>
this should be alternative solution for this question:
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:background="color/secondary_text"
android:layout_marginTop="16dp" />
I don't know why people are adding shape to the layered-list but for me this works just fine.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="10dp" android:height="2dp" android:drawable="#android:color/holo_blue_dark"/>
<item android:height="22dp" android:drawable="#android:color/transparent"/>
</layer-list>
1st item will be 10dp from the top and with a height of 2dp and a color of holo_blue_dark. (This will be the divider)
Now to give 10dp margin to bottom we need to create another transparent item with a height of 22dp (10dp (top margin) + 2dp (height of divider) + 10dp(bottom margin from divider)).
This is working fine for me. I hope this will work for you too.
set your list item divider height and color:
<ListView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="#+id/listV_main"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:divider="#android:color/darker_gray"
android:dividerHeight="1dp"/>

Categories

Resources