is possible to have a separator between elements of a GridView?
Thanks
You'll probably have to play with padding and background colors. Set the background for the table to one color, and the background to each View in the table to another color. Set a 1 or 2 pixel padding around each View in the table, and you should have a border between.
from #FreewheelNat's comment, "In your xml for your GridView, use android:horizontalSpacing="1dp" and android:verticalSpacing="1dp" to set a 1 (density independent) pixel padding around each cell for example. "
this is a much better solution than #Jeff Barger's
Related
I would like to have my 2-line text view in android-xml centered horizontally with the smallest width necessary. At the moment it's like the first line is completely filled and the second with the rest of the words.
Does anyone know how to achieve that both lines are filled kind of equally? Below are 2 images, the first showing the default multi-line text view, the second what I would like to achieve. As you can see, the second image requires much less width than the first and is more in the center of the view.
Thanks in advance!
Try setting android:breakStrategy attribute of textView to balanced
android:breakStrategy="balanced"
Add this line to your textView's xml code, this will balance the lines.
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
I have a horizontal layout to display 5 icons.
I would like to know if there is a way to display the first one always X dp from margin left and the last one X dp from the margin right and the others with the same space between then.
With LinearLayout the space is always the same even when I change the device , so sometimes my horizontal row became much more bigger than the space that the icons use and with RelativeLayout i just can align 3 items in this way ( alignParentLeft, centerInParent, alignParentRight)
With RelativeLayout I think you can easily put three of your icons at the proper place. For the last two icons, I don't think there's something you can do in your xml. Actually I have met the same problem before.
An approach is (maybe not the best one) to calculate the margins of the two icons in your java code. It's really easy to get the parent view width (in pixels) and you know how much space(a percentage) there should be before/after your views. Just a multiplication and addView with LayoutParam is sufficient.
A LinearLayout with android:width="match_parent", android:layout_marginRight and android:layout_marginLeft within a RelativeLayout should work.
The Linear Layout will be xdp from the left and xdp from the right, with the icons spaced evenly inside.
If you use the Linear Layout, you can use the weight attribute to calculate the correct spacing between the images.
Set each of their weights to 1 so that they take an equal space in the row :
android:layout_weight="1"
I hope that can assist you.
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).
I have TableLayout in my project which contains 6 TableRow layouts. Each TableRow layout has 7 TextViews in it. I want to set a background image to this TableLayout which is currently displaying 42 TextViews. I also had set some background color to each TextView. I tried this
mDateSelectionBar.setBackgroundResource(resid);
But it is not showing up my Image. However, If I do this for my 42 TextViews..
TextView.setBackgroundResource(0);
Then my Image is visible. But I want to display TableLayout background Image as well as background color of each TextView. How can I do this? Please Help! Thanks :D
The issue is that the background of the individual cells in a table layout is drawn over the overall layout background. If you remove the background of the cell, then the overall background is visible. If you need to display the background of the table as well as some colouring of the cells, consider setting the alpha channel of the cell background colour to less than 100%, e.g. 0.8 or something.
When you set the colour for your cells, use #ARGB syntax, e.g.
setBackgroundColour(Color.argb(192, 255, 0, 0));