Saturday, September 12, 2015

Android Alert Dialog Tutorials Android Studio

First create android new project >>

project XML Code Under>>


1>   Activity_main xml


<RelativeLayout  

  android:layout_width="fill_parent"  
  android:layout_height="fill_parent"  
  android:layout_marginTop="@dimen/activity_horizontal_margin"  
  android:layout_marginLeft="@dimen/activity_horizontal_margin" 
  android:layout_margin="@dimen/activity_horizontal_margin"   
  android:layout_marginEnd="@dimen/activity_horizontal_margin"  
  android:layout_marginRight="@dimen/activity_horizontal_margin" 
  android:layout_marginBottom="@dimen/activity_horizontal_margin"  
  android:layout_marginStart="@dimen/activity_horizontal_margin"   
  android:gravity="center_horizontal"   
  xmlns:android="http://schemas.android.com/apk/res/android">

 <TextView        
 android:id="@+id/textView"        
 android:layout_width="wrap_content"       
 android:layout_height="wrap_content"       
 android:text="Alert Dialog"       
 android:textAppearance="?android:attr/textAppearanceLarge"       
 android:textColor="#c820ff46"       
 android:layout_alignParentTop="true"        
 android:layout_centerHorizontal="true" />

 <Button        
 android:id="@+id/button"        
 android:layout_width="fill_parent"       
 android:layout_height="wrap_content"        
 android:layout_below="@+id/textView"        
 android:layout_marginTop="10dp"       
 android:text="FirstButton"/>



</RelativeLayout>


layout xml Screenshoot




MainActivity java code

package com.example.faruk.alert;

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

public class MainActivity extends Activity  {

    Button button;

    @Override   
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button)findViewById(R.id.button);


        button.setOnClickListener(new View.OnClickListener() {
            @Override           
        public void onClick(View v) {
                final AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
                alert.setIcon(R.drawable.screeb);
                alert.setTitle("Error");
                alert.setMessage("Are you sure");
                alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override                   
 public void onClick(DialogInterface dialog, int which) {
 Toast.makeText(getApplicationContext(), "You Click yes button", Toast.LENGTH_LONG).show();
                       
       finish();
                    }
                });

                alert.setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override                   
 public void onClick(DialogInterface dialog, int which) {
                        
Toast.makeText(getApplicationContext(),"You click no button",Toast.LENGTH_LONG).show();
                    }
                });
                AlertDialog alertDialog = alert.create();
                alertDialog.show();
            }
        });
    }

}

Final Result Screen shoot  under>>







0 comments

Post a Comment

Thank you for your comment..................................