<TextView
android:id="#+id/first_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<TextView
android:id="#+id/second_textview"
android:layoutbelow="#id/first_textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is my first Android Application!" />
<Button
android:id="#+id/first_button"
android:layoutbelow="#id/second_textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="And this is a clickable button!" />
I got error when tried to run.
There is a "red" cross at the lines of:
- second
Any idea?
At following two places:
android:layoutbelow="#id/first_textview"
android:layoutbelow="#id/second_textview"
Change layoutbelow to layout_below. Note the use of underscore: _. If you use Ctrl + Spacebar while writing these attributes, eclipse will show you the options available(In case you use eclipse). This way you decrease the chances to such typo mistakes. Hope this helps.
Replace your textview and button line-
android:layoutbelow="#id/first_textview"
with this one-
android:layout_below="#+id/first_textview"
Use "#+id" in giving reference, not the "#id" here and you are missing the "_" in the layoutbelow attribute.
Related
How to display an image next to last alphabet of a text ?
i got a problem when having a textview with more than 1 lines, the image wont follow the last alphabet of the text
The output that I should make should be like number 1 and number 2, but my output just be like number 1 and number 3 (image below)
*I tried to make my explanation clear so I make it with png to figure what I mean.
To make the correct output, what I should do on my XML ? or I should go with additional programming code in java. please help, thanks
I might try this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/textLine1"
android:layout_width="wrap_content"
android:layout_height="12sp"
android:text="Username of" />
<TextView
android:id="#+id/textLine2"
android:layout_below="#id/textLine1"
android:layout_width="wrap_content"
android:layout_height="12sp"
android:text="a man" />
<ImageView
android:id="#+id/image"
android:layout_width="12sp"
android:layout_height="12sp"
android:layout_below="#+id/textLine1"
android:layout_toEndOf="#+id/textLine2"
android:src="#drawable/image" />
</RelativeLayout>
So yeah this will help. And as you can I haven't use android:layout_marginTop attribute here but if you use it make sure it's the same for textLine2 and iamge so that it would look good. And if you want to add more such name with images just copy -paste the same code below it and simply change their android:id.
Also change the id here in this line: android:layout_below="#+id/textLine1"
So that new views will be added below the previous once.
If you need more clarification please drop a comment below.
Try it
<TextView
android:id="#+id/TextView27"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableRight="#drawable/star"
android:drawablePadding="5dp"
android:paddingLeft="10dp"
android:text="Username" />
Try this:
<TextView
android:id="#+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="200dp"
android:text="Testing my \n text for images"
/>
and in your java code:
SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append("Testing for text view \n and images")
.append(" ", new ImageSpan(getActivity(), R.drawable.com_facebook_button_icon), 0)
.append(" ");
myTextview.setText(builder);
I have the next problem: I use a lot the RelativeLayout, like the Android documentation is telling me to do. But, since few days, I don´t understand why, this is not working anymore:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#drawable/background"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/boton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Button" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/boton"
android:layout_centerHorizontal="true"
android:layout_marginTop="60dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000" />
</RelativeLayout>
This is my layout, very simple. So, according to this one, the textView should be BELOW the button, but, instead, it appears OVER the button!
.
Сan anyone explain me what happens?
EDIT: The problem is not when I try to run it on a device, the problem comes when I see it on the graphical layout of the ADT Plugin for Eclipse...
I try this code it's working properly.
If your side not working properly Try to Clean build your project and then Run.
If still having problem then Try this
android:layout_below="#+id/boton"
remove above line with this
android:layout_below="#id/boton"
Because "+" create new reference to R.java file
This may be the issue.
try by removing the below line from your code
android:layout_marginTop="60dp"
Take the '+' sign out of the TextView's layout_below attribute. The '+' tells it to deine a new id. Without the '+' it should refer to the existing one.
I think your issue is the drawable for the background, since I am not seeing an image in your preview.
Having this issue is giving you a build error so the preview doesn't show the layout in its current state (it reverts to the last sucessful build). Either remove that line and save to see the preview or check to make sure that you have an image in your drawables folder titled "background"
Try this, without the drawable to check if it is working for you:
<?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="match_parent" >
<Button
android:id="#+id/boton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Button" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/boton"
android:layout_centerHorizontal="true"
android:layout_marginTop="60dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000" />
</RelativeLayout>
I am trying to run it, and now, I know what´s the problem.
When running the app in a terminal, it is working properly, the problems comes when I try to see it on the graphical layout on the ADT plugin on Eclipse! Anyone with the same problem??
Add </RelativeLayout> at the end of your relative layout to close it.
The layouts in my application are built using SDK 16 and I've tested and confirmed they work fine on ICS and JB but GB has the following issue:
In the code below, I'm just trying to create a simple layout with only text. The text keeps getting cut off on Gingerbread at the end of the first line though, instead of going on to the next line. I've tried setting singeLine to false, played around with lines, maxLines, marquee, layoutWeight, changing layoutHeight and layoutWidth to every imaginable combination, etc. This issue truly baffles me.
Code:-
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text keeps overflowing"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#33B5E5"
android:paddingTop="6dp"
/>
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text gets cut off when I input a long string like this one"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF"
/>
</LinearLayout>
</ScrollView>
Also, if I change the theme of the layout to Gingerbread, I get the error:
Failed to find style 'scrollViewStyle' in current theme
Failed to find style 'textViewStyle' in current theme
android.content.res.Resources$NotFoundException
Thanks in advance.
Fixed it myself. Couldn't find out why it was happening but I fixed it by adding the following lines to the code for TextViews:-
android:ellipsize="none"
android:maxLines="10"
android:scrollHorizontally="false"
Okay, so this is starting to really annoy me. This error pops up in a very special, not very logical way.
Let me start out by saying that I have already looked at the other questions regarding this error, Google'd it too. As far as I can tell, most similar problems occur because people refer to a String resource or something else not within the same layout file, they misplace the '+' in '#id+' or something similar.
The problem I am having occurs in a layout .xml file with a RelativeLayout. This contains a TableLayout, two LinearLayouts containing some text and finally a ProgressBar. What I want is the progress bar to be aligned with the relative layout using android:layout_alignParentBottom="true" and then align the two linear layouts above the progress bar (the bottom linear layout aligning above the progress bar, the other aligning above the bottom linear layout).
It should be simple enough and looks as if it works, i.e. the graphic view shows the desired result. However, and here comes the problem, Eclipse gives me an error on the two linear layouts,
"Error: No resource found that matches the given name (at 'layout_above' with value '#id/LinearLayout_acc')."
and the same error for the other linear layout referring to the progress bar. I have more than triple checked that there are no typos (the id's also exist in packagename.R.java), and I have tried cleaning the project a dozen times.
I don't get the error when saving (and auto building), not until I decide to run the project. Another weird thing is that when I refer to the bottom linear layout from the progress bar instead of the top linear layout, I get no error!
My layout file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background_activity" >
<TableLayout
... />
<LinearLayout
android:id="#+id/LinearLayout_dist"
android:layout_above="#id/LinearLayout_acc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp" >
<TextView
... />
<TextView
... />
</LinearLayout>
<LinearLayout
android:id="#+id/LinearLayout_acc"
android:layout_above="#id/ProgressBar_statusScreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" >
<TextView
... />
<TextView
... />
</LinearLayout>
<ProgressBar
android:id="#+id/ProgressBar_statusScreen"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_margin="16dp" />
</RelativeLayout>
Please help, I have no idea what causes this error!
Edit with answers
Shrikant came with the solution of changing the order of appearance in the layout file so that elements reference only other elements already defined when the reference is read.
Also, as others have posted, changing #id/ to #+id/, even in a reference, does remove the error messages. As Marco W. wrote in this thread, the thing is you have to use #+id/ the first time each id is mentioned and then use #id/ afterwards, even though the first time may not be a definition.
I made most of the design and set the referred id's in Eclipse's graphical editor, so code that resulted in an error message was automatically inserted. Maybe this is a bug in Eclipse.
change
#id/LinearLayout_acc
to
#+id/LinearLayout_acc
Please check the below code
<?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"
android:background="#drawable/ic_launcher" >
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:id="#+id/LinearLayout_dist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/LinearLayout_acc"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FIRST" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SECOND" />
</LinearLayout>
<LinearLayout
android:id="#+id/LinearLayout_acc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/ProgressBar_statusScreen"
android:layout_centerHorizontal="true" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="THIRD" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FOURTH" />
</LinearLayout>
<ProgressBar
android:id="#+id/ProgressBar_statusScreen"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_margin="16dp" />
</RelativeLayout>
Also check the following link.
It says that android:layout_below="#id/myTextView" won't recognise an element with id "myTextView" if it is written after the element you're using it in.
Change every id #id to #+id, no matter when it's defining or referencing an id. With this, you won't get
Android xml error: “No resource found that matches the given name” with RelativeLayout (#id/LinearLayout_acc, #id/ProgressBar_statusScreen).
<LinearLayout
android:id="#+id/LinearLayout_dist"
android:layout_above="#+id/LinearLayout_acc" <--- here might be a problem you forgot + sign
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp" >
Below is an outline of my layout. I have my ScrollView inside another LinearLayout because I would be adding more views to it in runtime.
Anyway, the problem is - the text in the TextView is always truncated to a single line, and is never shown in multiple lines.
Is there any way to make it display a multilined TextViw without having to set an arbitrary number of lines using android:lines or android:maxLines?
Thanks.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical" android:background="#drawable/background"
android:id="#+id/XyzLayout"
android:paddingRight="10dp">
<ScrollView android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="#drawable/background"
android:fillViewport="true">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="vertical"
android:layout_marginBottom="0dip" android:layout_marginTop="1dip">
<TextView android:id="#+id/textView1"
android:text="#string/String1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginTop="20dip"
android:layout_marginRight="10dip" android:textSize="18dip"
android:typeface="sans" android:textStyle="bold"
android:layout_marginBottom="4dip"
android:inputType="text|textMultiLine"/>
<TextView android:id="#+id/textView2"
android:text="#string/String2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginTop="20dip"
android:layout_marginRight="10dip" android:textSize="16sp"
android:typeface="sans" android:textStyle="bold"
android:layout_marginBottom="4dip"
android:inputType="text|textMultiLine"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
i've got this problem with a i5800 with CM7(2.3.7) and the emulator and works fine on a SGS-i9000 with CM9 (4.0.3).
The problem was setting the theme at the manifest:
<application
android:icon="#drawable/icon"
android:label="#string/app_name"
android:theme="#android:style/Theme.DeviceDefault" >
I put " android:theme="#android:style/Theme.Holo " and if the device doesn't have it, the textview doesn't allow MultiLine.
You can use the library " holoeverywhere " to port the holo theme to all devices.
Regards.
Try deleting the setting android:inputType="text".
Just had the same problem. I had android:multiLines="2", then I tried android:lines="2", then tried adding android:singleLine="false", but still my EditText would only show one line. I could scroll horizontally to get to the end of my line. What ended up working was deleting the setting android:inputType="text" . This allowed me to type in a long line that would wrap around to a second line.
It could be false by default but did you try to add this in your TextView
android:singleLine="false"
-serkan
To add to #KNU's answer...
Here's a good related answer showing that you can use a different theme depending on the version of Android it is running on - just create a versioned styles folder.
Answer here: TextView won't break text
Set input type as inputType="text|textMultiLine". This made to resolve my issue.
<TextView
android:id="#+id/itemTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text|textMultiLine"
android:textSize="#dimen/text_size_12"
android:layout_gravity="center"
android:gravity="center"
android:text="Your Text Here"/>