I'm trying to create the layout part of my app. I wanna know the best approach of going about this section.
I'm thinking of going with something like this, but i think its too much for just displaying three elements
This is a common practice for displaying similar layout. If you want, you can replace all nested layout to ConstraintLayout. It can improve code extensibility, but it can slightly slow down the rendering speed of the UI.
As a result, I think your current variant is the most optimal.
Yeah, thats fine but for the ImageView you will also need to nest it inside a CardView
<androidx.cardview.widget.CardView
android:id="#+id/one"
android:layout_width="200dp"
android:layout_height="200dp"
app:cardCornerRadius="12dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/beach_bg_placeholder" />
</androidx.cardview.widget.CardView>
Related
I want to use absolute positioning in such a way that each object in the layout is in about the same position on each device. Is there maybe a library or a formula or something else I can use to achieve this?
You certainly are aware that it's not a very good idea (because you'll have difficulties to adapt to different screen sizes/orientation), however, you may achieve this with FrameLayout and paddings
<FrameLayout ...>
<View
android:layout_paddingLeft="20dip"
android:layout_paddingTop="10dip"
... />
</FrameLayout>
Use <AbsoluteLayout ></AbsoluteLayout>
hope it will work..
I'm no Android expert but I am aware of discussions regarding the appropriate use of LinearLayout and RelativeLayout, keeping the view hierarchy as small as possible, avoid unnecessary passes of onMeasure(), etc.
Lets imagine I have two ImageView's that I want to position completely INDEPENDENTLY, the first in the center of the parent and the second in the bottom left of the parent. (note this is a vastly simplified example of far more complex real life requirements).
The obvious way to solve this is using a RelativeLayout...
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/first_image" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="true"
android:layout_alignBottom="true"
android:src="#drawable/second_image" />
</RelativeLayout>
However something keeps telling me that a RelativeLayout isn't appropriate in this situation because I don't want to organise the children relative to each other. All I want to do is position the children according to the parent and I wonder if using a RelativeLayout causes some unnecessary layout calculations that I don't really require.
I am wondering if there is another ViewGroup type that would perform better? Its totally possible to achieve what I want with a FrameLayout for example but I've no idea if this is more performant or if I am abusing the intent of a FrameLayout etc...
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/first_image" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|bottom"
android:src="#drawable/second_image" />
</FrameLayout>
Maybe there is another Layout type I am unaware of?
Short answer
Given the information you have provided, the FrameLayout has a good chance of performing better. As you may have learned already watching (Adam Powell quote in Google I/O 2013 conference),
[...] RelativeLayouts will measure child views more than once in order to solve some of the constraints you give it. So, being generic has a cost [...]
From what I read and understood, this is not guaranteed and it depends on the constraints you give it.
Long answer
It really depends.
We all read Romain Guy blog post : Android Layout Tricks #1, who said that most people misinterpret his post and started using RelativeLayout everywhere.
This post, if you haven't read it, talks about how removing one hierarchy level using a RelativeLayout instead of a LinearLayout which saves loading time in a list.
Basically, it means that if you don't need them, as you described it yourself
[...] I have two ImageViews that I want to position completely INDEPENDENTLY [...] I don't want to organize the children relative to each other
you should not use them because of that reason.
Concrete example of : "Don't use them if you don't need to."
For instance, in one of our applications, we have serious performance issues on devices running Gingerbread -- which we want to support.
Our most complex layout involves a vertical ScrollView, attached to the current activity, in which we have several containers and one HorizontalScrollView that displays images and information which are contained in a complex LinearLayout.
We started to replace the LinearLayout by RelativeLayout. The result: no obvious improvement -- equivalent or maybe worse.
Since that layout is fairly complex, adding more RelativeLayouts embedded in each other just increased the onMeasure() calls that were made for a single draw.
Even a small circular ProgressBar was now spamming the UI thread with several measure calls because it was in one of those embedded RelativeLayout which triggered recalculations of the whole view.
I need to implement 2 MapViews, one displayed on the entire width and height of the screen and the other on top of this map but will be smaller. I need both MapViews to be displaying at the same time showing different maps. I know that this is not supported currently but I know work-arounds can be done, just not sure how to get the two maps in the same view and displaying at the same time anyone have any ideas or examples?
You can use Framelayout for arranging a view on top of the other.
Framelayout can be used for overlapping views
<FrameLayout
android:layout_width="wrap_content"
android:layout_weight="1.0"
android:layout_height="wrap_content">
<EditText
android:layout_width="fill_parent"
android:hint="Search..."
android:layout_margin="3dip"
android:id="#+id/txtsearch"
android:background="#drawable/searchback"
android:layout_height="wrap_content"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|right"
android:id="#+id/btntitlesearch"
android:background="#drawable/searchbutton"/>
</FrameLayout>
This will make the button overlap the EditText
Use an empty RelativeLayout as a container. This way, you can put two layouts on top of each other. http://developer.android.com/guide/topics/ui/layout/relative.html
Just make one fill_parent
make your second whereever you want it (like android:layout_alignParentRight="true")
There you go!
Before you get to far, I'd like to quote what Google says about running multiple MapView's in the same Activity:
"Only one MapActivity is supported per process. Multiple MapActivities running simultaneously are likely to interfere in unexpected and undesired ways."
It looks like an Activity only supports one MapView, so you may want to find a different design path.
I want to achieve the following:
It works with the following layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_weight="3"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:orientation="vertical" >
<fragment
android:name="com.bobjohn.DetailsMenuFragment"
android:id="#+id/detailsMenuFragment"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="6"
/>
<fragment
android:name="com.bobjohn.SummaryFragment"
android:id="#+id/summaryFragment"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_margin="10dp"
android:layout_weight="4"/>
</LinearLayout>
<TextView
android:layout_width="0dp"
android:layout_weight="7"
android:layout_height="fill_parent"
android:text="Test Text"/>
</LinearLayout>
However, I get the warning about nested weights being bad for performance. I understand the error but I don't know how to express this layout in another way. What is the alternative?
There are NEW updates in SUPPORT Libs, Please check Accepted Answer too.
Updated Answer :-
Whenever you create any view, It calls it's measure events to know the height width of view on the screen, If you are not using WRAP_CONTENT or FILL_PARENT or FIXEDSIZE and using Weights then it's becoming more complex to render your layout on the screen.
Means,
First your main layout is rendered and calls it's measure..then based on weight all child views calls it's measure events recursively so it consumes more time to load.
So, One should avoid nesting of weights.
Alternative to Nested weights :-
You should consider using different layout and drawable folder specific to different sizes. Write your views in your XML with specific height-width OR make it wrap_content and use specific background images OR make it fill_parent.
I believe that as developer we may be wrong several time, but as creator Android (Lint) they may be wrong only in rare case, should listen to those warnings to make your code better.
BELOW ANSWER WAS WRITTEN WITH LACK OF KNOWLEDGE ABOUT ANDROID LAYOUT
AFAIK, I think you have done right, this is the best written XML for the same.
You have used the weight attribute perfectly as it should have been used. You just ignore the Warnings.
What is the alternative?
I have coded all my XML in the same way in my projects so This has been the best alternative to me,So I dont think there is any other alternative to CODE the XML to get Such layout until and unless you use RelativeLayout as parent layout with some fixed sizes height and width of the child views. Still I advice you keep it as it is.
I would have deleted this answer as I still don't completely know Android Layouts but keeping it to receive new comments and answer based on this
Yes we have the alternative for nested LinearLayout weight by android's percent support library
Code and concept HERE !
GitHub Project HERE !
Consider this simple layout where I have totally avoided weight property of LinearLayout
<android.support.percent.PercentRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/fifty_huntv"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#ff7acfff"
android:text="20% - 50%"
android:textColor="#android:color/white"
app:layout_heightPercent="20%"
app:layout_widthPercent="50%" />
<TextView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_toRightOf="#id/fifty_huntv"
android:background="#ffff5566"
android:text="80%-50%"
app:layout_heightPercent="80%"
app:layout_widthPercent="50%"
/>
</android.support.percent.PercentRelativeLayout>
Really awesome !!!
I think (and I will probably be flamed for this), but again I think my phone has a quad core processor to rival (if not utterly destroy) most peoples home PC's.
I also think this kind of hardware capability is the future of phones.
So I come to a conclusion, that as long as you are not getting carried away with nesting (in MHO a layout should never be more then 4 levels deep, and if it is you are probably doing it wrong), your phone could care less about having weights.
There are many things you can do that will have a much more far reaching effect on performance, then worrying about your processor doing some extra math.
(please note that I am being slightly humorous, and so not to take anything too seriously from this post, other then the idea that there are other things you should optimize first, and that worrying about a 2-3 level deep weight is not helping your health)
I am a new one in Android so please sorry for stupidity.
Well my problem is in multiple layers -
I would like to combine two transparent ImageViews one above the other. This is similar to photoshops layers, what is the sample layers activity in android?
You can use a RelativeLayout for this.
The property android:layout_centerInParent If true, centers the child horizontally and vertically within its parent. [boolean]
Similarly there are properties like ,
android:layout_alignParentLeft,
android:layout_alignParentRight,
android:layout_alignParentTop,
android:layout_alignParentBottom.
Try these.
Already answered Overlapping Views in Android ? That should be all you need, using RelativeLayout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/layout"
>
<ImageView android:id="#+id/imageview1"
android:background="#00000000"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/image1"
/>
<ImageView android:id="#+id/imageview2"
android:background="#00000000"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/image2"
/>
</RelativeLayout>
would be an example code for you.
you can use the layer-list, for a full investigation, please refer to http://developer.android.com/guide/topics/resources/drawable-resource.html#LayerList
Well its quite possible to do in android, but the thing is what actually you are going to do with that. you can give the same positions for both the images in layout now both will be overlapped and based on your condition or situation you can show and hide one another programatically. In the same way you can give multiple images overlapped on one another. Hope this will help you out to understand :)