android linearlayout baseline align - android

I have 3 imageviews but since they overlap i wont make them clickable, i want make buttons on top of each imageview(but smaller).
I know in RelativeLayout there is easy way out using align_baseline but it is very important that i use LinearLayout for these images because they use layout_weight
And its important that button is connected with imageview, not just appearing on top of it
And here is my code it might help
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
...
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/twitter"
android:layout_alignParentLeft="true"
android:orientation="vertical"
android:weightSum="3"
android:layout_marginBottom="25dp"
android:layout_alignParentTop="true" >
<ImageView
android:id="#+id/imgDis"
android:layout_weight="1"
android:layout_width="300dp"
android:layout_height="120dp"
android:layout_marginBottom="-20dp"
android:background="#drawable/img1" />
<ImageView
android:id="#+id/imgCal"
android:layout_weight="1"
android:layout_width="300dp"
android:layout_height="120dp"
android:layout_gravity="right"
android:layout_marginBottom="-25dp"
android:background="#drawable/img2"
android:paddingLeft="25dp" />
<ImageView
android:id="#+id/imgDe"
android:layout_weight="1"
android:layout_width="300dp"
android:layout_height="120dp"
android:background="#drawable/img3" />
</LinearLayout>
...
</RelativeLayout>

put the image in another linear layout vertical and place that layout inside your mainlayout and give the wieght to this linearlayout instead of the image

Here is your answer, while using weight you must define 0dp to height/width to view as per case.
You want Button on every Imageview something like below?
I tried, check below xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="25dp"
android:orientation="vertical"
android:weightSum="6" >
<Button
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginTop="5dp"
android:layout_weight="0.5"
android:text="button first" />
<ImageView
android:id="#+id/imgDis"
android:layout_width="300dp"
android:layout_height="0dp"
android:layout_marginTop="5dp"
android:layout_weight="1.5"
android:background="#drawable/logo" />
<Button
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginTop="5dp"
android:layout_weight="0.5"
android:text="button first" />
<ImageView
android:id="#+id/imgCal"
android:layout_width="300dp"
android:layout_height="0dp"
android:layout_gravity="right"
android:layout_marginTop="5dp"
android:layout_weight="1.5"
android:background="#drawable/logo"
android:paddingLeft="25dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginTop="5dp"
android:layout_weight="0.5"
android:text="button first" />
<ImageView
android:id="#+id/imgDe"
android:layout_width="300dp"
android:layout_height="0dp"
android:layout_marginTop="5dp"
android:layout_weight="1.5"
android:background="#drawable/logo" />
</LinearLayout>
</RelativeLayout>

Related

why is my layout_weight not working?

I have this xml file.I can't understand why my second linearlayout which contains editText and callButton isn't looking right. It looks like this (the line with the part picture of phone) :
I have the layout_weights set to 1 in each of these views, editText and callButton, so the phone image should take up half the screen, right? Here's my code. I would like both views to take up the same width - and the editText shouldn't stretch when text is entered, as is currently happening. Thanks for any help.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:paddingBottom="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
tools:context=".MainActivity">
<ListView
android:id="#+id/ListView_2"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight="1">
</ListView>
<LinearLayout
android:id="#+id/LinearLayout2"
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal"
>
<!--android:adjustViewBounds="true"-->
<!-- we want the text to begin in the centre of the edittext view
that's what gravity does-->
<EditText
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/editText"
android:inputType="phone"
android:gravity="center"
android:background="#d9d9d9"
android:layout_weight="1"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/callButton"
android:src="#drawable/call"
android:clickable="true"
android:onClick="softkeyboardButton"
android:background="#ffa62b"
android:layout_weight="1"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal" >
<CheckBox
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:background="#ffa500"
android:text="New CheckBox"
android:id="#+id/checkBox"
android:layout_weight="4"
/>
<ImageButton
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:id="#+id/imageButton"
android:src="#drawable/softkeyboard"
android:clickable="true"
android:onClick="softkeyboardButton"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:background="#drawable/buttonstates"
/>
<View android:layout_height="fill_parent"
android:layout_width="2px"
android:background="#90909090"/>
<ImageButton
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:id="#+id/imageButton2"
android:src="#drawable/settingsicon"
android:background="#drawable/buttonstates"
android:layout_gravity="center_horizontal"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
Change your EditText and Call button with layout_width to "0dp" -
<EditText
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="#+id/editText"
android:inputType="phone"
android:gravity="center"
android:background="#d9d9d9"
android:layout_weight="1"
/>
<ImageButton
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="#+id/callButton"
android:src="#drawable/call"
android:clickable="true"
android:onClick="softkeyboardButton"
android:background="#ffa62b"
android:layout_weight="1"
/>
Try setting the layout_width of the EditText and the ImageButton to 0dp.
I also recommend you to put the attribute android:scaleType="fitCenter" in the ImageButton so that the image fits inside the button and it doesn't look bigger like in the screenshot you provided.
Here is the XML file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:paddingBottom="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
tools:context=".MainActivity">
<ListView
android:id="#+id/ListView_2"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight="1">
</ListView>
<LinearLayout
android:id="#+id/LinearLayout2"
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal"
>
<!--android:adjustViewBounds="true"-->
<!-- we want the text to begin in the centre of the edittext view
that's what gravity does-->
<EditText
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="#+id/editText"
android:inputType="phone"
android:gravity="center"
android:hint="Edit Text"
android:background="#d9d9d9"
android:layout_weight="1"
/>
<ImageButton
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="#+id/callButton"
android:src="#android:drawable/sym_call_incoming"
android:clickable="true"
android:scaleType="fitCenter"
android:onClick="softkeyboardButton"
android:background="#ffa62b"
android:layout_weight="1"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal" >
<CheckBox
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:background="#ffa500"
android:text="New CheckBox"
android:id="#+id/checkBox"
android:layout_weight="4"
/>
<ImageButton
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:id="#+id/imageButton"
android:src="#android:drawable/ic_menu_gallery"
android:clickable="true"
android:onClick="softkeyboardButton"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:background="#android:drawable/ic_menu_gallery"
/>
<View android:layout_height="fill_parent"
android:layout_width="2px"
android:background="#90909090"/>
<ImageButton
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:id="#+id/imageButton2"
android:src="#android:drawable/ic_menu_set_as"
android:background="#android:drawable/ic_menu_gallery"
android:layout_gravity="center_horizontal"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
And this is how it looks:
I hope it helps!
For layout_weight to work, you should set either the layout_height or layout_weight to be 0dp on the widgets with weight.
From the looks of things I think you should spend some more time looking into how layout_weight works exactly
https://developer.android.com/guide/topics/ui/layout/linear.html#Weight

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>

android layout center and bottom

In my layout i want to display two imagebuttons in the center and a my amountmeter below it on the bottom, all centered vertically, i have tried many ways but cant seem to find that perfect layout. all the component align vertically and center but it makes my amountmeter small and not full width of the screen.
How it should be
how it is now
updated code
current layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
<ImageButton
android:id="#+id/purchase"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/round_button"
android:src="#drawable/ic_cart" />
<ImageButton
android:id="#+id/sell"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/round_button"
android:src="#drawable/ic_sell" />
<com.example.mobile.view.AmountMeterView
android:id="#+id/mobileAmount"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center|bottom"
android:padding="4dp" />
COPY PASTE BELOW CODE
<?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:orientation="horizontal" >
<ImageButton
android:id="#+id/purchase"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/ic_launcher" />
<ImageButton
android:id="#+id/sell"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/purchase"
android:layout_centerHorizontal="true"
android:src="#drawable/ic_launcher" />
<com.example.mobile.view.AmountMeterView
android:id="#+id/mobileAmount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/sell"
android:padding="4dp"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
AFTER THAT
android:background="#drawable/round_button"
android:src="#drawable/ic_sell"
android:background="#drawable/round_button"
android:src="#drawable/ic_cart"
PUT THIS LINE TO YOUR TWO IMAGE BUTTONS SURELY IT WILL WORK FOR YOU
One Linear layout is enough for this. Do not put your AmountMeterView into it's own LinearLayout.
Just make one Linear layout (vertical), and put all three elements in there. Gravity center_horizontal for all elements.
Try in this way hope this will help you
<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="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageButton
android:id="#+id/purchase"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/round_button"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageButton
android:id="#+id/sell"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/round_button"
android:src="#drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:orientation="vertical" >
<com.example.mobile.view.AmountMeterView
android:id="#+id/mobileAmount"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="4dp" />
</LinearLayout>
</LinearLayout>

5 sided dice like layout in Android

I'm currently working on an Android app and I would like to make a nice layout.
What I'm trying to achieve is the following layout:
The circles will be of equal size ofcourse. Besides that i need to be able to click them so they need to be buttons or imageviews.
What would be the best approach to achieve this kind of layout?
You could use a RelativeLayout, and say where each of the dots is supposed to be in relation to each of the others.
You could also use a LinearLayout, creating three 'rows' of buttons, the top and bottom rows containing two of the 'dots' and the middle one containing one in the center.
The layout would look something like this:
And the code would be something 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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/dot"
android:background="#android:color/transparent"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/dot2"
android:background="#android:color/transparent"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/dot3"
android:background="#android:color/transparent"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/dot4"
android:background="#android:color/transparent"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/dot5"
android:background="#android:color/transparent"/>
</LinearLayout>
</LinearLayout>
Try this..
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
>
<ImageView
android:src="#drawable/ic_launcher"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
/>
<ImageView
android:src="#drawable/ic_launcher"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
>
<ImageView
android:src="#drawable/ic_launcher"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_centerInParent="true"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
>
<ImageView
android:src="#drawable/ic_launcher"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
/>
<ImageView
android:src="#drawable/ic_launcher"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
/>
</RelativeLayout>
</LinearLayout>
Use a nest of LinearLayout or RelativeLayout. To use the images as button, then use ImageView and indicate in the xml corresponding ID for each image so you can use it in your activity.

Android Layout left and right align in horizontal layout

I am have a ImageView and a ImageButton. I have them next to each other in a horizontal layout. I am trying to make it so that the image is left aligned on the screen, and the button is right aligned. I have tried setting gravity but it doesnt seem to make a difference. Where am I going wrong?
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="1"
android:gravity="left"
android:src="#drawable/short_logo" />
<ImageButton
android:id="#+id/terminateSeed"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:background="#null"
android:src="#drawable/unregister_icon" />
</LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_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:src="#drawable/ic_launcher" />
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button" />
</RelativeLayout>
Use...
<?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="horizontal"
android:weightSum="1.0" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginRight="80dp"
android:layout_weight="0.5"
android:gravity="left"
android:src="#drawable/ic_launcher" />
<ImageButton
android:id="#+id/terminateSeed"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:gravity="right"
android:layout_marginLeft="80dp"
android:background="#null"
android:src="#drawable/ic_launcher" />
</LinearLayout>
Use This example if you want to use LinearLayout without giving Weight
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="right"
/>
</LinearLayout>
<RelativeLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_alignParentLeft="true"
android:src="#drawable/short_logo" />
<ImageButton
android:id="#+id/terminateSeed"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#null"
android:src="#drawable/unregister_icon" />
</RelativeLayout >
Make imageview and imagebutton wrap_content and set layout_gravity to left and right accordingly.
a RelativeLayout would solve your problem with a snap, but if you still insist on using LinearLayout (just because we're badass and do things the hard way), you do it like this:
Your parent LinearLayout is fill-parent, left-aligned, and horizontally oriented,
and contains your ImageView, and another LinearLayout.
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="left"
Second LinearLayout is fill-parent, right-aligned, and horizontally oriented, and contains your ImageButton.
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="right"
Another approach for LinearLayout, using layout_weight and gravity:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="BUTTON 1"/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="BUTTON 2" />
</LinearLayout>

Categories

Resources