Layout with image and "bottom bar" issues - android

I am fairly new at the whole Android thing so I am hoping this is something obvious that I am overlooking since I have not been able to find a solution online yet.
I am creating a view which contains an ImageView and a "bottom bar". The problem I am having is that since the image is larger than the screen and I would prefer to use something similar to "fill_parent" or "wrap_content". Ideally I would set the ImageView's height to "fill_parent-44px", but I have not found a way to do this in the XML.
Eventually I plan to have the ImageView function with multi-touch zoom, so it is therefore not an option to resize the image for different screen resolutions.
Thanks for any help.

It's easy to do this with a RelativeLayout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<View android:id="#+id/bottombar"
android:background="#FFCC00"
android:layout_height="40dp"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true" />
<ImageView android:id="#+id/image"
android:src="#drawable/someimage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#id/bottombar"/>
</RelativeLayout>
The important things here are android:layout_alignParentBottom and android:layout_above (see RelativeLayout.LayoutParams for all possible layout attributes).
The android:layout_height="fill_parent" is more or less ignored given the right RelativeLayout parameters, it just needs to be there to please the Android UI system.
The android:background is just for highlighting in the UI designer, and the bottombar View can be replaced by any other view like, TextView, LinearLayout, etc.

Related

API19 doesn't show Child with layout_height="match_parent" in a RelativeLayout with layout_height="wrap_content" in a RecyclerView

I have a RecyclerView that has to inflate the following layout in the ViewHolder:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.percent.PercentRelativeLayout
android:id="#+id/imageRelative"
....
....
<LinearLayout
android:id="#+id/premiumBlockerView"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:alpha="0.5"
android:background="#color/Black"
android:layout_alignBottom="#id/linearLayout1" />
</RelativeLayout>
The RelativeLayout's content changes (image, video, text, etc.) so I'm not able to know the height (the width is the width of the device).
I have to add a view (my premiumBlockerView LinearLayout) that I will show as semi transparent glass or not depends on content type.
The problem is that the height=match_parent in the premiumBlockerView doesn't work and it is allways zero. So, like others post suggested, I added the layout_alignParent and alignBottom parameters and this work arround solution works propery for APIs greater than 20.
Now the application must to work for API 19 and this work arround doesn't work.
Do you have any suggestion?
Thanks!!!
I face the same problem too. I am sorry that i still can not find a perfect solution in RelativeLayout, But you can solve this problem by using ConstraintLayout or another layout instead of RelativeLayout.
Note:
if you set parent RelativeLayout
android:layout_height="wrap_content">
and child view
android:layout_height="match_parent"
in RecylerView.
then the child view will get
"wrap_content"
instead of match_parent, you can find why by reading source code of RelativeLayout onMeasure method.

Left aligning images in LinearLayout using gravity or linear_gravity in XML

First off, this is not a duplicate question, to best of my ability I've tried all (there are many) similar questions. Solutions to such problems appear to be very subjective, specific to a given scenario.
My layout currently appears as follows. Black boxes are images (logo and body, respectively), colours represent each layout:
My XML:
<?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:background="#000"
android:padding="0px"
android:layout_margin="0px"
android:orientation="vertical">
<LinearLayout
android:layout_weight="16"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFF"
android:gravity="top|center"
android:orientation="horizontal">
<ImageView
android:id="#+id/logo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/logo"
android:layout_gravity="top|center" />
</LinearLayout>
<LinearLayout
android:layout_weight="4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00F"
android:gravity="bottom|left"
android:orientation="vertical">
<ImageView
android:id="#+id/body"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/body"
android:layout_gravity="bottom|left" />
</LinearLayout>
</LinearLayout>
Here you can see I have a parent linear layout, split into two children linear layouts. This is because I need the images to be positioned differently within that part of the page.
In a nutshell, I need logo to be vertically aligned to the top, and body horizontally aligned to bottom-left.
Now, a few things that I've tried:
Using RelativeLayout rather than Linear
Switching gravity with layout_gravity for both LinearLayout and ImageView, along with combinations of excluding each
Fairly confident match_parent for width and height is what I want, but I have tried different combinations with wrap_content
What I've come to understand:
gravity:top requires the parent view use orientation:horizontal
gravity:left requires the parent view use orientation:vertical
gravity applies to the children of the view
linear_gravity applies how the child aligns with it's parent
Using the same value for gravity on the parent and linear_gravity on the child might have the same effect (when using one instead of the other)?
Hopefully this is enough information. I'm having a very difficult time wrapping my head around how these layouts work.
Thank you SO much for the help!
I think your problem is you are setting dimensions of the image views to match_parent. I would use a RelativeLayout as it seems to be the most efficient in your case (pseudo-XML-code):
RelativeLayout (width=match_parent, height=match_parent)
ImageView (width=wrap_content, height=wrap_content,
alignParentTop=true, centerHorizontal=true)
ImageView (width=wrap_content, height=wrap_content,
alignParentBottom=true, alignParentLeft=true)
You don't need any gravity setting here. You might want to play with the scaleType attribute depending on your image sizes.

ImageView won't fill parent

I have a ScrollView on one of my screens. I want the right edge to have a shadow. I decided the easiest way to do this was to make the child of the ScrollView a RelativeLayout, and have two children of the RelativeLayout -- one being a LinearLayout that will house the layout of the screen, and the second View being the shadow.
Like so...
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="none" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- stuff -->
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="#drawable/shadow"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
</ScrollView>
Unfortunately, this doesn't quite work. The ImageView is forcing its dimensions to be the size of the image file. It will not stretch vertically to be the height of the RelativeLayout. I've also tried "match_parent" to no avail. The image is a 9-patch.
Ideas?
Applying drawable content as the source of an ImageView somewhat carries with it an inherent requirement that you want the view to do what it can to accomodate the content without modifying the content itself very much. Typically, this is the behavior you would want out of an ImageView.
What you really want is the behavior you get by setting drawable content as the background of a view, for which you don't really need ImageView at all. A background is designed to simply stretch, fill, etc. to whatever size the view is. Also, since you are using RelativeLayout you can tell the view to match the bound of the view you are shadowing by adding an id and some extra layout_alignparameters.
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/content_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- stuff -->
</LinearLayout>
<View
android:layout_width="11dp"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignTop="#id/content_layout"
android:layout_alignBottom="#id/content_layout"
android:background="#drawable/shadow"
/>
</RelativeLayout>
try this
<ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#drawable/ic_launcher"
android:scaleType="fitXY"
android:layout_alignParentRight="true"
/>
here is what I get
and code id
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:scrollbars="none" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- stuff -->
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:background="#drawable/ic_launcher"
android:scaleType="fitXY" />
</RelativeLayout>
</ScrollView>
Your problem has nothing to do with the ImageView or 9-patch itself, but rather with the fact that you're wrapping everything in a ScrollView. A ScrollView will automatically force its children direct child to wrap its content, no matter whether you tell it to FILL_PARENT or MATCH_PARENT - both do exactly the same thing by the way; the only difference is the name, which reflects better the actual behaviour of the flag.
Fortunately ScrollView provides a way to force it to fill the viewport with a flag, which will make the behaviour pretty similar to setting FILL_PARENT to a regular view. Either add the attribute android:fillViewport or use setFillViewport() from code.
Edit: Just to be clear, you need to set that flag on the ScrollView. Also, if it's the ScrollView that should have the shadow, can you not send your 9-patch as background to it? I suppose it does depend on what your actual image looks like. Regarding you comment: yes, the RelativeLayout is flexible in terms of positioning and sizing children, but any child will still be bound to the size of its parent.
I do have the feeling that some of us may be working towards something different than what you have in mind. It would definitely help to clarify things with a simple drawing.
You wanted a Shadow towards the right of your image, Then use single layout with Horizontal Orientation, It's good that you have decide to use Relative Layout. Use
android:orientation="vertical"
inside this layout, add those two images. If you still have a doubt, give me those two images or sample images, i will give you the code

Android - Background at bottom

I should like to have an android app which has its background image on the bottom of the view.
Hopefully this could be achieved by only using XML.
My background image (bg.png) is located at the folder "res/drawable-mdpi".
The XML code is now:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/bg"
android:layout_alignParentBottom="true"
android:layout_marginBottom="0dip" />
</LinearLayout>
But this is stretching the whole image over the whole page. I only want it stretched in horizontal direction.
The height has to be in proportion with the (stretched) width.
And this image has to be the background of a view/layout, I want to keep using the layout and add items/objects to it.
I hope someone could help me with this, I've been looking on the internet for it for a few hours and I couldn't find anything more helpful then I have now.
Thanks in advance,
Dennis
Firstly Change your LinearLayout to a RelativeLayout (android:layout_alignParentBottom only works with RelativeLayout).
Secondly use android:src="#drawable/bg" on your ImageView (not background)
if you really want to use the LinearLayout, you can try adding this attribute to your imageview android:layout_gravity="bottom"

Positioning a view with top expressed as a percentage of display height

I just know this is simple and in about 30 minutes time, I'll hate myself...
I have a splashscreen which consists of a static image which fills the screen. So I simply set the background attribute of whatever root view I use in my layout.
The image has a blank area over which I need to place an "I accept" button. To deal with different resolutions, I must position it using a percentage of the display height - 58% is the spot.
I can't use layout_weight because that sizes the button and absolutelayout (setting the y position in code) is deprecated.
How can I achieve this? I don't care what viewgroup is the parent and I'm fine with having "blank" views filling up space.
I am aiming to do this entirely in layout XML to keep my code clean...
Thanks!
You say you can't use layout_weight, but that's your only option if you want to do it purely in XML. I don't understand why you think you can't use it anyway. Here's an example of how you might do it:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/splash"
android:orientation="vertical" >
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="58" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="42" >
<!-- Place buttons here -->
</LinearLayout>
</LinearLayout>
I don't see any other way that to use a layout_weight... Also the whole class AbsoluteLayout is deprecated, so try to avoid using it. I suggest to use an LinearLayout as your rootView with a given weight_sum of 1. add another Space-filling LinearLayout width a weight of 0.58 and below your Button with wrap_content attributes. Unfortunately I cannot tell you more unless you post your xml, so that I can see, what you try to achieve.
Kind of this should work:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/your_desired_background"
android:orientation="vertical"
android:weight_sum="1">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight=".58" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>

Categories

Resources