What is divider in android

How can a divider line be added in an Android RecyclerView?

I am developing an android application where I am using RecyclerView . I need to add a divider in RecyclerView . I tried to add —

below is my xml code —

24 Answers 24

In the October 2016 update, the support library v25.0.0 now has a default implementation of basic horizontal and vertical dividers available!

The Right way is to define ItemDecoration for the RecyclerView is as following

SimpleDividerItemDecoration.java

Or If you are using kotlin: SimpleDividerItemDecoration.kt

line_divider.xml:

Finally set it like this

As pointed by @Alan Solitar

is depreciated instead of that you can use

If you want to have both horizontal and vertical dividers:

Define horizontal & vertical divider drawables:

horizontal_divider.xml

vertical_divider.xml

Add this code segment below:

All of these answers got me close but they were each missing a key detail. After a bit of research, I found the easiest route to be a combination of these 3 steps:

  1. Use the support library’s DividerItemDecoration
  2. Create a divider with the right color
  3. Set this divider in your theme as the listDivider

Step 1: while configuring RecyclerView

Step 2: in a file like res/drawable/divider_gray.xml

Step 3: in the app’s theme

EDIT: Updated to skip last divider:
After using this a bit, I realized it was drawing a divider after the last item, which was annoying. So I modified Step 1 as follows to override that default behavior in DividerItemDecoration (of course, making a separate class is another option):

Источник

How to add (vertical) divider to a horizontal LinearLayout?

I’m trying to add a divider to a horizontal linear layout but am getting nowhere. The divider just doesn’t show. I am a total newbie with Android.

This is my layout XML:

11 Answers 11

use this for horizontal divider

and this for vertical divider

OR if you can use the LinearLayout divider, for horizontal divider

and in LinearLayout

If you want to user vertical divider then in place of android:height=»1dp» in shape use android:width=»1dp»

Tip: Don’t forget the android:showDividers item.

Try this, create a divider in the res/drawable folder:

And use the divider attribute in LinearLayout like this:

Note: android:divider is only available in Android 3.0 (API level 11) or higher.

It is easy to add divider to layout, we don’t need a separate view.

Above code make vertical divider for LinearLayout

Update: pre-Honeycomb using AppCompat

If you are using the AppCompat library v7 you may want to use the LinearLayoutCompat view. Using this approach you can use drawable dividers on Android 2.1, 2.2 and 2.3.

Example code:

drawable/divider.xml: (divider with some padding on the top and bottom)

Читайте также:  Мен оф вар для андроид

Very important note: The LinearLayoutCompat view does not extend LinearLayout and therefor you should not use the android:showDividers or android:divider properties but the custom ones: app:showDividers and app:divider . In code you should also use the LinearLayoutCompat.LayoutParams not the LinearLayout.LayoutParams !

I just ran into the same problem today. As the previous answers indicate, the problem stems from the use of a color in the divider tag, rather than a drawable. However, instead of writing my own drawable xml, I prefer to use themed attributes as much as possible. You can use the android:attr/dividerHorizontal and android:attr/dividerVertical to get a predefined drawable instead:

The attributes are available in API 11 and above.

Also, as mentioned by bocekm in his answer, the dividerPadding property does NOT add extra padding on either side of a vertical divider, as one might assume. Instead it defines top and bottom padding and thus may truncate the divider if it’s too large.

You can use the built in divider, this will work for both orientations.

Frustratingly, you have to enable showing the dividers from code in your activity. For example:

Your divider may not be showing due to too large dividerPadding. You set 22dip, that means the divider is truncated by 22dip from top and by 22dip from bottom. If your layout height is less than or equal 44dip then no divider is visible.

If the answer of Kapil Vats is not working try something like this:

drawable/divider_horizontal_green_22.xml

layout/your_layout.xml

I encountered an issue where the padding attribute wasn’t working, thus I had to set the height of the divider directly in the divider.

Note:

If you want to use it in vertical LinearLayout, make a new one, like this: drawable/divider_vertical_green_22.xml

Источник

How to create a simple divider in the new NavigationView?

Google introduced the NavigationView in the Design Support Library version 22.2.0 with which you can create a drawer very easily using a menu resource.

How can I create a simple divider line between two items? Grouping the items didn’t work. Creating a sub items section does create a divider line, but it requires a title, which I don’t want.

Any help would be appreciated.

14 Answers 14

All you need to do is define a group with an unique ID, I have checked the implementation if group has different id’s it will create a divider.

Example menu, creating the separator:

create a drawable drawer_item_bg.xml like this,

and add it to NavigationView as app:itemBackground=»@drawable/drawer_item_bg»

and here we go.

You can add dividers easily by setting Menu Item Background via XML using app:itemBackground

And use LayerDrawable as the background. It preserves material design ripple overlay for clicks.

Simple add DeviderItemDecoration :

It look like this :

I think I have an even better solution for the problem of multiple checked items. Using my way you don’t have to worry about changing your code when adding new sections to your menu. My menu looks just like the accepted solution written by Jared, with the difference of using andoid:checkableBehavior=»all» on the groups:

Читайте также:  Битвы с боссами андроид

The checkableBehavior of ‘all’ makes it possible to check/uncheck single items at will, independently from which group they belong to. You just have to store the last checked menuitem. The java-code looks like this:

I’m not sure if this is fixed in API 26 (Android 8) or it was possible all the time. I’m still a noob in Android programming. However I added parent groups as below. Testing on Android 6 physical phone,

  1. I have the divider
  2. Selecting any item from either nav_g1 or nav_g2 will deselect other items.
  3. No extra padding added due to nested groups.
  4. I didn’t add any Java code to listeners

Menu Code

Notes

  1. As mentioned in this answer each group need to have a unique Id to show the divider.
  2. As per this answer spaces are matching the google material design specs.
  3. Having android:checkableBehavior=»single» for parent group is required so the problem of multiple selected menu items in this answer (mentioned in comments by hungryghost) does not happen

Источник

Android Drawing Separator/Divider Line in Layout?

I would like to draw a line right in the middle of a layout and use it as a separator of other items like TextView. Is there a good widget for this. I don’t really want to use an image as it would be hard to match the other components to it. And I want it to be relatively positioned as well. Thanks

30 Answers 30

I usually use this code to add horizontal line:

To add vertical separator, switch the layout_width and layout_height values

To improve on the answers provided by Alex Kucherenko and Dan Dar3

I added this to my styles:

Then in my layouts is less code and simpler to read.

Add this in your layout where you want the divider (modify the attributes to fit your need):

You can use this in LinearLayout :

Easiest Way:

Vertical divider :

Horizontal divider :

That’s all yes!

Just put this in res>values>styles.xml

use this code. It will help

Between two Layouts Put this code to get Divider.

Just write this :

if you use actionBarSherlock, you can use the com.actionbarsherlock.internal.widget.IcsLinearLayout class in order to support dividers and show them between the views .

example of usage:

do note that for some reason, the preview in the graphical designer says «android.graphics.bitmap_delegate.nativeRecycle(I)Z» . not sure what it means, but it can be ignored as it works fine on both new versions of android and old ones (tested on android 4.2 and 2.3) .

seems the error is only shown when using API17 for the graphical designer.

Adding this view; that draws a separator between your textviews

Its very simple. Just create a View with the black background color.

This will create a horizontal line with background color. You can also add other attributes such as margins, paddings etc just like any other view.

You can use this element just after the First TextView.

Here is your answer..this is an example to draw line between controls.

Читайте также:  Everlasting summer wallpaper android

This code draw line between two controls.

It adds a horizontal divider to anywhere in your layout.

use this xml code to add vertical line

use this xml code to add horizontal line

In cases where one is using android:layout_weight property to assign available screen space to layout components, for instance

To add a separator between the existing two layouts which has taken the entire screen space already, we cannot just add another LinearLayout with android:weight:»1″ because that will make three equal width columns which we don’t want. Instead, we will decrease the amount of space we will be giving to this new layout. Final code would look like this:

If you are going to use it a lot, best thing to do is

Источник

Android ListView Divider

I have this code:

where @drawable/list_divider is:

but I can’t see any divider.

12 Answers 12

Folks, here’s why you should use 1px instead of 1dp or 1dip: if you specify 1dp or 1dip, Android will scale that down. On a 120dpi device, that becomes something like 0.75px translated, which rounds to 0. On some devices, that translates to 2-3 pixels, and it usually looks ugly or sloppy

For dividers, 1px is the correct height if you want a 1 pixel divider and is one of the exceptions for the «everything should be dip» rule. It’ll be 1 pixel on all screens. Plus, 1px usually looks better on hdpi and above screens

«It’s not 2012 anymore» edit: you may have to switch over to dp/dip starting at a certain screen density

This is a workaround, but works for me:

Created res/drawable/divider.xml as follows:

And in styles.xml for listview item, I added the following lines:

Crucial part was to include this 1px setting. Of course, drawable uses gradient (with 1px) and that’s not the optimal solution. I tried using stroke but didn’t get it to work. (You don’t seem to use styles, so just add android:dividerHeight=»1px» attribute for the ListView.

Add android:dividerHeight=»1px» and it will work:

The problem you’re having stems from the fact that you’re missing android:dividerHeight, which you need, and the fact that you’re trying to specify a line weight in your drawable, which you can’t do with dividers for some odd reason. Essentially to get your example to work you could do something like the following:

Create your drawable as either a rectangle or a line, either works you just can’t try to set any dimensions on it, so either:

Then create a custom style (just a preference but I like to be able to reuse stuff)

Finally declare your list view using the custom style:

I’m assuming you know how to use these snippets, if not let me know. Basically the answer to your question is that you can’t set the divider thickness in the drawable, you have to leave the width undefined there and use android:dividerHeight to set it instead.

Источник

Оцените статью