android simple alert dialog

Simple Dialog using AlertDialog in android

Simple Dialog using AlertDialog in android

A dialog is a small window that come upon the screen and user can give input on it according to it’s functionality. In this application we learn how create a simple alert dialog using ‘AlertDialog’ class.

 

  A short-code  is here below

 new AlertDialog.Builder(this)

        .setIcon(android.R.drawable.ic_dialog_info)

         .setTitle("Android From Home")

         .setMessage("Have You learning Android from www.androidfromhome.com ?")

          .setPositiveButton("Yes",new android.content.DialogInterface.OnClickListener(){

          public void onClick(DialogInterface dialog, int which)

          {

 

          }

          })

          .setNegativeButton("No",new android.content.DialogInterface.OnClickListener(){

          public void onClick(DialogInterface dialog, int which)

          {        }

         }).show();

 

Full Source Code

Now We will check source code of this application.


SimpleAlertActivity.java


 

package com.androidfromhome.simplealert;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class SimpleAlertActivity extends Activity implements OnClickListener {

    private Button btn_click;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple_alert);
        
        btn_click=(Button)findViewById(R.id.btn_click);
        btn_click.setOnClickListener(this);
    }

    public void getAlertDialog(){
        
         new AlertDialog.Builder(SimpleAlertActivity.this)
          .setIcon(android.R.drawable.ic_dialog_info)
          .setTitle("Android From Home")
          .setMessage("Have You learning Android from www.androidfromhome.com ?")
           .setPositiveButton("Yes",new android.content.DialogInterface.OnClickListener(){
           public void onClick(DialogInterface dialog, int which)
           {

           }
           })
           .setNegativeButton("No",new android.content.DialogInterface.OnClickListener(){
           public void onClick(DialogInterface dialog, int which)
           {        }
          }).show();
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.btn_click:
            getAlertDialog();
            break;

        default:
            break;
        }
        
    }
}

 

Now check xml layout.


activity_simple_alert.xml


 

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/LinearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#A758AB"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="15dp" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="2dp"
            android:text="Android From Home"
            android:textColor="#ffffff"
            android:textSize="22dp"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="vertical" >

        <Button
            android:id="@+id/btn_click"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#7BC168"
            android:padding="10dp"
            android:text="Simple Alert Dialog"
            android:textColor="#ffffff"
            android:textStyle="bold" />

    </LinearLayout>

</LinearLayout>


Download Full Source Code

 

Related Post:-

Put file to emulator sd card

Switching One activity  to another activity in Android by intent

Android Splash Screen Example

Custom Toast example in android example

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Get Widget