I'm going crazy with this problem...
I would like to do something like this:
This is my SIMPLY code into the xml layout file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:p1="http://schemas.android.com/apk/res/android"
p1:minWidth="25px"
p1:minHeight="25px"
p1:layout_width="match_parent"
p1:layout_height="match_parent"
p1:id="#+id/relativeLayout1">
<WebView
p1:layout_width="match_parent"
p1:layout_height="wrap_content"
p1:id="#+id/webView1"
p1:layout_alignParentTop="true"
p1:layout_above="#id/textView1" />
<TextView
p1:text="Medium Text"
p1:textAppearance="?android:attr/textAppearanceMedium"
p1:layout_width="match_parent"
p1:layout_height="wrap_content"
p1:id="#+id/textView1"
p1:layout_alignParentBottom="true" />
</RelativeLayout>
but the error that I receive is this:
Error: No resource found that matches the given name (at 'layout_above' with value '#id/textView1').
That is so strange for me.... because I have specified textView1!!!!
Some idea?!?
The TextView with the id textView1 is parsed after the WebView that is referencing it. Using the #id/* syntax specifies that the parser is resolving an existing id. In this case it will fail as your TextView hasn't been parsed yet.
Use layout_above=#+id/textView1 instead to predeclare the id for your TextView.
Related
I have a listActivity that displays via an adapter an xml feed fetched from the web, adn the layout file activity_list_feed.xml :
`<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
android:transcriptMode="normal"
/>`
In the graphic editor i cannot drag a button into this layout, and when i try to hardcode as per this file :
`<?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" >
<Button
android:id="#+id/addBtn"
android:text="Add New Item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="addItems"/>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
android:transcriptMode="normal"
/>
</LinearLayout>`
i get a compile error message :
java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
How can i add a button on top of the list because i want to refresh the pull.thank you.
The code that you provided actually works in its current state in Android Studio:
Make sure that in your java code, you are referencing the correct item. Make sure that you're setting the overall layout to be that layout, and THEN doing findViewById(R.layout.addBtn).
Also, try changing the Android Version in your IDE to 22 (as I have it set in the top right corner of the picture). That may solve your error.
I managed to add the button in the editor and this without setting to API 22 as per your picture. However the program didn't compile still.
The message error "ArrayAdapter requires the resource ID to be a TextView" meant i didn't provide the right argument to the adapter.
According to this answer :
"ArrayAdapter requires the resource ID to be a TextView" xml problems
it appears that the choice of the constructor is important, since i wanted to add a button in the view, i must use the constructor with 4 arguments (the additional argument being the id of the view):
http://developer.android.com/reference/android/widget/ArrayAdapter.html#ArrayAdapter(android.content.Context,int,int,java.util.List)
Using this constructor solved the problem.
Using Android Studio in the XML-Layout-editor,
When an error saying something about the xmlns not being found, I checked quickly for a fix with the help of the editor, and I chose one that said something about ignore.
Suddenly, this error,
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
....
</RelativeLayout>
Where it says element RelativeLayout must be declared and this follows for all childrens aswell.
The <RelativeLayout is the part that has the error. Also the childern, such as <LinearLayout has this error.
Where can I remove the ignore?
You can check if the file is in "layout" folder.
I also met the error element RelativeLayout must be declared. I realized that my file was located in "drawable" folder, instead of the "layout" folder. That's why the error showed up.
Why does this XML give errors? (Relevant portion extracted from larger file.)
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<!-- android:layout_gravity="center_horizontal|vertical" -->
android:padding="5dip"
/>
This gives two errors:
Element type "TextView" must be followed by either attribute specifications, ">" or "/>".
error: Error parsing XML: not well-formed (invalid token)
You can't use comment inside xml element. i.e. in TextView element only attribules (like name="some value") are allowed. That should be a fresh question...
I've got some custom views and custom defined attributes. I'd like to define which attributes my views use so that eclipse can use that for content-assist.
For example, I've got a custom xml attribute declared like so in my res/values folder:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TagAttrs">
<attr name="spColor" format="color" />
</declare-styleable>
</resources>
And my layout file uses a custom view with the custom attribute:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.twp"
...
>
<com.twp.TestView
android:id="#+id/testView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:spColor="#FF00FF" >
...
</com.twp.TestView>
</LinearLayout>
If I use the android: prefix, eclipse presents all the options it would for a LinearLayout (from which TestView derives), but if I use the app: prefix, it shows in red at the bottom that "Element com.twp.TestView not available". Also, if I try content-assist inside of spColor, it says "Content Assist not available at the current location." I would think since I defined the "app" namespace, it would be able to find my stylable declaration and at least know that spColor is a color.
So it this just a limitation of eclipse, or can I explicitly define a view's custom attributes?
I believe the value for the name attribute in declare-styleable needs to match the unqualified name of the associated class or the unqualified name of a superclass of the associated class. So if the direct superclass of TestView is LinearLayout, then the value of the name attribute in the associated declare-styleable should be TestView, not TagAttrs.
I would like to be able to define a ShapeDrawable in my layout file and then inflate that so I can draw on it.
I have followed the tutorial on the android developers' site but I cannot see how I reference my ShapeDrawable in my main class file.
How do I actually inflate it?
I have put the following the following into my layout file:
<com.example.shapedrawable.CustomDrawableView
android:id="#+id/customDrawableView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
Once you have defined the ShapeDrawable in its own XML file, you need to add the following to the layout.xml where you want it to be included:
<ImageView
android:src="#drawable/shapeDrawable_filename"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
If you want to do it programmatically, I don't have an answer (yet), but from your question, it seems this should work!
Let me know if that helps ;)