simplified.
simplified.

Reputation: 31

Android XML Layout Problems

I was trying to create a row layout where each of my list item will have a template to follow. Currently I have this, which allows me to display a single line text in each list.

<?xml version="1.0" encoding="utf-8"?>
<TextView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:textSize="20sp"
  android:padding="100dp" >  
</TextView>

However, when I try to change it to allow me to add more items such as image buttons and more text field, it always doesn't allow me to compile.

I tried taking a piece of xml from a tutorial on developer.android's website,

 <?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:orientation="vertical">

 <TextView android:id="@+id/text1"
     android:textSize="16sp"
     android:textStyle="bold"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"/>

 <TextView android:id="@+id/text2"
     android:textSize="16sp"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"/>
 </LinearLayout>

But it throws me an error stating - The processing instruction target matching "[xX][mM][lL]" is not allowed. - error: Error parsing XML: XML or text declaration not at start of entity

Can someone help me with this? I am not too sure what this error means.

UPDATE:

what i am trying to do is to have a fragment like a side bar on the right which shows a list of items, with the first xml that i posted, i'm able to get the results i want, but i couldn't make any changes to it.

package com.project.test;

import android.app.ListFragment;  
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class TestListFragment extends ListFragment {

    String [] Items = {"Item A", "Item B", "Item C"};

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(getActivity(),
                R.layout.listtemplate, Items));
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        Log.i("FragmentList", "Item clicked: " + id);
    }
}

Upvotes: 1

Views: 4449

Answers (2)

Ruwantha
Ruwantha

Reputation: 2653

You would not believe the solution to this... I had the same issue and the answer was "remove all the white spaces before start the xml first line"

Upvotes: 21

Mads Lee Jensen
Mads Lee Jensen

Reputation: 4658

Might be because you are missing the starting tag of the LinearLayout?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:orientation="vertical">

 <TextView android:id="@+id/text1"
     android:textSize="16sp"
     android:textStyle="bold"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"/>

 <TextView android:id="@+id/text2"
     android:textSize="16sp"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"/>
 </LinearLayout>

Upvotes: 2

Related Questions