Post Top Ad

Showing posts with label Learn android. Show all posts
Showing posts with label Learn android. Show all posts
May 22, 2014

How to call a method after a delay in Android

I recently wanted to call a method after some time for my project. What i rally wanted was, i have already created a splash screen for my app. And then after 5seconds or so i wanted to remain it in active and start another activity.( in fact MainActivity).

public void DoSomething()
{
//do something here
}

IOS Approach For This Problem

Ex: In ios you will find a method called to do this task easilty
[self performSelector:@selector(DoSomething) withObject:nil afterDelay:5];
 

In Android what would be the best way to achieve this

I achieved this using java.util.concurrent package.

Here is the code

private static final ScheduledExecutorService worker = 
Executors.newSingleThreadScheduledExecutor();

void someMethod() {

Runnable task = new Runnable() {
public void run() {
/* Do something… */
}
};
worker
.schedule(task, 5, TimeUnit.SECONDS);
⋮
}
 
Note:
 
1. worker.schedule(task, 5, TimeUnit.SECONDS); 
   Here i have set the delay to 5 seconds. 
2. /* Do something… */
   Insert your code to be run there after the given time exceeds.   
 
 
 
 
 
May 21, 2014

Android 2nd Tutorial

Android Hello World Example



This is the most easy tutorial as far as android is concerned.





When you create a new project, by default Google has included hello world code for you. But what happens behind the code is the important part.
 
Summary steps to develop an Android application :
  1. Install Android SDK
  2. Install ADT Eclipse plugin
  3. Create an Android Virtual Device (AVD)
  4. Create Android Project with Eclipse (Wizard)
  5. Code it…
  6. Start it in Android Virtual Device (AVD)
In android all the code is normally written in Java
GUI is created using XML
So you have to attach GUI elements to the code. How we do this is via a file called R. R is a automatically generated file where you have all the default name for the elements which you have included in the GUI.
When you run the code what happen is all the components will be linked together with the code and display the output


But How come Hello world String appear in the GUI
Hello world string is in actually String file. 

String Resources

 A string resource provides text strings for your application with optional text styling and formatting. There are three types of resources that can provide your application with strings:
String

    XML resource that provides a single string.

String Array
    XML resource that provides an array of strings.

Quantity Strings (Plurals)
    XML resource that carries different strings for pluralization.
All strings are capable of applying some styling markup and formatting arguments. For information about styling and formatting strings.
Useful Links

Android Developer Guide

 



My Instagram