Possibly useless Linear Layout (I don't think so) - android

I'm doing an Android layout and the compiler says "This LinearLayout layout or its LinearLayout parent is possibly useless". I want a few edit texts and buttons to be in a vertical layout like a form but all of them with 70% of the screen width. So i did:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1.0" >
<LinearLayout
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.7"
android:orientation="vertical" >
... my elements...
</LinearLayout>
</LinearLayout>
It worked but why the compilers insists with this warning?

That's a lint warning, and it's not perfect. I have a case similar to yours where it warns about a useless container, but it's really not useless. Many times lint helps, occasionally you just have to pat its head and ignore it.

Just to complete the previous answer:
Lint will ignore this problem with tools:ignore="UselessParent"
(you will need to add xmlns:tools="http://schemas.android.com/tools" as a namespace)
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1.0" >
<LinearLayout
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.7"
android:orientation="vertical"
tools:ignore="UselessParent>
</LinearLayout>
</LinearLayout>

Related

Scrollview fill_parent with "layout_weight"

I'm having problems when putting android:layout_width="fill_parent" Because spaces eats and grows all the greater .
When I have in wrap_content , it works fine , but when I turn , I have half the blank screen.
Is there any way to fix it?
My scroll (with rows static, not dinamically):
<ScrollView
android:id="#+id/scrollView19"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:fillViewport="true">
My row
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center">
My space into scroll
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.1">
I'm trying to test all cases of scrollviews for Android ( Google ) has no deficiencies.
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="Match_parent"
android:layout_weight="0.7"
android:gravity="center">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.3">
Do try with the weights,may be it would work for you....
I solved create a new layout-land,, it seems the developers thought about how to go creating layouts, I tried to give the same layout (resizable for all views that exist), now in the same layout (portrait-horitzontal) does not work, maybe in the future can do not create a layout-land.

Layout issue with Android Studio

Recently picking up android development, I have hit a snag in the road. I'm having trouble positioning my layouts. Screenshots are as follows:
I'm trying to input either another layout type/list view in the upper section of the screen, without disrupting the button/text box at the bottom, though.. When extending this layout. I hit the following snag:
The entire contents of the original frame shift when the box of the new layout is extended, I've tried modifying:
android:layout_gravity="top">
and other layout attributes such as weight, margin, height/width.. This always hits the same problem.
My XML for this view is:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="483dp"
android:layout_weight="1.06"
android:layout_gravity="top">
</FrameLayout>
<EditText android:id="#+id/edit_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/edit_message"
android:layout_gravity="bottom" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:layout_gravity="bottom"
android:onClick="SendMessageButton"
/>
</LinearLayout>
Though, i'm some-what stuck on how to make the correct changes & any response to this question would be greatly appreciated!
At the moment, all your child views live inside a single horizontally-oriented LinearLayout. LinearLayouts always arrange views sequentially, as you are experiencing.
There are a couple different ways to achieve the layout you are looking for. I'm going to suggest one that uses nested LinearLayouts (an outer one to stack things vertically, and then a nested one to arrange the EditText and Button horizontally), but you could also consider using a RelativeLayout for this.
Updated layout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="#+id/edit_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="SendMessageButton" />
</LinearLayout>
</LinearLayout>
Note that a LinearLayout is oriented horizontally by default; I have explicitly included the attribute here to make the structure more clear.
The reason, your button and editText is appearing far from the frame layout and not near the corner is the parent layout orientation is Horizontal.
Change it to Vertical.
Now, if you need your button and editText to be arranged in the same line, it should be mentioned as described in samgak answer.
However, i would like to suggest the following.
Using framelayout might create bad user experience across different screen sizes in android.
If the parent layout in Linear, if the screen size is x and all your components added if the height it takes is x-20, then the theme you set for parent layout would not cover the entire screen. Therefore, it is recommended to use RelativeLayout and for the button and editText, use the layout_alignParentBottom = true attribute.
If needed, i can share the code sample for this. Added Vertical Scroll to the layout.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="483dp"
android:layout_gravity="top">
</FrameLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText android:id="#+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Edit Message"
android:layout_gravity="bottom" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
android:layout_gravity="bottom"
android:onClick="SendMessageButton"
/>
</LinearLayout>
</LinearLayout>
</ScrollView>

LinearLayout weights not filling parent

After looking around for some time, I've yet to find a question that matches my problem. My issue is this, I have a ListView whose entries adhere to 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="wrap_content"
android:baselineAligned="false"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:orientation="horizontal"
android:descendantFocusability="blocksDescendants" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
amdroid:layout_weight="1"
android:orientation="vertical" >
...
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="vertical" >
...
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
...
</LinearLayout>
</LinearLayout>
I would expect this to produce an entry in which the 1st nested layout gets 1/4 of the available width, the 2nd gets 1/2 of the width, and the 3rd gets 1/4 of the width. This isn't what happens, though; instead, the width of each nested layout is wrapped for some reason.
Interestingly, if I specify a particular width in the parent LinearLayout instead of "match_parent", the nested layout widths obey the weights as expected. For example, the following produces the expected result:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="290dp"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:orientation="horizontal"
android:descendantFocusability="blocksDescendants" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
amdroid:layout_weight="1"
android:orientation="vertical" >
...
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="vertical" >
...
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
...
</LinearLayout>
</LinearLayout>
Obviously, though, specifying a particular width ignores the whole point of weights. So my question is this: why aren't the weights actually filling the parent LinearLayout when "match_parent" is used, and how can this be corrected?
Oh and one final point of interest, the graphical layout preview in Eclipse produces expected results when "match_parent" is used for the parent LinearLayout width. It's only when emulated or used on an actual device that the contents of the nested LinearLayouts get width-wrapped. I'm guessing that this is due to the layout's use inside of a ListView, but who knows?
Thanks
Alright, I feel dumb now; my suspicion was correct: the problem was with the ListView.
I had specified "wrap_content" for the ListView width in the layout that instantiates the ListView. Correcting the ListView width by specifying "match_parent" produces the expected results for the entry layout without relying on a hard-coded layout_width.

Splitting views into equal parts

I'm trying to design my layout as below. My current approach is to have a LinearLayout wrapping two other LinearLayouts. Each of these has layout_weight=1. Then the bottom layout wraps another two and each of those also has layout_weight=1. I've heard that nested weights is not advised - but is it? Otherwise, what would be a better alternative?
Thanks
This would be my approach:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="2">
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<View android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff00ff"/>
</LinearLayout>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:weightSum="2"
android:orientation="horizontal">
<View android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:background="#ffff00"/>
<View android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:background="#00ffff"/>
</LinearLayout>
</LinearLayout>
I believe weights might be "inefficient” because in order to apply them appropriately, a layout has to be measured first. If its children apply weights as well, it may lead to repeated measurements of same layouts, potentially hampering performance.
If your layout will be complex, you might want to consider alternative approaches (e.g. creating a custom ViewGroup), but remember:
We should forget about small efficiencies, say about 97% of the time:
premature optimization is the root of all evil.
so make sure you actually have a problem before trying to solve it.

Problems with RelativeLayout

I separated the interface of my app in three areas: header, content and footer.
The header has a fixed size (it has only one image), while the footer and content have sizes that can vary.
In devices with higher resolutions I thought to insert the header and footer on the screen, and reserve any free space for the content area.
In devices with low resolutions thought of putting the content length as little as possible (something like wrap_content) and insert the footer below (requiring the user to perform scroll to view the footer).
The best I got was using RelativeView:
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentTop="true" >
(...)
</LinearLayout>
<LinearLayout
android:id="#+id/footer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentBottom="true" >
(...)
</LinearLayout>
<FrameLayout
android:id="#+id/content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/footer"
android:layout_below="#+id/header"
android:lay >
(...)
</FrameLayout>
</RelativeLayout>
For resolutions larger works as expected, the problem is that for small resolutions: the content is less than it should, because it takes the space between the header and footer.
How can I solve the problem?
I could not find another way to get content assuming all the free space of the screen (in large resolutions), because I can not simply use fill_parent, since the content is between the header and footer.
I also tried using min-height, but without success.
Top level RelativeLayout layout_height make that fill_parent.
Then FrameLayout remove the layout_above property, just saying it's below the header should be enough.
Finally, FrameLayout may be causing the problem as it's normally used when only 1 element is on the screen and it fills the screen. Try replacing this with a LinearLayout. I've done something exactly like what you want in one of my apps, the layout is (keep in mind in my case I swap out the FrameLayouts for Fragments which are LinearLayout or RelativeLayout based.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainBack"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/transparent" >
<FrameLayout
android:id="#+id/headerFrag"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#+id/homeAdMsgFrag"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
<ListView
android:id="#+id/contactList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/homeAdMsgFrag"
android:layout_alignParentLeft="true"
android:layout_below="#id/headerFrag"
android:background="#color/transparent"
android:cacheColorHint="#color/transparent" >
</ListView>
</RelativeLayout>
Some days before I also faced this issue, to solved what I did that I created Header.xml and footer.xml and included this two xml in my all others activities xmls because this two are common in all others activities.
To meet global resolution issue, I used weightsum and weight, applying weight will fixed your header and footer area and content area too.
This way I done in my one of project to resolve this issue, just try it, hope it will works for you.
EXAMPLE
<LinearLayout
android:weightSum="10"
android:orientation="vertical"
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/header"
android:layout_width="fill_parent"
android:layout_weight="2"
android:layout_height="0dp"
android:orientation="vertical" >
(...)
</LinearLayout>
<FrameLayout
android:id="#+id/content"
android:layout_width="fill_parent"
android:layout_weight="6"
android:layout_height="0dp">
(...)
</FrameLayout>
<LinearLayout
android:id="#+id/footer"
android:layout_width="fill_parent"
android:layout_weight="2"
android:layout_height="0dp"
android:orientation="vertical" >
(...)
</LinearLayout>
</LinearLayout>
Thanks.

Categories

Resources