Thursday, 3 May 2012

Alert Message Box in Android


ALERTDIALOG.BUILDER ADB=NEW ALERTDIALOG.BUILDER(ACTIVITY);

    adb.setTitle("Alert?");
    adb.setMessage("Do you want to delete Note.");
    adb.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                
            System.out.println("zdfsdfksjDfjkds:hi me hi me  "+holder.id.getText());
          }
    }
      );
    adb.setNegativeButton("No", null);
    adb.show();

Method of create signed apk for android

Click here...... This link use full for create signed your apk

How to make a progress dialog In Android

The first thing you have to do is, making your progress dialog accessible from the entire Main class.
So, how do we do that? easy!
Define the progress dialog in the name space as so:

1
private ProgressDialog progressDialog;
Now we want to create a functions which will be holding our Thread and ProgressDialog.
We can do this by adding the following code:

01
private void runDialog(final int seconds)
02 {
03         progressDialog = ProgressDialog.show(this"Please wait....""Here your message");
04
05         new Thread(new Runnable(){
06             public void run(){
07                 try {
08                             Thread.sleep(seconds * 1000);
09                     progressDialog.dismiss();
10                 catch (InterruptedException e) {
11                     e.printStackTrace();
12                 }
13             }
14         }).start();
15 }
So what does that piece of code do exactly? Lets go over this code 1 step at a time.

1
progressDialog = ProgressDialog.show(this"Please wait....""Here your message");
The following code create's and starts the ProgressDialog.

01
new Thread(new Runnable(){
02     public void run(){
03         try {
04             Thread.sleep(seconds * 1000);
05             progressDialog.dismiss();
06         catch (InterruptedException e) {
07             e.printStackTrace();
08         }
09     }
10 }).start();
This piece of code create a "Thread" a thread runs in the background of your applications, which lets your GUI (Graphical User Interface) run without interference. If you would do this without a Thread, you GUI would freeze / lock untill the process is completed.
Lets move to the next piece:

1
Thread.sleep(seconds * 1000);
After the thread starts to run it's code, when it reaches the thread.sleep() command it's waits for a given amount of time and the continues to execute the rest of the code. So in our case it waits for 5 seconds before running the following command:

1
progressDialog.dismiss();
This piece of code dismisses / stops the progress dialog.
A thread is most commonly used for background (service) operations, for example....you create aThread (worker) which checks if you receive any message's or, checks your battery status, play music, and the list goes on and on.

Wednesday, 2 May 2012

Auto Switch between tow activity in android

Our First Activity class Logo. :-



public class Logo extends Activity {
private static final int SPLASH_DISPLAY_TIME = 4000;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   requestWindowFeature(Window.FEATURE_NO_TITLE);
        // making it full screen
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.logo);
        new Handler().postDelayed(new Runnable() {
            public void run() {

                Intent intent = new Intent();
                intent.setClass(Logo.this, Splesh.class);

                Logo.this.startActivity(intent);
                Logo.this.finish();


            }
        }, SPLASH_DISPLAY_TIME);
   // TODO Auto-generated method stub
}

}

Second Activity Class Splesh :-


public class Splesh extends Activity {
private static final int SPLASH_DISPLAY_TIME = 4000;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   requestWindowFeature(Window.FEATURE_NO_TITLE);
        // making it full screen
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
 
        setContentView(R.layout.splesh);
     
        new Handler().postDelayed(new Runnable() {
            public void run() {

                Intent intent = new Intent();
                intent.setClass(Splesh.this, NewActivity.class);

                Splesh.this.startActivity(intent);
                Splesh.this.finish();
     

            }
        }, SPLASH_DISPLAY_TIME);
   // TODO Auto-generated method stub
}


}



Another Way to Make Splash Screen in android

Here is process to show splash screen :-



public class Logo extends Activity {
private static final int SPLASH_DISPLAY_TIME = 4000;

@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.logo);
             new Handler().postDelayed(new Runnable() {
              public void run() {
                Intent intent = new Intent();
                intent.setClass(Logo.this, Splesh.class);
                Logo.this.startActivity(intent);
                Logo.this.finish();
            }
        }, SPLASH_DISPLAY_TIME);
 
}

}

How to make a basic splash screen

The following code will show you how to create a basic splashscreen for your Android application that will stay for 5 seconds. If we don't want to wait we can tap the screen to go directly to the next Activity. Source project is on the bottom of the tutorial.
Lets go through the code to see how simple it is to make a very basic splash screen for your application.
The splashscreen will be the startup Activity for our application and on application launch a Thread will start that will listen for touch events.
01public class SplashScreen extends Activity {
02
03    //how long until we go to the next activity
04    protected int _splashTime = 5000;
05
06    private Thread splashTread;
07
08    /** Called when the activity is first created. */
09    @Override
10    public void onCreate(Bundle savedInstanceState) {
11        super.onCreate(savedInstanceState);
12        setContentView(R.layout.splash);
13
14        final SplashScreen sPlashScreen = this;
15
16        // thread for displaying the SplashScreen
17        splashTread = new Thread() {
18            @Override
19            public void run() {
20                try {
21                    synchronized(this){
22
23                        //wait 5 sec
24                        wait(_splashTime);
25                    }
26
27                catch(InterruptedException e) {}
28                finally {
29                    finish();
30
31                    //start a new activity
32                    Intent i = new Intent();
33                    i.setClass(sPlashScreen, Main.class);
34                    startActivity(i);
35
36                    stop();
37                }
38            }
39        };
40
41        splashTread.start();
42    }
43
44    //Function that will handle the touch
45    @Override
46    public boolean onTouchEvent(MotionEvent event) {
47        if (event.getAction() == MotionEvent.ACTION_DOWN) {
48            synchronized(splashTread){
49                splashTread.notifyAll();
50            }
51        }
52        return true;
53    }
54
55}

Show Textview, LinearLayout in Different shape


This is a generic shape defined in XML.we can use this xml to create new shape of android controls like textview and linearlayout etc...
file location:
res/drawable/filename.xml
The filename is used as the resource ID.
compiled resource datatype:
Resource pointer to a GradientDrawable.
resource reference:
In Java: R.drawable.filename
In XML: @[package:]drawable/filename
syntax:
<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape=["rectangle" | "oval" | "line" | "ring"] >
    <corners
        android:radius="integer"
        android:topLeftRadius="integer"
        android:topRightRadius="integer"
        android:bottomLeftRadius="integer"
        android:bottomRightRadius="integer" />
    <gradient
        android:angle="integer"
        android:centerX="integer"
        android:centerY="integer"
        android:centerColor="integer"
        android:endColor="color"
        android:gradientRadius="integer"
        android:startColor="color"
        android:type=["linear" | "radial" | "sweep"]
        android:useLevel=["true" | "false"] />
    <padding
        android:left="integer"
        android:top="integer"
        android:right="integer"
        android:bottom="integer" />
    <size
        android:width="integer"
        android:height="integer" />
    <solid
        android:color="color" />
    <stroke
        android:width="integer"
        android:color="color"
        android:dashWidth="integer"
        android:dashGap="integer" />
</shape>

example:
XML file saved at res/drawable/gradient_box.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:startColor="#FFFF0000"
        android:endColor="#80FF00FF"
        android:angle="45"/>
    <padding android:left="7dp"
        android:top="7dp"
        android:right="7dp"
        android:bottom="7dp" />
    <corners android:radius="8dp" />
</shape>
This layout XML applies the shape drawable to a View:
<TextView
    android:background="@drawable/gradient_box"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content" />
This application code gets the shape drawable and applies it to a View:
Resources res = getResources();
Drawable shape = res. getDrawable(R.drawable.gradient_box);
TextView tv = (TextView)findViewByID(R.id.textview);
tv.setBackground(shape);