Why do RelativeLayout buttons override each other? - android

I seem to be having trouble using relative links to display buttons. They stick together, like buttons overriding each other, and I've tried to get them to not stick, but to no avail.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:background="#drawable/formation"
android:layout_height="fill_parent" >
<Button
android:layout_weight="1.0"
android:layout_gravity="bottom"
android:layout_alignParentTop="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Laws" />
<Button
android:layout_weight="1.0"
android:layout_gravity="bottom"
android:layout_alignParentTop="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Qualifications of a naval officer" />
<Button
android:layout_weight="1.0"
android:layout_gravity="bottom"
android:layout_alignParentTop="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Code of Conduct" />
</RelativeLayout>

while creating any xml file, keep in mind that every widget is unique by its id, so first of all you have to assign id to each widget.
Now, here you are using Relative Layout. so every widget you added here will relate to each other.
Now see this code, i did change in your code.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:background="#drawable/formation"
android:layout_height="fill_parent" >
<Button
android:id="#+id/btn1"
android:layout_weight="1.0"
android:layout_gravity="bottom"
android:layout_alignParentTop="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Laws" />
<Button
android:id="#+id/btn2"
android:layout_gravity="bottom"
android:layout_below="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Qualifications of a naval officer" />
<Button
android:id="#+id/btn3"
android:layout_gravity="bottom"
android:layout_below="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Code of Conduct" />
</RelativeLayout>
So all button will display one by one.

This will put things one next to other side ways.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:background="#drawable/formation"
android:layout_height="fill_parent" >
<Button
android:id="#+id/button1"
android:layout_weight="1.0"
android:layout_gravity="bottom"
android:layout_alignParentTop="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Laws" />
<Button
android:id="#+id/button2"
android:layout_toRightOf="#id/button1"
android:layout_weight="1.0"
android:layout_gravity="bottom"
android:layout_alignParentTop="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Qualifications of a naval officer" />
<Button
android:id="#+id/button3"
android:layout_toRightOf="#id/button2"
android:layout_weight="1.0"
android:layout_gravity="bottom"
android:layout_alignParentTop="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Code of Conduct" />
</RelativeLayout>

Try this
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/formation" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_gravity="bottom"
android:text="#string/Laws" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_gravity="bottom"
android:layout_toRightOf="#+id/button1"
android:text="Code of Conduct" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_toRightOf="#+id/button2"
android:text="Qualifications of a naval officer" />
</RelativeLayout>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/white" >
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Laws" />
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/btn1"
android:text="Code of Conduct" />
<Button
android:id="#+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/btn2"
android:text="Qualifications of a naval officer" />
</RelativeLayout>

Related

How to view only two TextView in Android

I have a questionnaire, in which I want the user to see only the previous and current question. How to achieve this in Android.
This is my XML View.
The user can see maximum two questions -- previous and current.
I have used a RelativeLayout which contains ScrollView, and in that I'm using multiple TextView for questions and Button for true/false
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
style="#android:style/Widget.DeviceDefault.Light.ScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:fillViewport="false"
android:scrollbarStyle="outsideOverlay"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.scanqr_android.ScrollingActivity"
tools:showIn="#layout/activity_scrolling">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#3f51b5"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="MY PAGE TITLE"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#fff" />
<TextView
android:id="#+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Caterpillars turn into butterflies."
tools:text="Caterpillars turn into butterflies." />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="true" />
<Button
android:id="#+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="false" />
</LinearLayout>
<TextView
android:id="#+id/textView9"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Bubble gum contains rubber."
tools:text="Bubble gum contains rubber." />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="#+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="true" />
<Button
android:id="#+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="false" />
</LinearLayout>
<TextView
android:id="#+id/textView10"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="The deadliest earthquake of 2017 took place in Iran and Iraq." />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="#+id/Button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="true" />
<Button
android:id="#+id/button10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="false" />
</LinearLayout>
<TextView
android:id="#+id/textView11"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Electrons are larger than molecules." />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="#+id/button11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="true" />
<Button
android:id="#+id/button12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="false" />
</LinearLayout>
<TextView
android:id="#+id/textView12"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Thunderstorms have particular sound frequencies that can hurt a dog’s ears." />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="#+id/button13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="true" />
<Button
android:id="#+id/button14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="false" />
</LinearLayout>
<TextView
android:id="#+id/textView13"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" There are 30 days in May." />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="#+id/button15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="true" />
<Button
android:id="#+id/button16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="false" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
Firstly register your layout id by using findViewById() in java file.
Then you can hide the layout of textviews using like this -
linearLayout.setVisibility(View.GONE); by checking conditions.
You just hide the textview like this
textView.setVisibility(View.INVISIBLE);
or
textView.setVisibility(View.GONE);
Take a look at this How to set visibility of TextView?

Center button between two other buttons

I'm trying to center a button in the middle of other two buttons. I tried using weight but with no luck (Android Studio keep saying that there's an error), using layout_marginRight is not a good idea because it's not resolution independent.
Here's my XML code I hope it can help you understanding my problem.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/app_bar_main"
tools:context=".MainActivity">
<TextView
android:text="Hello World!"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_bottom"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:background="?attr/colorPrimary"
android:layout_alignParentBottom="true"
android:minHeight="?attr/actionBarSize" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:gravity="center">
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/bottom_LeftButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lft"
android:layout_alignParentLeft="true" />
<Button
android:id="#+id/bottom_LeftCenterButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LftC"
android:layout_alignParentLeft="true" />
<Button
android:id="#+id/bottom_CenterButtom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Cnt" />
<Button
android:id="#+id/bottom_RightCenterButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RhtC"
android:layout_alignParentRight="true" />
<Button
android:id="#+id/bottom_RightButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rht"
android:layout_alignParentRight="true" />
</RelativeLayout>
</LinearLayout>
</android.support.v7.widget.Toolbar>
<LinearLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/bottom_LeftButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lft"
android:layout_weight="1"
android:layout_alignParentLeft="true" />
<Button
android:id="#+id/bottom_LeftCenterButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LftC"
android:layout_weight="1"
android:layout_alignParentLeft="true" />
<Button
android:id="#+id/bottom_CenterButtom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Cnt" />
<Button
android:id="#+id/bottom_RightCenterButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RhtC"
android:layout_weight="1"
android:layout_alignParentRight="true" />
<Button
android:id="#+id/bottom_RightButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rht"
android:layout_weight="1"
android:layout_alignParentRight="true" />
</LinearLayout>
To keep all the buttons in of same size you need to to keep the width attribute to "fill_parent". Below is the working xml. weightsum should be equal to the number of buttons you want to use.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="4">
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/button3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/button4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
Use Linear layout then you include "WEIGHT"to your Button,because weight belongs to linearlayout.

layout alignment for different screens

i am trying to align the page for different screens namely 4.5inch(xperia ion) and 4inch(s advance) it works with s advance.. but in xperia ion screen it breaks..
the emulator image of both xperia ion and s advance...
i stretched the buttons to avoid mistakes.. but yet when it comes to services.. the system fails... the big buttons on top and bottom are buffer buttons.. they are meant to simply align the rest..
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/rail" >
<Button
android:id="#+id/btnBufferTop" <-- buffer top button
android:layout_width="wrap_content"
android:layout_height="130dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button" />
<Button
android:id="#+id/btnBufferBottom" <-- buffer bottom button
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="Button" />
<Button
android:id="#+id/button1" <-- home button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/btnBufferTop"
android:text="Button" />
<Button
android:id="#+id/button2" <-- About us button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/button1"
android:text="Button" />
<Button
android:id="#+id/button3" <-- services
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/btnBufferBottom"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="Button" />
<Button
android:id="#+id/button4" <-- clients
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button3"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="Button" />
<Button
android:id="#+id/button5" <-- contacts
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button4"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="14dp"
android:text="Button" />
is there any other way to do it???? without buffer buttons
// Try this way,hope this will help you to solve your problem...
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/btnBufferTop"
android:layout_width="match_parent"
android:layout_height="130dp"
android:text="Button" />
<Button
android:id="#+id/btnBufferBottom"
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="Button" />
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="#+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="#+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="#+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="14dp"
android:text="Button" />
</LinearLayout>
its better to go with linear layout with weights.as weights device each screen in equal parts.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum= 2
android:orientation="vertical">
<Button
android:id="#+id/btnBufferTop"
android: layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="Button" />
<Button
android:id="#+id/btnBufferBottom"
android: layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="Button" />
</LinearLayout>
If the space between the buttons are the issue, thenTry android:layout_marginTop=""
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:color/white"
android:orientation="vertical"
android:gravity="center">
<Button
android:id="#+id/okbutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="SET"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:color/white"
android:orientation="vertical"
android:gravity="center">
<Button
android:id="#+id/okbuttodn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="SET"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:color/white"
android:orientation="vertical"
android:gravity="center">
<Button
android:id="#+id/okbuttdtgyodn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="SET"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:color/white"
android:orientation="vertical"
android:gravity="center">
<Button
android:id="#+id/okbuttrtodn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="SET"
/>
</LinearLayout>
</LinearLayout>

How can I make this layout for android?

[SOLVED]I have a very specific layout that I want to make. It feels like it should be very easy to create but I can't seem to wrap my fingers around it. So I want to create a layout that looks like the image below. How the hell do I get this? I just get the text and the 2 images and then get the buttons to be visible. Posted my layout below as well.
Thanks in advance // eXpliCo.
http://imgur.com/Qu5CRDC
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_layout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="20"
android:orientation="vertical" >
<TextView
android:id="#+id/question_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/question_text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:id="#+id/image_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/left_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/picture_placeholder" />
<ImageView
android:id="#+id/right_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/picture_placeholder" />
</LinearLayout>
<LinearLayout
android:id="#+id/navigation_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Button" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Button" />
</LinearLayout>
<LinearLayout
android:id="#+id/vote_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|center" >
<Button
android:id="#+id/vote_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Button" />
<Button
android:id="#+id/vote_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Button" />
</LinearLayout>
</LinearLayout>
EDIT: I got a solution now. I don't know if it's the best but it works for me. But I can't answer my own code yet so I'll just post my solution here.
<LinearLayout 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/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Test123 312 123 312"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="100" >
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Test" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Test" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="test" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="test" />
</LinearLayout>
</LinearLayout>
In your second LinearLayout, set the orientation to horizontal.
Here you go
<?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:text="Some Text here"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/SomeText"
android:gravity="center"
android:textSize="20sp" />
<LinearLayout
android:id="#+id/image_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/SomeText"
android:orientation="horizontal" >
<ImageView
android:id="#+id/left_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:layout_weight="1" />
<ImageView
android:id="#+id/right_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:id="#+id/button_layout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/image_layout"
android:orientation="horizontal" >
<Button
android:id="#+id/vote_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"
android:layout_weight="1"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp" />
<Button
android:id="#+id/vote_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2"
android:layout_weight="1"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp" />
</LinearLayout>
<LinearLayout
android:id="#+id/button_layout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/button_layout1"
android:orientation="horizontal" >
<Button
android:id="#+id/vote_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3"
android:layout_weight="1"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp" />
<Button
android:id="#+id/vote_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 4"
android:layout_weight="1"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp" />
</LinearLayout>
</RelativeLayout>
It sounds like this works with smaller images but not larger images. If this is the case then use layout_weight in each child LinearLayout so that each takes up a certain amount of space. This will help the layout to look similar on different screen sizes and resolutions.
So, for instance...
<TextView
android:text="Some Text here"
android:layout_weight="1"
...
LinearLayout
android:id="#+id/image_layout"
android:layout_weight="3"
...
<LinearLayout
android:id="#+id/button_layout1"
android:layout_weight="2"
...
<LinearLayout
android:id="#+id/button_layout2"
android:layout_weight="2"
...
You may have to adjust the weights to what you want/need but something like that should help. You will also want to set your height for each of those to 0dp since the parent LinearLayout is vertical.

place and align a button above 4 views

I want to place a button above 4 other views:
The views b1-b4 are not really buttons, but they look and behave like buttons (clickable and focusable). Equal spacing is desirable.
I have found Evenly spaced out row of buttons required but how do I make button0 occupy exactly the width of b1-b4?
Try this.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button0" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="b1" />
<Button
android:id="#+id/button3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="b2" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="b3" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="b4" />
</LinearLayout>
</LinearLayout>
Now let me know, is it helpful or not..??
Try this..
<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" >
<LinearLayout
android:id="#+id/linear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B2" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B3" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B4" />
</LinearLayout>
<Button
android:id="#+id/button0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/linear"
android:layout_alignLeft="#+id/linear"
android:layout_alignRight="#+id/linear"
android:text="Button0" />
</RelativeLayout>

Categories

Resources