Official website for Linux User & Developer
FOLLOW US ON:
Jul
21

Android Development masterclass

by Kunal Deo

It’s time to go beyond the ‘hello world’ app. Let’s look into real-world situations and start doing big things with your Android development project…

Generating a random number
Although Android has ditched all of the Java ME components, it still has a large number of standard Java packages such as io, lang, annotation, net, math, security, net and util. This gives us access to a huge library of Java classes that can be used to achieve a lot of crucial tasks easily that would otherwise be hard to implement.
In this tip we will show you how to use the Java.util.Random package to generate a random number inside Android applications.
It supports two public constructors:
Random() – Constructs a random generator with the current time of day in milliseconds as the initial state.
Random(long seed) – This constructs a random generator with the given seed as the initial state.
Code:

import java.util.Random;
final Random myRandom = new Random();

You can also use Math.random() to generate random numbers. The following example will generate a random number and then generate it on the screen:

 super.onCreate(savedInstanceState);
 TextView tv = new TextView(this);
 tv.setText(“Random Number : “ + Math.random());
 int random = (int)Math.ceil(Math.random()*100);
 setContentView(tv);

Using ProgressBar
ProgressBar is an essential way to keep your user informed about your application’s progress. It displays a bar to the user, representing how far the operation has progressed. Applications are allowed to change the amount of progress as it moves forward.
To demonstrate this we will implement a circle ProgressBar (default) and Horizontal ProgressBar. We will implement a Runnable Thread to send a message to the Handle to modify the progress of the ProgressBar.
Add the two ProgressBar UI components to the main layout file, main.xml.  By default, both will be circle ProgressBars. We will modify the second progress bar to a horizontal style. If you are using ADT, you can simply drag and drop the ProgressBar UI component onto the layout and then modify the XML file.
@code: main.xml

<?xml version=”1.0” encoding=”utf-8”?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
 android:orientation=”vertical”
 android:layout_width=”fill_parent”
 android:layout_height=”fill_parent”
 >
<TextView
 android:layout_width=”fill_parent”
 android:layout_height=”wrap_content”
 android:text=”@string/hello”
 />
<ProgressBar
 android:id=”@+id/CircularProgressBar”
 android:layout_width=”wrap_content”
 android:layout_height=”wrap_content”
 />
<ProgressBar
 android:id=”@+id/HorizontalProgressBar”
 android:layout_width=”fill_parent”
 android:layout_height=”wrap_content”
 style = “?android:attr/progressBarStyleHorizontal”
 android:max=”100”
 />
</LinearLayout>
@code:MainActivity.java
package com.ludprogressbar;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ProgressBar;
public class MainActivity extends Activity {
 ProgressBar progress;
 int completion = 0;
/** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 progress = (ProgressBar)findViewById(R.id.HorizontalProgressBar);
 new Thread(progessThread).start();
 }
 private Runnable progessThread = new Runnable() {
 @Override
 public void run() {
 // TODO Auto-generated method stub
 while (completion < 100) {
 try{
handle.sendMessage(handle.obtainMessage());
Thread.sleep (1000);
 }
 catch (Throwable t) {
 }
 }
 }
 Handler handle = new Handler() {
 @Override
 public void handleMessage(Message msg){
completion++;
 progress.setProgress(completion);
 }
 };
 };
}
twitter follow us
Pages: 1 2 3 4 5 6
  • Tell a Friend
  • Follow our Twitter to find out about all the latest Linux news, reviews, previews, interviews, features and a whole more.

    One Comment »

    What's your opinion?

    Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

    Be nice. Keep it clean. Stay on topic. No spam.

    * Required fields