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.
Related
I have a problem with the background of a View in this project.
There is one View with a transparent layer, which is not always displayed... It looks like if, in some cases, the height of this View was automatically set to 0, as I can see in the preview of the activity that contains the View. The code of this element is as follows:
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:alpha="0.5"
android:background="#drawable/background_layers" />
The main view group in this activity is a ScrollView. If the whole layout is displayed and is visible on the screen (so you don't need to scroll down) everything works fine (look at the transparent layer, which is the topic of this question):
But if the screen density is low, or if the device is in portrait orientation, and I must use the scroll because the full layout is not displayed at the same time... the previous View is not shown, the transparent layer is not visible :(
It happens when the app is running and also in Android Studio, in the preview screen, as I said. Here is an example with vertical orientation and low screen density, as seen on the preview tab of the activity in Android Studio:
Any idea to fix this isue?
Thanks in advance.
David.
I think I found the problem with your layout. The transparent_background view sometimes takes height 0 because its height is match_parent and its parent (the main_relative_layout view) has its height set as wrap_content. You should set the transparent_background dimensions relatively to another view, the LinearLayout that wraps the content maybe, using android:layout_alignTop, android:layout_alignBottom, android:layout_alignLeft and android:layout_alignRight attributes. You may need to remove the LinearLayout margin and use padding instead to align the transparent_background view correctly, let me know if you need some help.
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.
I have an application where I want to achieve a layout where the product image will be in the background and top of that to the right bottom, I want to place the price of the product and to the left bottom I want to place an add button.
Should I use frame layout or relative layout ???
As a practical rule, I think it's up to you. I personally tend to use RelativeLayouts because they're more flexible, but you can achieve the same effect with either.
This SO post explains the performance differences between the two layouts in more detail: FrameLayout vs RelativeLayout for overlays
Relative layout: When you have relation between siblings or parent.
Frame layout: When siblings are placed independent of each other and are only dependent on parent.
Based on your situation, you can opt in of any of these.
My advice, If you have specific size for your background for each device, then go for relative layout, and set the background to your image. Because when using background in RelativeLayout, It'll fit the size of the relative layout itself, whether the image suits the size of the RelativeLayout or not. (Can be stretched/pixelated/Not properly added)
If you're not sure about specific size, you should use FrameLayout, with ImageView, that handles the ScaleType, which can be centerCrop, and it'll fit the layout in good shape.
And for the TextView, use layout_gravity, which will handle the position based on the parent layout.
I'm working on the controls for a game, and require part of the control panel (gray in the figure below) to change dynamically, either showing a single canvas (left) or 5 buttons (right). The border between the lower-row views should always be positioned at exactly the same x-position as the border between the buttons on the upper row, as shown. At the same time, all twelve upper buttons should be scaled and distributed evenly.
I've considered several approaches, but as of yet none do all of what I want:
Using two LinearLayouts, one for each row of controls: reliably aligning the borders seems to be impossible, and replacing part of the layout is difficult at best.
Using a TableLayout: again, replacing a portion of the layout is difficult.
Using a RelativeLayout: resizing and aligning buttons independently of the screen size doesn't seem possible
Any suggestions for an alternative method, or on how to make one of the above approaches work? It would also be nice if there were some way to animate the change of views, i.e. sliding in the buttons from the left over the canvas. Thanks!
Interesting, I've done this several weeks ago. What I did is to make use of this property of View object: "Visibility". So that means at a fixed position, I can set any View to display on to, not depending on any type of Layout, it can be Visibility.GONE, Visibility.VISIBLE or Visibility.INVISIBLE.
In my app, I used RelativeLayout to set relative position to the right side TextView.
Give it a try :)
In order to close this question: I have solved the problem by writing a custom layout class that places and sizes the child views without heeding the measured size of the children. Effectively this gives me the behavior of a linear layout with layout weights, but is more deterministic with border placement.
A ViewAnimator is used to switch between the Canvas and the Buttons.
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....