LinearLayout usage - android

What I just want to do it to put some texts in the screen, together with a border that I'm doing(this border will be in the entire screen.)
The problem is that the LinearLayout is adding some texts to outside of the screen, and this I cannot understand why. Why the LinearLayout cannot just keep the elements in the screen size, even if will be very small sizes.
Take a look:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="#color/White">
<ImageView android:layout_height="wrap_content"
android:minHeight="2px" android:maxHeight="2px"
android:layout_gravity="top" android:src="#drawable/horizontal_bar"
android:scaleType="fitXY" android:layout_weight="1"
android:layout_width="fill_parent" />
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_weight="1">
<ImageView android:src="#drawable/vertical_bar"
android:minWidth="2px" android:maxWidth="2px" android:scaleType="fitXY"
android:layout_gravity="left" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:layout_weight="1" />
<TextView android:text="testing" android:layout_height="fill_parent"
android:layout_width="fill_parent" android:paddingLeft="15px"
android:textStyle="bold" android:textColor="#color/grey"
android:textSize="20px" android:layout_weight="1"
android:layout_gravity="center_horizontal|center" />
<TextView android:text="blablabla" android:layout_height="fill_parent"
android:layout_width="fill_parent" android:paddingLeft="15px"
android:textStyle="bold" android:textColor="#color/grey"
android:textSize="20px" android:layout_weight="1"
android:layout_gravity="center_horizontal|center" />
<TextView android:text="0000" android:id="#+id/number"
android:layout_height="fill_parent" android:layout_width="fill_parent"
android:layout_gravity="left" android:textStyle="bold"
android:textColor="#color/green" android:textSize="20px"
android:layout_weight="1" />
<ImageView android:src="#drawable/vertical_bar"
android:minWidth="2px" android:maxWidth="2px" android:scaleType="fitXY"
android:layout_gravity="right" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:layout_weight="1" />
</LinearLayout>
<ImageView android:layout_height="wrap_content"
android:minHeight="2px" android:maxHeight="2px"
android:layout_gravity="top" android:src="#drawable/horizontal_bar"
android:scaleType="fitXY" android:layout_weight="1"
android:layout_width="fill_parent" />
</LinearLayout>
Where horizontal_bar is a image with 6x4 and vertical_bar is a image with 4x6.
This image needs to be stretch to be able to became a border.
Any idea why the LinearLayout is going out of the screen size?

Your
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_weight="1">
should have vertical orientation.

Related

Cannot Click Button on App

When I launch my app, I go to one of my screens to test out my game. Though, when I go to tap on the buttons, they don't work and do not change images like I wanted them to. So after various amounts of googling and testing, I believe I came across the problem. I believe I just have my text layer on top of my button layer, causing it so the buttons cannot be clicked as they are not the top layer. My issue here is I need the text on top to change constantly. How do I make it so my text does not interfere with my buttons.
I apologize for the big amount of code. The buttons are in the third relative layout, and the text is all in the middle.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:gravity="center_horizontal">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/imageView"
android:background="#drawable/backgroundupg" />
</RelativeLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<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:layout_weight="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView5"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:background="#drawable/upgrades"
android:layout_gravity="center_horizontal" />
</RelativeLayout>
<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:layout_weight=".5">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="4">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/selector_state2_xml"
android:id="#+id/upgbutton"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/selector_state3_xml"
android:id="#+id/upgbutton1"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/selector_state4_xml"
android:id="#+id/upgbutton2"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/selector_state5_xml"
android:id="#+id/upgbutton3"
android:layout_gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="8">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:id="#+id/upgtext"
android:text="Accuracy"
style="#style/Base.TextAppearance.AppCompat.Large"
android:textColor="#ffffff" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight=".5"
android:gravity="center_vertical|right"
android:id="#+id/pricetext"
android:text="100"
style="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#ffffff" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:id="#+id/upg1cost"
android:background="#drawable/cookie" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical|center_horizontal"
android:id="#+id/bottomtext"
android:text="Eh, close enough."
style="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#ffffff" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:id="#+id/upgtext2"
android:text="Base Cookies"
style="#style/Base.TextAppearance.AppCompat.Large"
android:textColor="#ffffff" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight=".5"
android:gravity="center_vertical|right"
android:id="#+id/pricetext2"
android:text="500"
style="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#ffffff" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:id="#+id/upg1cost"
android:gravity="center_vertical"
android:layout_alignRight="#+id/upgtext2"
android:background="#drawable/cookie" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical|center_horizontal"
android:id="#+id/bottomtext2"
android:text="Double the cookies? Sweet!"
style="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#ffffff" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:id="#+id/upgtext3"
android:text="Stopping Time"
style="#style/Base.TextAppearance.AppCompat.Large"
android:textColor="#ffffff" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight=".5"
android:gravity="center_vertical|right"
android:id="#+id/pricetext3"
android:text="1000"
style="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#ffffff" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:id="#+id/upg1cost"
android:gravity="center_vertical"
android:layout_alignRight="#+id/upgtext3"
android:background="#drawable/cookie" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical|center_horizontal"
android:id="#+id/bottomtext3"
android:text="I save an extra second!"
style="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#ffffff" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:id="#+id/upgtext4"
android:text="Secret"
style="#style/Base.TextAppearance.AppCompat.Large"
android:textColor="#ffffff" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight=".5"
android:gravity="center_vertical|right"
android:id="#+id/pricetext4"
android:text="250 000"
style="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#ffffff" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1.5"
android:id="#+id/upg1cost"
android:gravity="center_vertical"
android:layout_alignRight="#+id/upgtext4"
android:background="#drawable/cookie" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical|center_horizontal"
android:id="#+id/bottomtext4"
android:text="End game?!?!"
style="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#ffffff" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
<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:layout_weight="1">
</RelativeLayout>
</LinearLayout>
</FrameLayout>
I also included an image to help give a visual representation of my code.
As well as my selector state for those who would like to know exactly what I put in it
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button" android:state_enabled="true"></item>
<item android:drawable="#drawable/buttonclick" android:state_pressed="true"></item>
<item android:drawable="#drawable/buttongray" android:state_enabled="false"></item>
</selector>
Button is used when the user has enough cash to purchase the upgrade.
Buttongray is used when the user does not have enough cash, I do not want the button to be pressable then. The Button color just turns gray.
Buttonclick is just a highlight of Button.
I have tried interchanging both states as I thought that was the initial problem. Neither can be pressed.
the problem is because you are having your TextView objects above your Button objects and they steal the click event which you intend for the Button. The easiest fix would be to set:
android:focusable="false"
for all the TextView objects that overlay Button objects.
But I would still say your coding style needs to be improved. Why not dynamically change text in code based on events that happen in run time. You can use the setText(CharSequence text) method for buttons to dynamically set the text they should display.
You can use Button's attribute android:text="TEXT_HERE" in xml to set initial text for your button and later on with on click you can set a different text by this
Button button = (Button)findViewById(R.id.upgbutton);
button.setText("New_TEXT");
you button code in XML will look like this
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text = "Accuracy"
android:background="#drawable/selector_state2_xml"
android:id="#+id/upgbutton"
android:layout_gravity="center_horizontal" />
Since each "button" is so complex, you should consider creating a custom view.

RelativeLayout Not displaying an image

Ok, so here is where I sit: I cannot for the life of me figure out why the image to the left will not display. It should be the same image as the one to the right of notifications. I have gone over it a few different ways and cannot get that image to display on the left side of notifications.
<LinearLayout
android:orientation="vertical"
android:id="#id/notificationCart"
android:background="#000000"
android:visibility="gone"
android:layout_width="fill_parent"
android:layout_height="#dimen/status_bar_expanded_notification_category_height">
<RelativeLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_gravity="left|center"
android:layout_toLeftOf="#id/latestNotificationText"
android:layout_width="wrap_content"
android:layout_height="#dimen/tw_close_handle_height"
android:src="#drawable/tw_quick_panel_plnm_setting_dv"
android:scaleType="fitXY"/>
<TextView
android:textSize="#dimen/status_bar_expanded_notification_category_text_size"
android:textStyle="bold"
android:layout_gravity="center_horizontal"
android:textColor="#color/notification_category_color"
android:gravity="center"
android:id="#id/latestNotificationText"
android:paddingLeft="10.0dip"
android:paddingRight="10.0dip"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="#string/status_bar_latest_events_title"/>
<ImageView
android:layout_gravity="right|center"
android:layout_toRightOf="#id/latestNotificationText"
android:layout_width="wrap_content"
android:layout_height="#dimen/tw_close_handle_height"
android:src="#drawable/tw_quick_panel_plnm_setting_dv"
android:scaleType="fitXY"/>
</RelativeLayout>
</LinearLayout>
If you need to place two images left and right of the TextView
why do you need the RelativeLayout?
<LinearLayout android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout android:orientation="horizontal"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
....>
<ImageView
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<LinearLayout />
<LinearLayout />
Note that the parent FrameLayout has it's width set to match_parent.

LinearLayout: TextViews at left, big ImageView at right

i'm working on a LinearLayout but unfortunately it's not working as it should.
The goal is to have a LinearLayout with two TextViews (one placed below the other) on the left side, and an ImageView on the right side.
The ImageView should be as big as possible, the TextViews should take the remaining space.
At the moment my layout XML is like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_linearlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="1dp"
android:background="#drawable/background" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="2dp"
android:orientation="vertical" >
<TextView
android:id="#+id/layout1label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:singleLine="true"
android:text="1234"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/layout2label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1234_label2"
android:textSize="14dp" />
</LinearLayout>
<ImageView
android:id="#+id/layout_image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical"
android:layout_margin="2dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:src="#drawable/ic_launcher" />
The part that isn't working: If the text in the TextViews is "too long", the ImageView gets shrinked. I want it exactly the other way round.
Any solutions?
It would be more efficient to use RelativeLayout instead of LinearLayout. Then you can place your views without having to nest layouts:
<?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" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/image"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
/>
<TextView
android:id="#+id/subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/image"
android:layout_below="#+id/title"
/>
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
/>
</RelativeLayout>
By arranging the TextViews to be relative to the ImageView instead of the other way around, the ImageView takes priority for the space, and the text works with the remainder.
this might be help to you
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_linearlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="1dp"
android:background="#drawable/background" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="#+id/layout1label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:singleLine="true"
android:text="1234"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/layout2label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1234_labj hairu iue rel2"
android:textSize="14dp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
<ImageView
android:id="#+id/layout_image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical"
android:layout_margin="2dp"
android:adjustViewBounds="true"
android:src="#drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>
A little modification to Kirans Code..worked for me
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="#+id/layout1label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:singleLine="true"
android:text="Name: BalaVishnu"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
<ImageView
android:id="#+id/layout_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:adjustViewBounds="true"
android:src="#drawable/edit_icon" />
</LinearLayout>

Positioning ImageView

I need to positioning the ImageView at the same level of the user's name (vito in the example).
This is what I have:
And I want this (look at the second icon):
My xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:paddingBottom="5px"
android:paddingTop="5px"
android:paddingLeft="5px"
android:orientation="horizontal"
style="#style/ListRow">
<ImageView
android:id="#+id/imagenItem"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="6dip"
android:layout_gravity="top"
android:gravity="top"
android:layout_alignParentTop="true"
style="#style/iconLISTA" />
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/idUsuario"
android:textSize="#dimen/font_size_small"
android:textStyle="bold"
android:textColor="#color/list_item_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"/>
<TextView
android:id="#+id/hora"
android:textSize="#dimen/font_size_small"
android:textStyle="bold"
android:textColor="#color/list_item_secondary_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="215px" />
</RelativeLayout>
<TextView
android:id="#+id/comentarios"
android:textSize="#dimen/font_size_small"
android:color="#color/list_item_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="0px" />
</LinearLayout>
</LinearLayout>
Thanks.
Change the ImageView and following LinearLayout hight attributes to both be wrap_content.
The ImageView is currently fill_parent which is making it take all available vertical space and centering it vertically.
Add:
android:scaleType="fitStart"
to the ImageView layout.
e.g.
<ImageView
android:id="#+id/imagenItem"
android:scaleType="fitStart"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="6dip"
android:layout_gravity="top"
android:gravity="top"
android:layout_alignParentTop="true"
style="#style/iconLISTA" />
Try this, is bit simplified:
<?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="wrap_content"
android:orientation="horizontal"
android:padding="5dp" >
<ImageView
android:id="#+id/imagenItem"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="top"
android:scaleType="fitCenter"
android:src="#drawable/avatar_default" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/idUsuario"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:text="usuario"
android:textStyle="bold" />
<TextView
android:id="#+id/hora"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="hora"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/comentarios"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="commmmment" />
</LinearLayout>
</LinearLayout>
And you shouldn't use px, use always dp/dip.

Linearlayout for different screen sizes

I have a linearLayout which is surrounded by a scrollView in order to make it for smaller screens scrollable. In the linear layout I have 3 childs which are also linearlayout.
This is working fine. But I also want to support larger screens. At the moment I get with a larger screen space left on the bottom of the screen. I want to have the buttons (navigation bar) be always at the bottom. for larger screens (height) I would like to add at the top of the screen an empty view which fills out the space that is more provided by the larger screen.
How can I achieve that? i tried a lot with weight attribute but didnt get it working.
Here the layout:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#8db9f6"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#8db9f6">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_marginTop="8dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:text="test"
android:id="#+id/test"
android:paddingLeft="10dp"
android:textStyle="bold"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<View
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1"/>
<ImageButton
android:id="#+id/btnName"
android:scaleType="center"
android:layout_width="60dp"
android:layout_height="48dp"
android:src="#android:drawable/ic_menu_edit">
</ImageButton>
</LinearLayout>
<EditText
android:id="#+id/edittest"
android:editable="false"
android:hint="#string/test"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</EditText>
<TextView
android:text="#string/optionalText"
android:id="#+id/test1"
android:paddingLeft="10dp"
android:textSize="12dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<TextView
android:id="#+id/space"
android:layout_width="fill_parent"
android:layout_marginTop="4dp"
android:layout_height="3sp"
android:background="#333142">
</TextView>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_marginTop="5dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:text="TEST TESt"
android:id="#+id/test2"
android:paddingLeft="10dp"
android:layout_marginTop="5dp"
android:textStyle="bold"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<View
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1"/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:id="#+id/progressbar"/>
<ImageButton
android:id="#+id/btnPicture"
android:scaleType="center"
android:layout_width="60dp"
android:layout_height="48dp"
android:src="#android:drawable/ic_menu_edit">
</ImageButton>
</LinearLayout>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:background="#ffffff"
android:layout_gravity="center"
android:layout_width="fill_parent"
android:layout_height="150dip">
<ImageView
android:id="#+id/test3"
android:layout_width="400dip"
android:layout_height="150dip"
android:layout_gravity="center"
android:scaleType="fitCenter"
android:src="#drawable/test1"/>
<TextView
android:id="#+id/text"
android:layout_gravity="bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="8dp"
android:text="test test"/>
</FrameLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_gravity="bottom"
android:layout_marginTop="3dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/scaleMinus"
android:textSize="14dp"
android:layout_weight="1"
android:layout_gravity="bottom"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:text="#string/zoomOut"/>
<Button
android:id="#+id/scalePlus"
android:textSize="14dp"
android:layout_weight="1"
android:layout_gravity="bottom"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:text="#string/zoomIn"/>
</LinearLayout>
<TextView
android:id="#+id/balken"
android:layout_width="fill_parent"
android:layout_marginTop="4dp"
android:layout_height="3sp"
android:background="#333142">
</TextView>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_marginTop="5dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:text="test test"
android:id="#+id/test3"
android:paddingLeft="10dp"
android:textStyle="bold"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<View
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1"/>
<EditText
android:id="#+id/test4"
android:editable="false"
android:layout_width="100dp"
android:layout_height="wrap_content">
</EditText>
<TextView
android:text="test"
android:id="#+id/mtest4"
android:paddingLeft="10dp"
android:textStyle="bold"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<View
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1"/>
<ImageButton
android:id="#+id/test5"
android:scaleType="center"
android:layout_width="60dp"
android:layout_height="48dp"
android:src="#android:drawable/ic_menu_edit">
</ImageButton>
</LinearLayout>
<TextView
android:id="#+id/balken"
android:layout_width="fill_parent"
android:layout_marginTop="4dp"
android:layout_height="3sp"
android:background="#333142">
</TextView>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_gravity="bottom"
android:background="#000000"
android:paddingTop="5dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/save"
android:textSize="18dp"
android:layout_weight="1"
android:layout_gravity="bottom"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:text="#string/save"/>
<Button
android:id="#+id/cancel"
android:textSize="18dp"
android:layout_weight="1"
android:layout_gravity="bottom"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:text="#string/abort"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
Wrap the entire layout in a Relative Layout. Use a relative layout as your root element and set the Linear Layout containing your buttons to layout_alignParentBottom="true" and the ScrollView set to layout_alignParentTop="true".
The LinearLayout containing the buttons and the ScrollView should be direct children of the RelativeLayout.
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout <-- containing the layout minus the buttons
android:id="#+id/SV"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
<The Rest Of The Layout>
</LinearLayout>
<LinearLayout
android:id="#+id/LLButtons"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Buttons>
</LinearLayout>
</ScrollView>
If you want the buttons to be always at the bottom of the screen alignParentBottom. If you want the buttons to always be at the bottom of the scrollview alignBelow="#id/SV"
android:layout_height="wrap_content" in your inner LinearLayout is your trouble here, changing it to 'fill_parent' should help. In general, you may want to use hierarchyviewer to help troubleshoot layout issues like this.

Categories

Resources