Hai Pembaca, saya harap Anda semua baik-baik saja. Jadi hari ini kita akan belajar tentang cara membuat MaterialAlertDialogBuilder di kotlin apakah Anda siap untuk belajarš¤.
Mari kita Mulai:
Langkah 1: Tambahkan Ketergantungan ke build.Gradle
// Material Dialogs
implementation "com.afollestad.material-dialogs:core:3.0.0-rc1
// Material theme
implementation "com.google.android.material:material:1.4.0-rc01"
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab_ofdialog"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_marginBottom="16dp"
android:backgroundTint="#FF0000"
android:src="@drawable/fab_location"
app:rippleColor="#ff492d"
app:iconTint="#ffffff"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.954"
app:layout_constraintStart_toStartOf="parent" />
Buat custom_fragment.xml dalam XML ini kita akan membuat UI kita sendiri. Silakan salin kodenya.
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/iv_locationIcon"
android:layout_width="45dp"
android:layout_height="45dp"
android:src="@drawable/location1"
android:layout_marginTop="18dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.textview.MaterialTextView
android:id="@+id/tv_DeviceLocationIsOff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:textStyle="bold|normal"
android:textSize="18sp"
android:textColor="?android:attr/textColorPrimary"
android:hint="@string/device_location_is_off"
app:layout_constraintEnd_toEndOf="@+id/iv_locationIcon"
app:layout_constraintStart_toStartOf="@+id/iv_locationIcon"
app:layout_constraintTop_toBottomOf="@+id/iv_locationIcon" />
<com.google.android.material.textview.MaterialTextView
android:id="@+id/tv_messageToTurnOn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textStyle="bold"
android:textColor="?android:attr/textColorSecondary"
android:layout_marginTop="8dp"
android:hint="@string/please_turn_on_your_device_location_to_ensure_hassele_free_experience"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_DeviceLocationIsOff" />
<com.google.android.material.button.MaterialButton
android:id="@+id/btn_turnOnDeviceLoc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#ffffff"
android:layout_marginTop="8dp"
android:hint="@string/deviceLocation"
app:icon="@drawable/ic_turn_on_device_loc"
app:iconPadding="4dp"
android:textColor="#000000"
app:iconGravity="start"
app:elevation="2dp"
app:rippleColor="#ff492d"
app:iconTint="#FF0000"
style="@style/Widget.MaterialComponents.Button.OutlinedButton.Icon"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_messageToTurnOn" />
<com.google.android.material.button.MaterialButton
android:id="@+id/btn_turnManuallyOnDeviceLoc"
style="@style/Widget.MaterialComponents.Button.OutlinedButton.Icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#ffffff"
android:hint="@string/manually"
android:textColor="#000000"
android:layout_marginBottom="18dp"
app:elevation="2dp"
app:icon="@drawable/search"
app:iconGravity="start"
app:rippleColor="#ff492d"
app:iconTint="#FF0000"
app:iconPadding="4dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn_turnOnDeviceLoc" />
</androidx.constraintlayout.widget.ConstraintLayout>
Sekarang buka HomeFragment.kt dan di onViewCreate rekatkan kode di bawah ini.
//1
fabOfdialog.setOnClickListener {
//2
val mview = LayoutInflater.from(requireActivity())
.inflate(R.layout.custom_dialouge, null, false)
//3 val dialog = MaterialAlertDialogBuilder(requireContext())
.setView(mview)
.setCancelable(true)
.setBackground(resources.getDrawable(R.drawable.radius))
.create()
//4 mview.btn_turnOnDeviceLoc.setOnClickListener {
Toast.makeText(requireContext(),"You have clicked First Button",Toast.LENGTH_SHORT).show()
// when user click and button and you want to close dialog so you //can use this if not you can remove this dialoge.dismiss()
dialog.dismiss()
//5 mview.btn_turnManuallyOnDeviceLoc.setOnClickListener {
Toast.makeText(requireContext(),"You have clicked Second Button",Toast.LENGTH_SHORT).show()
dialog.dismiss()
}
dialog.show()
}
}
//This will set the view and here view is our custom layout.
.setView(mview)
.setCancelable(true) // If we click anywhere outside the dialoge it will close.
.setBackground(resources.getDrawable(R.drawable.radius)) //Set BackGround color.
.create() //This will create the MaterialDialoge.
//Correct way, Here we are telling that this button is from custom layout. we are giving reference of button.
mview.btn_turnOnDeviceLoc.setOnClickListener {}
//InCorrect way, Here we are not providing the reference it will cost you an error.
btn_turnOnDeviceLoc.setOnClickListener {}
Kami akan mempelajari lebih banyak hal di Artikel mendatang. TETAP TERHUBUNG dan TERUS BELAJARāŗšš.
Jangan ragu untuk berkomentar jika Anda memiliki keraguan atau saran.
Juga, mari berteman di Twitter dan Instagram .
Terima kasihš.
Panduan langkah demi langkah untuk bermigrasi dari Dagger ke Hilt dalam project multi modul. Struktur Proyek Untuk memvisualisasikan proyek, ini berisi beberapa modul NetworkModule, StorageModule, CommonModule, FeatureModule dll.
Google baru-baru ini memperbarui aplikasi keyboard Gboard untuk Android untuk menyertakan tata letak baru yang dioptimalkan untuk tablet. Saya perhatikan pagi ini bahwa saya mengalami kesulitan mengetik, dan setelah pemeriksaan lebih lanjut, saya menyadari bahwa keyboard telah diganti.
Sudah lewat tengah malam ketika Hayden mengantar Katie kembali ke asrama kampusnya setelah malam mereka bersama. Dia memiliki kelas awal di pagi hari dan tidak memiliki pakaian ganti lagi setelah tinggal bersamanya malam sebelumnya.
Gatal di tangan saya menyebar saat saya menawar dengan penjual buku lokal untuk buku lain. Buku ini seperti buku lain yang saya beli baru-baru ini akan menemukan singgasananya di tumpukan 'To Be Read' (TBR) saya; namun, saya membelinya.
āHai Siri, mainkan Me Dive oleh Olivia Dean.ā Saat itu Mei 2021 ketika saya memulai magang di Future Academy Africa di Akure, Nigeria.
Itu mengejutkan saya Tiba-tiba seperti yang sering terjadi Ini di tengah jalan lagi <Tidak, bukan yang di pasifik Anda merah marun, yang di karnaval> Setiap berita utama kerasā¦..