I am trying to place an ImageView in the middle of the screen, with the aim of putting other views above and below it so I have decided to use a LinearLayout (vertical) and a LinearLayout (horizontal). However, when I use layout_weight for the ImageView, the image disappears completely (a blank screen is shown). The ImageView's image is set in onCreate(). I would like the ImageView to take up 50% of the width of the screen, hence my use of weights.
<?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="vertical">
<View
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="horizontal"
android:layout_weight="2">
<View android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<ImageView android:layout_weight="2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="#+id/albumArtImageView"></ImageView>
<View android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
</LinearLayout>
For layout_weight to be useful for anything, there needs to be left-over space. Your first view under the top LinearLayout has layout_height="fill_parent". That takes up all the room. Try setting layout_height="0dp" everywhere that you want the dimension determined by the layout_weight and I think your results will be better.
try to change the orientation of second linearlayout to vertical
Related
When I set MATCH_PARENT for View + layout_weight the elements of the view behave strangely. Please can you explain why this is so. For example, here is the code on which to experiment. I can't understand the pattern. The less put the weight of the item, so it is more, however, it is unclear how many times.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="5"
tools:context=".MainActivity">
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F00"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F0F"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0FF"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00F"
android:orientation="horizontal">
</LinearLayout>
</LinearLayout>
Testing for Yellow layout: set layout_weight= 0.5 > large?? how much bigger? Did the elements shrink in size or were they thrown out of the parent?
<LinearLayout
android:layout_weight="0.5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F00"
android:orientation="horizontal">
</LinearLayout>
Update, PS: I know how the layout will work with WRAP_CONTENT (or 0dp). If you set MATCH_PARENT + layout_weight, then we have a fixed element size regardless of the content, when WRAP_CONTENT does not guarantee a fixed size from the content (and if the content size is increased, the element will be stretched). I am only interested in the pattern MATCH_PARENT + layout_weight, because it guarantees the block size when the content size is exceeded.
The less put the weight of the item, so it is more...
The reason is because you are assigning android:layout_width as match_parent to all LinearLayout. To get android:layout_weight worked, you should set android:layout_width as 0.
First of all, give android:weightSum to the parent of the layouts.
Secondly, if you have layouts placed horizontally next to each other then set android:layout_width="0dp" for each element.
Similarly, for vertically aligned elements, give android:layout_height="0dp".
I want to achieve this
I am doing this
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/upper"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/wallet_dim_foreground_inverse_disabled_holo_dark">
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:layout_below="#+id/upper"
android:background="#color/blueAccentColor">
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/upper"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/lower"
android:background="#color/wallet_dim_foreground_inverse_disabled_holo_dark">
</LinearLayout>
<LinearLayout
android:id="#+id/lower"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:background="#color/blueAccentColor"></LinearLayout>
</RelativeLayout>
You have to set the first Layout to be above the seconde.
With the code you provided you will have a top layout taking all space, and a bottom layout being below him. If you think about it you can see that it's logic that the result will be different.
If you remove android:layout_below="#+id/upper"and add android:layout_alignParentBottom="true"from the lower one, this Layout will be anchored to the bottom side, whichever the screen size is.
Than adding android:layout_above="#+id/lower" to the top Layout, he will take all space(thanks to android:layout_height="fill_parent") but it will stay above the lower Layout.
Here is the result you want :)
I want to design a divided screen into two non equal parts in a landscape mode of screen. So, I thought automatically in using fragments. But the problem that I have encountered is that each fragment match the half of the screen. And that's not what I want exactly. What I want is a screen with two parts non equal in their width. I want something like this:
You can make use of the weight parameter.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3" />
</LinearLayout>
Please notice the weights used for the two LinearLayouts. The first one has a weight of 1 (it can occupy 1/4 th of the screen) and the second one 3 (it can occupy 3/4 th of the screen).
Just use this exact same concept for your fragments and maintain nay proportion as you wish.
Hope I could make myself clear.
Use weight parameter
<LinearLayout android:orientation="vertical" android:layout_height="fill_parent" android:layout_width="fill_parent">
<LinearLayout android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="fill_parent"/>
<LinearLayout android:layout_weight="3" android:layout_height="fill_parent" android:layout_width="fill_parent"/>
</LinearLayout>
set Linear layout weight to achieve this...
<?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="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
</LinearLayout>
Basically, I am using a popular tactic to center a background image for a LinearLayout by wrapping it in a FrameLayout and adding an ImageView that uses fill_parent before the LinearLayout. However, when I add a ScrollView in the game, everything changes.
The issues is that the image is quite large vertically and the ScrollView respects the height of both the ImageView and the LinearLayout due to the wrap_content height attribute. This causes an undesired blank space below the LinearLayout when the image is vertically larger than it.
How do I make the FrameLayout's height stretch only to the child LinearLayout's height? If possible, without mangling with its sizes programmatically on run-time/draw-time.
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="center"
android:src="#drawable/cash_bg" />
<LinearLayout
android:id="#+id/main_ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<!-- My main views are here -->
</LinearLayout>
</FrameLayout>
</ScrollView>
I had the same problem and gone nuts searching for an answer. I think this is an Android bug. What I have done is to include the FrameLayout into a LinearLayout to be able to force "fill_parent" on FrameLayout's height, which increases the complexity, but solves the problem. Did not find any other (better) solution.
Try this:
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="center"
android:src="#drawable/cash_bg" />
<LinearLayout
android:id="#+id/main_ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<!-- My main views are here -->
</LinearLayout>
</FrameLayout>
</LinearLayout>
</ScrollView>
I'm having the following problem;
I have a LinearLayout with in it a ScrollView, in the ScrollView is some custom view. Now I want to put an image in the background of the LinearLayout and keep the image in its original size. But when I set the background property of the LinearLayout it stretches to fit the full screen. How can I make it so that the image keeps its original size?
My xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/some_drawable">
<ScrollView
android:id="#+id/scrollview"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:fillViewport="true">
<some.custom.layout
android:id="#+id/mainLayout"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
/>
</ScrollView>
</LinearLayout>
Code example for Egor's answer:
<FrameLayout
android:id="#+id/FrameLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/background"
/>
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
</LinearLayout>
</FrameLayout>
Try using FrameLayout with an ImageView and LinearLayout inside. For example, try changing the alpha of the image and move it to foreground in your FrameLayout, thus the LinearLayout stays on background. Hope this helps!
<RelativeLayout
android:id="#+id/relLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/layoutLogin"
android:layout_alignLeft="#id/layoutLogin"
android:layout_alignRight="#id/layoutLogin"
android:layout_alignTop="#+id/layoutLogin"
android:adjustViewBounds="true"
android:background="#mipmap/ic_photo_bg"
android:scaleType="fitXY" />
<LinearLayout
android:id="#+id/layoutLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</RelativeLayout>
Alternatively to ColdForged's suggestion, you could move from LinearLayout to RelativeLayout and use ImageView to show the image instead of changing background to it. The views in RelativeLayout can interfere unlike in LinearLayout.
You might take a look at the InsetDrawable. Either that or it would likely require you to subclass your layout to draw the way you wish as the background element doesn't have the ability you need.