Margins and paddings between elements in LibGDX - android

I have a VerticalGroup of Buttons on my screen. The problem is that buttons located very closely one to one. I want to add some spaces between buttons, something like padding or margin. I find pad method in Table in API, but VerticalGroup doesn't extends Table and therefore doesn't contain this method. Please point me how I can add some spacing between buttons inside VerticalGroup
Sample code
VerticalGroup buttons = new VerticalGroup();
buttons.addActor(btn1);
buttons.addActor(bnt2);
// ... and so on

Better late than never: libGDX uses TableLayout (https://github.com/EsotericSoftware/tablelayout) to order the widgets. When you follow the link and you go to the 'Padding' section, you will have an image illustrating your situation. In order to get the margin (the spacing outside of the button) you have to use the following code:
table.add(button).width(100).pad(10);
table.row();
table.add(lastButton);

This is very, very new, but for anyone reading this question in the future, there is a space method in VerticalGroup which is intended to allow you to define the spacing between elements:
http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/scenes/scene2d/ui/VerticalGroup.html#space(float)
You can find an example of it in action, here:
https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests/src/com/badlogic/gdx/tests/Scene2dTest.java
For those working with a slightly older version of libgdx, there is a setSpacing method that can be used instead.

Instead of adding padding from the parent (the VerticalGroup) add the padding to each element (the Buttons).
A libGDX Button is also a Table and Tables support various pad methods. Those are documented to change the padding around the outside of the table (or button in your case).

Related

How to disable auto alignment?

I'm building an android project and I'm using eclipse.
I just can't figure out how to disable the annoying auto alignment.
I just want to place buttons wherever I want to drop them on the GUI interface but it just keeps
to align them one to another. I've tried to delete those alignment lines in the xml code
but it still brings them back as I move the buttons on the GUI interface.
Is there an way to disable that function?
Thank you,
Alex
Is there an way to disable that function?
Not in a way that you will find satisfactory, I suspect.
You have not really explained what the "alignment lines" are, so we are forced to guess. My guess is that the "alignment lines" are because you are working with a RelativeLayout container. Quoting the JavaDocs for RelativeLayout, RelativeLayout is:
A Layout where the positions of the children can be described in relation to each other or to the parent.
And, quoting the guide for RelativeLayout:
RelativeLayout is a view group that displays child views in relative positions. The position of each view can be specified as relative to sibling elements (such as to the left-of or below another view) or in positions relative to the parent RelativeLayout area (such as aligned to the bottom, left of center).
Hence, the "alignment lines" are there, and are replaced by the GUI builder, because they are the point behind using a RelativeLayout container.
Of course, you are welcome to change the container that you are working with to something else.
However, in general, Android does not really support very well your stated objective ("I just want to place buttons wherever I want to drop them on the GUI interface"). Just as you don't do that in Web development, you don't do that in Android development, and for much the same reason: you need to take different sizes into account (browser window size for Web, screen size for Android). RelativeLayout, LinearLayout, TableLayout, and GridLayout are all designed to have you specify widgets plus rules for positioning and sizing, so that you can design a UI that will accommodate the difference between a 3" and a 4.5" screen, for example. This is akin to using HTML tags and CSS rules to define content and its positioning in a Web page. Eclipse's drag-and-drop GUI builder for Android can assist in your definitions of these rules, as you are perhaps seeing with your "alignment lines" for RelativeLayout.
I think I may be able to help. If you set your layout to Relative Layout you can drag and drop any of the views wherever on the eclipse GUI.

Is there somewhere I can get the typical XML styles from the Android design guides?

This is driving me insane at times, why don't they just provide XML snippets of what they are showing?
Example:
http://developer.android.com/design/building-blocks/lists.html
It'd be nice to just know how they are using for the 2 lined list item layout.
SDK sources.
There are many examples, metrics and everything you need. Including drawables. Shamelessly copy them to your project keeping the copyright notice and that's all.
But of course, you'll need to study the code just a bit...
By the way, the 2 line list item seems to be a medium and small text, 4dp (or 8) between them, and 16 do margins on top and bottom. The left margin I think is 8dp.
There is also a standard R.layout including a two_line_list_item, but To put stuff on the sides you'll need to go custom. The header is the standard list header which is also available under R.layout.*
It follows the design guides. Nothing fancy.

How to reposition objects in Android layout?

I've added two controls, say Text Box.
But it is added side by side.
I want to move the objects, like in Visual Studio.
Can anyone help me how to do it?
you can use Relative layout for placing your views, so that you can place them variably like left of,right of, above, below, align etc.
You want one above the other? Try RelativeLayout and you can specify layout_below.

Section Header using Android XML?

enter code hereLooking through the Android UI guidelines I came across this section / example:
In the example above, there are a few headers called 'SECTION' with a horizontal line underneath them.
Im still getting grips with the appropriate way to replicate this -
Is there something specific like a Header XML element to use that will automatically style the font and horizontal line? Or is something like this generally more primitive, i.e. A TextView with a 1 pixel horizontal image (ImageView?) underneath it?
Possible duplicate question of this one: Android 4.0 Sub-Title (section) Label Styling
In short: try adding attribute style="?android:attr/listSeparatorTextViewStyle" to the TextView element, which you wish to make look like that, in the XML layout file.

Two column table on android

Hey everyone. I'm not big into UI programming so this may be an easy thing I overlooked. I am trying have a screen that shows 8 TextView in a 2 column x 4 row table. And, of course, I want the TextViews, that might have different lengths, to be centered. I tried this in a table layout, for obvious reasons but I feel like this is not the way to do it because it doesn't have much control where I put everything once it is in a row. Should I be using a different combinations of layouts or is there something I overlooked.
I can post my xml file if you really need but this is really more of a concept question than a specific one.
Thanks,
Jake
Can you explain why a table layout didn't work in more detail? If you're just trying to center the contents of the cells, you can set the android:gravity attribute in the table layout to "center"
EDIT: You can set the spacing between items using the android:padding attribute (Documentation). There are a number of other attributes in that link you can use to modify the way your table is laid out, as well as table-specific attributes at this link.
Check out apps-for-android's GridLayout. You may have to modify it a bit to get it to do exactly what you want, however, it's probably a good starting point.

Categories

Resources