Bu derste dialog oluşturmak için dialogfragment kullanımından bahsettim.
Bir fragment oluşturup, bunu istediğimiz layoutu atayıp bunu bir dialog penceresi olarak gösterebiliriz. Bunun için oluşturduğumuz fragmenti DialogFragment sınıfından türetmemiz gerekiyor.
Burda dikkat edilecek yer fragmentimizin oncreateview methodu. inflater.inflate() methodunda root’a attach etmememiz gerekiyor, çünkü fragmenti activity’e bağlamayacağız. Bunun için inflater methodunun ikinci parametresine null atıyoruz.
Fragmentimizi dialog olarak göstermek için activity’de butona tıklanıldığında fragment nesnesi oluşturup, show transaction’ı için show methodunu kullanıyoruz.
Dialog fragmentteki buton tıklanmalarını ele almak ve de ilgili değerleri activity’e göndermek için fragment’ten activity’e veri gönderirken yaptığımız işlemi yani bir interface ile veri alış verişini sağlıyoruz.
Örneğin kodları aşağıdaki gibidir.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.emrealtunbilek.dialogfragmentornek.MainActivity"> <Button android:text="Show Dialog" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentStart="true" android:onClick="showDialog" android:id="@+id/btnShowDialog" /> </RelativeLayout> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
public class MainActivity extends AppCompatActivity implements MyListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void showDialog(View view) { MyFragment myFragment=new MyFragment(); myFragment.show(getFragmentManager(), "My Dialog"); } @Override public void dialogVerisiniGOnder(String mesaj) { Toast.makeText(this, mesaj, Toast.LENGTH_LONG).show(); } } |
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 |
public class MyFragment extends DialogFragment implements View.OnClickListener{ Button evet, hayir; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view=inflater.inflate(R.layout.my_dialog, null); evet= (Button) view.findViewById(R.id.btnEvet); hayir= (Button) view.findViewById(R.id.btnHayir); evet.setOnClickListener(this); hayir.setOnClickListener(this); setCancelable(false); return view; } @Override public void onClick(View v) { MyListener myListener= (MyListener) getActivity(); if(v.getId()==R.id.btnEvet){ myListener.dialogVerisiniGOnder("EVET TIKLANDI"); dismiss(); }else{ myListener.dialogVerisiniGOnder("HAYIR TIKLANDI"); dismiss(); } } } |
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 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:padding="16dp" android:layout_height="match_parent"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:text="Bu dialog penceresini beğendiniz mi?" /> <Button android:text="HAYIR" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView" android:layout_alignParentStart="true" android:layout_marginTop="20dp" android:id="@+id/btnHayir" /> <Button android:text="EVET" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnEvet" android:layout_alignBaseline="@+id/btnHayir" android:layout_alignBottom="@+id/btnHayir" android:layout_toEndOf="@+id/btnHayir" android:layout_marginStart="18dp" /> </RelativeLayout> |
1 2 3 4 |
public interface MyListener { void dialogVerisiniGOnder(String mesaj); } |