Coding Villa

create Dialogue/ Alert /Message box in android

Author : Mehreen Jamil

Share with Friends
Views : 3148
Ratings : 0
Bookmarks : 0
Farourities : 0

Description


In this simple article you will learn that how we can create Dialogue / Alert / Message box in android.


Advertisements


 

 

Message Box in Android:

There are two ways to display a message Box in android.

1.    Toast

              Display a short balloon pop-up that will disappear after few seconds. For toast we have to import

              import android.widget.Toast;

            

            There is a Toast class for this purpose. Toast has a static function makeText which is used to                  display a message on screen  

 

public static Toast makeText (Context context, CharSequence text, int duration)

Parameters

 

context

The context to use. Usually your Application or Activity object.

Text

The text to show. Can be formatted text.

Duration

How long to display the message. Either LENGTH_SHORT or LENGTH_LONG

 

 

finally we call the Show() method to display the toast.

 

Code:

 

 

package sample.mytoast;

 

import android.app.Activity;

 

import android.os.Bundle;

import android.widget.Toast;

 

public class Article_androidActivity extends Activity {

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

       

        this.setContentView(R.layout.main);

 

        Toast.makeText(this, "Display a message", 3).show();

               

    }

 

        }

 

 

      2. AlertDialog 

            

 Display a pop-up window with buttons.For AlertDialog we have to add

 

 import android.app.AlertDialog;

 

Android has a class AlertDialog to show a dialog box.

 

 

 

Code:

 

package sample.my.tablelayout;

 

import android.app.Activity;

import android.app.AlertDialog;

import android.os.Bundle;

public class Article_androidActivity extends Activity {

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

       

        this.setContentView(R.layout.main);

 

         AlertDialog ad = new AlertDialog.Builder(this).create(); 

         ad.setCancelable(false); // This blocks the 'BACK' button 

         ad.setMessage("Hello World"); 

         ad.show();

       

    }

 

        }

This simple article tells that How to create Dialogue / Alert / Message box in android.

 

 



Article Tags
Share with Friends

Comments


No Image PIR FAHIM SHAH
This one is also working good.

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder
.setTitle("Deleting a Contact No")
.setMessage("Are you sure?")
.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
//do some thing here which you need
}
}); //Do nothing on no
builder.setNegativeButton("No", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
dialog.dismiss();
}
});


Leave a Reply

Name (required)  
Mail (will not be published) (required)   
 
Enter This Code
Captcha
 

Advertisements

Article Categories

.NET


Databases


Mobile Development


Operating Systems


Web Development


Coding Villa on Facebook