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?)
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?
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>.
I had a list with one column which worked with an adapter, and it worked. Now I am trying to make it into two columns, and I am getting exceptions.
I am following this tutorial which seems to be the simplest: http://www.heikkitoivonen.net/blog/2009/02/15/multicolumn-listview-in-android/
I have a Java Activity that I declare like this:
public class SeeAllQuestionsActivity extends ListActivity
and here is a bit of code:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
FlurryAgent.onStartSession(this, "8CA5LTZ5M73EG8R35SXG");
setContentView(R.layout.all_question_page);
...
Here is the all_question_page xml
<?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:padding="3px"
>
<include android:id="#+id/header"
layout="#layout/header"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
<TextView
android:id="#+id/loading_questions"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Some prompt text."
android:textSize="10sp"
/>
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#+id/label"
android:textSize="20px" >
</ListView>
</LinearLayout>
By the way, if someone can explain to me the diffrence between two syntaxes for referencing the list id, it would be great:
android:id="#android:id/list"
android:id="#+id/list"
FYI, neither of these is working for me :)
And here is the questions_list.xml layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="4dip"
android:paddingBottom="6dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="#+id/TRAIN_CELL"
android:layout_width="50dip"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/FROM_CELL"
android:layout_width="70dip"
android:layout_height="wrap_content" android:layout_weight="1"/>
<TextView android:id="#+id/TO_CELL"
android:layout_width="60dip"
android:layout_height="wrap_content" android:layout_weight="1"/>
</LinearLayout>
When I run this on the simulator, it crashes with a runtime exception complaining about this line: setContentView(R.layout.all_question_page); and the error is:
java.lang.RuntimeException:
Your content must have a ListView whose id attribute is 'android.R.id.list'
Please help, I am pretty stuck
Thank you!!
The difference seems to be that the former is what you'd need to use in order to use a ListActivity and the latter is what you'd use if you weren't going to use ListActivity, but instead just a normal Activity. So you'll have to stick with. (Note: I've never personally used ListActivity, so I can't exactly vouch for this)
android:id="#android:id/list"
I would also suggest removing these too lines from your ListView:
android:text="#+id/label"
android:textSize="20px"
Neither text, nor textSize are valid attributes of ListView. And for any widget that text is a valid attribute of I don't think you'd ever want to set the text equal to something like "#+id/label". If you are trying to reference a string from your strings.xml file you'd need to use "#string/label". Referencing an id like that for the text will put some hex code into the text that will have no meaning to the user(if it works at all).
It is possible that removing those might fix your trouble, if so my guess is that having the text set to another id was confusing something into thinking that your list didn't have the id list
If that does not solve your problem I would suggest switching to a plain Activity instead of ListActivity and getting the reference to your ListView via findViewById() like they do in the example that you linked.
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.
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?