Gridlayout pictures to small eclipse - android

I have a gridLayout form 3 by 3. with in there imagebuttons.
the grid is working, but i want the images to fill the weight of the parent.
this is the code i have:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.tict.MainActivity"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<GridLayout
android:id="#+id/mygrid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:columnCount="3"
android:rowCount="3" >
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<ImageButton
android:id="#+id/imageButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<ImageButton
android:id="#+id/imageButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<ImageButton
android:id="#+id/imageButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"/>
<ImageButton
android:id="#+id/imageButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"/>
<ImageButton
android:id="#+id/imageButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<ImageButton
android:id="#+id/imageButton8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<ImageButton
android:id="#+id/imageButton9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</GridLayout>
I want to keep the gridLayout if that is posible.
EDIT: I want 3 image buttons at the top row. All 3 being 1/3 weight of the parent.

You are using layout_width and layout_height properties with
wrap_content : , which means that the view wants to be just big enough to enclose its content (plus padding)
change to:
match_parent : which means that the view wants to be as big as its parent (minus padding)

I could't find a solution, so made a nested LiniarLayout.
Part of the new code is now:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/background">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/TG" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/ib_1"
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight=".30"
android:background="#android:color/white"
android:contentDescription="#string/btn"
android:onClick="onClick"
android:scaleType="fitXY"
android:layout_margin="5dp"
/>

Related

Android: keeping overlay textview size same as underlying imageview

I have an image (.png) that is sliced into 3 pieces top, middle, bottom.
(The pieces mentioned are not of the same size).
What I am trying to achieve is to have a layout where the original image is assembled from the above 3 pieces - but the middle piece should also display a textview over it.
In a sense, I have managed the most of the above, however with my current layout - when the text of the textview exceeds some length (horizontally or vertically) the image is then broken, as then there is a gap between middle and bottom part (vertical excess) or the middle part is shifted related to the top and the bottom (horizontal excess).
Actually what I want to achieve is that the textview will always remain the same size as the underlying middle imageview.
My layout code is as follows (The code will be used for android 4.0 +):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#88666666"
android:id="#+id/top_layout">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingTop="25dp" >
<ImageView
android:id="#+id/ivTop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingTop="0dp"
android:layout_marginRight="15dp"
android:clickable="false"
android:paddingLeft="20dip"
android:scaleType="center"
android:src="#drawable/quotetops" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/ivMiddle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingTop="0dp"
android:layout_marginRight="15dp"
android:clickable="false"
android:paddingLeft="20dip"
android:scaleType="center"
android:src="#drawable/quotemiddles" />
<TextView
android:id="#+id/mytext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
android:text="ajsdhaj\n\n\n\n\n\n\nhdjajhsdjfhsdjdfjsdjfksdjfkjkfj\ndkfjkdjfkj"
/>
</RelativeLayout>
<ImageView
android:id="#+id/ivBottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingTop="0dp"
android:layout_marginRight="15dp"
android:clickable="false"
android:paddingLeft="20dip"
android:scaleType="center"
android:src="#drawable/quotebottoms" />
</LinearLayout>
</RelativeLayout>
Thanks.
do this for all three children of LinearLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:weight="1dp"
for middle element do this for children of RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
Also you can ditch top RelativeLayout as it is redundant. Your final layout file should look like this
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="25dp"
android:background="#88666666"
android:id="#+id/top_layout">
<ImageView
android:id="#+id/ivTop"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="0dp"
android:paddingTop="0dp"
android:layout_marginRight="15dp"
android:clickable="false"
android:paddingLeft="20dip"
android:scaleType="center"
android:src="#drawable/quotetops" />
<FrameLayout
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="0dp">
<ImageView
android:id="#+id/ivMiddle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_alignParentRight="true"
android:paddingTop="0dp"
android:layout_marginRight="15dp"
android:clickable="false"
android:paddingLeft="20dip"
android:scaleType="center"
android:src="#drawable/quotemiddles" />
<TextView
android:id="#+id/mytext"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
android:text="ajsdhaj\n\n\n\n\n\n\nhdjajhsdjfhsdjdfjsdjfksdjfkjkfj\ndkfjkdjfkj"
/>
</FrameLayout>
<ImageView
android:id="#+id/ivBottom"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="0dp"
android:layout_alignParentRight="true"
android:paddingTop="0dp"
android:layout_marginRight="15dp"
android:clickable="false"
android:paddingLeft="20dip"
android:scaleType="center"
android:src="#drawable/quotebottoms" />
</LinearLayout>
Finally, solved it by using android:layout_align... and referencing the middle imageview.
(I also, removed the unnecessary android:layout_alignParentRight="true" from middle imageview - but that was not the main problem here)
So, the final layout became:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#88666666"
android:id="#+id/top_layout">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingTop="25dp" >
<ImageView
android:id="#+id/ivTop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingTop="0dp"
android:layout_marginRight="15dp"
android:clickable="false"
android:paddingLeft="20dip"
android:scaleType="center"
android:src="#drawable/quotetops" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/ivMiddle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="0dp"
android:layout_marginRight="15dp"
android:clickable="false"
android:paddingLeft="20dip"
android:scaleType="center"
android:src="#drawable/quotemiddles" />
<TextView
android:id="#+id/mytext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
android:text="ajsdhaj\n\n\n\n\n\n\nhdjajhsdjfhsdjdfjsdjfksdjfkjkfj\ndkfjkdjfkj"
android:layout_alignLeft="#+id/ivMiddle"
android:layout_alignRight="#+id/ivMiddle"
android:layout_alignTop="#+id/ivMiddle"
android:layout_alignBottom="#+id/ivMiddle"
/>
</RelativeLayout>
<ImageView
android:id="#+id/ivBottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingTop="0dp"
android:layout_marginRight="15dp"
android:clickable="false"
android:paddingLeft="20dip"
android:scaleType="center"
android:src="#drawable/quotebottoms" />
</LinearLayout>
</RelativeLayout>

Android XML - Unable to Prevent ImageView Overlap

I'm attempting to prevent two images from overlapping - and I though I'd be able to do so using two RelativeLayouts inside a LinearLayout - both set to wrap_content - however the two imageViews ( #+id/imageView1 - the boxart and #+id/background - the background) however they still seem to overlap.
Can someone spot what I may have done wrong in this implementation?
XML Source:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/download"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/black" >
<RelativeLayout
android:id="#+id/rl_ListView2"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#color/black" >
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"
android:background="#drawable/boxart"
android:gravity="left"
android:paddingBottom="65dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/background"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:background="#drawable/background_faded"
android:gravity="right"
android:paddingBottom="65dp"
android:scaleType="fitXY" />
</RelativeLayout>
<ImageView
android:id="#+id/downloadbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:layout_marginTop="200dp"
android:paddingLeft="500dp"
android:src="#drawable/button_download" />
<ProgressBar
android:id="#+id/progressbar_Horizontal"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="585dp"
android:layout_height="5dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginBottom="440dp"
android:layout_marginRight="100dp"
android:max="100"
android:progressDrawable="#drawable/progressbar2" />
<LinearLayout
android:id="#+id/footer"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:layout_alignParentBottom="true"
android:background="#drawable/timeline_bottom_android"
android:orientation="horizontal" >
<ImageView
android:id="#+id/backbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:gravity="left"
android:src="#drawable/icon_back_arrow" />
<TextView
android:id="#+id/backButtonTxt"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignRight="#+id/saveButton"
android:gravity="center_vertical"
android:text="Movies"
android:textSize="40sp" />
</LinearLayout>
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/downloadbtn"
android:layout_below="#+id/downloadbtn"
android:layout_marginRight="207dp"
android:layout_marginTop="110dp" />
</RelativeLayout>
</LinearLayout>
Screenshot:
Instead of using Relative layout use a Linear layout with orientation horizontal and then use two Linear layouts with layout_weight=1 for each ImageView. It will divide your screen into two equal parts horizontally.
For example :-
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical" />
<LinearLayout android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical" />
</LinearLayout>
As per your given layout update your layout as follows:-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/download"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/black"
android:orientation="vertical">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/rl_ListView2"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="#drawable/boxart"
android:paddingBottom="65dp" />
</LinearLayout >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/background"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:background="#drawable/background_faded"
android:gravity="right"
android:paddingBottom="65dp"
android:scaleType="fitXY" />
</RelativeLayout>
<ImageView
android:id="#+id/downloadbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:layout_marginTop="200dp"
android:paddingLeft="500dp"
android:src="#drawable/button_download" />
<ProgressBar
android:id="#+id/progressbar_Horizontal"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="585dp"
android:layout_height="5dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginBottom="440dp"
android:layout_marginRight="100dp"
android:max="100"
android:progressDrawable="#drawable/progressbar2" />
<LinearLayout
android:id="#+id/footer"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:layout_alignParentBottom="true"
android:background="#drawable/timeline_bottom_android"
android:orientation="horizontal" >
<ImageView
android:id="#+id/backbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:gravity="left"
android:src="#drawable/icon_back_arrow" />
<TextView
android:id="#+id/backButtonTxt"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignRight="#+id/saveButton"
android:gravity="center_vertical"
android:text="Movies"
android:textSize="40sp" />
</LinearLayout>
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/downloadbtn"
android:layout_below="#+id/downloadbtn"
android:layout_marginRight="207dp"
android:layout_marginTop="110dp" />
</LinearLayout>
With regards to your particular situation, the 2 ImageViews causing problems are placed each in a RelativeLayout, which are also placed in a RelativeLayout. Views placed in a relative layout can overlap each other. So when you added the 2 children RelativeLayouts in the parent one, exactly this happened. To make one follow the other, you can add android:layout_toRightOf="#id/id_of_the_layout_you_want_on_the_left_of_this_one" to the second child.
Also, if you are willing to change your layout a bit, you could achieve this through weight and LinearLayout (note however that this will work only if the desired combined width of the 2 children is less than what the parent is willing to offer):
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
... />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
... />
</LinearLayout>
</LinearLayout>

How to obtain this xml android layout

I've seen this layout ( http://cdn0.tnwcdn.com/wp-content/blogs.dir/1/files/2014/03/Instaedit1.jpg ) and i wanna do somthing like that but i don't know what i have to do to obtain that result.
i've tried with a relative layout which contains a imageview and a gridlayout which contains three imagebutton.
<RelativeLayout 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"
tools:context="com.example.prova.MainActivity$PlaceholderFragment" xmlns:app="http://schemas.android.com/apk/res/com.example.prova">
<ImageView
android:id="#+id/imageView1"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/profile" />
<android.support.v7.widget.GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/imageView1"
app:columnCount="3"
app:orientation="horizontal"
app:rowCount="1"
app:useDefaultMargins="false" >
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_gravity="fill_horizontal"
android:scaleType="center"
android:src="#drawable/friend" />
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_gravity="fill_horizontal"
android:maxHeight="100dp"
android:src="#drawable/fotocamera" />
<ImageButton
android:id="#+id/imageButton3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/eventi" />
</android.support.v7.widget.GridLayout>
</RelativeLayout>
But i don't obtain the same result of the top of the image.
I've tried changing the gravity value and width value but nothing.
Can you help me??? (sorry for my bad english :P Thanks).
I worked on your code to obtain this: http://it.tinypic.com/view.php?pic=2aj1cms&s=8#.UyhKXtT5OBM
It's a better result but i don't understand why there are s space between the imagebutton.
My code is this:
<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/com.example.prova"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.prova.MainActivity$PlaceholderFragment" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="4" >
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/profile" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="3"
android:orientation="vertical"
android:weightSum="3" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:weightSum="3" >
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="#drawable/friend" />
<ImageButton
android:id="#+id/imageButton3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="#drawable/fotocamera" />
<ImageButton
android:id="#+id/imageButton4"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="#drawable/eventi" />
</LinearLayout>
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Aggiungi agli amici" />
</LinearLayout>
</LinearLayout>
Any idea??

How to align three ImageViews horizontally in Android?

I want to show three ImageViews at the bottom of the screen with equal weight. But the size of the images is not fixed. I want to show 2 images and one should be hidden, what should I do?
How to adjust the space in between three ImageViews which should be side by side of each other, and the 3rd ImageView should not overlap the two ImageViews which are visible on the screen?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/main_imge_1"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#F9F939"
android:src="#drawable/ic_launcher"
android:layout_toLeftOf ="#+id/imageView6"
android:layout_alignParentBottom="true"
android:padding="15dp" />
<ImageView
android:id="#+id/imageView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#54F71D"
android:src="#drawable/ic_launcher"
android:layout_alignParentBottom="true"
android:padding="15dp"
android:layout_margin="10dp" />
<ImageView
android:id="#+id/imageView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#1DF7AB"
android:src="#drawable/ic_launcher"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#+id/imageView6"
android:padding="15dp" />
</RelativeLayout>
Try this way
<?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:gravity="center|bottom" >
<ImageView
android:id="#+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#F9F939"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#54F71D"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#1DF7AB"
android:src="#drawable/ic_launcher" />
</LinearLayout>
Output
From what I was able to decrypt, you're probably trying to achieve this:
<?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"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal">
<ImageView
android:id="#+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#F9F939"
android:src="#drawable/ic_launcher"
android:padding="15dp"
android:layout_weight="1"
/>
<ImageView
android:id="#+id/imageView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#F9F939"
android:src="#drawable/ic_launcher"
android:padding="15dp"
android:layout_weight="1"
/>
<ImageView
android:id="#+id/imageView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#F9F939"
android:src="#drawable/ic_launcher"
android:padding="15dp"
android:layout_weight="1"
/>
</LinearLayout>
</RelativeLayout>
The result:
The key is to use an enclosing LinearLayout to hold your ImageViews and having that LinearLayout aligned to Bottom in your RelativeLayout.
Use android:layout_weight="1" for each ImageView. And replace your RelativeLayout to Linearlayout.

android - imagebutton - place top and bottom?

i want, that the first imagebutton fill the top place and the second imagebutton fill the bottom place. Did I forget something?
<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" >
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#color/white"
android:src="#android:drawable/btn_star"
android:layout_gravity="top" />
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:background="#color/white"
android:src="#android:drawable/btn_star"
android:layout_gravity="bottom" />
</LinearLayout>
The problem is that you need to use a RelativeLayout Try:
<RelativeLayout 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" >
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#ffffff"
android:src="#android:drawable/btn_star"
android:layout_gravity="top" />
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:background="#ffffff"
android:src="#android:drawable/btn_star"
android:layout_gravity="bottom" />
</RelativeLayout>
Hope that helps. Tell me if that is what you want to accomplish, or if you want both buttons to fill halve the height of the screen equally.
If is then second option then try this:
<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" >
<ImageButton
android:id="#+id/imageButton1"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#ffffff"
android:src="#drawable/icon"
/>
<ImageButton
android:id="#+id/imageButton2"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#ffffff"
android:src="#drawable/icon"
/>
</LinearLayout>
Which would look like this:
Try using Relative layout. Using which you can specify the positions of your buttons much easier.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#color/white"
android:src="#android:drawable/btn_star"
android:layout_gravity="top" />
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/imageButton1"
android:background="#color/white"
android:src="#android:drawable/btn_star"
android:layout_gravity="bottom" />
</RelativeLayout>
you can use more positions like layout_below, and its easier to align.
for more positions, http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html
If by "fill" you mean it should take full width, use fill_parent instead of wrap_content for android:layout_width. Otherwise you should use RelativeLayout as your container instead of LinearLayout:
<RelativeLayout 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" >
<ImageButton
...
android:layout_gravity="top" />
<ImageButton
...
android:layout_gravity="bottom" />
</RelativeLayout>

Categories

Resources