The xml file in an android project project is typically formatted like this:
<?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" >
<Button
android:id="#+id/btn_dialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Click to display a dialog"
android:onClick="onClick" />
</LinearLayout>
Is it possible to comment out just one or two parameters within a tag? For example, can I comment out just the line with android:layout_width or two lines with android:layout_width and android:layout_height in the above code?
Sorry, XML does not permit you to embed comments inside tags.
http://www.w3.org/TR/2008/REC-xml-20081126/#sec-comments
Edit :
Sorry, Comments cannot occur within tags, e.g. <tag ></tag>.
Related
I want to dynamically add a custom styles to textviews in code. I can use the following workaround to do this when there is only textview in question:
android set style in code
However, if I try this method and create a template.xml file with 2 textview, each with a different style applied, this no longer works.
i.e. my template file will look something like this instead:
<?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"
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is a template"
style="#style/my_style" />
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is another template"
style="#style/my_style2" />
</LinearLayout>
How do I extend this solution so it works with more than one textview and more than one style?
So I have two files:
where_include_happens.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/hours"
android:layout_height="wrap_content"
android:layout_width="60dp">
<include android:id="#+id/some_id"
layout="#layout/whats_included"
android:layout_width="18dp"
android:layout_height="18dp" />
</RelativeLayout>
And
whats_included.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_margin="6dp"
android:contentDescription="#string/some_description"
android:src="#drawable/some_awesome_drawable" />
My problem is that each one of those elements should have android:layout_margin="6dp" parameter, but it seems to be ignored by include :(. Only thing in documentation, that I find related is:
Caution: If you want to override layout attributes using the tag, you must override both android:layout_height and android:layout_width in order for other layout attributes to take effect.
My question is:
Is there something I'm not seeing, or am I condemned to write android:layout_margin="6dp" in every single include in my project?
Since your include.xml consists of a single ImageView only you can, instead of using include directive, define a style for your ImageView and apply that style as needed.
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.
In my code i'm using and XML file for showing a view in Android. The file is as follows :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText
android:text="Search Box"
android:id="#+id/EditText01"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1"/>
</LinearLayout>
<ListView
android:id="#+id/ListView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
I'm a part of a multi member team, and the situation is that the code for the EditText and ListView will be written by 2 different developers in 2 different XML files. Then we will use import statements to import the code from these 2 XML files into the main XML file.
The problem is that we would like to give custom heights, widths and weights to the EditText and ListView in the main XML file only and not in their individual files. (Basically we want to set the layout of different elements in the main file, so that we can create any element like an EditText in a seperate XML file, import it into the main XML file, and then postion it at will on the screen)
Is this possible? If yes, then how can it be done?
Regards
I'd say you would want to define a style?
From that page:
For example, by using a style, you can take this layout XML:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#00FF00"
android:typeface="monospace"
android:text="#string/hello" />
And turn it into this:
<TextView
style="#style/CodeFont"
android:text="#string/hello" />
You have one place where the style is (maybe main.xml isn't the best place for that, but I think your goal would be met even with a separate style file?)
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?