Setting layouts to the size of their contents - android

I have a simple XML I am trying to inflate, and struggling with the layout. What I want to achieve is quite simple (or so I thought), so I am sure I am going about it incorrectly. Basically I have three elements, 2 TextViews, and an Imageview. I want the two text boxes placed on top of one another, with the image on the right of them, aligned to the right side, and its view fitted it the size of the image. Like this:
Text 1 |==|
Text 2 would be below text 1 |==|
Heres my XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/entry_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:orientation="horizontal"
android:paddingRight="?android:attr/scrollbarSize" >
<LinearLayout
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/entry1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:gravity="left"
android:paddingLeft="#dimen/tab_host_default_height"
android:text="#string/test_string"
android:textStyle="bold" />
<TextView
android:id="#+id/entry2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:gravity="left"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:text="#string/test_string" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
>
<ImageButton
android:id="#+id/entry3"
style="#style/ImageButtonBDTheme"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/activated_background_holo_light"
android:contentDescription="#string/click_for_repair_report"
android:paddingRight="5dp"
android:src="#drawable/entry_link" />
</LinearLayout>
</LinearLayout>
Problem is that I cannot get the image to display without hard coding the size of the LinearLayout containing the two text boxes.
Any help is appreciated!

// try this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/entry_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingRight="?android:attr/scrollbarSize"
android:gravity="center">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/entry1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:paddingLeft="#dimen/tab_host_default_height"
android:text="#string/test_string"
android:textStyle="bold" />
<TextView
android:id="#+id/entry2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:text="#string/test_string" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:orientation="vertical">
<ImageButton
android:id="#+id/entry3"
style="#style/ImageButtonBDTheme"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/activated_background_holo_light"
android:contentDescription="#string/click_for_repair_report"
android:paddingRight="5dp"
android:src="#drawable/entry_link" />
</LinearLayout>
</LinearLayout>

Try this..
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/entry_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:orientation="horizontal"
android:paddingRight="?android:attr/scrollbarSize" >
<LinearLayout
android:layout_width="0dp" //changes here
android:layout_weight="0.6" //changes here
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/entry1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:gravity="left"
android:paddingLeft="#dimen/tab_host_default_height"
android:text="#string/test_string"
android:textStyle="bold" />
<TextView
android:id="#+id/entry2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:gravity="left"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:text="#string/test_string" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp" //changes here
android:layout_weight="0.4" //changes here
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
>
<ImageButton
android:id="#+id/entry3"
style="#style/ImageButtonBDTheme"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/activated_background_holo_light"
android:contentDescription="#string/click_for_repair_report"
android:paddingRight="5dp"
android:src="#drawable/entry_link" />
</LinearLayout>
</LinearLayout>

Always Use Relative Layout, it requires lesser no. of code as compared to other Layouts.
Try below, i guess this is what you want, set Padding/Margin according to your requirement.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/entry_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:orientation="horizontal"
android:paddingRight="?android:attr/scrollbarSize" >
<TextView
android:id="#+id/entry1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:gravity="left"
android:paddingLeft="#dimen/tab_host_default_height"
android:text="#string/test_string"
android:textStyle="bold" />
<TextView
android:id="#+id/entry2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/entry1"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:gravity="left"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:text="#string/test_string" />
<ImageButton
android:id="#+id/entry3"
style="#style/ImageButtonBDTheme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#drawable/ic_launcher"
android:contentDescription="#string/click_for_repair_report"
android:paddingRight="5dp"
android:src="#drawable/entry_link" />
</RelativeLayout>

Related

Failed to get Scrolling in device S3 using Android?

Here is my xml code i want to apply scrolling in View but no scroll appears in my device screen.Here is the XML Layout.Can someone please suggest me possible solutions to overcome on this situation.....
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/home_bg"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/askfatwa_header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/askfatwa_top_bar" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/askfatwa_header"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="5dp"
android:orientation="vertical" >
<TextView
android:id="#+id/name_askscreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/name"
android:textColor="#android:color/white"
android:textSize="20sp" />
<EditText
android:id="#+id/askscreen_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:paddingLeft="5dp"
android:maxLines="1"
android:inputType="text"
android:background="#drawable/textfield" >
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="5dp"
android:orientation="vertical" >
<TextView
android:id="#+id/email_askscreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Email"
android:textColor="#android:color/white"
android:textSize="20sp" />
<EditText
android:id="#+id/askscreen_email"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:maxLines="1"
android:inputType="textEmailAddress"
android:background="#drawable/textfield"
android:paddingLeft="5dp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="5dp"
android:orientation="vertical" >
<TextView
android:id="#+id/address_askscreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Address"
android:textColor="#android:color/white"
android:textSize="20sp" />
<EditText
android:id="#+id/askscreen_address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:paddingLeft="5dp"
android:inputType="textMultiLine"
android:background="#drawable/textfield" />
</LinearLayout>
<!-- <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="5dp"
android:orientation="vertical" >
<TextView
android:id="#+id/contact_askscreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Contact"
android:textColor="#android:color/white"
android:textSize="20sp" />
<EditText
android:id="#+id/askscreen_contact"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:paddingLeft="5dp"
android:background="#drawable/textfield" />
</LinearLayout> -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="5dp"
android:orientation="vertical" >
<TextView
android:id="#+id/subject_askscreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Subject"
android:textColor="#android:color/white"
android:textSize="20sp" />
<!-- <EditText
android:id="#+id/askscreen_subject"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:paddingLeft="5dp" /> -->
<Spinner
android:id="#+id/askscreen_subject"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:paddingLeft="5dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="5dp"
android:orientation="vertical" >
<!-- <TextView
android:id="#+id/body_askscreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Type Your Question"
android:textColor="#android:color/white"
android:textSize="20sp" /> -->
<EditText
android:id="#+id/askscreen_question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:background="#drawable/textfield"
android:hint="Type Your Question"
android:minLines="1"
android:inputType="textMultiLine"
android:minHeight="8dp"
android:minWidth="10dp"
android:lines="15"
android:paddingLeft="5dp" />
</LinearLayout>
<ImageView
android:id="#+id/askscreen_submit_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:paddingTop="20dp"
android:src="#drawable/askftwa_submit_button" />
</LinearLayout>
</ScrollView>
Add more elements and the scroll will appear. Right now all the elements are fitting within the screen and that's why you don't see any scroll
First a fall by seeing your xml, you are creating very expensive layout containing
unnecessarily many Linear layouts ie when orientation of your all views are vertical.
Why are you defining your views like textbox and edittext in separate Linear Layouts,
it creates performance issues.
Secondly, you should not define set any background and orientation in scroll view.
By default orientation of scroll view is vertical.
Although i just run your code with few changes and its working fine on my emulator
and on my device.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
// This is Main layout which contains all your child view
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/home_bg"
android:orientation="vertical" >
// Define your remaining child views here.....
// And as i can see all views are oriented vertically, so you can define all view
inside this Main Layout itself.
// Accordingly you can set Margins of your views too.
</LinearLayout>
</ScrollView>

Why does the imageview disappear on the layout?

The imageview is on the right of the layout. It doesn't display if the content of messagedetail_row_text is too long. The imageview does display if the content of messagedetail_row_text is not too long.
<?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="wrap_content"
android:layout_marginTop="15dp"
android:gravity="right"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/dfdwasdfds"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/outgoing"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="22dip"
android:orientation="horizontal" >
<TextView
android:id="#+id/messagedetail_row_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="1dp"
android:textColor="#000000"
android:textSize="18sp" />
<TextView
android:id="#+id/messagedetail_row_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip"
android:paddingTop="1dp"
android:textColor="#000000"
android:textSize="16sp" />
</LinearLayout>
<TextView
android:id="#+id/messagedetail_row_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="2dp"
android:textColor="#000000"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_toRightOf="#id/dfdwasdfds"
>
<ImageView
android:id="#+id/messagegedetail_rov_icon2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:gravity="right"
android:src="#drawable/retouxiang" />
</LinearLayout>
</RelativeLayout>
It is not clear what you are looking for exactly. This is from what I understand:
<?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="wrap_content"
android:layout_marginTop="15dp"
android:gravity="right"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/xyz"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ImageView
android:id="#+id/messagegedetail_rov_icon2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:gravity="right"
android:src="#drawable/retouxiang" />
</LinearLayout>
<LinearLayout
android:id="#+id/dfdwasdfds"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/xyz"
android:background="#drawable/outgoing"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="22dip"
android:orientation="horizontal" >
<TextView
android:id="#+id/messagedetail_row_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="1dp"
android:textColor="#000000"
android:text="messagedetail_row_name"
android:textSize="18sp" />
<TextView
android:id="#+id/messagedetail_row_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip"
android:paddingTop="1dp"
android:text="messagedetail_row_date"
android:textColor="#000000"
android:textSize="16sp" />
</LinearLayout>
<TextView
android:id="#+id/messagedetail_row_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="2dp"
android:text="messagedetail_row_text and this is a long text"
android:textColor="#000000"
android:textSize="20sp" />
</LinearLayout>
</RelativeLayout>
Try to change
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_toRightOf="#id/dfdwasdfds"
>
to
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_below="#id/dfdwasdfds"
>
I think it is happening because you have given your textView width fill_parent and it takes up as much space is its length is, then its parent width is wrap_content, so it goes upto its child occupies space(i.e TextView), so you dont have space to display the ImageView.
You can try using LinearLayout as root layout instead of relativeLayout..and manage your views by giving them weights..

How do I prevent a view in my ListvView from being pushed out of view

I list view with some text views in. one of the text views sometimes gets pushed out of view when one of the other text view has a long string in it.
How do I make the text views ellipsize and never push other views out?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" android:padding="5dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/rest_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:maxLines="2"
android:ellipsize="end"
android:text="Name"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal" >
<TextView
android:id="#+id/rest_rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1.5"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/rest_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:ellipsize="end"
android:singleLine="true"
android:capitalize="words"
android:textAppearance="?android:attr/textAppearanceSmall" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal" >
<TextView
android:id="#+id/rest_open"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:text="Open"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
I know it's wierd, but if you take out
android:capitalize="words"
you should see the results that you want. you don't even need android:ellipsize="end"
<TextView
android:id="#+id/rest_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:singleLine="true" // <-- the play maker
android:textAppearance="?android:attr/textAppearanceSmall" />

Parent linear layout not taking up entire screen despite having fill_parent

I have a strange issue right now with a parent linear layout, I have its height set to fill_parent yet it seems intent on wrapping the content instead, what really is puzzling me even more is that in the graphical layout view its showing up the way Im intending for it to look but yet its still not doing what It should in the emulator?
here is my code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/title_color_dark_transparent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical|center_horizontal|center"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="5dp"
android:orientation="vertical"
android:paddingTop="5dp" >
<EditText
android:id="#+id/menutweetedittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/edit_text"
android:gravity="top"
android:hint="#string/edittext_hint"
android:inputType="textCapSentences|textMultiLine|textAutoCorrect|textAutoComplete"
android:lines="4"
android:textAppearance="#style/TextAppearance.EditText" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="right"
android:orientation="horizontal" >
<TextView
android:id="#+id/menucharactersremaining"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/characters"
android:textColor="#FFFFFF"
android:textSize="20dp" />
<Button
android:id="#+id/menuposttweetbutton"
style="#style/TextAppearance.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:background="#drawable/button_background"
android:onClick="posttweetbuttonClicked"
android:text="#string/postbuttonstext"
android:textSize="15dp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="8dp"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
<com.google.ads.AdView
android:id="#+id/menuadView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="a14f5be094d0328"
ads:loadAdOnCreate="true" />
</LinearLayout>
I took a shot at simplifying your layout. I ditched all your styling, but maybe the objects are in the right places. Is this what you're looking for?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/darker_gray" >
<EditText
android:id="#+id/menutweetedittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:gravity="top"
android:hint="Hint"
android:inputType="textCapSentences|textMultiLine|textAutoCorrect|textAutoComplete"
android:lines="4"
android:textAppearance="#android:style/TextAppearance.Medium" />
<Button
android:id="#+id/menuposttweetbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/menutweetedittext"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:onClick="posttweetbuttonClicked"
android:text="Tweet"
android:textSize="15dp" />
<TextView
android:id="#+id/menucharactersremaining"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/menuposttweetbutton"
android:layout_toLeftOf="#+id/menuposttweetbutton"
android:text="This is the text view"
android:textColor="#FFFFFF"
android:textSize="20dp" />
<ImageView
android:id="#+id/menuadView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#android:color/white"
android:src="#drawable/icon" />
</RelativeLayout>

Android LinearLayout Alignment

Being new to android I am still learning the intricacies of layouts. I am trying to create a simple bar on top of a map. For the most part this works fine.
My issue is that I want everything to be right aligned except for the Button which I want left aligned. I have tried quite a few combinations and am unable to get desired layout.
This is beginning to make me believe my structure as a whole is not correct. This seems like there should be an easy fix. What am I missing??
<LinearLayout
android:id="#+id/transparent_panel_hud"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="right">
<Button
android:text="View"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableRight="#drawable/arrow_down"
android:textSize="10sp"
android:drawablePadding="3dp"/>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingRight="15dp" >
<TextView
android:id="#+id/latitude"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="right"
android:text="#string/default_latitude"
android:textSize="18sp" />
<TextView
android:id="#+id/longitude"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="#string/default_longitude"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingRight="10dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/speed"
android:textSize="18sp" />
<TextView
android:id="#+id/speed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/default_speed"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingRight="10dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/heading"
android:textSize="18sp" />
<TextView
android:id="#+id/heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/default_heading"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>![screenie][1]
Do the following changes to your XML layout, you will get the output as you mentioned. Try this.
Remove the line android:gravity="right" in LinearLayout with id=transparent_panel_hud
Keep your Button in a LinearLayout as below.
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="left" >
<Button ... as you like />
</LinearLayout>
Keep your remaing 3 vertical LinearLayouts in a LinearLayout as below.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="right" >
<LinearLayout vertical 1 ... as you like />
<LinearLayout vertical 2 ... as you like />
<LinearLayout vertical 3 ... as you like />
</LinearLayout>
I tested above changes to your code, its working. You too check it and let me know the result.
Try this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/transparent_panel_hud"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:text="View"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
android:layout_alignParentLeft="true"
android:drawablePadding="3dp"/>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="#+id/rightlayout"
android:layout_alignParentRight="true"
android:paddingRight="10dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="heading"
android:textSize="18sp" />
<TextView
android:id="#+id/heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="default_heading"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_toLeftOf="#id/rightlayout"
android:orientation="vertical"
android:paddingRight="10dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="speed"
android:textSize="18sp" />
<TextView
android:id="#+id/speed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="default_speed"
android:textSize="18sp" />
</LinearLayout>
</RelativeLayout>

Categories

Resources