Android layout view id name issue - android

I recently faced an issue when i named a view as android:id="#+id/TextView1" and the code
TextView tv1 = (TextView) findViewById(R.id.TextView1);
was returning wrong view even though there were no warnings given for the naming convention.
When i changed the name to text_view_1 it worked.
I know the usual naming convention is all small case like android:id="#+id/text_view_1" or camel case like android:id="#+id/textView1"
Is there any documented resource which explains the issue or any answer for it?
Also i want to know what m stands for in the variable name which i often see in open source code. e.g.
TextView mTextView;
Its a noob question but i couldn't find a answer anywhere.

Try to clean and build you project.
Android coding syle guide states:
Non-public, non-static field names start with m.
Static field names start with s.
Other fields start with a lower case letter.
Public static final fields (constants) are ALL_CAPS_WITH_UNDERSCORES.
It would be private ImageButton mButtonStart; or private static sSingletonThing;
That convention is by the way only required to be used if you want to contribute to the Android sourcecode, you can write your own apps in any style.

Thats a known issue in Eclipse. It was returning the wrong view because there must have been some referencing issues. Generally cleaning the project or restarting eclipse helps.
And regarding the prefix m, 'm' is a naming convention used to denote private member variables and methods.

Related

why Google calls variables with the prefix "m"?

why Google calls variables with the prefix "m" for example:
private int mSectionResourceId;
private int mTextResourceId;
I see it in all examples. But i not understand why they do it?
And now i have some example where it practic very good. If a called variabels without prefix i need write
public SimpleSectionedRecyclerViewAdapter(Context context, int sectionResourceId, int textResourceId,
RecyclerView.Adapter baseAdapter) {
this.sectionResourceId = sectionResourceId;
this.textResourceId = textResourceId;
but if i use prefix i can write
public SimpleSectionedRecyclerViewAdapter(Context context, int sectionResourceId, int textResourceId,
RecyclerView.Adapter baseAdapter) {
mSectionResourceId = sectionResourceId;
mTextResourceId = textResourceId;
I think it more readable. Who can explain to me the pros and cons of a prefix?
The variables starting with m are telling you they are variables in the scope of your class. Member of the class.
Link to Android Code Style Guide
The m just stands for 'Member'. It is simply declared that your Variable is a Class-Member.
It is more readable Code, because you know where Class Members got declared, so you can find it pretty fast. You don't need to write this, even if you don't prefix your Variables with an m.
In your Example, this only makes it more readable when there is no prefix-m. Another developer knows that it is a instance variable (member variable) and so declared on top or bottom of the class.
It is a prefix for class member variables. It's just a naming convention.
Mostly sure, taken from Hungarian Notation where similar prefix: m_ stands for exactly the same).
Referring to pros & cons:
Pros:
it allows to type fewer chars during programming,
programmers that are used to use Hungarian Notation may found it easier to follow the code.
Cons:
as the code changes very often, it is easy to forget about changing prefixes every time, when variable changes it's purpose (especially during prototyping),
it makes the code starts to smell bad,
Generally, it is some kind of reinventing the wheel. Java has this keyword that should be more than enough for accessing proper variable. If it's not, the code requires refactoring, maybe because of naming glitches or using too wide variable scopes.
Personally, I do not recommend to use Hungarian Notation (even the part of Android Code Style). We have great IDEs that increases the readability of the code.
There is an exception. The code, where Hungarian Notation (or more general, specific code style) was already been used. It is a matter of consistency.
The m is just a member variable. A class member if you will. Useable with constructors like WebView M WebView then later on you would use something like mWebView.loadurl("example.com"); it's just a placeholder for the variable you created. You don't have to add the member class variable as an m but it's more organized if you do

How to store resource file with same name but different extensions in android

I am working on one static custom Android Media Player Application where i have many media files stored in raw folder.
Where as some files have same name but different extensions, here android is giving me
res\raw\then.mp4:0: error: Resource entry then is already defined.
res\raw\then.mp3:0: Originally defined here..
Can anybody please suggest me something on this?
Also there are few media files with java keywords like if, else, return, switch, case,class, else, final, long, new, this,true... where android is giving me error of invalid symbol.
Please suggest me solution for that also.
Thanks in advance...
As #blackbelt said, it's not possible, I'll just add that you can instead put your files in the assets directory instead of res.
You will not be able to use them like R.id.file, but you will get more flexibility.
simply you can't. Raw is build at compile-time inside R.java, and the name key must follow the java convention for naming. Since
if, else, return, switch, case,class, else, final, long, new,
this,true
are reserved keywords you can not use them.
Edit: R.java would look lie:
public static final class raw {
public static final int if=0x70000;

Android coding good habit examples?

Ive been searching the internet for a while now but cant find a good page for good habit examples in android programming. For examples im interested in things like how to name classes or xml files (what case letters, where to use _) and also in file things like naming variables and fields or edittexts, prefixes and everything like that.
If someone could help me with a link i would be very grateful!1
Android developers has its answer for this.
If you need anything elaborated I'll update my answer from your comments :-)
For Classes use Java naming convention:
First letter capitalized, no "_" in the class names but start with capitalized letter for each new word ei:
MyActivity or MySettingsActivity
For xml object naming you have some other limitations as in, first letter cannot be capitalized, therefor I suggest your either use the general java naming convention for methods (First letter decapitalized, and then new words capitalized like: buttonQuit or quitButton), if it should be button first or last is up to you, but stick with 1 style. don't name 1 item: nameTextView (or nameTxtVw) and then something else buttonSubmit (Have the indicators in the same orders).
For xml files, use lower case separated with underscore "_".
Why a lot java files are called Activity in the end. It is to describe in the name that they inherit from from the superclass "Activity". It is a principle that came became big with Android, which uses it a lot, examples if these classes are inherited:
(super class = end of their heirs name)
AsyncTask = Task
Service = Service
Activity = Activity
Handler = Handler
I could go on :)
For xml files, belonging to activities I personally like to call them the name before Activity, so MainActivity's xml layout would be main.xml
If you are looking for some coding guidelines, have a look at Code Style Guidelines for Contributors
If you want to enforce this rules without remembering all of them, and you are using eclipse then you can use formatting rules provided by Android team: android-formatting.xml. Just import it into eclipse using Preferences->Java->Code Style->Formatter, click Import.
You should learn Java code conventions, and so read this:
java style
It's good to practice always name classes upper camel style (e.g CustomerService)
Class names should be noun,
For naming variables you should use lower Camel Style (e.g myVariable)
Xml file shoudl be lower case and words should be separated by under score
please read the above links!
[Edited] this part added after the first comment!
I don't know any good reference for good naming convention and things like this but those are things that you can find out be practicing! for example:
"activity" prefix may be not useful in case if all your layouts are for activities. but if there is bunch of other style (e.g dialog layouts) it could help to put "activity" prefix.
another example: I prefer to put "Activity" prefix for all classes that extends Activity, because eclipse shows classes base on name, and this could help to somehow a better management (but it's against Java naming convention! because you should suffix your class name with super class name)
For naming Id: You could prefix the resource with the name of what it is (e.g titleStatus), it doesn't really matters! but you for better managing your Ids and not losing in lots of name, always follow the same rule, if your name some element in your status like "statusTitle" the other element should be "statusDescription" but not "descriptionStatus"!

Eval function in Dalvik

If I know a variable's pattern such as R.id.edit_x where x (1..N), how can I get a reference to a given EditText, like findViewByID(R.id.edit_1). Is there something like an "eval" function in Dalvik ? Thanks.
Try Java reflection. Discussion on retrieving static final fields via reflection is here - Accessing Java static final ivar value through reflection
hoha's answer is good. Another thing you can do is create a look-up table that maps 1..N to the resource IDs. (Presumably you know all the resource IDs ahead of time.)
maybe, you can check roboguice. it is a ioc framework for android and it's realy easy to use. i copy some code from the sample from the project to show how to use it:
public class AstroboyMasterConsole extends RoboActivity {
#InjectView(R.id.self_destruct) Button selfDestructButton;
#InjectView(R.id.say_text) EditText sayText;
#InjectView(R.id.brush_teeth) Button brushTeethButton;
#InjectView(tag="fightevil") Button fightEvilButton; // we can also use tags if we want
}
then you can you these injected variables in your code!

Whence construct R.id?

In some code we're trying out, coming from various tutorials, we end up missing R.id, which should be generated in R.java, obviously. We can add it in (by analogy with other "first" Android projects we've done), but as this file is auto-generated, anything we do like that just gets overwritten ultimately.
public static final class id
{
public static final int Button01=0x7f060004;
.
.
.
}
Was there a construct to put into strings.xml, main.xml, etc. that causes this to be generated?
(Yeah, we're total noobs. Sorry.)
Thanks for any help you can give,
Russ Bateman
Say I have an XML file with the following content:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
>
<ListView android:id="#+id/ListView01"
/>
</RelativeLayout>
I save it in res/layout/ .
Then R.id.ListView01 gets automatically created.
You might want to look at the Notebook Sample application and how it's organised. At least that's how I got familiar with androids organisation.
R.java is auto generated by Eclipse. If it is not built, this means you have probably an error somewhere in your xml files, or a resource with a name that is not allowed.
Sometimes it is just Eclipse doing strange things, in this case, you can try :
Project > Clean > all project
Then let Eclipse work.
Sometimes it solves the issue. If not, it's highly probable that you have an error somewhere in your resources.
To create this file, Eclipse gathers the ids you declared in your xml files with #+id,but also the layout names, images names, string names, ...
It appears that project/res/layout/main.xml contains the constructs that lead to the generation of id in R.java. (I'm not limiting the source for these only to that XML file.)
Look specifically for android:id in various widgets (I think they're called) such as TableLayout, TextView, EditText, etc. There will be corresponding
public static final class id
{
public static final int x=0x7f05006;
public static final int y=0x7f05000;
public static final int z=0x7f0500c;
.
.
.
}
where x, y, z correspond to the (TableLayout, TextView, EditText, etc.) identifier coded immediately after the "#+id/" construct in the XML file (and the initialized value in R.java is magically generated--you don't have to worry about that).
Thanks to all who contributed.

Categories

Resources