What's wrong with this comment in Android Layout XML? - android-layout

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...

Related

i cant add button to linear layout

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.

Problems with relative layout: No resource found that matches the given name

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.

Strange issue with android:ellipsize="end"

I am using android:ellipsize="end" in android xml file, & surprisingly I am not getting the layout that I want, the 3 dots(...) are showing but after that dots there is another word truncated. Also this is a "not-always" behavior, check the ListView attached, sometimes, the behavior is normal & sometimes not.
Here's the screenshot of the layout from my device,
I don't have any idea why this is happening. Here's my xml file, having problem with the tv_news_content TextView -
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#color/white" >
<ImageView
android:id="#+id/iv_next_tier"
android:layout_width="18dp"
android:layout_height="21dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:src="#drawable/right_arrow" >
</ImageView>
<TextView
android:id="#+id/tv_news_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="7dp"
android:layout_marginRight="5dp"
android:layout_marginTop="3dp"
android:layout_toLeftOf="#+id/iv_next_tier"
android:ellipsize="end"
android:maxLines="2"
android:text="News Title"
android:textColor="#color/black"
android:textSize="17dp"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_news_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tv_news_title"
android:layout_below="#+id/tv_news_title"
android:layout_marginRight="5dp"
android:layout_toLeftOf="#+id/iv_next_tier"
android:ellipsize="end"
android:maxLines="2"
android:text="News Contents"
android:textColor="#color/black_light"
android:textSize="15dp" />
<View
android:id="#+id/view"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_below="#+id/tv_news_content"
android:layout_marginTop="5dp" />
To make things clear, tv_news_title is the topmost bold TextView, & iv_next_tier is the small arrow-type ImageView at the right. & tv_news_content is the TextView that I am facing problem with.
Any solution why I am not getting desired output? Desired output means the always normal behaviour - I want those 3 dots at the end of second line of tv_news_content TextView, not before a truncated word.
Any suggestion is appreciated.
Finally found what was causing this issue in my app!!
While RuAware's answer led me on the right path, I found that it didn't fix the problem for all of my text blocks. Given that it worked for some, I felt it was clearly due to some malicious character. So I went through one broken text block and just started removing characters until it stopped breaking.
It turns out it was the new line character '\n' that was causing it. Even if the new line was after the ellipsize, the new line character was [inconsistently] causing this problem as long as it was somewhere within the string.
The issue is solved by doing something similar to the following before setting the text to your view:
text = text.replace('\n',' ');
When setting the text for the textview make sure the text is not greater than 750 characters so use txt = theText.substring(0, 750) or something like that before calling settext. This works on the emulator with your feed. and should be enough characters for a 10inch tablet too
Take a look at this post. There is a custom EllipsizingTextView which solves ellipsizing problem for multiline view. However it may solve you problem too.
I was experiencing the same problem when displaying a String containing html formatted text.
Removing all html tags with myText.replaceAll("\\<.*?>","") solved the problem.
You can also remove html tags using Html.fromHtml(myText).toString(), but notice that you must use .toString() and not apply the fromHtml output directly to the TextView. If some special characters are html encoded (&amp, &gt, &lt...) you can safely apply again Html.fromHtml() to the final String.
i used :
if (myModel.mDataText().length() > 150) {
mTextView.setText(Html.fromHtml(myModel.mDataText().substring(0, 150) + "..."));
} else {
mTextView.setText(Html.fromHtml(myModel.mDataText() + "..."));
}

android, xmlns, No resource indentifier error

In an xml layout file, the xmlns is already defined, but I get this error at compile time: No resource identifier found for attribute. Why is that? thanks
I'm following a Facebook dev tutorial on facebook site https://developers.facebook.com/docs/getting-started/facebook-sdk-for-android/3.0/ the layout file is as follows:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<com.facebook.widget.ProfilePictureView
android:id="#+id/profilepic"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
app:preset_size="large" app:is_cropped="true"
/>
</ScrollView>
I figured it out, have to replace /res-auto with res/com.facebook.samples.profilepicture
Your layout file looks correct. check that in your 'project.properties' file, the 'android.library.reference.1' value points to the correct location.
The res-auto is feature properly supported in API level 17. It must be applied for all layouts using custom attributes. It is automatically replaced on build time, by eclipse.
Ref Updated SDK Tools and ADT revision 17

How to use a ShapeDrawable with an xml layout resource

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 ;)

Resources