What does it mean to set the android:text attribute of a view in a layout xml file to something like #+id/xyz. An example can be found at https://github.com/freezy/android-xbmcremote/blob/master/res/layout/actor_item.xml
Relevant code copied here:
<TextView
android:text="#+id/actor_name"
android:id="#+id/actor_name"
android:textColor="#ffffff"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="#+id/actor_role"
android:id="#+id/actor_role"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
That doesn't make much sense. According to documentation what you assign to android:text must be a string value:
Must be a string value, using \\; to escape characters such as \\n or \\uxxxx for a unicode character.
This may also be a reference to a resource (in the form "#[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.
Values of #id are integers.
"#+id/id" means you are giving a unique name or id for textview which will help to identify that textview and android:text is like body of the textview u can give anything which u want to display.
"android:text" requires to set String to it. It's the text that will appear in the TextView.
You can either use a plain string or use #string to extract the string from res/values/strings.xml
<TextView
android:text="#string/actor_name"
android:id="#+id/actor_name"
android:textColor="#ffffff"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
"#+id/id" is a kind of action that tells the android framework to create an id. The plus symbol, +, indicates that this is a new resource ID and will be created if doesn't exist.
please visit http://developer.android.com/guide/topics/ui/declaring-layout.html#id
The plus (+) sign just indicates it that the ID should be created if it is not under existence at the moment.
It is a general practice to use #+id/something when defining a new View in a layout, and then later use #id/something to reference the View from another part of the layout (say, in any RelativeLayout ) or R.id.something to reference it from our java code.
Related
Does someone knows what the #attribute value means in this xml file?
<EditText
android:id="#+id/firmaText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:ems="8"
android:textColor="#color/black"
android:textSize="14sp"
android:paddingLeft="5sp"
android:inputType="textPassword"
android:lines="1"
android:maxLength="#{longitud}"
android:text="#{pin}"/>
I know the meaning and use of the maxLength attribute, the problem is the value it have.
I also think is a kind of preprocessing, but, it doesnt compile...
Solved, it was a syntax error of the library.
Thanks to all.
android:maxLength
Set an input filter to constrain the text length to the specified number.
Must be an integer value, such as "100".
For Example : android:maxLength="6" limits the length of the EditText to 6 characters.
This may also be a reference to a resource (in the form "#[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.
I have some EditText views in a RelativeLayout. The first one receives focus correctly, but when the user clicks "Finished" on the keyboard, it usually doesn't send focus to the view the user expects. Assuming that the "Finished" button uses the FOCUS_FORWARD ID, I have tried to fix this behaviour by using the android:nextFocusForward attribute like so:
<EditText
android:id="#+id/editTextName"
...
android:nextFocusForward="#id/editTextNameColour" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editTextNameColour"
...
android:nextFocusForward="#id/editTextBackgroundColour" />
<EditText
android:id="#+id/editTextBackgroundColour"
... />
It doesn't compile because Eclipse gives me an error message like: "error: Error: No resource found that matches the given name (at 'nextFocusForward' with value '#id/editTextNameColour')".
I know the name is correct because I selected it from a dropdown list in Eclipse and all files have been saved.
Android Developers Reference says that EditText is the right type (a View).
If there's a better way than using android:nextFocusXxx attributes, including doing it in the Java code, that's fine too.
I'm not open to solving this by using a LinearLayout.
Also, am I right that the "Finished" button uses the FOCUS_FORWARD?
Thanks
Solution: I need to use #+id... rather than #id because I am referencing objects declared later in the code; and the keyboard appears to use FOCUS_DOWN rather than FOCUS_FORWARD.
It's because of #id vs #+id. Just use #+id. You're trying to use an ID before its been assigned a resource. You can also switch the order around that you're declaring stuff, if its in relativeLayout.
If that is the order, then you are referencing elements that hasn't been defined yet. You should do something like:
<EditText
android:id="#+id/editTextName"
...
android:nextFocusForward="#+id/editTextNameColour" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editTextNameColour"
...
android:nextFocusForward="#+id/editTextBackgroundColour" />
<EditText
android:id="#+id/editTextBackgroundColour"
... />
These two views are inside of RelativeLayout. The IDE throws an error there is no #id/et_pass, but if I set #+id/et_pass it is OK. Why is that?
<ImageView
android:id="#+id/devider_zero"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/et_pass" <!-- Error is here -->
android:src="#drawable/registration_line" />
<EditText
android:id="#+id/et_pass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/devider_first"
android:background="#android:color/transparent"
android:layout_gravity="left"
android:ellipsize="start"
android:ems="8"
android:hint="#string/password"
android:inputType="textPassword"
android:layout_marginTop="#dimen/register_layout_edittext_margin_top"
android:maxLines="1" />
The difference between #+id/something and #id/something is that the first one is creating an id, and the second one is referencing an id that has already been created. The first time that you mention an id you have to create it using #+id/, and anything after that can use #id/.
When you give a view the attribute android:id you do not have to use #+id/ if you have already used that somewhere earlier in your file.
Because of the way android compiles XML files, it reads your image view first, reaches the point where you write #id/ searches for an id in the R file, and can't find it. But if you call #+id/ the eclipse searches for the id in the R file, can't find it, and adds it.
Also, this is not specific to RelativeLayouts, if you put the same code in a linear layout, you would also get that error
#+id instructs the parser to create the ID if it does not exist. #id is used to refer to an existing ID.
hi have problem when i view graphical.layout of this file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="this is main text" />
</LinearLayout>
when i view graphical layout of this view this xml file then this warning show
[I18N] Hardcoded string "this is main text", should use #string resource
but i dont want to declare string valiable i want to show this "this is main text" without declaring string
The warning about 'should use #string resource' is just a reminder that locale-specific information should be placed in 'resource bundles' (eg /res/values/strings.xml) so that labels and other text can be provided in different languages without having to re-code and recompile the entire application for each language.
If you're just testing things out, you can safely ignore that warning, but it's a good idea to get in the habit of referencing strings as resources in external files rather than hardcoding them directly in the application itself.
It is not good practice to hard code strings into your layout files. You should add them to a string resource file and then reference them from your layout.
refer this answer and this one also.
You must declare your text using `#string` resource that
is a good programming format.You should prefer using
`string.xml` in value folder in res folder if you want
to use texts.
If you want to suppress the specific warning use
tools:ignore="HardcodedText"
more details
http://tools.android.com/recent/ignoringlintwarnings
This page in the Android documentation defines an element id as follows:
<TextView android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Type here:" />
However this page defines it as:
<EditText id="text"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:textColor="#color/opaque_red"
android:text="Hello, World!" />
I thought I had a decent understanding of what was going on until I saw this second example. In the first case, you need the + character so that id 'label' is added to the R file, correct? In the second case, would the EditText's id not be added to the R file because it does not contain the + character?
Also, the second example does not include the android namespace on the id. Does having or not having the Android namespace affect whether that id will be added to the R file?
Thanks for any clarification.
This format without the android: namespace
id="text"
is from an earlier version of the Android SDK.
You are correct in your initial assessment. It's worth noting that the second id tag
<EditText id="text"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:textColor="#color/opaque_red"
android:text="Hello, World!" />
Is missing the android: namespace so it actually isn't an android xml tag. The first one is an example of how to add that view's id to the R file so you can access it in your code. To be honest, I'm not sure what the purpose of the id in the second example is*, but I know that android wouldn't know what to do with it. The first one is the correct syntax.
*This is just speculation, but I'm willing to bet it was a typo somebody didn't notice or didn't care to fix because they were trying to illustrate something else.
The plus-symbol (+) means that this is a new resource name that must be created and added to our resources (in the R.java file). There are a number of other ID resources that are offered by the Android framework. When referencing an Android resource ID, you do not need the plus-symbol, but must add the android package namespace, like so:
android:id="#android:id/empty"
Taken from Declaring Layout | Android Developers in the ID section.
However, in your second example there is no #android:id/ provided before the id text so to be brutally honest, I have never seen that notation before and wonder if that could be a typo on the author's part.
The second example is wrong. The attribute is always android:id and the value should be either #+id/myId (to create a new id called "myId") or #id/myId (to use an already defined id called "myId".) Using #android:id/theId lets you use ids defined by the android platform.