try to display seven buttons with scrollview in a small screen - android

I'm trying to put in my layout seven buttons in a small screen to test it (Galaxy young), So I used the scrollview but I still have problems (the screen doesn't scroll in LandScape mode ).
this is my xml code:
<?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:layout_weight="1"
android:fillViewport="true">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:id="#+id/lieu_depart"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Lieu de prise en charge"
style="" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentLeft="false"
android:layout_alignParentRight="false"
android:layout_below="#+id/linearLayout1"
android:layout_centerHorizontal="true"
android:layout_marginTop="#dimen/activity_vertical_margin" >
<Button
android:id="#+id/button2_date_depart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:text="Date1" />
<Button
android:id="#+id/time_depart5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:text="Time1" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout3"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="#+id/linearLayout2"
android:layout_centerHorizontal="true"
android:orientation="horizontal" >
<Button
android:id="#+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minWidth="#dimen/activity_vertical_margin"
android:text="Lieu de restitution" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/linearLayout3"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:orientation="horizontal" >
<Button
android:id="#+id/button5_date_arrive"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:text="Date2" />
<Button
android:id="#+id/button6_time_arrive"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:text="Time2" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/linearLayout4"
android:orientation="horizontal" >
<Button
android:id="#+id/Button03"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Button" />
</LinearLayout>
</RelativeLayout>
</ScrollView>
EDIT:
Portrait mode:
Landscape mode:
what should I use to limit this problem?
Thank you

As I understand from your question it works in portrait but not landscape, so try this. As you rotate your device it should still scroll. I have placed this is in one Layout instead of multiple layouts.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="#+id/lieu_depart"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Lieu de prise en charge"
style="" />
<Button
android:id="#+id/button2_date_depart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:text="Date1" />
<Button
android:id="#+id/time_depart5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:text="Time1" />
<Button
android:id="#+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minWidth="#dimen/activity_vertical_margin"
android:text="Lieu de restitution" />
<Button
android:id="#+id/button5_date_arrive"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:text="Date2" />
<Button
android:id="#+id/button6_time_arrive"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:text="Time2" />
<Button
android:id="#+id/Button03"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Button" />
</RelativeLayout>
</ScrollView>
</RelativeLayout>
EDIT...
You need to add the "android:layout_below="#+id/your button id" at every button so that the buttons will be displayed underneath each other..
<Button
android:id="#+id/lieu_depart"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Lieu de prise en charge"
style="" />
<Button
android:id="#+id/button2_date_depart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/lieu_depart"
android:layout_marginLeft="50dp"
android:text="Date1" />
<Button
android:id="#+id/time_depart5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button2_date_depart"
android:layout_marginLeft="70dp"
android:text="Time1" />
<Button
android:id="#+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/time_depart5"
android:minWidth="#dimen/activity_vertical_margin"
android:text="Lieu de restitution" />
<Button
android:id="#+id/button5_date_arrive"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button4"
android:layout_marginLeft="50dp"
android:text="Date2" />
<Button
android:id="#+id/button6_time_arrive"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:layout_below="#+id/button5_date_arrive"
android:text="Time2" />
<Button
android:id="#+id/Button03"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/button6_time_arrive"
android:gravity="center"
android:text="Button" />
You can then play with the layout of your buttons by setting the gravity and margins. This should now fix your problem you are having.

You must use LienarLayout inside of ScrollView. RelativeLayout ignores orientation and used when you want put your items with absolute position. Your xml layout has many things to change . Can you send image of your layout to understand what exacly you want.
Here you have :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Starting place" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date 1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Time 1" />
</LinearLayout>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Arrival place" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:text="Time2" />
</LinearLayout>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Submit" />
</LinearLayout>
</ScrollView>
In last linearlayout 2nd button i have include layout_marginLeft but with linearlayout gravity center it will center buttons and divide this space between two button, so you can manipulate with space between them.
By default layout will fill parent but in your second image you have something like right margin if you need it you can use android configuration qualifiers but better way to not duplicate layout xml is using values-land qualifier and add dimens folder where you can add padding value and add to your layout into first layout child of scrollview android:paddingRight="#dimen/your_property_name". More about dimen resources.

Related

relative layout gravity center not aligning correctly

I'm trying to center the chilldren of my relative layout in the center of my screen but it's acting like it's aligned to the top of the parent and I can't figure out why.
my .XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:background="#f70909">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView2"
android:adjustViewBounds="true"
android:src="#drawable/dice"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/textMessage"
android:layout_centerHorizontal="true"
android:layout_below="#+id/imageView2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editUserSplash"
android:hint="Username"
android:gravity="center"
android:layout_centerHorizontal="true"
android:layout_below="#+id/textMessage" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editPasswordSplash"
android:hint="Password"
android:layout_centerHorizontal="true"
android:gravity="center"
android:layout_below="#+id/editUserSplash" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:id="#+id/btnSplash"
android:layout_centerHorizontal="true"
android:layout_below="#+id/editPasswordSplash" />
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/prgSplash"
style="#android:style/Widget.DeviceDefault.ProgressBar.Large"
android:layout_centerHorizontal="true"
android:indeterminate="true"
android:layout_below="#+id/btnSplash" />
</RelativeLayout>
I've tried making the parent a relativelayout without success and it won't align to the bottom either. Initially I thought the layout wasn't filling the whole screen but since its height and width are match_parent I don't think that's the problem. in android studio it is displaying correctly though so I must be missing something small.
I also tried using the gravity and layoutgravity parameters and a combination of the two but without success
Edit: I need the views to stay in the same formation relative to each other but centered in the screen vertically.
Edit 2:I set the background of the RelativeLayout to red and got this: So the relativelayout isn't filling my screen.
Edit 3:
Edit 4:
try this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f70909">
<RelativeLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView2"
android:adjustViewBounds="true"
android:src="#drawable/dice"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/textMessage"
android:layout_centerHorizontal="true"
android:layout_below="#+id/imageView2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editUserSplash"
android:hint="Username"
android:gravity="center"
android:layout_centerHorizontal="true"
android:layout_below="#+id/textMessage" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editPasswordSplash"
android:hint="Password"
android:layout_centerHorizontal="true"
android:gravity="center"
android:layout_below="#+id/editUserSplash" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:id="#+id/btnSplash"
android:layout_centerHorizontal="true"
android:layout_below="#+id/editPasswordSplash" />
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/prgSplash"
style="#android:style/Widget.DeviceDefault.ProgressBar.Large"
android:layout_centerHorizontal="true"
android:indeterminate="true"
android:layout_below="#+id/btnSplash" />
</RelativeLayout>
</RelativeLayout>
Try something like this.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView2"
android:adjustViewBounds="true"
android:layout_centerInParent="true"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:layout_centerInParent="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/textMessage" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editUserSplash"
android:hint="Username"
android:gravity="center" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editPasswordSplash"
android:hint="Password"
android:gravity="center" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:id="#+id/btnSplash" />
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/prgSplash"
style="#android:style/Widget.DeviceDefault.ProgressBar.Large"
android:indeterminate="true" />
</LinearLayout>
</RelativeLayout>
UPDATE:
If setting the xml in an AlertDialog, it's possible that there is a space allotted at the bottom. I google and found this alert_dialog.xml for reference. It seems that there is a buttonPanel at the bottom with a minimum height of 54dip.
<LinearLayout android:id="#+id/buttonPanel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="54dip"
android:orientation="vertical" >
<LinearLayout
style="?android:attr/buttonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="4dip"
android:paddingStart="2dip"
android:paddingEnd="2dip"
android:measureWithLargestChild="true">
<LinearLayout android:id="#+id/leftSpacer"
android:layout_weight="0.25"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:visibility="gone" />
<Button android:id="#+id/button1"
android:layout_width="0dip"
android:layout_gravity="start"
android:layout_weight="1"
style="?android:attr/buttonBarButtonStyle"
android:maxLines="2"
android:layout_height="wrap_content" />
<Button android:id="#+id/button3"
android:layout_width="0dip"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
style="?android:attr/buttonBarButtonStyle"
android:maxLines="2"
android:layout_height="wrap_content" />
<Button android:id="#+id/button2"
android:layout_width="0dip"
android:layout_gravity="end"
android:layout_weight="1"
style="?android:attr/buttonBarButtonStyle"
android:maxLines="2"
android:layout_height="wrap_content" />
<LinearLayout android:id="#+id/rightSpacer"
android:layout_width="0dip"
android:layout_weight="0.25"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:visibility="gone" />
</LinearLayout>
</LinearLayout>
I think this may help you
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:gravity="center">
<RelativeLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/textMessage"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<EditText
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editUserSplash"
android:hint="Username"
android:layout_below="#+id/textMessage"
android:gravity="center" />
<EditText
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editPasswordSplash"
android:hint="Password"
android:layout_below="#+id/editUserSplash"
android:gravity="center" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:id="#+id/btnSplash"
android:layout_below="#+id/editPasswordSplash"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Thanks to ank I was able to figure out that the reason why the RelativeLayout didn't fill my screen is that I used it in an alertDialog. So RelativeLayouts parent isn't the screen but the alertDialog. Since an alertDialog wraps its content it doesn't fill the entire screen.

How to center two views within a relative layout?

The task is simple: there are two buttons and a TextView above them. All the widgets shoud be centered within the relative layout. The only one idea I have is create the third widget View and use it as a center axis for the buttons. Any ideas? A redundant layout isn't a good solution.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/tv_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="#string/app_name" />
<View
android:id="#+id/view_axis"
android:layout_width="1dp"
android:layout_height="1dp"
android:layout_below="#id/tv_progress"
android:layout_centerInParent="true" />
<Button
android:id="#+id/button_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tv_progress"
android:layout_toLeftOf="#id/view_axis"
android:text="#string/start" />
<Button
android:id="#+id/button_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tv_progress"
android:layout_toRightOf="#id/view_axis"
android:text="#string/stop" />
</RelativeLayout>
If I understand what you want correctly, you can put the Buttons in a LinearLayout and center that
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/tv_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="#string/app_name" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#id/tv_progress">
<Button
android:id="#+id/button_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/start" />
<Button
android:id="#+id/button_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/stop" />
</LinearLayout>
I'm not sure if that's what you meant by a "redundant layout" but doing this is fine if it gives you what you want.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Spinner
android:id="#+id/sp_rooms"
android:layout_toLeftOf="#id/space"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Space
android:id="#+id/space"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn_registration"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/space"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
This will vertically and horizontally center the whole block consisting of the textview + buttons
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerInParent="true">
<TextView
android:id="#+id/tv_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_name" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/button_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/start" />
<Button
android:id="#+id/button_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/stop" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>

Android: how to align views vertically and horizontally

I'm wondering how I can align the buttons as the following: Button1 and Button2-2 are supposed to align horizontally, Button2-1 and Button2-2 should align vertically.
[Button2-1]
[other views] [Button1] [Button2-2]
Here is my code but it doesn't work:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ButtonGroups"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<Button
android:id="#+id/Button2-1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<Button
android:id="#+id/Button2-2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
EDITED:
To be more clear, I posted more code here and hopefully it could explain my needs better:
What I want is to align the text view horizontally with the image buttons while vertically with the seekbar above it.
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ButtonGroups"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/Button00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/background00"
android:gravity="bottom"
android:scaleType="center"
android:src="#drawable/button00" />
<ImageButton android:id="#+id/Button11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/button11"
android:scaleType="center"
android:gravity="bottom"
android:background="#drawable/background11"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom"
android:orientation="vertical" >
<SeekBar
android:id="#+id/SeekBar1"
style="#style/MySeekBar1"
android:layout_width="60dp"
android:layout_height="35dp"
android:layout_marginBottom="20dp"
/>
<TextView
android:id="#+id/MyText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom"
android:background="#drawable/MyTextImg"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Use RelativeLayout. Stacking LinearLayouts is usually not the way to go!
https://developer.android.com/reference/android/widget/RelativeLayout.html
https://developer.android.com/guide/topics/ui/layout/relative.html
Try changing the gravity attribute of the first button (Button 1) to bottom.
As someone suggested, use a RelativeLayout to design this type of pattern! Here's an example for your situation:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ButtonGroups"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/Button2-2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/Button1"
android:layout_alignBottom="#+id/Button1"
android:layout_toRightOf="#+id/Button1"
android:text="button 2-2" />
<Button
android:id="#+id/Button2-1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/Button2-2"
android:layout_alignLeft="#+id/Button2-2"
android:text="button 2-1" />
<Button
android:id="#+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="54dp"
android:text="button 1" />
</RelativeLayout>
I hope this get you started!
Try the code below:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="*">
<TableRow >
<TextView />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2-1" />
</TableRow>
<TableRow >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2-2" />
</TableRow>
</TableLayout>
The trick is to use a blank text view for the empty space in the first row. Let me know if this helps you. :)

Making linearlayouts stay at bottom of screen in android

I am making an android application that uses these views:
(Root) - LinearLayout
(Child of root) - ListView + 3 other linearlayouts beneeth that listview. But once i add to many items to the listview, the layouts are being placed outside of the screen. Which i don't want to. How can i make this happen? So that all of the three linearlayouts stay on the screen, at the bottom??? Please help and thanks in advance!
My screen XML code:
<?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:background="#drawable/background"
android:orientation="vertical" >
<TextView
android:id="#+id/NotesWelcomeTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/NotesWelcomeText" />
<ListView
android:id="#+android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView
<LinearLayout
android:id="#+id/DeleteAllItemsFromListViewLinearLayout"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:visibility="invisible" >
<Button
android:id="#+id/CancelButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Cancel" />
<Button
android:id="#+id/DeleteAllButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Delete" />
</LinearLayout>
<LinearLayout
android:id="#+id/DeleteItemFromListViewLinearLayout"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:visibility="invisible" >
<Button
android:id="#+id/CancelButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Cancel" />
<Button
android:id="#+id/DeleteButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Delete" />
</LinearLayout>
<LinearLayout
android:id="#+id/AddItemToListViewLinearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone" >
<EditText
android:id="#+id/AddItemToListViewEditText"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_weight="1" >
</EditText>
<Button
android:id="#+id/AddItemToListViewButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/Add" />
</LinearLayout>
</LinearLayout>
Try this, you'll have to add the string references and background again, I removed them so I wouldn't have errors
<?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:orientation="vertical">
<TextView android:id="#+id/NotesWelcomeTextView"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="NotesWelcomeText" />
<ListView android:id="#+android:id/list" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_above="#+id/AddItemToListViewLinearLayout"
android:layout_below="#+id/NotesWelcomeTextView">
</ListView>
<LinearLayout android:id="#+id/DeleteAllItemsFromListViewLinearLayout"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_weight="1" android:visibility="invisible"
android:layout_alignParentBottom="true">
<Button android:id="#+id/CancelButton2" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Cancel" />
<Button android:id="#+id/DeleteAllButton" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Delete" />
</LinearLayout>
<LinearLayout android:id="#+id/DeleteItemFromListViewLinearLayout"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_weight="1" android:visibility="invisible"
android:layout_alignParentBottom="true">
<Button android:id="#+id/CancelButton" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Cancel" />
<Button android:id="#+id/DeleteButton" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Delete" />
</LinearLayout>
<LinearLayout android:id="#+id/AddItemToListViewLinearLayout"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:visibility="gone" android:layout_alignParentBottom="true">
<EditText android:id="#+id/AddItemToListViewEditText"
android:layout_width="250dp" android:layout_height="wrap_content"
android:layout_weight="1">
</EditText>
<Button android:id="#+id/AddItemToListViewButton"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:text="Add" />
</LinearLayout>
Give your ListView a weight of 0 and set its height to 0. This will tell the ListView to only take up the space that is not use by other views.
<ListView ...
android:layout_weight="0"
android:layout_height="0"
...
/>

Complex layout with same layout_margin in Android

I have a problems with my layout_margin. I want to make my layout look like that:
(with "a" is margin)
My problems is, when i build my layout in other screen size, it look like that:
How can i make it beautiful with different screen size? This is my layout:
<LinearLayout
android:id="#+id/footer_result_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:visibility="gone" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal" >
<ImageView
android:id="#+id/btn_recommendtion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/coodinate" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_marginTop="20dip"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal" >
<ImageView
android:id="#+id/btn_facebook"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_margin="5dip"
android:layout_weight="1"
android:adjustViewBounds="true"
android:src="#drawable/fb" />
<ImageView
android:id="#+id/btn_mixi"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_margin="5dip"
android:layout_weight="1"
android:adjustViewBounds="true"
android:src="#drawable/mixi" />
</LinearLayout>
</LinearLayout>
Try using a relative layout.
Here is an example using buttons. You can swap out the values of the buttons with your image views, and adjust your margins as needed. This should center the buttons, with the same margins on any screen.
<Button
android:id="#+id/btn_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:text="BUTTON 1"
android:layout_centerHorizontal="true"/>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="#+id/btn_1"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dip"
android:orientation="horizontal" >
<Button
android:id="#+id/btn_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:layout_margin="5dip"
android:text="BUTTON 2" />
<Button
android:id="#+id/btn_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:layout_margin="5dip"
android:text="BUTTON 3" />
</LinearLayout>
It looks like this:
You'll need to use RelativeLayout for that.
Center button1 with android:layout_alignParentTop="true" and align it in the parent top with android:layout_centerHorizontal="true and work from there.
The code:-
<?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">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"
android:layout_margin="10dip"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2"
android:layout_margin="10dip"
android:layout_alignParentLeft="true"
android:layout_below="#+id/button1"/>
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3"
android:layout_margin="10dip"
android:layout_alignParentRight="true"
android:layout_below="#+id/button1"/>
</RelativeLayout>
Considering the a=10

Categories

Resources