main.xml: Failed to parse file - android

I'm developing a test application for Android, and I'm trying to add a custom image to a button. But when I try to save I get the error "main.xml: Failed to parse file C:\Program Files\Eclipse Workspace\App\res\drawable\custom_button.xml". I don't know how to solve it. I think both main.xml and custom_button.xml are correct.
custom_button.xml:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_pressed"
android:state_pressed="true" />
<item android:drawable="#drawable/button_normal"
android:state_focused="true" />
<item android:drawable="#drawable/button_normal" />
</selector>
main.xml:
<?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/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/custom_button"/>
</LinearLayout>
For making this, I followed these two tutorials:
http://developer.android.com/resources/tutorials/views/hello-formstuff.html
And:
http://developer.android.com/reference/android/widget/Button.html

This is not an answer but since I dont have comment perm I will write it here anyways.
Maybe "#drawable/button_pressed" or the other drawable may not be present.
Or like already said in the comment try recreating it. I have had this problem previously. And make sure that if you are copy pasting, not to include any invalid characters.

Related

"Error parsing XML: junk after document element"

I get this error message while trying to create custom button, i have already tried most of the answers similar to my question but cant get it working yet. Any help much appreciated. Also, as this is my first time posting here, forgive me if anything wrong with the rules.
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout
<Selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/touchlighter" android:state_pressed="true"/>
<item android:drawable="#drawable/touch"/>
</Selector>
</RelativeLayout>
You're typing <Selector ... where the attributes of the <RelativeLayout> should go.
I think the right XML should be:
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android">
<Selector>
<item android:drawable="#drawable/touchlighter" android:state_pressed="true"/>
<item android:drawable="#drawable/touch"/>
</Selector>
</RelativeLayout>

Android button effect does not work

I want to use 2 pictures for one button to make a effect when user click it.
I think I did properly but, the effect does not work.
Specifically, the button background set to camera_button_after.jpg which is meant for pressed state and does not change when it is clicked.
Could you tell me what's wrong? I'll be very appreciated.
Here is the main_frame.xml which contains buttons.
main_btn1.xml, main_btn2.xml, main_btn3.xml are selectors.
Those three selectors have same structure and also same problem..
<?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" >
<Button
android:id="#+id/button001"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/main_btn1"
android:text=" Mood"
android:textSize="30dp"/>
<Button
android:id="#+id/button002"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/main_btn2"
android:text=" Mood Lists"
android:textSize="30dp"/>
<Button
android:id="#+id/button003"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/main_btn3"
android:text=" Help"
android:textSize="30dp" />
</LinearLayout>
Here is the main_btn1.xml
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="false"
android:drawable="#drawable/camera_button_before" />
<item
android:state_pressed="true"
android:drawable="#drawable/camera_button_after"/>
</selector>
One more question is..I think the eclipse doesn't fully recognize the pictures and xml files. I cleaned the project few times and reboot the eclipse but it they still have question mark like below
I was facing the similar problem. I tried the below code. I did get this solution from stack overflow itself, but do not have its link for reference. So writing the code snippet for xml in drawable folder. Do remember to provide the right image names :)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="false"
android:drawable="#drawable/image1" />
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="#drawable/image2" />
<item
android:state_focused="true"
android:state_enabled="true"
android:drawable="#drawable/image3" />
<item
android:state_enabled="true"
android:drawable="#drawable/image4" />
</selector>
Let me know how it works for you.
Replace your xml with this.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#drawable/camera_button_before" />
<item
android:state_pressed="true"
android:drawable="#drawable/camera_button_after"/>

highlight selected item on listview

before someone marked this question as duplicate, I must say I already know that this question is already asked several times, but every time I tried I always failed.
so I have a listview of date on the left and I want to show data on the right, I want to make a listview that can show which item is selected so I know which date that I picked.
So far I've tried this;
on listview I've added android:choiceMode="singleChoice";
<ListView
android:id="#+id/listTglRMPasien"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_above="#+id/btnObatAlkes"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:choiceMode="singleChoice">
</ListView>
on item of listview I've added android:background="#drawable/list_selector";
<?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" >
<TextView
android:id="#+id/txtListRekamMedikTanggal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/emptyString"
android:textAppearance="?android:attr/textAppearanceLarge"
android:background="#drawable/list_selector"/>
<TextView
android:id="#+id/txtListRekamMedikTabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/emptyString"
android:textAppearance="?android:attr/textAppearanceLarge"
android:visibility="gone" />
</LinearLayout>
and on list_selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_launcher" android:state_activated="false"/>
<item android:drawable="#drawable/ic_launcher" android:state_pressed="false"/>
<item android:drawable="#drawable/blue" android:state_pressed="true"/>
<item android:drawable="#drawable/blue" android:state_activated="true"/>
</selector>
note that ic_launcher and blue here are png file, it's supposed to be "#drawable/listitem_pressed" and "#drawable/solid_white", I find this answer from here
but somehow it got an error, it says No resource found that matches the given name and I think something is missing so I changed it to png file
anyway, when I tried to run this, it always show ic_launcher whether I select it or not, it never show blue.
apparently it happens because my API level is too low, I changed it from API level 8 to API level 14.
this is a good answer about how to change min. API level for your Android app in Eclipse
Here is what I did on my ListView. Create your own theme on res/values folder, you can name it for example mytheme.xml. Here what I added on mytheme.xml
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="mytheme" parent="android:Theme.Holo.Light">
<item name="android:textColor">#fff</item>
</style>
</resources>
Then in your Manifest add this android:theme="#style/mytheme" inside the activity of your ListView Class.
Hope it helps.

Eclipse disorders an XML file after pressing Ctrl + Shift + F

Ctrl + Shift + F does a good job when putting java or layout files in order. But does it disorder things in a file which starts with <selector xmlns:android="http://schemas.android.com/apk/res/android"> (as an example)?
This is what I have.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"><nine-patch android:src="#drawable/bg_list_row_pic" />
</item>
<item><nine-patch android:src="#drawable/bg_list_row_pic" />
</item>
</selector>
This would be much better.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<nine-patch android:src="#drawable/bg_list_row_pic" />
</item>
<item>
<nine-patch android:src="#drawable/bg_list_row_pic" />
</item>
</selector>
I've skimmed over Window - Preferences - XML but haven't found anything to solve the matter.
ADDITION.
if I use Format XML files using the standard Android XML..., I have the following in a layout file, but a file containing a <selector> and <item>s looks properly (see "This would be much better" in this message).
It doesn't suit me.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_gravity="center"
android:paddingLeft="#dimen/sixteenDp" android:paddingRight="#dimen/sixteenDp">
<Button android:id="#+id/mainAct_btn_RunTest"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/sixteenDp" android:text="#string/run_test" />
<Button android:id="#+id/mainAct_btn_ShowList"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_below="#id/mainAct_btn_RunTest" android:text="#string/show_list" />
</RelativeLayout>
This layout file suits me.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingLeft="#dimen/sixteenDp"
android:paddingRight="#dimen/sixteenDp" >
<Button
android:id="#+id/mainAct_btn_RunTest"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/sixteenDp"
android:text="#string/run_test" />
<Button
android:id="#+id/mainAct_btn_ShowList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/mainAct_btn_RunTest"
android:text="#string/show_list" />
</RelativeLayout>
You can try this. Go to Windows->Preferences->Android->Editors and disable first line "Format XML files using the standard Android XML..."

Defining IDs within style, is it safe or a disaster?

Following question has kept me puzzled for a while and I thought maybe asking about this does no harm. I have the following layout.xml and style.xml files;
res/layout/layout.xml
<?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" >
<LinearLayout
style="#style/headerContainer" />
<LinearLayout
style="#style/footerContainer" />
<ScrollView
style="#style/contentContainer" />
</RelativeLayout>
res/values/style.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="container">
<item name="android:layout_width">fill_parent</item>
</style>
<style name="headerContainer" parent="container">
<item name="android:layout_height">40dp</item>
<item name="android:layout_alignParentTop">true</item>
<item name="android:background">#80FF0000</item>
<item name="android:id">#+id/header</item>
</style>
<style name="footerContainer" parent="container">
<item name="android:layout_height">50dp</item>
<item name="android:layout_alignParentBottom">true</item>
<item name="android:background">#8000FF00</item>
<item name="android:id">#+id/footer</item>
</style>
<style name="contentContainer" parent="container">
<item name="android:layout_height">60dp</item>
<item name="android:layout_below">#id/header</item>
<item name="android:layout_above">#id/footer</item>
<item name="android:background">#800000FF</item>
</style>
</resources>
Now, the question is, is there a danger of overlapping IDs as I'm introducing them in style.xml? Funny thing is that this approach works, on the emulator I'm using at least, but the created IDs are not being added to the R class. And I'm a bit confused how they are defined once my layout is inflated.
Don't use #+id/... in styles.
#+id/... can only be used in layouts.
Otherwise you can get Error executing apt: return code 139 during build.
Use #id/... and generate ids with help resource file if needed:
res/values/ids.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="id" name="header" />
<item type="id" name="footer" />
</resources>
http://developer.android.com/guide/topics/resources/more-resources.html#Id
Its not safe my friend. You should use different id for different files. Here the emulator will not act problem. It will understand because for every xml file there is unique code defined R.java file automatically. So emulator will understand from there very easily. But if you need to improve or edit the code for any up gradation surely you will get confused which id is belonging to which xml file's layout or wedged. So provide unique id to every widget of layout. It will be helpful if you provide the id including some tag of respective file name.
Example: If file name is filldetails.xml then you can use id=#+fd_name
It will be helpful to know the flow of application.
I am doing it this way and have had good luck:
Layout
res/layout/main.xml
<?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"
>
<include layout="#layout/action_bar"/>
</RelativeLayout>
Common:
res/layout/action_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/action_bar_container"
android:layout_width="fill_parent"
android:layout_height="#dimen/action_bar_height"
android:layout_alignParentTop="true"
android:paddingLeft="5dip"
android:paddingRight="5dip"
>
<TextView
android:id="#+id/action_bar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:textSize="20dip"
android:textStyle="bold"
/>
</RelativeLayout>

Categories

Resources