Can I use a colon in the id attribute in android? - android

I need help with something. I found a code sample that does not fully understand. Can I use a colon in the id attribute?
<TextView
android:id="#+id/Customer:Name" />
Thanks in advance.

This is not very well documented on the official site, but there are certain types of punctuation that are allowed as part of the ID in a layout XML file, including semi-colons and periods. However, when you actually reference these ids in Java code, they are converted to underscores. Thus something like this:
<TextView
android:id="#+id/customer.name"/>
Is referenced in java code like this:
getView().findViewById(R.id.customer_name);
It's legal (not an error) to do so, but it isn't a good practice, because it makes searching for the ID more difficult.

Related

android layout id naming: use of R.group instead of R.id

I've seen that all the layout ids are identified by #+id/viewid, but it seems the #+my_group_name/viewid works as well. Is it normal to use this naming convention, or the id must be in the "id" class ? I've checked the forum and found prefix based naming conventions but nothing like this.
In examples: I have a dialog layout called dlgeffect. In this layout:
<CheckedTextView
android:id="#+dlgeffect/text"
android:layout_width="0dip" ... >
I now the id's are reused, but during layout modifications if the ids are unique for the whole project the compiler gives error this way (and not runtime error)
Thanks,
Gzp
EDIT: and of course from java it is referenced as R.dlgeffect.text
I really dont know if you can do this, but always stick to standards, if you want it for reuse porpuse or setting property for more than element at once you can use same id for different element

usage of resources in layout

I don't want to specify hard coded test size values in my layout.xml, hence i am using the following specification :
android:layout_height="#integer/intervalViewHt"
and
#integer/intervalViewHt value is as follows:
<integer name="medium">15</integer>
Now, the while inflating android is creating a problem saying that it cannot inflate the view. I want to actually specify value in dp so the actual value should be like
android:layout_height="15dp"
Can anyone help me here ?
What you're looking for are "dimensions" rather than plain integers.
Declaration:
<dimen name="intervalViewHt">15dp</dimen>
Usage:
android:layout_height="#dimen/intervalViewHt"
Have a look at the given link for more examples in the Android docs.
I use something like this for text size across different devices. It's java based, not xml.
tvOutput.setTextSize(TypedValue.COMPLEX_UNIT_DP, getResource().getInteger(R.integer.medium));
I haven't tested the code so you might have to play with it. I'm sure you could do something like.
button1.setHeight(TypedValue.COMPLEX_UNIT_DP, getResource().getInteger(R.integer.medium));
Though again I haven't tried it and you may need something different then TypedValue
What if you give it a String rather than an int. i.e in strings resource you have a string "15dp" that you reference

Sending a generated value to XML for formatting

I would like to begin by saying I don't have the best java knowledge because I'm still learning it in school, but I decided to go off on my own and attempt to create an android application.
Right now, I have my program generating two different random numbers. I'm trying to get these generated ints to be shown in the application. I want it to be done through XML formatting tho because of the control I have over what I want it to look like.
My issue is that I can't seem to figure out how to pass the values over to the XML file. I'm not sure if I should be using "android:text" and somehow integrating the name of the ints in that(Which i've attempted.) or what.
I've tried to google for an explanation of what can be done, but I think my lack of ability to really put my issue into words is holding me back.
You won't be using the XML file to set the Text. You use the xml file to set the look and feel, like this (note the android:id= line):
<TextView android:id="#+id/randomInt1"
android:layout_height="wrap_content" android:layout_width="wrap_content"
<TextView android:id="#+id/randomInt1" />
android:layout_height="wrap_content" android:layout_width="wrap_content"/>
then in your java code in onCreate for the activity that will be showing the ints you will do something like this:
int random_int1=0;
int random_int2=100;
((TextView)this.findViewById(R.id.randomInt1)).setText(random_int1);
((TextView)this.findViewById(R.id.randomInt2)).setText(random_int2);
The way it works is you need to make a "link" between the java and the xml.
This can be done in java with the following line(quick example I pulled out of my code):
TextView nameText = (TextView) findViewById(R.id.nameTextView);
then this can be set using:
nameView.setText(PutStringHere);
This is a fundamental part of android development. It's the link between the java and the xml. You can edit other things though the java TextView.

Android with eclipse

I've started learning coding for the android, I know the basics of programming in general and thought that android would be fun, Which it has so far.
Now in my exercises in the book I have, It says to add more text to the application. The application is nothing at the moment but 1 string, And I have to add another string.
Now when I have added the string to the strings.xml file and then on the main.xml I type:
android:text="#string/AppName" />
AppName is the new string I made which in the strings.xml it looks like this:
This App is called Droid1
The weird thing is when I type in the main xml to referr to the string, It doesnt even get colour coded when I type the android:text part. The whole line stays as the black text colour. Im sure im not missing anything as the string is all colour coded and so is the last string that I referred to while following the examples in the book which is:
android:text="#string/hello" />
And this is what is confusing me. So please point out the obvious or not so obvious thing that I have done wrong. Any help at all will be appreciated
Android uses the XML format to define interfaces and such. The line
android:text="#string/AppName" />
is not valid XML, and the black syntax is Eclipse's way of showing this. This is probably because you have forgotten to put some lines above it. What you want to have is something like this:
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/AppName" />
This tells Android that you would like to put a TextView (or, a text label or whatever) with your string in it. Also note that the android:layout_width and android:layout_height attributes here are always required: without them, you will get an error and you can't build your application.
If you are not already familiar XML, I would highly recommend taking a look at an XML tutorial (for instance at Tizag or W3Schools) and learn the basics of XML, since understanding the XML language simplifies Android programming a lot.
If you want your text to have color, you can set android:textColor="######" in your xml either textview or whatever you used to display the string. ###### is your color code.

When should you use `#+id` instead of `#id`?

I have a bunch of Views in a <merge>, and I included that <merge> into a RelativeLayout. I try to refer to the IDs of those included Views to act as anchors for my other Views, but Eclipse complains that the IDs are not resolving. I found a workaround by using #+id rather than #id when I first refer to them rather than when I actually define the objects they refer to. I've already defined the two IDs in a Style and in the included <merge> where they are declared, so it feels a bit inefficient if I keep repeating the definition of the ID.
Is this the correct way of doing it? I'm assuming it's bad cause the '+' is another initialization. My current hypothesis is that you should use #+id when you first use the ID rather than when you initialize the object that the ID is going to represent, a bit like C/C++ and how they require at least a function prototype in the lines prior to the actual code that uses the function.
Another question I have is when you use the GUI-based UI builder of Eclipse, I noticed that they always use #+id rather than #id. Is this acceptable, cause it seems inefficient to me; it's as if the application will be spending more time determining whether or not the ID has been declared in R.id.
Using #+id format tells the Android asset compiler to assign an ID to your element, it isn't actually an id itself. So if I use #+id/myNewId the asset compiler will create a new id named myNewId and provide a number for it. The actual number can be accessed from your code as R.id.myNewId.
If you use an #id, the compiler will look for R.id.id. You can define your own id's in XML files, as explained here: http://developer.android.com/guide/topics/resources/more-resources.html#Id. You could create your own file in res/values/[your_filename].xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item
type="id"
name="id_name" />
</resources>
and then refer to #id_name, for e.g.
You can also use the Id's defined in the Android namespace: #android:id/empty
This is well explained in the Android documentation: http://developer.android.com/guide/topics/ui/declaring-layout.html#id
There's also some further discussion here: android:id what is the plus sign for

Categories

Resources