vijay

welcome Netizen

Share Your Knowledge.It is a way to achieve immortality

Friday, July 13, 2012

Show Dialog or Message box in android application


Here I share a bit of useful information to use efficient show dialog box in android application.
Showing message box to user is very important to get the confirmation from the user.so dialog box must use in all the activity to result to user. When use dialog we need create separate layout every time we must call the layout set the message to the user. Instead of writing the code in all the activity create separate class for dialog box just pass only the message.
By writing the code by this way to reduce the code size, it will be very neat maintaining the code.
1.       First create the xml layout for to use in Dialog box.
dialog.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">
    <ScrollView
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">
                            <LinearLayout
                     android:orientation="vertical"
                     android:layout_width="fill_parent"
                     android:layout_height="fill_parent"
                     android:paddingLeft="5px"
                     android:paddingRight="5px"
                     android:paddingBottom="10px" >
                     <TextView
                           android:id="@+id/textView1"
                           android:layout_width="wrap_content"
                           android:layout_height="wrap_content"
                           android:layout_centerHorizontal="true"
                           android:layout_marginBottom="5dip"
                           android:layout_marginLeft="5dip"
                           android:layout_marginRight="5dip"/>            
                     <Button
                           android:id="@+id/button1"
                           android:text="@string/clc"
                           android:layout_width="wrap_content"
                           android:layout_height="wrap_content"
                           android:layout_gravity="center"/>       
              </LinearLayout>     
       </ScrollView>   
</LinearLayout>

2.       

 create common class file ,here I use class file name as setdialog.class


Write the below code on the class file..
Code:
void alert(final Dialog dialog,String msg )
       {
              // For Dialog Button**********************************
              dialog.setContentView(R.layout.dialog);
              dialog.setTitle("Result");
              final TextView dialogtxt = (TextView) dialog
                           .findViewById(R.id.textView1);
              final Button closeButton = (Button) dialog
                           .findViewById(R.id.button1);
              closeButton.setOnClickListener(new View.OnClickListener() {
                     @Override
                     public void onClick(View v) {
                           // TODO Auto-generated method stub
                           dialog.dismiss();
                     }
              });
                     dialogtxt.setText(msg);
              dialog.show();
              // ****************************************************
       }

Next step we must create the object for the class we use the dialog box wherever we want….
For an example I pass the message to dialog box from the activity.
First I create object for setdialog class and pass the message and dialog and alos don’t forget the below code activity class file
final Dialog dialog = new Dialog(this);
setdialog show=new setdialog();
show.alert(dialog,"Details are succesfully updated");

That’s it…In the above code if you have any doubt plz share with comments…

0 comments:

Post a Comment