setting layout attribute for include - android

In my Android app I declare a custom view in a separate XML file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/download_interval_setter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/Button_interval_up"
style="?android:attr/buttonStyleSmall">
</Button>
<EditText
android:id="#+id/Download_interval"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:numeric="integer">
</EditText>
<Button
android:id="#+id/Button_interval_down"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</Button>
</LinearLayout>
Then I have my main.xml file where I would like to include the view declared in above XML like this:
<?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">
<ToggleButton android:layout_height="wrap_content"
android:id="#+id/XMLStart"
android:textOn="#string/auto_update_on"
android:textOff="#string/auto_update_off"
android:layout_width="wrap_content" />
<include layout="#layout/download_interval_setter"
android:layout_toRightOf="#id/XMLStart"/>
<Button android:layout_height="wrap_content"
android:id="#+id/ShowRadars"
android:text="#string/show_radars"
android:layout_width="wrap_content"
android:layout_below="#id/XMLStart"/>
<Button android:layout_height="wrap_content"
android:id="#+id/ShowAccidents"
android:text="#string/show_accidents"
android:layout_width="wrap_content"
android:layout_toRightOf="#id/ShowRadars"
android:layout_below="#id/XMLStart"/>
<Button android:layout_height="wrap_content"
android:id="#+id/ShowConstraints"
android:text="#string/show_constraints"
android:layout_width="wrap_content"
android:layout_below="#id/ShowRadars"/>
<Button android:layout_height="wrap_content"
android:id="#+id/ShowPatrols"
android:text="#string/show_patrols"
android:layout_width="wrap_content"
android:layout_below="#id/ShowRadars"
android:layout_toRightOf="#id/ShowConstraints"/>
</RelativeLayout>
As you can see I am trying to set the layout_toRightOf attribute of the included view but it is not set, as I can see also in hierarchyviewer
On the other hand if I simply copy-paste the content of the download_interval_setter.xml file to main.xml and declare the layout_toRightOf parameter there , the view will be positioned to right of as I want.
Where am I going wrong?
Thanks

You must use RelativeLayout as root layout in the included xml file, because android:layout_toRightOf cannot be used with LinearLayout.

You have not specified the width and the height of the include layout

Related

ListView hidden behind button

The contents of my list view get hidden behind the button as follows:
The xml file is:
<?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="fill_parent"
android:layout_gravity="top"
>
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
/>
<RelativeLayout
android:id="#+id/InnerRelativeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
>
<Button
android:text="Send"
android:id="#+id/Button"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="addItems"
>
</Button>
<EditText
android:id="#+id/message"
android:layout_width="fill_parent"
android:layout_toLeftOf="#id/Button"
android:layout_height="wrap_content"
>
</EditText>
</RelativeLayout>
</RelativeLayout>
The TextView for each row is as follows:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content
android:layout_gravity="right"
android:gravity="right"
android:textSize="20sp"
/>
What should i do to align it properly (as in above the button)?
Also when the listView is with just one or two entries, and when the keyboard is opened to type, the whole view shifts? How do I fix that as well? Thanks in advance
Cleaned up and corected the code a bit. Perhaps this is what you were looking for. Let me know if it works. Cheers !
<?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" >
<RelativeLayout
android:id="#+id/InnerRelativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<Button
android:id="#+id/Button"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:onClick="addItems"
android:text="Send" />
<EditText
android:id="#+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:layout_toLeftOf="#id/Button" />
</RelativeLayout>
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/InnerRelativeLayout" />
</RelativeLayout>
adding android:layout_above="#+id/InnerRelativeLayout" to your ListView.
RelativeLayout has child views specify their position relative to the parent view or to each other (specified by ID). So you can align two elements by right border, or make one below another, centered in the screen, centered left, and so on. By default, all child views are drawn at the top-left of the layout, so you must define the position of each view using the various layout properties available from RelativeLayout.LayoutParams.
For put your InnerRelativeLayout layout at Top add android:layout_alignParentTop="true" to your InnerRelativeLayout and remove android:layout_alignParentBottom="true"
Update: set up your Layout like below:
<?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="#color/Beige "
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/InnerRelativeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
<Button
android:id="#+id/Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:onClick="addItems"
android:text="Send" >
</Button>
<EditText
android:id="#+id/message"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/Button"
android:hint="Enter text" >
</EditText>
</RelativeLayout>
<ListView
android:id="#+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/InnerRelativeLayout"
android:drawSelectorOnTop="false" />
</RelativeLayout>
Output:
use android:layout_above="#+id/InnerRelativeLayout" in your listview
Make the outer RelativeLayout into a LinearLayout with orientation vertical. Then, for the ListView set layout_height as 0dp and layout_weight as 1. The ListView will fill up all the remaining space in the LinearLayout without overlapping other views.
Also, stop using fill_parent, it's been deprecated and you should use match_parent instead.

error in creating menu.xml of Game Menu

I am designing game menu,I am taking reference of http://p-xr.com/android-tutorial-game-menu-with-a-custom-font/comment-page-1/#comment-857
My main.xml file currently contains:
<?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">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/wallpaper1" />
<LinearLayout
android:orientation="vertical"
android:layout_margin="10dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:text="START GAME"
android:layout_gravity="right"
android:id="#+id/start"/>
<TextView
android:text="QUICK GAME"
android:id="#+id/quick"/>
<TextView
android:text="SETTINGS"
android:id="#+id/settings"/>
<TextView
android:text="ABOUT"
android:id="#+id/about"/>
<TextView
android:text="EXIT"
android:id="#+id/exit"/>
</LinearLayout>
</RelativeLayout>
However there seems to be an error error, which I do not understand, in graphical layout which says: "You must specify layout_height and layout_width".
AFAIK, attributes android:layout_width and android:layout_height are mandatory for each view declared in a XML layout file.
Just define them for each TextView element.

Android: Layout List header

I have a header for a list that contains two image buttons. I want one of those image buttons to be on the left, and the other to be on the right. This is what my xml file looks like right now but it does not work, it just puts the two image buttons on the left together. I have also tried without the additional LinearLayouts but no luck.
<?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="horizontal"
android:gravity="fill_horizontal"
android:background="#drawable/list_header_background">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:gravity="left"
android:background="#drawable/list_header_background">
<ImageButton
android:id="#+id/refreshBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:src="#drawable/ic_refresh"
android:background="#drawable/list_header_background"
/>
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:gravity="right"
android:background="#drawable/list_header_background">
<ImageButton
android:id="#+id/battleBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:src="#drawable/ic_battle"
android:background="#drawable/list_header_background"
/>
</LinearLayout>
</LinearLayout>
As kabuko wrote, the RelativeLayout width is equal to the parent width. When we have a child view inside that RelativeLayout, we can use the android:layout_alignParentXXXXXX="true" parameter in order to align it (the child) accordingly to the parent.
<?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="horizontal"
android:background="#drawable/list_header_background">
<ImageButton
android:id="#+id/refreshBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:src="#drawable/ic_refresh"
android:layout_alignParentLeft="true"
android:background="#drawable/list_header_background"
/>
<ImageButton
android:id="#+id/battleBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:src="#drawable/ic_battle"
android:layout_alignParentRight="true"
android:background="#drawable/list_header_background"
/>
</RelativeLayout>
You probably want to use a RelativeLayout with layout_alignParentLeft and layout_alignParentRight on the buttons.

Layout Drawer doesn't add new view in a new line

Hy!!
If i want to add a textview and a textedit, both are in the same line
I have a Linear Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:text="#+id/TextView01"
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<EditText
android:text="#+id/EditText01"
android:id="#+id/EditText01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
What i have made wrong?
You need to add android:orientation="vertical" to your LinearLayout. By default, it adds items horizontally.
EDIT: Also, your android:text attribute shouldn't contain an #+id/, it defines what is displayed in the TextView. Only your android:id attribute should have that.

Why can't get IgnoreGravity within RelativeLayout to work?

I have two buttons. I want one to be aligned at the top center of the application. I want to get the other button to ignore the gravity settings and go where I tell it. How do I do this?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relative_layout"
android:layout_width="fill_parent"
android:gravity="center_horizontal"
android:layout_height="fill_parent" >
<Button
android:id="#+id/the_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Centered Button"
android:layout_alignTop="#id/relative_layout" />
<Button
android:id="#+id/the_button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Random Button"
android:ignoreGravity="#id/relative_layout" />
</RelativeLayout>
Do not use gravity attribute of RelativeLayout, elements within RelativeLayout can be positioned with additional attributes. Try this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relative_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/the_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Centered Button"
android:layout_centerHorizontal="true" />
<Button
android:id="#+id/the_button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Random Button"/>
</RelativeLayout>
See RelativeLayout tutorial and RelativeLayout.LayoutParams docs for more information about usage of RelativeLayout.

Categories

Resources