I'm coding new XML fils for desinging an android app and I've some problem by using 2 linearLayour into the same xml...
I've "Error in a XML file: aborting build" with the following code :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#+id/texte_firsttab"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="#+id/accessGraphe"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="test"
android:onClick="selfDestruct" />
<Button android:id="#+id/accessGraphe2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="test2"
android:onClick="selfDestruct" />
</LinearLayout>
Can we put two linear layout in the same xml file?
Yes you can. But not two at top level.
See this example : http://developer.android.com/resources/tutorials/views/hello-linearlayout.html
You cannot have two top-level layouts. How would the system know how to arrange them? You need to enclose them in another layout that defines this.
I assume that the blanks in front of the <?xml ... tag are due to code formatting in your post and are not present in the actual layout file. That would also cause a problem.
No you cannot.... how would you refer to one or the other from the source code ?
If you want to have two linear layouts at the same time (one on top and one at the bottom), then you need to embed those within another layout :
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weigth="1">
<TextView android:id="#+id/texte_firsttab"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weigth="1" >
<Button android:id="#+id/accessGraphe"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="test"
android:onClick="selfDestruct" />
<Button android:id="#+id/accessGraphe2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="test2"
android:onClick="selfDestruct" />
</LinearLayout>
</LinearLayout>
You have two root-level items, which shouldn't happen (this is not specific to Android layout files, you can have only one document element in any XML file).
I wouldn't recommend wrapping the to LinearLayouts in another one, that's too complicated; and it's generally a good idea to avoid nesting layouts, see this article about efficient layouts.
For a TextView and two Buttons, a RelativeLayout would be perfect. It's also more flexible than LLs.
Related
I want to show a button bar at the end of my activity. I created a layout named: batton_bar_ref.xml:
<?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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#drawable/shape_blue_light_horizental_gradiant_notrounded"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="10dip"
android:paddingTop="10dip" >
<Button
android:id="#+id/pay_button"
android:layout_width="100dip"
android:layout_height="50dip"
android:text="#string/pay"
android:textSize="16sp"
android:textStyle="bold" >
</Button>
</LinearLayout>
</RelativeLayout>
And add it with <include layout="#layout/button_bar_ref"/> to another layout.
It is showing a warning message :
This LinearLayout layout or its RelativeLayout parent is useless; transfer the background attribute to the other view.
Could you let me know how can I fix it?
Addenda :
button bar should be at the end of screen.
Note that Linear layout has a background.
It is just suggestion to optimize your layout by removing tags that are not required. In your case since you do not have any children under RelativeLayout other than LinearLayout, this particular layout is not really correctly designed.
First you can ignore this warning, and once you have successfully designed the layout file in which you are planning to include this layout file, you will realize that one of the layouts declared in this file can be eliminated.
Remove the relative layout.
<?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="wrap_content"
android:layout_alignParentBottom="true"
android:background="#drawable/shape_blue_light_horizental_gradiant_notrounded"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="10dip"
android:paddingTop="10dip" >
<Button
android:id="#+id/pay_button"
android:layout_width="100dip"
android:layout_height="50dip"
android:text="#string/pay"
android:textSize="16sp"
android:textStyle="bold" >
</Button>
</LinearLayout>
try removing the RelativeLayout tag from the xml file
This warning is triggered, when a Layout has only one child that is also a Layout. In this case one of both can be removed without any problems. It is recommended to remove these redundant layouts as they reduce the overall performance of the UI.
In your case there is no use of RelativeLayout. There will be no change if you remove this. So this warning is showing. But this is just a warning. There is no harm in using this
Please use bellow code fix your issue.
<?xml version="1.0" encoding="utf-8"?>
<Button
android:id="#+id/pay_button"
android:layout_width="100dip"
android:layout_height="50dip"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="#string/pay"
android:textSize="16sp"
android:textStyle="bold" >
</Button>
I want to create an application that have 5 buttons in bottom of page, when user clicks
on the any button, the page will changes but 5buttons remain in bottom of page. now i create
this program but when i call setContentView() in other class becuase i call other layout,
buttons removed. have ways for remain 5buttons? or i should create 5buttons again in 5layout?
Thanks
Cheers
Easiest and Simple way is to make a layout having 5 buttons and it in all your layout files
Example
<include layout="#layout/titlebar"/>
Or another way is to use fragments so tht only fragments with change keeping other of your layout stuff same
Edit
OR Use the <merge> Tag
The <merge /> tag helps eliminate redundant view groups in your view hierarchy when including one layout within another. For example, if your main layout is a vertical LinearLayout in which two consecutive views can be re-used in multiple layouts, then the re-usable layout in which you place the two views requires its own root view. However, using another LinearLayout as the root for the re-usable layout would result in a vertical LinearLayout inside a vertical LinearLayout. The nested LinearLayout serves no real purpose other than to slow down your UI performance.
To avoid including such a redundant view group, you can instead use the element as the root view for the re-usable layout. For example:
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/add"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/delete"/>
</merge>
Now, when you include this layout in another layout (using the tag), the system ignores the element and places the two buttons directly in the layout, in place of the tag.
Please go through this link to gain more information about Reusing layouts
From what I can gather, I think you are looking for tabs instead of Buttons. Take a look at this.
Thanks for answer. I read developer link and i add merge and include tag to xml file but the program has errors. Why?
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<include layout="#layout/location"/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<WebView
android:id="#+id/web_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.0" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/background"
android:gravity="bottom"
android:orientation="horizontal" >
<Button
android:id="#+id/button_home"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/home_icon"
android:text="#string/button_home"
android:textColor="#color/text_home" />
<Button
android:id="#+id/button_product"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/product_icon"
android:onClick="Product"
android:text="#string/button_product"
android:textColor="#color/text_product" />
<Button
android:id="#+id/button_places"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/places_icon"
android:onClick="Places"
android:text="#string/button_places"
android:textColor="#color/text_places" />
<Button
android:id="#+id/button_rewards"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/rewards_icon"
android:onClick="Rewards"
android:text="#string/button_rewards"
android:textColor="#color/text_rewards" />
<Button
android:id="#+id/button_more"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/more_icon"
android:onClick="More"
android:text="#string/button_more"
android:textColor="#color/text_more" />
</LinearLayout>
</LinearLayout>
location.xml
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView>
<com.google.android.maps.MapView
android:id="#+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:apiKey="0cPRv243zM1_S3ydsNg8MJP9_6BfCp642jOhPvQ"/>
<LinearLayout
android:id="#+id/zoom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
</LinearLayout>
</merge>
what is incorrect?
Thank you
I have this 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:id="#+id/solution_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Label1"
/>
<TextView
android:id="#+id/solution_description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Label2"
/>
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+id/label"
android:textSize="20px" >
</ListView>
</LinearLayout>
I am combining two TextView elements with a ListView. For some reason, the TextView items render next to each other instead of one below the other. Why does that happen? And how do I make them line up one below the other?
Thanks!
in the LinearLayout you need to specify the orientation as follow:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
That's because the LinearLayout has a horizontal orientation by default.
Specify android:orientation="vertical" for it.
See LinearLayout tutorial
But you want: android:orientation="vertical"
Have you tried to set the attribute:
android:orientation="vertical"
Hello See Vertical orientation
You may consider to have two linear layouts and have the vertical orientation within another linear layout which is horizontal if that helps.
I'm having issues with a ListView using custom rows that are loaded from a database.
If, for the list screen, I place a button above the ListView, no visible rows appear in the listview.
However as soon as I remove the button, everything works fine. I want the button (or any other component) to appear above to make it more user friendly. Attached are the two code samples below.
This is the XML file of the ListView Activity that works:
<?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="#color/real_red_dark">
<LinearLayout
android:id="#+id/llMain"
android:layout_height="fill_parent"
android:background="#drawable/real_background"
android:layout_width="fill_parent"
android:layout_gravity="center"
android:gravity="center">
<ListView
android:id="#+android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/llButton"
android:layout_alignParentLeft="true" />
<TextView
android:id="#+android:id/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/no_sessions"
android:textStyle="bold"
android:textSize="18dp"
android:gravity="center"/>
</LinearLayout>
</LinearLayout>
However, if I have the Button added above it, it will not show whatsoever:
<?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="#color/real_red_dark">
<LinearLayout
android:id="#+id/llMain"
android:layout_height="fill_parent"
android:background="#drawable/real_background"
android:layout_width="fill_parent"
android:layout_gravity="center"
android:gravity="center">
<Button
android:id="#+id/btnSearch"
android:layout_width="fill_parent"
android:layout_height="35dip"
android:layout_gravity="center_horizontal"
android:layout_marginTop="3dip"
android:layout_marginLeft="15dip"
android:layout_marginRight="15dip"
android:text="Find Sessions"
android:textStyle="bold" />
<ListView
android:id="#+android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/llButton"
android:layout_alignParentLeft="true" />
<TextView
android:id="#+android:id/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/no_sessions"
android:textStyle="bold"
android:textSize="18dp"
android:gravity="center"/>
</LinearLayout>
</LinearLayout>
You could try adding the button programmatically as a header view in the listview itself, rather than in the xml layout.
use listView.addHeaderView(View)
http://developer.android.com/reference/android/widget/ListView.html#addHeaderView(android.view.View)
It is better if you use RelativeLayout than LinearLayout,it is also recommended by the android docs.Try to use android:layout_height value as "wrap_content" for ListView and TextView(which you may use to indicate for empty rows),it may help to you.
Something which doesn't look right but I'm assuming it's just a typo in the above...
android:id="#+android:id/list"
...there shouldn't be a + between # and android:. Using #+ is for adding a new resource id of your own, i.e., #+id:. You're also doing the same thing for the TextView...
android:id="#+android:id/empty"
Another thing but not sure it's relevant is you're specifying...
android:layout_below="#+id/llButton"
...I doubt it's the problem as android:layout_below isn't valid for a LinearLayout (it's for RelativeLayout) but there isn't a Button with the id of llButton in your layout. If there was, the + would also be incorrect as you should be specifying an existing id.
Not sure if amending the above would fix things but it could just be that the layout inflation is coming out 'wrong' due to those issues.
I have made myself a custom LinearLayout by the name of com.theflyingnerd.DroidMe.DiscreteNumericalRangeSelectorWidget that hosts a couple of spinner widgets. This custom LinearLayout inflates the following XML layout (You might not need to look at this too carefully but it's here for completeness):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<!-- Min value Spinner -->
<Spinner
android:id="#+id/discrete_numerical_range_selector_min_value_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:layout_weight="1" />
<TextView
android:id="#+id/to_text"
android:text="to"
android:textSize="14sp"
android:paddingLeft="10sp"
android:paddingRight="10sp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_weight="0">
</TextView>
<!-- Max value Spinner -->
<Spinner
android:id="#+id/discrete_numerical_range_selector_max_value_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_weight="1" />
I have placed one such object in the layout for one of my activities like so:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<include layout="#layout/search_form_section_generic_top"/>
<include layout="#layout/search_form_section_car_specific"/>
<com.theflyingnerd.DroidMe.DiscreteNumericalRangeSelectorWidget/>
<include layout="#layout/search_form_section_advanced_options" />
</LinearLayout>
The problem is that my app force closes immediately upon startup. I've checked by putting breakpoints in my custom LinearLayout that none of my custom code is even being run yet. Furthermore, if I copy-paste the layout code for my compound widget in place everything works, which indicates to me that I probably haven't left any important XML attributes out. What could be going wrong?
I fixed it by making the LinearLayout XML element in the widget layout into a merge instead, and moved all of the layout parameters out of the widget XML file and into the activity XML itself, thus replacing
<com.theflyingnerd.DroidMe.DiscreteNumericalRangeSelectorWidget/>
with
<com.theflyingnerd.DroidMe.DiscreteNumericalRangeSelectorWidget
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
If someone could tell me why this worked, it might help me and others doing it again, and you can take credit.
because you must specify the width and height of every view you use in you xml?