Add padding for each cell in RecyclerView - android

I have a RecyclerView with a background color of gray, in the RecyclerView, I populate it with 3 different types of cells, with background color white.
I want to add padding to the left and right for all cells.
If I add the left and right padding to the RecyclerView however, it makes the background look funky and it shows gray bars since the padding is for RecyclerView rather than the cells.
Is there any way to do it without adding padding to each type of cell?

Yes adding the padding to each cell is certainly ok.
If you want it to be easily maintainable and for all values to be the same. Add a padding resource to dimens.xml
<dimen name="recycler_view_h_padding">16dp</dimen>
And then apply the padding to each cell with the reference
android:paddingStart="#dimen/reycler_view_h_padding"
android:paddingEnd="#dimen/recycler_view_h_padding"

Related

android GridLayout divider row

I'm trying to implement a tabular layout that has a header and a bunch of rows underneath it. I've chosen the GridLayout (android.support.v7.widget.GridLayout) as there's some requirements for some elements to span multiple columns (but those are of no concern to the question).
My header cells each contain a LinearLayout with a bunch of TextViews, they're dynamically filled in code,for the sake of example, have a look at the image below.
The second row should contain the divider which is a simple view, that should span my header columns (3).
The problem is the width of the divider - if I choose MATCH_PARENT, it will push the GridLayout to fill the whole remaining space to the right. The grid needs to wrap the content and center itself horizontally. It seems to me there's a conflict between the grid's layout (WRAP_CONTENT) and the divider's layout (MATCH_PARENT).
How can I fix the width of the divider without hardcoding it?
http://i61.tinypic.com/2415eg5.png
In red, my LinearLayouts (header), green, the GridLayout itself, the thin blue line at the bottom is the divider.
Thanks,
MO
SOLUTION (as provided below):
I had to set the column weight for the divider to 1, without specifying a width (actually setting it to zero). Because of my specific requirement to handle all of these in code, the solution was to manually instantiate the GridLayout.LayoutParams class and use
ColumnSpec = GridLayout.InvokeSpec(row_index, num_spanned_cols, weight)
Hope this helps others in the future.
If you set the width to 0dp then give it android:layout_weight="1"
(you could give it any weight you want) it should fill all the available space and not push your bounds if I understand what you are asking correctly

Android beginner difference between padding and margin [duplicate]

This question already has answers here:
Difference between a View's Padding and Margin
(15 answers)
Closed 4 years ago.
I have referred questions on SO. ALso checked an answer:
Padding is the space inside the border, between the border and the actual view's content. Note that padding goes completely around the content: there is padding on the top, bottom, right and left sides (which can be independent).
Margins are the spaces outside the border, between the border and the other elements next to this view. In the image, the margin is the grey area outside the entire object. Note that, like the padding, the margin goes completely around the content: there are margins on the top, bottom, right, and left sides.
Also, more on padding and margins from:
http://developer.android.com/reference/android/view/View.html
http://developer.android.com/reference/android/view/ViewGroup.MarginLayoutParams.html
But what is the difference fundamentally between padding and margins ? Would the behaviour differ depending on O.S. and devices?
I have a normal, simple layout. No problem with code, have used layout folders - layout and layout-sw600dp plus drawables-4dpi. Can't make layout without margin or padding, which one is more appropriate?
Margin
Margins make up the vertical and horizontal areas between elements. If elements have no margins around them, they will bump right up against each other. In other words, he space outside of, or between, elements is what comprises the margin areas.
Padding
The padding of an element is the horizontal and vertical space that’s set around the content area of the targeted element. So padding is on the inside of a box, not the outside.
Padding is for inside/within components. Eg. TextView , Button, EditText etc.
Eg. space between the Text and Border
Margin is to be applied for the on-outside of the components.
Eg. space between left edge of the screen and border of your component
Visual representation is great in : Difference between a View's Padding and Margin
With Padding, i have seen a difference in 2.2, 2.3 and say 4.3, 4.4
in such cases:
<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=".MainActivity" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="ASDFGHJKL" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="15dp"
android:text="#string/hello_world" />
</RelativeLayout>
Also, check the use of dimens:
http://developer.android.com/guide/topics/resources/more-resources.html
In Simple words .. if you want to take your widget like TextView, EditText far away from other. You should use margin from top,right,left,bottom.
By increasing padding it will increase the inner spacing not making the widget far apart from others..
Like buttons, for example, the characteristic button background image includes the padding, but not the margin. In other words, adding more padding makes the button look visually bigger, while adding more margin just makes the gap between the button and the next control wider.
Margin: Between border and its parent layout
Padding: Between content and border
refer to this
The difference between margin and padding and use cases are clearly explained by +Nick Butcher in Udacity's video. Here's the excerpt:
...if you want the touchable area or the background of the object to be enlarged, then use padding, otherwise use margin...
what is the difference fundamentally between padding and margins ?
For the differences - Rohan Khandwal has shared a very perfect link.
Would the behaviour differ depending on O.S. and devices?
Now If we are talking about the behaviour of the view which has been given diffrent margins & padding. Then It will definitely look diffrent in different devices with diffrent resolutions.
Thats why we are given diffrent dimen/values/layout folders which have their own meanings.
The difference between android margin and padding is that even though the text is how much sp you want away from the edge, margin is not spaced or colored in. It is only the text and the color you set with it all "alone". With padding though, the text is away from the edge of the screen just like margin but, in padding the text is away and all the space between the text and the edge of the screen is filled in with the color or any text preference you chose to be. This is the difference between android margin and android padding.
Padding is the space inside the border, between the border and the actual view's content. Note that padding goes completely around the content: there is padding on the top, bottom, right and left sides (which can be independent).
Margins are the spaces outside the border, between the border and the other elements next to this view. In the image, the margin is the grey area outside the entire object. Note that, like the padding, the margin goes completely around the content: there are margins on the top, bottom, right, and left sides.
Padding Increases the size of the view where as margin doesn't because it is outside the view
The padding is expressed in pixels for the left, top, right, and bottom parts of the view. Padding can be used to offset the content of the view by a specific amount of pixels.
For instance, a left padding of 2 will push the view's content by 2 pixels to the right of the left edge.
Padding can be set using the setPadding(int, int, int, int) method and queried by calling getPaddingLeft(), getPaddingTop(), getPaddingRight(), and getPaddingBottom().
Margins are the spaces outside the border, between the border and the other elements next to this view.
Note that, like the padding, the margin goes completely around the content :there are margins on the top, bottom, right, and left sides.
Margin can be set using the setMargins(int left, int top, int right, int bottom) method.

Android ellipse overflow priority

I have 2 textviews per horizontal linearlayout row on two rows. All the views are set as 0dp width and weight 1. They all have the same font and text size etc.
The views on the left side are gravity aligned left and the two on the right are gravity aligned right.
When both textviews text length overflow android always gives precedence to the textview on the right and ellipse the views on the left.
Is there a method that can be used to control which view ellipses when both views on the same row would not fit.
Ideally I want the views on the right to ellipse in favor of those on the left. Or failing that make them ellipse evenly per row.
thanks
i don't think there is a feature of order of the views to manage how they are measured.
you can customize the linearLayout by extending it in order to support this feature , but this is too hardcore for this task .
i would suggest putting the problematic views (those that take too much space and you don't with them to take too much space) into a new layout , and set its width to match_parent .
this way , it should take the rest of the space at the end of the measurements of the other views ,

Set actual text padding in TextView with compound drawables

I have a ListView populated with an ArrayAdapter. For items I use just a single TextView layout. I want some of the rows to have compound drawables set.
Question: is there a way to set padding for the actual text that is contained in TextView so that the compound drawables don't get the padding too? Other solution would be to lock the width of text. Do I need to add ImageViews to my layout?
Quite simple:
android:drawablePadding="5dp"
It will automatically use the padding according to the direction.
I'm posting this as an answer so that someone, who comes across this post finds the answer.
There's no direct way to set a padding only to text, but you can set a positive padding to the whole TextView and set a negative padding for the drawable.
Improving on Gio's sugestion, which is probably for a dated API/SDK...
TL:DR - just apply android:drawablePadding to increase distance between image and text.
Explanation:
android:drawablePadding now applies padding between CompoundDrawable and original View, in this case the TextView containing the text;
android:padding and modifiers now apply to the whole group, including all compounds.
Doing the negative-positive hack doesn't seem to work no more.
So depending on the drawable position (drawableTop/Left/...), the padding will apply in the opposite direction of it, right next to the drawable, in other words between the two elements. For instance, Applying drawablePadding="10dp" to a left placed drawable would have a similar effect as setting an individual ImageView to the left of the TextView with paddingRight="10dp" (at least padding-wise).

Android - spinner default margins

I am trying to center a spinner vertically, but it does not work since the spinner seems to have default margins that are not symmetric (the bottom margin is a bit larger than the top margin). If I set any margins to the spinner element, they are added to the default margin.
What is the recommended way to center the Spinner element vertically?
I think that bigger bottom margin exist because of the drawable(9 patch PNG) used for the spinner(you can check the drawable in the SDK):
A solution to this is to make your own spinner's drawable(9 patch PNG) that has equal space on top and at the bottom.

Categories

Resources