888 views 0 comments

Android 101 : Day 2 | Approaches In Android

by on July 21, 2015
 

Hi guys, this is our Third Tutorial for Android Application Development and today we’ll be Building a “Simple Toast Application” using different Approaches in Android. In our last Tutorial, we learnt how to Build and test an Application on an Android Device. Before moving further, you should be aware of a few Directories and Files in the Android Project…

Components Of Android Application Project

Android 101 - Day 2 - Android App Project structure

#1. src
This contains the .java source files for your project. By default, it includes a MainActivity.java source file having an activity class that runs when your application is launched.

#2. gen
This contains the R.java file, a compiler-generated file that references all the resources and controls found in your project. * This file must not be modified *

#3. res/layout
This is a directory for files that define your app’s user interface. All the designing part of the application is done in layout. (We’ll be designing our Applications inside activity_main)

#4. AndroidManifest.xml
This is the manifest file which describes the fundamental characteristics of the app and defines each of its components. 

Approaches In Android

Application Development in Android basically includes :

  • Designing of Layout
  • Designing of View Controls(Buttons, TextViews etc)
  • Text for every specific Control
  • Event Handling 
  • Business Logic for our Application

In Android, there are basically 3 types of Approaches for designing an Application :

  • XML Approach
  • Java Approach
  • Hybrid Approach (XML + JAVA)

Now, every Developer can have its own Approach for designing Applications but we’ll be using the most common one, that is the Hybrid Approach(Text, Event Handling in Java & Designing in XML). However today we’ll be designing a Simple Toast Application using XML and Java Approach so as to find out differences between all three Approaches.

But wait! What is a Toast ?

Toast is nothing but a Pop up, that can be displayed when an event occurs.
In our Application, we will display some Toast message on a Button Click. To display a toast message, we use a predefined class Toast. 

The Toast.makeText() method is a factory method which creates a Toast object. It takes 3 parameters :

Context : The activity within which the toast has to be displayed
Text : The toast message that has to be displayed
Duration : The set time for which the Toast has to be displayed

The Toast.makeText() method returns an object and the Toast is displayed on the screen using the show() method.

Well, that’s enough theory for today. Let’s start building our Application!

XML Approach

Following are the steps to Create a Simple Toast Application using XML Approach :

  • Create a New Android Application Project.
    >> Open File > New > Android Application Project
    >> Configure Project and click on Next
    >> Configure Launcher icon and click on Next
    >>Configure Activity and click on Next 
    >>Enter Activity & Layout name (XmlActivity.java & activity_xml.xml) and click on Finish
    (Having Problems ?! Learn to Create a new Android Application Project from our Last Tutorial)
  • As we’re using the XML approach, we’ll perform Event Handling and Designing inside our xml file .
    NOTE :  Business Logic is the executable code and hence it’s always coded inside our Java file. This is because xml is a parsable language while java is the executable one.

    activity_xml.xml :

Graphical Layout :

xmlApplication

XmlActivity.java

Here is a screenshot of the XmlApp :
Screenshot_XmlApp

Java Approach

Following are the steps to Create a Simple Toast Application using Java Approach :

  • Create a New Android Application Project.
  • In Java Approach, we’ll perform Event Handling and Designing of the App inside MainActivity.java.

    MainActivity.java :

Here is a screenshot of the JavaApp :
Screenshot_JavaApp


Simple Toast Application – Source code


That’s all for our Today’s Tutorial.
In our Next Tutorial, we’ll design a Caller Application using Hybrid Approach and start with Intent in Android.
So stay tuned for our next Tutorial, which will be coming out shortly. 😉

Be the first to comment!
 
Leave a reply »

 

Leave a Response