Android Studio AlertDialog Kullanımı

AlertDialog uygulamamızda yeni bir küçük pencere oluşturup yönlendirme yapmamıza olanak sağlar. İki şekilde tıklama seçeneği yerine ( Evet ve Hayır ) tek seçenek yapmakta mümkün onun için de setNegativeButton veya setPositiveButton fonksiyonundan birini kaldırmamız yeterli olacaktır. Daha fazla tıklama seçenekleri de mevcut ancak şu an kolay anlaşılması için iki seçenek kullanılarak örnek yapalım.
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.esatgozcu.alertdialogkullanimi.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingTop="150dp"> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Alert Dialog Kullanımı" android:textAlignment="center" android:textColor="@android:color/black" android:textSize="24sp" /> <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="80dp" android:layout_marginRight="80dp" android:layout_marginTop="20dp" android:background="@android:color/black" android:text="Git" android:textColor="@android:color/white" android:textSize="24sp" /> </LinearLayout> </android.support.constraint.ConstraintLayout> |
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
package com.example.esatgozcu.alertdialogkullanimi; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.QuickContactBadge; public class MainActivity extends AppCompatActivity { Button gitbuton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gitbuton=(Button)findViewById(R.id.button); gitbuton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { function(); } }); } public void function() { //AlertDialog'u nesnesi türetiyoruz AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); //Başlık builder.setTitle("www.esatgozcu.com"); //Mesaj builder.setMessage("Siteyi Ziyaret Et"); //Herhangi bir boşluğa basınca kapanmaması için true olursa kapanır //Geri tuşununu da pasif hale getiriyoruz builder.setCancelable(false); //AlertDialog'un iconunu belirliyoruz builder.setIcon(R.drawable.resim); builder.setNegativeButton("HAYIR", new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int id) { //Hayır butonuna basılınca yapılacaklar. //Sadece kapanması istenirse boş bırakılır } }); builder.setPositiveButton("EVET", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { //Tamam butonuna basılınca yapılacaklar //Websitesine gidiyoruz Uri link = Uri.parse("http://www.esatgozcu.com/android-studio/android-studio-alertdialog-kullanimi"); Intent tara = new Intent(Intent.ACTION_DEFAULT, link); startActivity(tara); } }); //AlertDiolag Gösteriliyor builder.show(); } } |
Uygulamayı buraya tıklayarak indirebilirsiniz.