Mehmet Ethem SULAN :: Android :: Android AlertDialog and Builder Sample

Android ile AlertDialog kullanmaya çalıştım. AlertDialog kullanıcıya seçenek sunmak için kullanılır.


AlertDialogActivity.java

package com.dialog.alert;

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

public class AlertDialogActivity extends Activity {
    private Button show_builder_dialog;
    private Button button_start;
    private AlertDialog.Builder alBuilder;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        show_builder_dialog=(Button) findViewById(R.id.button1_builder);
        button_start=(Button) findViewById(R.id.button1_activity);
        show_builder_dialog.setOnClickListener(builder_alert);
        button_start.setOnClickListener(dialog_alert);
//Builder create ediyoruz.
        create_builder();
        
        
    }

//Evet tiklandiginda activityi sonlandiriyor. Hayir tiklandiginda ise dialogu kapatiyor
	private void create_builder() {
		alBuilder=new AlertDialog.Builder(this);
        alBuilder.setMessage("Do you want to close this window?")
        .setCancelable(false)
        .setPositiveButton("Evet", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				finish();
				
			}
		})
		.setNegativeButton("No", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
				
			}
		});
	}
    OnClickListener builder_alert=new OnClickListener() {
		
		@Override
		public void onClick(View v) {
//BUrada builder create ediliyor ve gosteriliyor.
			AlertDialog alertDialog=alBuilder.create();
			alertDialog.setIcon(R.drawable.icon);
			alertDialog.show();
			
		}
	};
	
	OnClickListener dialog_alert=new OnClickListener() {
		
		@Override
		public void onClick(View v) {

			Intent intent=new Intent(getApplicationContext(), Dialog.class);
			startActivity(intent);
			
		}
	};
}

Dialog.java

package com.dialog.alert;

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

public class Dialog extends Activity {
	private AlertDialog alertDialog;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//Her tiklandidiginda dialog create ediytor.
		create_dialog();
		
	}
	
	//Dilaog ile uc secenek sunuyor
	private void create_dialog() {
		alertDialog=new AlertDialog.Builder(this).create();
        alertDialog.setMessage("This is alert dialog without builder");
        alertDialog.setButton("Tamam", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(getApplicationContext(), "Tamam tiklandi", Toast.LENGTH_SHORT).show();
				return;
				
			}
		});
        alertDialog.setButton2("Iptal", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(getApplicationContext(), "Iptal tiklandi", Toast.LENGTH_SHORT).show();
				return;
				
			}
		});
        alertDialog.setButton3("Close Activity", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				finish();
				return;
				
			}
		});
//dialogu gosteriyor
        alertDialog.show();
	}

}

main.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"
    >
    <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Show Builder" android:id="@+id/button1_builder"></Button>
    <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Start Activity" android:id="@+id/button1_activity"></Button>
</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.dialog.alert"
      android:versionCode="1"
      android:versionName="1.0">


    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".AlertDialogActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="Dialog"></activity>

    </application>
</manifest>

Dwonload AlertDialog.tar.gz

Comments

There is one comment for this post.

  1. Mira on Eylül 18, 2012 9:42 am

    Surprising to think of smeothing like that

Write a Comment

Let me know what you think?