21

I have a FrameLayout which contains 2 views , the second is something like a Close Button (X) and i want to position it on the right.

I've tried layout_gravity = " right ", but that didn't work.

This is my layout xml :

<FrameLayout android:layout_width="320dp"
    android:layout_height="wrap_content"
    android:id="@+id/layoutSmallImg">

    <ImageView android:id="@+id/bigImage"
        android:layout_width="320dp" android:layout_height="wrap_content"/>

    <ImageView android:id="@+id/bigImage"
        android:layout_width="320dp" android:layout_height="wrap_content"/>

    <ImageButton android:id="@+id/closebutton"
        android:background="@android:color/transparent"
        android:layout_height="30dp" android:layout_marginTop="10dp"
        android:layout_alignParentRight="true"
        android:layout_width="30dp" android:paddingLeft="40dp"
        android:visibility="gone" 
        android:src="@drawable/close" />
</FrameLayout>
cdbitesky
  • 1,390
  • 1
  • 13
  • 30
Houcine
  • 24,001
  • 13
  • 56
  • 83

6 Answers6

33

I recommend you to use a RelativeLayout instead

FrameLayout is designed to block out an area on the screen to display a single item. You can add multiple children to a FrameLayout and control their position within the FrameLayout using gravity. Children are drawn in a stack, with the most recently added child on top. The size of the frame layout is the size of its largest child (plus padding), visible or not (if the FrameLayout's parent permits). Views that are GONE are used for sizing only if setConsiderGoneChildrenWhenMeasuring() is set to true.

BTW, do you want the button on top of the ImageView or next to it? You're setting the width of both the layout and the imageView to the same size.


After the comment, I can see two options:

Community
  • 1
  • 1
Aleadam
  • 40,203
  • 9
  • 86
  • 108
  • thanks for your answer, i know that is possible with relativeLayout , i want to align it in a FrameLayout , :( yes i want the imageButton on top | right of imageView , it will looks like a Close button of My image , when i click , the Image will fade out – Houcine May 19 '11 at 15:14
  • thanks for your time, i think i will work with RelativeLayout again , i didn't find my happyness with the FrameLayout :) – Houcine May 19 '11 at 15:38
29

Read the DOCS:

FrameLayout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other. You can, however, add multiple children to a FrameLayout and control their position within the FrameLayout by assigning gravity to each child, using the android:layout_gravity attribute.

Try:

<TextView
     .....
     android:layout_gravity="right" />

You can place a view in 9 different places within a FrameLayout

Enjoy

Yotam
  • 309
  • 3
  • 4
  • this works. i believe that RelativeLayouts are more expensive to layout than FrameLayout as well. – Jon Willis Jul 23 '15 at 17:46
  • 1
    Thanks! This fixes problem when RelativeLayout takes all the screen https://stackoverflow.com/questions/6486214/relativelayout-is-taking-fullscreen-for-wrap-content – Boris Treukhov Nov 09 '17 at 13:26
9

Just put your elements inside FrameLayout to the second RelativeLayout. And use android:layout_alignParentRight="true" for the element which you want to align right.

<FrameLayout android:layout_width="320dp" android:layout_height="wrap_content" 
                                      android:id="@+id/layoutSmallImg">
 <RelativeLayout
            android:id="@+id/content"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
    <ImageView android:id="@+id/bigImage"
            android:layout_width="320dp" 
            android:layout_height="wrap_content"/>

    <ImageButton android:id="@+id/closebutton"
            android:background="@android:color/transparent"
            android:layout_height="30dp" android:layout_marginTop="10dp"
            android:layout_alignParentRight="true"
            android:layout_width="30dp" android:paddingLeft="40dp"
            android:visibility="gone" 
            android:src="@drawable/close" />
  </RelativeLayout>
</FrameLayout>
Henadzi Rabkin
  • 6,834
  • 3
  • 32
  • 39
1

Programatically, you can set the margin using FrameLayout.LayoutParams. The following would work to place a view on the bottom of your screen:

int desired_height_of_content = //whatever your want the height of your view to be
FrameLayout layout = new FrameLayout(this);
FrameLayout.LayoutParams flp = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, desired_height_of_content);
int difference = screen_height - desired_height_of_content
flp.setMargins(difference, 0, 0, 0);
layout.addView(your_view, flp);
Casey Murray
  • 1,582
  • 17
  • 21
1

Use a RelativeLayout instead.

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
0

Do you want to use FrameLayout or RelativeLayout?

My understanding says that FrameLayout is used if you want to replace your current view with another view. Your requirement can be fulfilled by RelativeLayout.

varuaa
  • 389
  • 2
  • 9
  • thanks for your reply i want to use the FrameLayout , b default , the FrameLAyout positionne Views at Top left of the frameLayout , i want to do that , but in Top Right , – Houcine May 19 '11 at 15:19
  • you can achieve what you want with FrameLayout as well. Set the layout_gravity for the ImageButton as "right". layout_alignParentRight works with RelativeLayout, I don't know if it works with FrameLayout as well. Anyone else, throw light on this. – varuaa May 19 '11 at 18:07
  • thanks, but i've tried layout_gravity="right " but it didnt work :) – Houcine May 19 '11 at 20:50
  • I actually copied your code, just removing the line, android:visibility="gone". And I added the layout_gravity="right". It works perfectly fine for me. Just the screen size of my mobile is not that big, so I get a half cut image, but i do get it aligned right, so it is working fine :) – varuaa May 20 '11 at 17:58
  • yes , i know that , i see that my logic is good enough to align the ImageButton to the right , but at last ,i've used the RelativeLayout , the essential that it works now :) Thanks for your time ;) – Houcine May 20 '11 at 19:39