Aligning in a grid - android

I'm developing an android calendar app so I need to align the days in a grid-like style. I'm using the API Level 8 so I need to align them by margins. But when switching to Bigger screens the numbers get to the left of screen and do not cover the whole screen.(I know that is because I use dp as a unit for my margin-left). Is there something like CSS % (percent) in Android Layouts?

Try this
android:layout_weight="1"
on each element in that section. It should space evenly.

It's weight attribute for views inside LinearLayout. Here is a good explanation what it means. But you can use it only to set view sizes, not margins. However you can put your view into RelativeLayout, place this layouts to take all available screen width and set attribute centerInParent=true in your view.

Related

Android sudokod layout

I want to develop a simple sudoku app. For the layout, I need to have a 9x9 table/grid for the board, and 12 buttons under it and all this must fit in one screen,i had a couple of ideas but there is a problem in each of them
Using GridView and pass a 2d array to the adapter, BUT, the grid is scrollable and the player must see the whole board.
Using TableView, but its not clickable as the grid.
Create 81 button for the board in the xml or programmatically, I think it will be complex.
Is there are any other simpler or more efficient ideas! And if not , which one from the above is better.
There's an option for getting this done using weighted width and height. As far as I know this feature is only available in LinearLayouts.
The main idea I've just explained in this answer.
Please first off read the answer I linked to and then you could use the following approach to laying out your buttons.
Declare a main vertical LinearLayout and set its width and height to match_parent.
Add 9 horizontal LinearLayouts while their width is set to match_parent and their height set to 0dp with layout_weigth equal to 1.
Add 9 buttons (i.e. your cells) to each LinearLayout while their width is set to 0dp and layout_weight set to 1 and their height is set to match_parent.

How to put a View in a determinate pixel/position

I need to put a view (a image for example) in a custom point of the interface.
Are there a method to put a view in a exact pixel / virtual pixel (dp) - zone of the screen?
I need that the view is displayed above the other views.
I think to obligatory put a RelativeLayout and inside it all the layouts of the app and then put the view relative to this layout but i need a method more transparent to the future developers be able use it
EDIT:
I forget to say that i need to put the view programmatically and in the run time. I create the interface, make things in my app and then put a image in a concrete zone of the view above the others views for example in the half of the screen or in the top or in the 20% of the screen or ...
You can set margins for layouts and padding for views inside them, and you can specify position relative to edges, or to other views, there. Also you can specify fixed position for layouts by replacing (match_parent/fill_parent or wrap_content) with actual size( Note that you should specify all dimensions in dp or dip)
For instance you have a linear layout and you want an image view to be 20 dp to the right of the left margin you can try something like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android::layout_marginLeft="20dp">
<!-- Nest your views here and if you want you can set padding for them
</LinearLayout>
If you want your views to be on a certain part of the screen, like top of the screen/ bottom of the screen etc, you can nest all of them inside a RelativeLayout. This is a nice way to do it, and it will remove any confusion.
Good luck,
Arkde
What about a FrameLayout as in the example I posted here?
You can use AbsoluteLayout, although it's deprecated.

Android: Layout views of the same size evenly

I'm trying to layout a set of horizontal and vertical buttons. The problem is that if I indicate 'layout_weight' for buttons, their dimensions don't follow the 'layout_width' & 'layout_height' tags.
Here's what I'm trying to achieve
So, buttons must have identical height & width and distribute evenly horizontally and vertically.
Can anyone suggest a solution please?
Thanks
UPDATE: After a lot of investigation and trying out different solutions, I came to a conclusion that my only option is to create my own custom layout and place buttons correctly there.
That configuration (the L shape) might make this a tad trickier... since the corner button might size like it's horizontal counterparts or it's vertical counterparts, depending on which linear layout they are apart of. In any event, you should be using wrap_content for the height or width, and they should all have a weight of 1. It's my understanding that the wrap_content for the height or width is important for the Linear Layout to size them correctly. It might work with fill_parent when the weights are all 1, but if you use different weights on different Views with fill_parent, things won't appear as you expect.
If you can't make it work with linearlayout and weights (which you should be able to do), you could always try to do some manual workarounds using relativelayout.

Advanced Explanation of Android Layout Properties?

I'm on a quest to learn how to layout components properly in Android. I'm a seasoned CSS/MXML developer and am having the hardest time getting a full understanding of layout properties in Android components.
One thing is I'm not sure the differences between these:
layout_margin vs. padding
layout_gravity vs. gravity vs. ignoreGravity
Should you use one over the other with Linear, Table or Relative Layouts? An example of something I'd like to learn is having an overall margin on a layout with separate components relating to the top/middle/bottom of the screen. The sdk docs are a good start, but they don't show how things work in different situations.
Any tips on where to go to learn a more complex/comprehensive layout design?
Any attribute with the prefix layout_ is a LayoutParams attribute. While most view attributes are parsed during view construction by the view itself, LayoutParams are special arguments to the parent view that provide hints about how the parent should size and position the child view. Which LayoutParams are valid on a view depends entirely on the type of the parent view.
layout_margin is therefore an instruction to a parent view that supports margins. It says, "put this much space between me and other views or the edge of the parent." Padding is space inside a view between
the view's edges and its content.
layout_gravity is gravity for a single child within its parent. gravity affects the contents of the view it appears on.
Which one you use depends on the result you want to achieve. If you want a layout to have a fixed amount of space between its edges and all of its contents, you want padding. If you want to move the layout's own edges in by a certain distance, you want margins. When you have layouts without backgrounds set, these two can be visually equivalent. When you start creating complex UIs where layouts have 9-patch backgrounds that visually group the contents the differences become apparent.
I hope you can see the difference between padding and margin. Padding is inside spacing while margin is outside spacing.

What is the most flexible layout?

What is the best layout to use to support the app on different devices (Size of screen)?
EDIT
I am not just talking about resizing the layout, obviously the OS does that automatically. I am talking about repositioning the objects in my layout.
by repositioning I mean moving the objects according to the size of the screen. For instance i created my layout for a larger screen which looks great, but when i run the app on a smaller device (smaller screen) some of my User Interface elements were out of the bound of my screen.
There is no "best" layout. Almost all types of layouts will scale to different devices (Android is designed around this concept) other than AbsoluteLayout, which is deprecated anyway.
LinearLayout is best if you just have a row (horizontal or vertical) of content to insert. Using values such as dip values, fill_parent, or wrap_content will automatically adjust themselves to their content or screen size.
RelativeLayout, as Vladimir pointed out, is best for layouts where views are positioned relative to other objects within the layout. For instance, a TextView positioned beside a "Submit" button, is a common example.
FrameLayout is sort of a replacement for AbsoluteLayout; you can layer objects on top of each other, just specifying margin offsets from the sides of the frame.
TableLayout is, as it sounds, a layout for Table style design. You can have multiple rows and columns, and set certain columns to stretch to fit the size of the display, so that no matter the screen size, the layout fits as you designed.
EDIT: If you're having objects falling outside of the screen area, try wrapping your root layout in a <ScrollView>. This will allow the layout to be scrollable.
e.g.
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout
..... //and so on
</RelativeLayout>
</ScrollView>
All Layouts are flexible in terms of size... the rule is: don't use AbsoluteLayout. That's all.
And with regards to the repositioning concerns... well, use always dips instead of pixels and you are good to go. Again, don't use AbsoluteLayout, the rest of the layouts should work fine on every screen size. Sometimes you can anticipate those "disappearing acts" by wrapping your layout in a ScrollView.
RelativeLayout is what you should be looking at. It easily resizes the elements relative to their neighbors. Just make sure to include drawables for all resolutions and densities
LinearLayout,RelativeLayout,FrameLayout are import Layouts....

Categories

Resources