Index

Android apps - General notes about coding


Comments

The code files that you need to know about are:

LanguagePath of fileWhat it is
Javaapp/java/com.(the project name)/MainActivitycode that does stuff
XMLapp/res/layout/activity_main.xmllayout of your screen
XMLapp/res/values/strings.xmldefinition of strings

There are also places for pictures. See Android Studio - what goes where for how to set up a new project, and then find all these things.

The guts of the app - the logic - is in MainActivity, and this is written in Java. Everything else that you are interested in (apart from pictures) is in XML, which is a posh form of HTML (which makes webpages).

First - please note that XML comments and Java comments are different. Comments are a Good Thing. They stop you getting bewildered by your own code. You can remove code you don't want without deleting it and finding you did want it after all. You can debug by removing suspicious code until you find the bit that's causing the problem. Here is how you code comments in Java and in XML.

<!-- This is an XML comment on one line -->
<!--
This is an XML comment
on several lines
-->
// This is a Java comment on one line
/*
This is a Java comment
on several lines
*/

A note - if you specify /* (to start a Java comment), AndroidStudo 'helpfully' puts in a */ for you, probably in the wrong place. Comments are shown in grey. Check you've got the comments where you want them!


Stuff about XML

Follow the lower/upper case conventions used by the code that's already there. (In other, words, do what programmers always do - copy stuff and change only what you need to!)

Layout of stuff is fairly flexible. I prefer one attributes to a line, and put the start and end of the tag on different lines in the screen format, just so I can change things around and not find I've moved a /> to the wrong place!


Stuff about Java

If you are a fluent Java programmer, then ignore this bit. This is for people who don't know what they're doing. (Who, me?)

A lot of Java coding is like JavaScript or indeed any other programming language, give or take far too many brackets. But...

Java is VERY fussy about lower/upper case. And there are strict conventions even for data names. So be careful.

You need to define the type of EVERYTHING. Not just data fields (which are probably called something else) but also methods. And the type may not be an obvious thing like 'int' (integer) or 'String' but may be something completely incomprehensible. You even need to define the type of parameters.

Every language that I know has sub-routines or functions. Java calls them methods. Here is how you would use and define a method:

String outp = format(3.14159) private String format(double fld) { String s = String.format("%3.2f", fld); // formats the input real field into a string with 2 dec places return s; }

private: this method will only be seen by this class (as opposed to 'public' which means other stuff (such as the screen format) can see it. Make everything public if you want.
String: this method returns a value when called. This says that the returned thing will be a string.
format: the name of the method. I'm going to format something, you see...
(double fld): there is one parameter of this method, it must be of type double and within the method, we are going to call it fld.

If you have more than one parameter, there are commas between them, and of course, each one has its type defintion before it. If you have no parameters, you still need the brackets. And while we're on the subject, specifying a method in the screen format XML (such as in onClick) does NOT give the brackets!

You can define a method which doesn't return a value like this by specifying void rather than the return type, e.g.:

private void format(double fld)

Layout of MainActivity

The Java in app/java/com.(the project name)/MainActivity first has some imports. You can ignore these, except for one thing. When coding, sometimes you get an error message which says "Cannot resolve symbol EditText" (or whatever). Click on the EditText (or whatever), and it tells you to do Alt Enter. Do so, and the error goes away. If you look at the imports (you might need to expand the list), you will see that a new one has appeared. Imports are chunks of stuff that the app needs. But you don't need to worry about them if there aren't any errors!

Then there is the definition of the class MainActivity:

public class MainActivity extends AppCompatActivity {

Don't touch this. You don't need to understand it. All you need to know is that the very last curly bracket } right at the end of this listing is the end of the class definition. So don't touch that, either! Knowing this might also help you to understand the layout of the code.

After the class MainActivity definition, it says "@Override". You can delete this, replacing it with any defintions of global variables you want - things that you want to stay throughout the whole life of the app, rather than things that you only use within a method.

There is then a method supplied for you:

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }

This gets actioned at the start of the app. You may need to add code here.

You will certainly want to add more methods afterwards. The obvious ones are those that have been mentioned in the screen formating in app/res/layout/activity_main.xml, by onClick, for example. Click on a button or a picture and the relevant method gets actioned. The definition of these method must be public. You may want your own methods as well - chunks of code that you seem to be repeating a lot, so you make them into methods which you call as needed. Those can be private.

Finally, right at the end, there should be the last curly bracket } already mentioned, to close everything. Do not get this in the wrong place, or delete it.


© Jo Edkins 2016 - Index to Android app index