I'm triying to put an ImageView in bottom right of LinearLayout ....
I wrote the code as bellow :
style.xml:
<?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="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="90dp"
android:background="#drawable/my_background"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:src="#drawable/my_small_image" />
</LinearLayout>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
This code results in the ImageView 'my_small_image' at the bottom right, but I need it in the bottom left.
Anyone have any ideas about this?
I hope somebody here can help me.
Best regards and thanks in advance, Fadel.
Using LinearLayout is possible:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:orientation="vertical">
<!-- other elements in linear layout here -->
<!-- making a RelativeLayout un-desirable -->
<ImageView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="1"
android:scaleType="fitEnd"
android:src="#drawable/your_image" />
</LinearLayout>
This will position the image at the center-bottom, for bottom-right use
android:layout_gravity="right"
Use RelativeLayout
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="90dp"
android:background="#drawable/my_background"
android:layout_alignParentBottom="true"
>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="#drawable/my_small_image" />
</RelativeLayout>
In LinearLayout you can use -
android:layout_gravity="bottom|right"
I would recomend Using RelativeLayout but if you have to useLinearLayout i know that android:scaleType" can help center or move image, did you try android:scaleType="fitEnd" or android:scaleType="fitXY"
for more Try This
Related
I have this simple activity in my android app but I don't know how to place my button so it appears at the same position on various screen sizes.
I want the button to always be placed at the bottom of the screen. The screenshot shown is from a phone screen. When displayed on a larger screen the button is higher up.
How can I fix this?
Thanks
See this screenshot
You can use RelativeLayout with this parameter (android:layout_alignParentBottom="true")
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Click Me!"/>
</RelativeLayout>
You should use a RelativeLayout
If you have a RelativeLayout that fills the whole screen you should be able to use android:layout_alignParentBottom to move the button to the bottom of the screen.
If your button at the bottom doesn't show then probably the layout above it takes all the space. In this case you can put the button first in your layout file and position the rest of the layout above the views with android:layout_above.
Have a look a RelativeLayout. Use this ViewGroup and add a Button inside it with alignParentBottom
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
<Button
android:id="#+id/button_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:alignParentBottom="true"
android:text="HORAIRE"
android:layout_margins="<your_margin_in_dp>" />
</RelativeLayout>
Make footerButton.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="HORAIRE"
android:textColor="#ffffff"
android:textSize="20sp" />
</RelativeLayout>
And add it in all your layout file like below line:
Android Activity_Layout
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- Footer aligned to bottom -->
<include layout="#layout/footer" />
<!-- Content above footer -->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#id/footer"
android:layout_below="#id/header"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Content goes here"
android:textColor="#FFFFFF"
android:textSize="20sp" />
</RelativeLayout>
</RelativeLayout>
Hope it will solve your problem
This is what I ended up doing. Thanks to everyone who posted, it helped me get in the right direction!
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="#+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_main"
tools:context="xxxx.MainActivity"
android:orientation="vertical"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dernières nouvelles Twitter"
android:id="#+id/textView"
android:textStyle="bold"
android:textSize="17dp"
android:paddingLeft="8dp"
android:paddingTop="6dp"
android:paddingRight="6dp"
android:paddingBottom="6dp"
android:layout_alignParentTop="true"/>
<ListView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="#+id/listView_tweets"
android:layout_above="#+id/buttonDisplay"
android:choiceMode="none"
android:paddingTop="25dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Horaire"
android:layout_margin="5dp"
android:id="#+id/buttonDisplay"
android:onClick="buttonGotoDisplay"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
I have looked up similar problems but their solutions aren't working. My Relative Layout is adding Left padding.
This is my XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:fillViewport="true"
android:padding="0dp" >
<ImageView
android:id="#+id/imageViewUserProfile"
android:layout_width="114dp"
android:layout_height="98dp"
android:layout_alignParentTop="false"
android:layout_margin="0dp"
android:adjustViewBounds="true"
android:padding="0dp"
android:src="#drawable/default_t" />
<TextView
android:id="#+id/textViewUserProfileName"
android:layout_width="fill_parent"
android:layout_height="44dp"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/imageViewUserProfile"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textViewUserProfileHighlights"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_toRightOf="#id/imageViewUserProfile"
android:layout_below="#id/textViewUserProfileName"/>
</RelativeLayout>
</LinearLayout>
I removed the parenting LinearLayout to double check that the left padding was coming from atleast the RelativeLayout block. I have tried most of what worked for other people. Any help or suggestions would be greatly appreciated.
For the ImageView set alignParentTop and alignParentLeft to true also remove the LinearLayout, it is unnecessary.
this is my code which place image on top senter of screen how i place image on center of screen? not top center my screen look like this http://imgur.com/65CD6
i wanna place on center of screen not op center
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="#+id/RelativeLayout01"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_gravity="center_vertical|center_horizontal"
android:background="#ffffff"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<ImageView android:id="#+id/ImageView01"
android:src="#drawable/agapplogo"
android:layout_width="wrap_content"
android:layout_marginLeft="60dip"
android:layout_height="wrap_content">
</ImageView>
<TextView android:id="#+id/TextView01"
android:text="#string/welcome"
android:layout_below="#id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</RelativeLayout>
You can try to use: android:layout_centerInParent="true" like:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_centerInParent="true"
android:src="#drawable/ic_launcher"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
You can either play with android:gravity="center" in <RelativeLayout>
OR
include android:centerInParent="true" inside ImageView
place android:gravity="center" for RelativeLayout
Just give your ImageView in xml this attribute:
android:layout_centerHorizontal="true"
this may happen due to RelativeLayout,try using LinearLayout
like the title said it there are some "space" before and after an image and i don't want it.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/fond"
>
<ImageView
android:id="#+id/imageView1"
android:contentDescription="#string/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/top"
android:layout_gravity="top"
/>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/fond1"
>
</LinearLayout>
</LinearLayout>
So like you can see i have a first Layout with a background.
The ImageView is just after and should be on the top but it didn't.
The second Layout is to much after the image.
->
first layout
--unwanted space--
image
--unwanted space--
second layout
If anyone know how to delete this "space", thanks.
Use android:adjustViewBounds="true" inside your ImageView. That way your ImageView will be adjusted to the provided image bounds.
Use Relative layout this Way::
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/fond"
>
<ImageView
android:id="#+id/imageView1"
android:contentDescription="#string/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/top"
android:layout_gravity="top"
android:layout_alignParentTop="true"
/>
<LinearLayout
android:layout_below="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/fond1"
>
</LinearLayout>
</RelativeLayout>
I have following in xml
I wanna put the second linear layout at the bottom of the screen.
How will i do?
I have set the property of second Relative layout to bottom but still not showing at bottom..
**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<RelativeLayout android:id="#+id/RelativeLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</RelativeLayout>
<RelativeLayout android:id="#+id/RelativeLayout02"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</RelativeLayout>
<RelativeLayout android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_gravity="bottom">
</RelativeLayout>
</LinearLayout>
**
Thanx in advance
I was searching for this a while too. My just found solution is to use the property:
android:layout_alignParentBottom="true"
instead of your existing android:layout_gravity="bottom"
This unfortunately with my layout only works when having an additional RelativeLayout as root layout and the rest added to it. Maybe this is not always the case, but with having a LinearLayout (even when told to use fill_parent) it only used the height all added elements needed and put my footer at the bottom of those.
I came to this solution, looking at this related question: How do I achieve the following result using RelativeLayout?
EDIT here because format in answer comment got lost:
Do you mean you can't re-write it or that it didn't work?
I had a vertical LinearLayout too and my new xml structure looks (without the layout size params etc.) sth. like this:
<RelativeLayout>
<RelativeLayout
android:id="#+id/RelativeLayout01">
</RelativeLayout>
<RelativeLayout
android:id="#+id/RelativeLayout02"
android:layout_below="#+id/RelativeLayout01">
</RelativeLayout>
<RelativeLayout
android:layout_below="#+id/RelativeLayout02"
android:layout_alignParentBottom="true">
</RelativeLayout>
</RelativeLayout>
The Layout you want at the bottom should have:
android:gravity = "bottom"
and its parent should have:
android:layout_height="fill_parent"
<Linearlayout android:layout_height="fill_parent"
android:orinationtation="vertical"
android:layout_width="fill_parent">
<RelativeLayout android:layout_height="0dp"
android:layout_width="fill_parent"
android:weight="1">
</RelativeLayout>
<RelativeLayout
android:layout_height="0dp"
android:layout_width="fill_parent"
android:weight="1"
>
</RelativeLayout>
</Linearlayout >
You should put a View before your last layout with weight 1, and it will put it to bottom. Like this:
<View android:id="#+id/emptySpace"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
Try using:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffb3e5fc"
android:id="#+id/linearLayout1">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom">
<Button
android:id="#+id/button1"
android:text="Left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/centerPoint" />
<TextView
android:id="#+id/centerPoint"
android:text=""
android:layout_width="1dip"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
<Button
android:id="#+id/button2"
android:text="Right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_toRightOf="#+id/centerPoint" />
</RelativeLayout>
</LinearLayout>
It produces something that looks like this
For my it's work.
<RelativeLayout
android:id="#+id/InnerRelativeLayout"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#fff"
android:gravity="bottom"
app:layout_anchor="#+id/fab_inout"
app:layout_anchorGravity="bottom|center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="dsfsdfasdf df" />
</RelativeLayout>