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

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.

Related

Android ExpandableListAdapter View layout not displaying as desired :-(

I have an XML layout for a view representing one item as displayed by a ExpandableListAdapter. (The XML that is duplicated once for each list item, not the main activity layout). I initially tried a LinearLayout but this hid the final of the three widgets (a image button). I understand why that doesn't work but I then tried a RelativeLayout but the text view does not show.
I'll display the relative layout first along with a screenshot and also append my initial layout and screenshot at the bottom FWIW. Thanks for any help.
<?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="55dip"
android:orientation="horizontal" >
<CheckBox
android:id="#+id/chkParent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:checked="false"/>
<EditText
android:id="#+id/edtParent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/chkParent"
android:textSize="17dip"
android:inputType="text"
android:hint="#string/strItem"/>
<ImageButton
android:id="#+id/btnExpand"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_toLeftOf="#id/edtParent"
android:src="#drawable/dark_expand"
android:clickable="true"
android:hint="#string/strViewSubItems"
android:contentDescription="#string/strViewSubItems"
android:background="#null"/>
</RelativeLayout>
Original linear layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="55dip"
android:orientation="horizontal" >
<CheckBox
android:id="#+id/chkParent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"/>
<EditText
android:id="#+id/edtParent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="17dip"
android:inputType="text"
android:hint="#string/strItem"/>
<ImageButton
android:id="#+id/btnExpand"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="#drawable/dark_expand"
android:clickable="true"
android:hint="#string/strViewSubItems"
android:contentDescription="#string/strViewSubItems"
android:background="#null"/>
</LinearLayout>
Pretty sure your toLeftOf attributes should be toRightOf attributes. Give that a shot and see what happens.
Actually, your EditText should be set toRightOf="#id/chkParent" and toLeftOf="#id/btnExpand. Your ImageButton doesn't even need the toRightOf modifier, the alignParentRight attribute should cover it.

Center list item vertically on ListView

I'm developing an Android application.
On one activity I have a List with list items. Those list items will have a TextView (I'm using my own layout because probably I will need to add more widgets to it).
This is ListActivity xml:
<?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" >
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="469dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="40dp">
</ListView>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/orderErrorMsg"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16dp" >
</TextView>
</RelativeLayout>
</LinearLayout>
And this is ListItem xml:
<?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="100dp"
android:orientation="vertical">
<TextView
android:id="#+id/orderToFillName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="20dp" />
</LinearLayout>
And, finally, this is the result:
Why TextView is not centered vertically?
You have "wrap_content" on the layout_height of the TextView = there is no space to center vertically on since the TextView is wrapped tightly.
Try changing the layout_height to "match_parent" or place gravity="center_vertical" attribute in the LinearLayout instead.
<TextView
android:id="#+id/orderToFillName"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:textSize="20dp" />
the answer is really simple: you have to add this to your TextView's XML:
android:gravity="center"
Currently you're not setting the text's position in the TextView but just the View's position.
Best wishes,
Tim
Try this way:
<TextView
android:id="#+id/orderToFillName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity= "center_vertical|center_horizontal"
android:textSize="20dp" />
or
android:gravity="center"

Why my text is always located on the top of screen not center?

I have a simple layout like below (which only shows a text "TEST"):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="TEST"
android:textColor="#0099cc"
/>
</LinearLayout>
</LinearLayout>
I suppose with this layout, my text "TEST" should be located in the middle of the screen. But the text "TEST" is located on the top of the screen. Why?
I used layout_gravity and gravity with value center, why my "TEST" text located on the top?
use android:gravity="center" in your main layout
just try it.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:gravity="center"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="TEST"
android:textColor="#0099cc"
/>
</LinearLayout>
</LinearLayout>
Hi leem android:gravity consult with the content of specific view that means your android:gravity="middle" place the text middle of text view not a middle of screen. your layout_gravity define the where should your view is going to be place? mean you have to set android:layout_gravity="center_vertical|center_horizontal" of textview to view your text middle of screen
Very simple.. In the first linear layout section of your xml file. Just add android:gravity="center" and it will work..

problem in displaying text view above a map

I have to display three text views above a map in one of my activities and I have the following xml...but I don't know why I see no text view only my map.
Here is my xml file:
<?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:id="#+id/first"
android:background="#0000ff">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:id="#+id/bar"
android:layout_below="#id/first"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/prod1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:textSize="12px"
android:text="Sursa"
/>
<TextView android:id="#+id/prod2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingLeft="83dip"
android:textSize="12px"
android:text="destinatie"
/>
<TextView android:id="#+id/prod3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:paddingLeft="85dip"
android:textSize="12px"
android:text="data"
/>
</LinearLayout>
<com.google.android.maps.MapView
android:id="#+id/mapview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="0egkyrbiooKAfYyj43YB6wW1cmlG-TiIILXjpBg"
/>
</RelativeLayout>
That's because you set the height of your MapView to 'fill_parent'. Set to some fixed height just for debugging purposes and you should see your text views - they may not appear above the map, as you want, because you've set the LinearLayout to go below the RelativeLayout and it contains the MapView.
Remove the code from the LinearLayout:
android:layout_below="#id/first"
and add this to the MapView element:
android:layout_below="#id/bar"
You can also use MERGE tags as the root for your layout, which allows for multiple layouts to be layered over each other.

setting layout attribute for include

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

Categories

Resources