How to achieve alignParentBottom="true" property in LinearLayout - android

If i want to set an image to the bottom of any screen then we can use android:layout_alignParentBottom="true" in relative layout. But because of some reason i am bound to use LinearLayout. There are other views (button, image button, listview) in the screen also. I want to place image at the bottom of my screen. Whatever may be the situation user wil be able to see this imageview at the bottom of the screen. How to achieve alignParentBottom="true" property in LinearLayout. See the folowing sample xml. I am using example1.xml but i want look and file that of example2.xml
example1.xml
<?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">
<TextView
android:id="#+id/textview1"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:background="#ffffff"
android:textColor="#000000"
android:text="I am sunil"
android:gravity="bottom"/>
</LinearLayout>
example2.xml
<
?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">
<TextView
android:id="#+id/textview1"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:background="#ffffff"
android:textColor="#000000"
android:text="I am sunil"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
Thanks

Assuming that you have both a gallery view and a listview, you could add weight to one of them, meaning that it would grow as much as possible. If you give a weight to more than one view, they will share the extra space proportionally.
For instance, add to your gallery and listview android:layout_weight="1"

You can use gravity="bottom" for LinearLayout.
For more information about Gravity property look here
Here is sample code, Try this:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="bottom">
<TextView
android:id="#+id/textview1"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:background="#ffffff"
android:textColor="#000000"
android:text="I am sunil"
android:gravity="bottom"/>
</LinearLayout>
Use gravity="bottom" for LinearLayout instead of TextView.

Related

Need help centering an EditText in a Linear Layout

I'm trying to center an edittext vertically and horizontally in a linear layout, but it is not working. Very simple problem really.
<?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"
android:background="#drawable/login">
<EditText
android:layout_width="150dp"
android:layout_height="wrap_content"
android:background="#drawable/loginbutton"
android:text="Username"
android:layout_gravity="center"
android:textSize="25dp"
android:gravity="center_horizontal|center_vertical"
android:textColor="#000000"
android:layout_margin="10dp"
android:id="#+id/username">
</EditText>
I'll try changing to a relativelayout in the mean time.
LinearLayout do not support centering in the direction it stacks items as far as I know. Unless the LinearLayout is crucial (which in your case it shouldn't be, as you also need the centering), I would recommend you switch to a RelativeLayout.
With a RelativeLayout, you can set the attribute android:layout_centerInParent="true" on the EditText to get it centred.
Using gravity affects the gravity of the contents of said UI item, this means (in this case) that the text inside the EditText would be centered. In order to center the UI item itself you need to define layout_gravity as this is the gravity a UI item holds within his parent. The code posted by the OP will do the horizontal center but not the vertical center.
As an alternative you can use RelativeLayout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/login"
android:orientation="vertical" >
<EditText
android:id="#+id/username"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#drawable/loginbutton"
android:layout_centerInParent="true"
android:text="Username"
android:textColor="#000000"
android:textSize="25dp" >
</EditText>
</RelativeLayout>
It is very simple. Add android:gravity="center" with the linear layout.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
>
Try this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/login"
android:layout_centerVertical="true"
android:layout_centerInParent="true">

Difficulty with ScrollView and LinearLayout

I'm trying to make an Android layout: 3 components inside a vertical LinearLayout. The center component is a ScrollView that contains a TextView. When the TextView contains a significant amount of text (more than can fit on the screen), the ScrollView grows all the way to the bottom of the screen, shows scrollbars, and pushes the last component, a LinearLayout with a Button inside, off the screen.
If the text inside the TextView inside the ScrollView is short enough, the button at the bottom of the screen is positioned perfectly.
The layout I'm trying to achieve is:
The XML for the layout I've written is:
<?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">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:layout_marginTop="10dip"
android:layout_marginBottom="10dip"
android:text="Title />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="web"
android:textColor="#FFFFFF"
android:background="#444444"
android:padding="10dip" />
</ScrollView>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<LinearLayout
android:orientation="horizontal"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button android:id="#+id/login_button"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_weight="1"
android:text="#string/next_button"/>
</LinearLayout>
</LinearLayout>
The scrollview is the second view object and is set to wrap_content, which is more than the screen.
I recommend a RelativeLayout. Top textview first with android:alignParentTop="true", the bottom LinearLayout next with android:alignParentBottom="true" and the scrollview listed last in the xml with the value android:alignBelow="#id/whatYouCallTheHeader.
This will align the bottom bar at the bottom of the screen, and the header at the top, no matter the size. Then the scrollview will have its own place, after the header and footer have been placed.
you should go for relativeLayout rather than LinearLayout. And you can use some properties like alignBelow and all.
Try adding a layout weight into the ScrollView ie.
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
This worked for me in a situation almost identical to the one you're presenting but left me wondering why, because it is counter-intuitive that increasing the layout weight of a control from 0 (the default if you don't specify a layout_weight) to 1 should make a control which is already using too much space smaller.
I suspect the reason it works is that by not specifying a layout_weight you actually allow the layout to ignore the size of the scroll view relative to other controls and conversely if do specify one you give it permission to shrink it in proportion to the weights you assign.
![Fixed Header-Footer and scrollable Body layout ][1]
This is what you are looking for . Most of the app in android had this type of layout ,
a fixed header and footer and a scrollable body . The xml for this layout is
<?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:background="#5599DD"
android:layout_height="fill_parent">
<!-- Header goes here -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:layout_marginTop="10dip"
android:layout_marginBottom="10dip"
android:textSize="20sp"
android:layout_gravity="center"
android:text="Title" />
<!-- Body goes here -->
<ScrollView
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="web"
android:text="#string/lorem_ipsum"
android:textColor="#FFFFFF"
android:padding="10dip" />
</ScrollView>
<!-- footer goes here -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="#+id/login_button"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="bottom"
android:layout_alignParentRight="true"
android:text="Button"/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>

problem with android layout

I have the following layout for a custom title bar. However, the problem is this: both the imageview and the imagebutton are coming at the centre. I was expecting the imagebutton to be at the extreme right. Can anyone kindly let me know what I did wrong here ? Thanks.
<?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="35dip"
android:gravity="center_vertical"
android:paddingLeft="5dip"
android:background="#323331">
<ImageView
android:src="#drawable/header"
android:id="#+id/header"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:gravity="center_horizontal">
</ImageView>
<ImageButton
android:id="#+id/saveButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/savetap"
android:background="#null"
android:gravity="right"
android:layout_above="#+id/header">
</ImageButton>
</LinearLayout>
Your layout is only 35dip tall, so pressumibly if you show your ImageView the ImageButton gets positioned outside the screen. Consider changing your layout_height to wrap_content, if appropiate.
You're using a vertically oriented LinearLayout, all View will be presented in a vertical fashion. Use a RelativeLayout so that you'll have more control over where Views are positioned. If you'd still like to use a LinearLayout, you'll have to use horizontal orientation so that the Views can be on the same "line".
Use layout_gravity to layout the item to the right, just gravity is used by the contents of the view.
At first if width is set to fill parent it really fills parent so it use all the width.
Second: why not RelativeLayout? It provides more options and control.
Maybe this is what you looking for:
`
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="35dip"
android:gravity="center_vertical"
android:paddingLeft="5dip"
android:background="#323331">
<ImageView
android:src="#drawable/header"
android:id="#+id/header"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="left">
</ImageView>
<ImageButton
android:id="#+id/saveButton"
android:layout_height="wrap_content"
android:src="#drawable/savetap"
android:background="#null"
android:layout_marginLeft="20dp"
android:layout_width="wrap_content" android:layout_alignParentRight="true">
</ImageButton>
</RelativeLayout>
`

Android layout_gravity problem?

Following is my xml
I want to set button1 to left centrally vertical and button2 to right centrally vertical but for me android:layout_gravity="center_vertical|right" is not working what is the wrong?
I know that i can achieve this by applying by using padding or margin but I won't know that why it is not working. How should I make it work?
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"
android:layout_gravity="center_vertical"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2"
android:layout_gravity="center_vertical|right"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</RelativeLayout>
</FrameLayout>
</HorizontalScrollView>
Here your problem is as you are using HorizontalScrollView , as a result FrameLayout is not being able to make its width to fill_parent.If you remove the HorizontalScrollView you will get the desired result I think.
Yes, if you replace HorizontalScrollView with LinearLayout, then you will see the desired effect.

Android UI Layout Question

I am new to Android programming and have done a lot of searching for the answer to this question however I can not find an answer. Maybe I am using the wrong search terms because it seems like a pretty basic request.
I my design (as an example), I would have two text fields defined in my relative layout followed by an image and I would like the image to span the remainder of the screen. Here is what I have so far:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/widget29"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<ImageView
android:id="#+id/widget38"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/widget37"
android:layout_alignParentLeft="true"
>
</ImageView>
<TextView
android:id="#+id/widget37"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="second view"
android:layout_below="#+id/widget36"
android:layout_alignParentLeft="true"
>
</TextView>
<TextView
android:id="#+id/widget36"
android:layout_width="fill_parent"
android:layout_height="50px"
android:text="first view"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
>
</TextView>
</RelativeLayout>
How do I tell my ImageView to span fill the remainder of the screen. If I define the height and width as "fill_parent" it fills over the two text views.
Thanks in advance for any help you can provide.
Jon
It looks like you're just stacking views... it seems to me that this situation would work a lot better with a vertical LinearLayout instead of a RelativeLayout, for one it would be significantly easier to get the image view to span the rest of the space without overlapping your text views:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/widget29"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TextView
android:id="#+id/widget36"
android:layout_width="fill_parent"
android:layout_height="50px"
android:text="first view"
android:layout_weight="0"
/>
<TextView
android:id="#+id/widget37"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="second view"
android:layout_weight="0"
/>
<ImageView
android:id="#+id/widget38"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
/>
</LinearLayout>
In the above example you use layout weights to force relative sharing of the screen space. Setting the weight to zero forces the layout manager to give only requested space to the two text views and since the image view is set to fill parent (with a weight of 1) it receives all of the remaining space
If it's RelativeLayout, then, you will have to do something like this in your ImageView:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/id_of_your_second_textview"
android:layout_alignParentRight="true"/>

Categories

Resources