Monday, 20 August 2012

What Does Android Application Development Involve?

With the advance of mobile technology, many people have become interested in Android development. If you're thinking of developing Android apps but are unsure where to start, that's understandable. The platform actually involves a range of technologies, so developing applications for it does require a varied skill set. However, if you have development experience of some kind, for example Web development, you can definitely pick these skills up. In this article we'll go over the technologies involved in Android development, explaining what tasks each language is used to accomplish as well as the overall development models and contexts.

Java

Java is the main programming language involved in developing Android apps. With Java, you define the logic of your applications, including handling user interaction. If you have no Java experience, but have other programming experience in a language such as PHP or even JavaScript, you should be able to learn. However, the first step in approaching Android development without Java experience should be to learn the basics of the language. Java is a strongly typed language, so programming in it may seem fundamentally different if you have only used weakly typed languages in the past.


Object Oriented Development

When you program in Java you use Object Oriented development. If you have done some Web development in the past you may be familiar with this. If not, you will need to learn the basic principles involved in it. Essentially, Object Oriented applications divide tasks between a group of objects. When you program your apps, you create a series of class declarations for object types. Each declaration defines the properties and behaviour that objects of the class will provide. Once you have a class declaration in your Android project, you can create objects of the class to carry out your application processes.

Inheritance

One of the main principles of Object Oriented development that you will need to learn in order to build Android applications is inheritance. With inheritance, you can re-use the functionality of an existing class by extending it, or inheriting from it. This includes your own classes as well as classes within the Java and Android platforms. All Android apps involve extending at least one of the Android classes. Don't worry if this seems intimidating, as it actually saves you a lot of work. By inheriting the Android and Java classes, your own application code does not have to handle the details of, for example, presenting a user interface screen to your users.

XML

The other main language involved in Android development is XML. XML models various aspects of Android applications, including data and the different elements within a user interface. One of the first things you will do with XML in Android is design the layout of your app interface. Many of the development tools for Android, particularly the Eclipse IDE (Integrated Development Environment), provide automated tools for generating and editing the XML for your projects through graphical interfaces. While some of these are useful, you will need some level of XML knowledge to create apps successfully.

Data

Android applications can use data loaded from within the app directory structure itself as well as data loaded from elsewhere on the user's device, such as the SD card, or over the Internet. The Resources directory within an Android app contains XML files with data items in it. You can additionally use data management technologies such as SQL, especially SQLite, to create databases for your apps. The Java language also provides resources for fetching data over the Web, including XML and relational database systems.

Developing

To develop Android apps, you need to download and install various resources. The official Android developer guide recommends Eclipse for developing and exporting your apps. You will also need to install the Android SDK (Software Development Kit) and ADT (Android Developer Tools). Using these resources, you can design and build Android apps on your computer as well as running them on virtual devices, or Android emulators. The Eclipse tools also provide extensive debugging utilities.

Releasing

To release an Android app through the official Android Market, now the Google Play Store, which is the main app store for distributing applications on the platform, you need to sign up for a developer account. You can export your apps through Eclipse, signing them with a secure key. In order to upload your apps for distribution through the Market/ Google Play, you must also version them correctly using your application Manifest file. The publishing process also allows you to choose whether your apps will be free to download, or available for a set fee if your project is commercial.

Links

Android Developer Guide
Android Things Blog
Developing Android Widgets | The Tech Interpreter
What Are Android APK Files? | The Tech Interpreter
Android Widget Development | The Tech Interpreter

Programming: Handling Code Errors

Encountering code errors is essentially a part of the daily reality of programming. The more programming you do, the more you begin to realise that the activity of coding is in large part about finding and fixing the mistakes you've made. You can forget about not making mistakes, unless you're a robot. Accepting the fact of code errors is actually a positive step in your development as a coder, as programming is primarily a problem solving task, which is ultimately why it's a rewarding one - honest!

Even if you could write an entire application without a single syntax error, you're always going to come up against errors or unforeseen problems with your code logic and implementation so it's vital to be able to handle them.

Approaches

Whether you're struggling with syntax errors (referred to as compile time errors if they prevent program compilation from occurring successfully), runtime errors, or logic errors preventing your programs doing what you need them to, there are a number of key helpful techniques.

These methods can help resolve code errors more quickly, and in some cases prevent them from arising in the first place. Effective error handling involves getting into good habits with your routine coding practises, as well as what you do when problems crop up, i.e. your debugging skills.

Planning

It may not be the sexiest part of programming, but taking the time to plan your development activities properly will inevitably reduce the amount of debugging you end up having to do. It's always tempting when you've just started working on an application or project to get stuck in start coding straight away, but it is genuinely the case that spending even a small amount of preparation time makes the development process less error prone and stressful.

Use a Code Highlighting Editor

For many developers this one goes without saying, but if you're perhaps just getting started as a coder, you might be amazed by how much easier it is to work with code that is highlighted. Depending on the language or technology you're working with, you may want to simply opt for a decent text editor that can highlight source code, such as Notepad++ on Windows or Gedit on Linux.

Use an IDE (Integrated Development Environment)

Depending on which language or platform you're working in, there may be one or more IDEs you can use for it. Integrated Development Environments come with a number of different tools, for example to help with debugging, visualising your applications and with the development process generally. At the very least, an IDE will highlight your code reliably, and in most cases will flag up any syntax errors so that you will typically catch them before you even attempt to compile and run your program.

Most IDEs also have some sort of error or output console which will indicate information about any runtime errors that occur. You can also use these output consoles for trace statements, as explained in the below section.

Use Comments and Documentation

It's something many programmers don't have the best habits for, but effective use of comments is one of the key defences against errors. Ideally, if you outline what the different elements in a program are going to do with comments before you even start writing code, as well as continuing as you write the code, this will provide a clear picture of what's going on throughout the development cycle.

Comments aren't just to help other people read and understand your code, as the more code you write the more you'll understand the regular nightmare of looking back at your own code and being totally baffled by it, sometimes only a short time after writing it in the first place.

Depending on the platform you're working in, and the size of application you're developing, you may want to consider documentation as part of your daily coding routine as well. For a language such as Java, the process of generating documentation is easily automated using tools such Javadoc, which again make the development process less frustrating.

Use Indentation and Whitespace

Effective use of indentation and whitespace in your source code files is another simple but often neglected practise that makes development much easier. Indenting your code makes the overall structures within it visible at a glance, particularly where structures such as conditionals, loops and class declarations are being used. If you're using an IDE, it may automate the process of correcting your indentation, particularly useful if it's in a bit of a mess.

Develop Incrementally

Sometimes you have to go against your instincts with programming to spare yourself some headaches down the line. If you've just started on a project or have just worked out how you want to approach part of it, it's tempting to get a load of code down quickly and without stopping. However, the more code you write in between testing, the more difficulty you'll have finding the syntax and logical errors within it.

Writing small amounts of code one at a time and testing regularly makes for programs that have fewer bugs, and in which the bugs are easier to locate and fix. If you're worried about remembering the details of an algorithm or solution to some problem you've been having, i.e. if you've just had a brainwave and want to get the code down before you forget the details, you can insert an overview as a note in a comment first, then set about implementing it in code.

Use Development Tools

Whether or not you're using an IDE, there will likely be development tools available for the language you're working with. If you're developing for the Web, there are a lot of browser tools that can help to find and fix errors. Tools such as Firebug can prove indispensable, particularly if you're working on large or complex applications.

If you're developing in an IDE, check out whatever debugging tools it has installed, or indeed whether there are any you can install as plugins. IDEs like Eclipse typically have a lot of optional extras you can install as well as the already extensive set of tools provided as standard.

Use Trace Statements

To fix code errors, you first have to locate them, and this can be the hardest part of the process. As well as developing in short bursts, testing each time you add some new aspect of functionality, you can give yourself some helpful clues as to what's going wrong using trace statements. A trace statement is generally something that you output to indicate what is happening at a specific point in your program.

Where your trace statements are output depends on the platform and IDE you're using, if appropriate. For IDEs there will typically be an output console, which you can write to directly, for example in this Java statement outputting the value of some variable at a particular point:

System.out.println("Data value: "+someVariable);

For Web development, you can output similar information from server side scripts to the browser, as in this PHP example:

echo "Data value: ".$someVariable."
";

In this case the value will simply appear within the webpage HTML, although this may not be ideal if you're working on a site that is already live - in these cases you can opt for something that will not be seen by site users, for example using server side tools such as writing to the PHP error log.

For client side scripting such as JavaScript, you can output information either to an alert dialog in the crudest example, which is again only suitable if you are working on a site that is not live:

alert("Data value: "+someVariable);

Alternatively, you can write your information to the log, which is also accessible using tools such as Firebug:

console.log("Data value: "+someVariable);

As well as using trace statements to check variable values as your code executes, you can use them to simply check whether the code is reaching a particular stage, for example to see if a function is definitely being called.

Isolate Error Prone Code Excerpts

If you're working on a large or complex project, it may help if you can separate any sections of code that are causing particular difficulties. This can help to locate the problem areas and can also make the testing process a little less cumbersome.

An alternative to copying chunks of source code into separate scripts, to be run independently, is to temporarily comment out those parts of code you do not need running to complete your debugging, letting you focus on the parts you're trying to troubleshoot.

Take Regular Breaks

It's yet another thing most of us know we should do but regularly fail to, but taking regular breaks can make the difference between success and failure when it comes to fixing code errors, as well as potentially stopping you going totally off your head! Sometimes I feel I've hit a complete dead end with a code problem, but after a short break I manage to find it relatively quickly and easily.

Links

Internet Technology: DHCP (Dynamic Host Configuration Protocol)

What is DHCP?

DHCP (Dynamic Host Configuration Protocol) is used in Internet communication. IP networks (Internet Protocol) form the basis for Internet services, including the Web and email. In order to use these services, a client's computer must make a connection to an IP network. To make this connection, a number of configuration processes must occur, and this is where protocols such as DHCP come in.

Internet Connections

To access Internet services, a client computer needs an IP address, and the DHCP protocol is one of the ways in which such addresses are assigned. Different ISPs (Internet Service Providers) have varying approaches to handling IP addressing. Dynamic addressing is where addresses are only assigned as and when the client computers need them, i.e. when they attempt to make connections to the network. This is the type of addressing in which DHCP is used.

The DHCP Process

The process by which client computers are assigned IP addresses, in order to connect to Internet services, requires a series of stages, during which messages are sent and received between the client and the network. The client initially sends out messages to discover DHCP servers, and these servers then automatically carry out the addressing process through a number of discrete steps, each in turn associated with particular types of message packet.

The general principle of IP addressing through DHCP is that addresses are leased, or hired, to clients only when required. This means that one of the primary tasks of the DHCP servers is to assign these IP addresses when clients need them, and the process is carried out automatically, without DHCP administrators having to get involved.

DCHP Messages

DHCP communication is achieved through messages being sent and received by the client computer trying to make a connection. A DHCPDISCOVER message packet is initially sent to discover DHCP servers. At this stage a client computer is able to request the same IP address as the last one assigned to it, although this may no longer be available.

After identifying an IP address that is currently available, a DHCP server will send an offer to the client, using the DHCPOFFER message packet. More than one of the DHCP servers may have offered the client an IP address, so the client must accept just one of these offers, and it does this by sending a request message, or DHCPREQUEST packet. The request packet will refer to the IP address offered as well as the location of the server that offered it. This allows the other servers to reallocate their offered IP addresses to any other potential clients.

The final stage, which ultimately lets the client connect to the Internet, involves the DHCP server confirming the assignment of the requested IP address using a DHCPPACK message. All information necessary for the client configuration allowing a connection to be made will be contained within this last packet, which may also indicate how long the IP address lease is for.

DHCP Advantages

There are a number of advantages to using DHCP for Internet connectivity. One benefit is the fact that the IP addressing process is automated, and does not require the manual input of network administrators. Another primary advantage is the fact that IP addresses are only assigned when they are being used, which allows networks to maximise on the pool of addresses that exist within their resources at any one time.

Alternatives To DHCP

The main alternatives to DHCP, which uses dynamic IP addressing, are static methods. Static IP addresses are assigned on a fixed basis, with clients using the same IP address each time a connection is made. There are certain Web and Internet services which require clients to use static IP addresses, but for most people dynamic addressing does not affect Internet use negatively at all. ISP policies are typically what dictate whether static or dynamic addressing is used.

Links

DHCP (Dynamic Host Configuration Protocol) Basics
Dynamic Host Configuration Protocol - Microsoft TechNet
What is DHCP? - Indiana University Knowledge Base
Understanding the DHCP Protocol (Part 1)
DHCP Messages - Microsoft TechNet
RFC 2131 - Dynamic Host Configuration Protocol
DHCP - The Computer Technology Documentation Project

Expand and Collapse HTML Text

Many websites display text that users can expand and collapse while interacting with a page. This allows you to give users control over what they can see when viewing your pages and can result in sites that are more usable. By using a small amount of JavaScript code in conjunction with HTML, you can expand and collapse Web page text elements when the user presses a button or other visible element. Giving users control over your pages in this way can help them to process essential information about your business and services.

Text Element

To expand and collapse a text element in an HTML page, you first have to include the text content. The following sample code demonstrates including a paragraph element with some demonstration text in it:

Here is some text


The opening paragraph tag includes an ID attribute so that the JavaScript code for the page can identify the element, altering its appearance. The paragraph could optionally contain a much longer section of text. The element does not need to be a paragraph, with many other options including <div> elements.

Interaction

To give the user control over whether the text is expanded or collapsed, you need to include an interactive element. This can be an HTML input element or any element the user can click, such as an image. The following code demonstrates a button input element, which can be included before or after the text element:



The button appears with "show/hide" displayed on it so that users understand its purpose. When the user clicks the button, the browser will execute a JavaScript function named "toggleText" which will also be included within the page code.

JavaScript Function

The page head section can include a JavaScript function to execute on user interaction with the button element. The following JavaScript section can appear between the opening and closing <head> tags in the page:



This code includes the function indicated within the "onclick" attribute for the button element. The script section first creates a variable storing the current state of the text, which is expanded when the page initially loads. Within the function, the code first gets a reference to the text element within the page, using its ID attribute. The remainder of the function must handle expanding and collapsing the element.

Collapse

To collapse the element, JavaScript code can set the display style property. The following code demonstrates, added inside the function, after the line in which a reference to the element is stored in a variable:

if(isShown) {
textElement.style.display="none";
isShown = false;
}

This conditional statement checks the variable to establish whether the text is currently shown or not. If it is, the code sets the element display property to "none" which both hides and collapses the element within the page. Finally, the code toggles the boolean "isShown" variable to false, indicating that the element is now hidden.

Expand

To expand the text, the function can alter the style property of the text element again. The following code demonstrates, added after the "if" statement but still inside the function:

else {
textElement.style.display="block";
isShown = true;
}

The "else" statement only executes when the "if" statement returns a false value, which occurs when the element is currently collapsed. The code first alters the display property to "block" which both shows the text content and expands the element it is contained in. Finally the code resets the boolean variable to true, indicating that the element is currently shown. Each time the user clicks the button the element will toggle between expanded and collapsed.

See also:
Implementing Image Links with JavaScript
Displaying a Substring in JavaScript
Using Scripts in HTML Pages

Monday, 6 August 2012

Special Characters In Programming

Computer programming and markup code is naturally written with alphanumeric and other characters. However, within each language or technology, there are normally a set of characters that have a specific meaning or usage. This means that special consideration has to be given when those characters are used, for example when text strings are output for reading by users. The special characters for particular languages vary, as do techniques for handling them.

Coding

Whether a language is for programming or markup, there are typically always characters that cannot be used in the normal way. Those characters that have a specific meaning separate to their mainstream typographic use are generally considered special characters.

For example, within HTML, the tags surrounding Web page content use the "less than" and "greater than" characters:

<p>

This means that if those characters are to be used within an HTML page, they need to be included using their ASCII or ISO codes. For "less than" there are two options:

&#60; &lt;

Problems

When coding is carried out without sufficient handling of special characters, a number of problems may arise. For programming languages, code may not compile, and if it does there may be runtime errors.

For Web coding such as HTML markup, pages may not be rendered correctly, and their appearance may differ across Web browsers. Tools used to validate HTML and other Web markup to check whether it is well-formed will also show errors if special characters are not included properly.

Types

The types of special character depend on the language being used. In general, punctuation and non-alphanumeric characters may need special handling. There may also be characters that have a specified meaning within a particular language. For example, in many languages it is common to include text strings within quotation marks, for example in PHP:

$text="Here is some text";

If the text string also contains a quotation mark, this may cause a problem, as the compiling program will interpret the second quotation character it encounters as the end of the text string, preventing it from being able to process the code that follows.

Coping

The main technique for coping with special characters is to use an "escape" function. This is a shorthand syntax method letting the compiler know that a character is not being treated as it would be normally. For example, to include quotation marks in the PHP string:

$text="Here is \"some\" text";

The backslash is a common escape character used in many languages. The escape character is typically included immediately prior to the special character, preventing the compilation process from going wrong when it encounters the character in question.

Links

Special Characters | Webmonkey
Common HTML Validation Problems
Characters (The Java Tutorials > Learning the Java Language > Numbers and Strings)
PHP: htmlspecialchars - Manual
Special Characters - Microsoft