android layout center and bottom - android

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>

Related

How to move the ImageView extreme right in android LinearLayout?

This is my xml. I need to place my icon to extreme right. But it's not moving to extreme right. "tri_logo" I have given gravity as right. But still not moving to right.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toplayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/lout_profile_header"
android:layout_width="match_parent"
android:layout_height="#dimen/top_view_height"
android:clickable="true"
android:gravity="center"
android:orientation="horizontal">
<com.test.customviews.CircleImageView
android:id="#+id/imgTopProfile"
android:layout_width="#dimen/profilie_icon_size"
android:layout_height="#dimen/profilie_icon_size"
android:layout_margin="#dimen/hdr_img_padding"
android:src="#mipmap/ic_profile" />
<TextView
android:id="#+id/textTopProfile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="#dimen/hdr_name_padding" />
<ImageView
android:id="#+id/tri_logo"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="right"
android:src="#drawable/tri_logo"/>
</LinearLayout>
</LinearLayout>
ImageView tri_logo is not moving to right.
Try this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toplayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:orientation="vertical">
<LinearLayout
android:id="#+id/lout_profile_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:weightSum="3"
android:layout_gravity="right"
android:orientation="horizontal">
<com.test.customviews.CircleImageView
android:id="#+id/imgTopProfile"
android:layout_weight="1"
android:layout_width="#dimen/profilie_icon_size"
android:layout_height="#dimen/profilie_icon_size"
android:layout_margin="#dimen/hdr_img_padding"
android:src="#mipmap/ic_profile" />
<TextView
android:layout_weight="1"
android:id="#+id/textTopProfile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
/>
<ImageView
android:layout_weight="1"
android:id="#+id/tri_logo"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="right"
android:src="#drawable/voyage"/>
</LinearLayout>
</LinearLayout>
You could use RelativeLayout and then use layout_alignParentEnd=true, if you want to stick on to LinearLayout try above one
Wrap up your ImageView in a RelativeLayout with match_parent width and use
android:layout_alignParentRight="true"
like this:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/tri_logo"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:src="#drawable/tri_logo"/>
</RelativeLayout>
Use the tools a LinearLayout provides. Give weight to your child views and change the parent layout's gravity to right
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toplayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/lout_profile_header"
android:layout_width="match_parent"
android:layout_height="100dp"
android:clickable="true"
android:gravity="center">
<com.test.customviews.CircleImageView
android:id="#+id/imgTopProfile"
android:layout_width="#dimen/profilie_icon_size"
android:layout_height="#dimen/profilie_icon_size"
android:layout_margin="#dimen/hdr_img_padding"
android:src="#mipmap/ic_profile" />
<TextView
android:id="#+id/textTopProfile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="100dp" />
<ImageView
android:layout_alignParentRight="true"
android:id="#+id/tri_logo"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="right"
android:src="#drawable/tri_logo"/>
</RelativeLayout>
</LinearLayout>
Use RelatiVeLayout as immediate parent .
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toplayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/lout_profile_header"
android:layout_width="match_parent"
android:layout_height="#dimen/top_view_height"
android:clickable="true"
>
<com.test.customviews.CircleImageView
android:id="#+id/imgTopProfile"
android:layout_width="#dimen/profilie_icon_size"
android:layout_height="#dimen/profilie_icon_size"
android:layout_centerVertical="true"
android:layout_margin="#dimen/hdr_img_padding"
android:src="#mipmap/ic_profile" />
<TextView
android:id="#+id/textTopProfile"
android:layout_width="match_parent"
android:layout_centerVertical="true"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_toLeftOf="#+id/tri_logo"
android:layout_toRightOf="#+id/imgTopProfile"
android:layout_marginLeft="#dimen/hdr_name_padding" />
<ImageView
android:id="#+id/tri_logo"
android:layout_width="30dp"
android:layout_centerVertical="true"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:src="#drawable/tri_logo"/>
</RelativeLayout>
</LinearLayout>

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 Relativelayout two buttons rightof one button

I was wondering how to use Relativelayout in order to make a tructure like this:
three buttons, let's call them A B and C; I want to put B and C one above the other and both of them on the right side of A as a single block matching the same width and height of A (so we'll have that B and C have half height of A but the same width).
Since I can't post images yet here you can find the image of what I'm looking for: http://img191.imageshack.us/img191/9765/stackg.jpg
The code I wrote about this part is this one:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/black"
android:paddingLeft="16dp"
android:paddingRight="16dp" >
<Spinner
android:id="#+id/A"
android:layout_width="0dp"
android:layout_height="96dp"
android:layout_below="#id/another_block_that_fills_all_the_line"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/B" />
<Spinner
android:id="#id/B"
android:layout_width="96dp"
android:layout_height="48dp"
android:layout_below="#id/another_block_that_fills_all_the_line"
android:layout_alignParentRight="true" />
<Spinner
android:id="#id/C"
android:layout_width="96dp"
android:layout_height="48dp"
android:layout_below="#id/B"
android:layout_alignParentRight="true" />
</RelativeLayout>
The problem is that it's not going to scale with every screen because I had to set static sizes. I tried a lot of different solutions like using all the possible combinations for the layout_align and changing wrap_content even randomly but still, I can't fit them in the way I want them to without using static sizes.
Use LinearLayout with layout_weight. Try this layout
<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="horizontal">
<Button android:layout_width="0dip" android:layout_height="fill_parent"
android:layout_weight="1.0"/>
<LinearLayout android:layout_width="0dip" android:layout_height="fill_parent"
android:layout_weight="1.0" android:orientation="vertical">
<Button android:layout_width="fill_parent" android:layout_height="0dip"
android:layout_weight="1.0"/>
<Button android:layout_width="fill_parent" android:layout_height="0dip"
android:layout_weight="1.0"/>
</LinearLayout>
</LinearLayout>
Try this,
<?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="#android:color/black">
<Button
android:id="#+id/A"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/relativeLayout"
android:layout_alignBottom="#+id/relativeLayout"
android:text="AAA"/>
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/A"
android:layout_centerVertical="true"
android:background="#android:color/black">
<Button
android:id="#+id/B"
android:layout_width="wrap_content"
android:text="BBB"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/C"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/B"
android:text="CCC"/>
</RelativeLayout>
</RelativeLayout>
It should be as below
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:padding="10dp" >
<View
android:id="#+id/helper"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Spinner
android:id="#+id/A"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#id/helper"
android:background="#android:color/black" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/A"
android:layout_alignParentRight="true"
android:layout_alignTop="#id/A"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#id/helper"
android:orientation="vertical" >
<Spinner
android:id="#+id/B"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginBottom="2dp"
android:layout_weight="1"
android:background="#android:color/black" />
<Spinner
android:id="#+id/C"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#android:color/black" />
</LinearLayout>

Align to the right of the screen with margin Android

I have a Linearlayout within a RelativeLayout that I'm trying to align to the rightmost side of the screen with margin. Example:
How should I modify the LinearLayout's properties to achieve this? thanks
Here is the XML file(the name of the LinearLayout that should be aligned to right is slide_right):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="top"
tools:context=".MainActivity" >
<WebView android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<LinearLayout
android:id="#+id/slide_left"
android:layout_width="400dp"
android:layout_height="550dp"
android:layout_marginLeft="-367dp"
android:layout_marginTop="150dp"
android:background="#drawable/left_slide"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="#+id/slide_right"
android:layout_width="400dp"
android:layout_height="550dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="-30dp"
android:orientation="horizontal"
android:background="#drawable/right_slide">
</LinearLayout>
<LinearLayout
android:id="#+id/header_linear_l"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/mapbt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/mapbt" />
<ImageButton
android:id="#+id/profilebt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/profilebt" />
<ImageButton
android:id="#+id/settingsbt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/settingsbt" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageButton
android:id="#+id/optionalbt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/optionbt" />
<ImageButton
android:id="#+id/lvlbt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/levelbt" />
</LinearLayout>
<ProgressBar
android:id="#+id/experiencebar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
<ImageButton
android:id="#+id/Attack_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/slide_left"
android:layout_marginBottom="129dp"
android:layout_marginLeft="36dp"
android:layout_toRightOf="#+id/slide_left"
android:src="#drawable/ic_launcher_old"
android:visibility="invisible" />
</RelativeLayout>
Change Your Relative layout's Property
from
android:layout_width="wrap_content"
to
android:layout_width="match_parent"
thats it... (definitely I have tested it see below snapshot)
See the following Code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/your_relative_layout_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white" >
<LinearLayout
android:id="#+id/your_linear_layout_id"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="-30dip"
android:background="#e42c2a"
android:orientation="horizontal" >
</LinearLayout>
</RelativeLayout>
Here
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="-30dip"
are of your use.

How to center a view using RelativeLayout?

I wanted to know how to center a View between two other views (or between a view and the parent edge) using RelativeLayout.
For example, if I have the following...
How do I vertically center the Button between the ImageView and the bottom of the screen using RelativeLayout?
I'm looking for a solution where...
the Button is not stretched in any way
there are no nested layouts
And I'm trying to do this in the XML layout (not programmatically).
You can use following:
<RelativeLayout
android:layout_below="#id/theImageView"
android:align_parentBottom="true"
android:layout_width="match_parent"
android:layout_height="200dp" >
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="onClickButton"
android:textSize="20sp"
android:text="Go"/>
</RelativeLayout>
There's unfortunately no easy way to do this. You can either nest layouts, or do it at runtime. The simplest way is to nest layouts:
<LinearLayout
android:width="match_parent"
android:height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/my_top_image"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="#string/my_button_label"/>
</RelativeLayout>
</LinearLayout>
This puts the image at the top. Below that, the layout_height=0 and layout_weight=1 attributes on the RelativeLayout cause it to take up all the remaining space. You can then center the button in the RelativeLayout. You can play with padding on the button to get it to the size you want.
Use below XMl code for that, it will solve your problem.
<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="fill_parent" >
<LinearLayout
android:id="#+id/mLlayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/mImgView1"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="#drawable/ic_launcher" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="2" >
<Button
android:id="#+id/Btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Dipak" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
use this android:layout_centerInParent="true" in XML file..
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1.0" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.8"
android:background="#00ff00" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.2" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Button" />
</RelativeLayout>
</LinearLayout>
#Gus you can create center view between two other view... this is not exact you must try with
this way....
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#00ccFF"
android:orientation="horizontal" >
</LinearLayout>
<LinearLayout
android:layout_width="59dp"
android:layout_height="59dp"
android:layout_alignRight="#+id/linearLayout1"
android:layout_alignTop="#+id/linearLayout1"
android:layout_marginRight="-21dp"
android:layout_marginTop="-21dp"
android:background="#FFccFF"
android:orientation="vertical" >
</LinearLayout>
</RelativeLayout>
I think what you just need is to allign the imageview as you would normally done and align the button using layout_below , no need for hardcoding nor using nested views use android:gravity="center" on the button you're done
<RelativeLayout
android:layout_below="#id/theImageView"
android:align_parentBottom="true"
android:layout_width="match_parent"
android:layout_height="200dp" >
<ImageView
android:id="#+id/imgview"
android:layout_width="match_parentmat"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imgview"
android:gravity="center"
android:onClick="onClickButton"
android:textSize="20sp"
android:text="Go"/>
</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" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:src="#drawable/wood" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageView1"
android:layout_below="#+id/imageView1"
android:layout_marginLeft="101dp"
android:layout_marginTop="53dp"
android:text="Image Button" />
</RelativeLayout>

Categories

Resources