RelativeLayout position problems - android

I have a layout which contains 4 horizontal LinearLayouts with 3 ImageButtons inside each one.
Now I want to convert those LinearLayout to RelativeLayout because I need to add some more elements and I have to use toRightOf, alignParentTop etc.
Here's the code I'm using to do this change:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="#drawable/bugbox_wall_port" >
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="vertical" >
<Button
android:id="#+id/writeNewMsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="New" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp" >
<ImageButton
android:id="#+id/spyMsgs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/msgspy" />
<ImageButton
android:id="#+id/playerMsgs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#drawable/msgplyr"
android:layout_toRightOf="#id/spyMsgs" />
<ImageButton
android:id="#+id/allyMsgs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/msgally"
android:layout_toRightOf="#id/playerMsgs" />
</RelativeLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp" >
<ImageButton
android:id="#+id/battleMsgs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/msgbatt" />
<ImageButton
android:id="#+id/skynetMsgs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#drawable/msgnet" />
<ImageButton
android:id="#+id/transportMsgs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/msgtrans" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp" >
<ImageButton
android:id="#+id/expeditionMsgs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/msgexp" />
<ImageButton
android:id="#+id/newsMsgs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#drawable/msgnews" />
<ImageButton
android:id="#+id/buildingMsgs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/msgbuild" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp" >
<ImageButton
android:id="#+id/allMsgs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/msgall" />
<ImageButton
android:id="#+id/binMsgs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:background="#drawable/msgbin" />
</LinearLayout>
</LinearLayout>
</ScrollView>
But I do not know why, the RelativeLayout is being problematic.
Here's an image where I explain graphically which's my problem:
http://img811.imageshack.us/img811/5479/stackes.jpg
It is driving me nuts. I do not know why is this happening. I've used RelativesLayout before and they weren't problematic.
So my question is: Why is this happening? and then How can I fix this error?
Thank you in advance!

Try this one. I commented out the linear layouts at the bottom. If the relative layout works the way you want it to then you can adjust the other linears accordingly.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:background="#drawable/bugbox_wall_port" >
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_gravity="center_horizontal"
android:orientation="vertical" >
<Button
android:id="#+id/writeNewMsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="New" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp" >
<ImageButton
android:id="#+id/spyMsgs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher" />
<ImageButton
android:id="#+id/playerMsgs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#drawable/ic_launcher"
android:layout_toRightOf="#id/spyMsgs" />
<ImageButton
android:id="#+id/allyMsgs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher"
android:layout_toRightOf="#id/playerMsgs" />
</RelativeLayout>
<!-- <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp" >
<ImageButton
android:id="#+id/battleMsgs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/msgbatt" />
<ImageButton
android:id="#+id/skynetMsgs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#drawable/msgnet" />
<ImageButton
android:id="#+id/transportMsgs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/msgtrans" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp" >
<ImageButton
android:id="#+id/expeditionMsgs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/msgexp" />
<ImageButton
android:id="#+id/newsMsgs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#drawable/msgnews" />
<ImageButton
android:id="#+id/buildingMsgs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/msgbuild" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp" >
<ImageButton
android:id="#+id/allMsgs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/msgall" />
<ImageButton
android:id="#+id/binMsgs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:background="#drawable/msgbin" />
</LinearLayout> -->
</LinearLayout>
</ScrollView>

Solved. I was setting an ID to the views programatically and I didn't realise...

Related

Difficulty in aligning child to the mostleft and mostright in-between another child

I want two images to be displayed on left and right and a textview in between them just at small distance to the left image but I couldn't get it done.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal">
<ImageView
android:id="#+id/image_of_services"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:src="#drawable/ic_menu_camera" />
<TextView
android:id="#+id/name_of_services"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:text="adasdsad"
android:layout_weight="8"
android:textSize="20sp" />
<ImageView
android:id="#+id/image_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="2"
android:src="#drawable/ic_menu_camera" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="10"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:id="#+id/image_of_services"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="2"
android:src="#drawable/ic_menu_camera" />
<TextView
android:id="#+id/name_of_services"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="adasdsad"
android:layout_weight="6"
android:textSize="20sp" />
<ImageView
android:id="#+id/image_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="2"
android:src="#drawable/ic_menu_camera" />
</LinearLayout>`
<?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="70dp"
android:orientation="horizontal">
<ImageView
android:id="#+id/image_of_services"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:src="#drawable/ic_menu_camera" />
<TextView
android:id="#+id/name_of_services"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_weight="1"
android:text="adasdsad"
android:textSize="20sp" />
<ImageView
android:id="#+id/image_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:src="#drawable/ic_menu_gallery" />
</LinearLayout>
i have edited the code and it worked perfectly as i desired

Actionbar overlapping Scrollbar inside fragment

I have a scrollview as the root view of a fragment. It's child is a vertical linear layout with 4 elements. The first element is an image. The issue is that the image is getting cut and I can't seem to scroll upwards. This issue is only in landscape mode. Following is my xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:weightSum="4"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/logo_dark"
android:id="#+id/imageView2"
android:layout_gravity="center" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp"
android:layout_gravity="center"
android:id="#+id/linearLayout3">
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/student"
android:layout_gravity="center" />
<AutoCompleteTextView
android:id="#+id/new_form_student"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="15"
android:textColor="#color/colorPrimary"
android:textColorHint="#fff"
android:padding="10dp" />
</LinearLayout>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/person"
android:layout_gravity="center" />
<AutoCompleteTextView
android:id="#+id/new_form_person"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="15"
android:textColor="#color/colorPrimary"
android:textColorHint="#fff"
android:padding="10dp" />
</LinearLayout>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/reason"
android:layout_gravity="center" />
<AutoCompleteTextView
android:id="#+id/new_form_reason"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="15"
android:textColor="#color/colorPrimary"
android:textColorHint="#fff"
android:padding="10dp" />
</LinearLayout>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/check"
android:layout_gravity="center" />
<AutoCompleteTextView
android:id="#+id/new_form_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="15"
android:textColor="#color/colorPrimary"
android:textColorHint="#fff"
android:padding="10dp" />
</LinearLayout>
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/submit"
android:id="#+id/new_form_submit"
android:padding="10dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/cancel"
android:id="#+id/new_form_cancel"
android:padding="10dp"/>
</LinearLayout>
And this is the screenshot of the issue:
Thanks,
Found it here:
Remove layout_gravity attribute from the Linear Layout that is directly placed inside scrollView, in your case,
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<!-- android:layout_gravity="center" -->
android:weightSum="4"
android:orientation="vertical">
instead of:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:weightSum="4"
android:orientation="vertical">

How can I make layout like this with inflated layout?

I inflate layout to listview and here is what I want:
But I always get something like this:
As you see, the button is always align at the bottom of parent. Here is my xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff" >
<ImageView
android:id="#+id/img_icon_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_margin="5dp" />
<TextView
android:id="#+id/txt_title_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/img_icon_type"
android:text="TextView"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="#+id/txt_post_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txt_title_type"
android:layout_marginTop="16dp"
android:layout_toRightOf="#+id/img_icon_type"
android:text="TextView"
android:textSize="15sp" />
<TextView
android:id="#+id/txt_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txt_post_time"
android:layout_toRightOf="#+id/img_icon_type"
android:text="TextView"
android:textSize="15sp" />
<ImageView
android:id="#+id/img_edit_post"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:onClick="openPostMenu"
android:src="#drawable/btn_memo_menu" />
<TextView
android:id="#+id/txt_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/img_icon_type"
android:layout_margin="10dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:text="TextView"
android:textSize="20sp" />
<ImageView
android:id="#+id/img_posted"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_below="#+id/txt_status"
android:layout_centerHorizontal="true"
android:src="#drawable/ic_launcher" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/img_posted"
android:layout_alignRight="#+id/img_posted"
android:layout_below="#+id/img_posted" >
<RelativeLayout
android:id="#+id/btn_confirm_route"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:background="#f2f2f2" >
<TextView
android:id="#+id/txt_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="#string/現在地からのル一トを確認" />
<ImageView
android:layout_width="20sp"
android:layout_height="20sp"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/txt_address"
android:src="#drawable/btn_memo_route_search_pressed" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
Please help me to fix this!Thank you!

ListView Row Layout Pushing ImageButton Off Screen

I am trying to create a Row layout to use in my ListView,
but when the text is too long, it pushes the right hand side ImageButton off the screen.
I want the text to just continue on the next line without interfering with the ImageButton-
This is the layout I have so far, I tried adding weight properties to set how much space could be use but I couldn't get it to work.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="horizontal" >
<ImageView
android:id="#+id/ImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/Text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/Text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/Text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="right"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/ImageButton"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:drawableTop="#drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Thank You.
No matter what lenght if text, first and last image will be of width 50dp occupied, rest space for text.
<LinearLayout
android:layout_width="50dp"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<ImageView
android:id="#+id/ImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="#+id/Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/Text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/Text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/Text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
<LinearLayout
android:layout_width="50dp"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/ImageButton"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:drawableTop="#drawable/ic_launcher" />
</LinearLayout>
You're using too many LinearLayouts, and also you're not defining any constrain for your views space.
Try 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" >
<ImageView
android:id="#+id/ImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:src="#drawable/ic_launcher" />
<ImageButton
android:id="#+id/ImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:drawableTop="#drawable/ic_launcher" />
<LinearLayout
android:id="#+id/LinearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/ImageButton"
android:layout_toRightOf="#id/ImageView"
android:orientation="vertical" >
<TextView
android:id="#+id/Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/Text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/Text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/Text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
</RelativeLayout>

why android:layout_gravity="right" not working here in android xml

my xml code for list view's row is
<?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:baselineAligned="false"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:orientation="vertical" >
<TextView
android:id="#+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp" />
<TextView
android:id="#+id/tv_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="2"
android:gravity="center" >
<ImageView
android:id="#+id/iv_image"
android:layout_width="50dp"
android:layout_height="40dp"
android:background="#drawable/app_icon_17"
android:contentDescription="#string/empty" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="2"
android:gravity="center" >
<Button
android:id="#+id/btn_delete"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/btn_delete_new"
android:focusable="false" />
</LinearLayout>
</LinearLayout>
I want the last button with id btn_delete to stay right aligned. And its showing as I wanted in "graphical layout" while designing. But when I run it, on emulator it's not working.
See the output :
So why the delete button not getting right aligned?
Use this, using relative layout
<?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" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true" >
<TextView
android:id="#+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp" />
<TextView
android:id="#+id/tv_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_name"
android:layout_marginLeft="10dp" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/rl_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" >
<Button
android:id="#+id/btn_delete"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/btn_delete_new"
android:focusable="false" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:layout_toLeftOf="#+id/rl_btn" >
<ImageView
android:id="#+id/iv_image"
android:layout_width="50dp"
android:layout_height="40dp"
android:background="#drawable/app_icon_17"
android:contentDescription="#string/empty" />
</RelativeLayout>
</RelativeLayout>
You seem to have misunderstood layouts, those embedded LinearLayouts aren't needed. Use a RelativeLayout, e.g.:
<?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="wrap_content" >
<TextView
android:id="#+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp" />
<TextView
android:id="#+id/tv_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp" />
<ImageView
android:id="#+id/iv_image"
android:layout_width="50dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#id/tv_time"
android:background="#drawable/app_icon_17"
android:contentDescription="#string/empty" />
<Button
android:id="#+id/btn_delete"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:background="#drawable/btn_delete_new"
android:focusable="false" />
</RelativeLayout>
As we can see that you have assign weight to your row layout.
If you assign weight your parent layout you just need to set accordingly height or with 0dp.
In your case android:layout_width="0dp"
but when you have child layout of that weighted layout then you must give that layouts property as fill_parent i.e: button's android:layout_height="fill_parent"
So if you don't want to change your parent layout and want to work with existing code you can try below code.
<?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:baselineAligned="false"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:orientation="vertical" >
<TextView
android:id="#+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp" />
<TextView
android:id="#+id/tv_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
>
<ImageView
android:id="#+id/iv_image"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="#drawable/icon"
android:contentDescription="#string/app_name" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2" >
<Button
android:id="#+id/btn_delete"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="#drawable/icon"
android:focusable="false" />
</LinearLayout>
</LinearLayout>
Change this
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="2"
android:gravity="center" >
<Button
android:id="#+id/btn_delete"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/btn_delete_new"
android:focusable="false" />
</LinearLayout>
to
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/btn_delete"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="right"
android:background="#drawable/btn_delete_new"
android:focusable="false" />
</LinearLayout>
You were applying android:layout_gravity="right" to layout. Instead, you should apply it to a button inside layout. And I don't think you need weight 2 for that layout. But you can add it if its required for you. Hope it helps.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="vertical" >
<Button
android:id="#+id/btn_delete"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="right"
android:gravity="right"
android:background="#drawable/btn_delete_new"
android:focusable="false" />
</LinearLayout>

Categories

Resources