├── .gitignore ├── .idea ├── .gitignore ├── .name ├── compiler.xml ├── gradle.xml ├── kotlinc.xml ├── misc.xml └── vcs.xml ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── dalemncy │ │ └── demo │ │ └── dialogs │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── dalemncy │ │ │ └── demo │ │ │ └── dialogs │ │ │ ├── MainActivity.kt │ │ │ └── dialogs │ │ │ └── CustomDialog.kt │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ ├── activity_main.xml │ │ └── dialog_custom.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── values-night │ │ └── themes.xml │ │ ├── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── themes.xml │ │ └── xml │ │ ├── backup_rules.xml │ │ └── data_extraction_rules.xml │ └── test │ └── java │ └── com │ └── dalemncy │ └── demo │ └── dialogs │ └── ExampleUnitTest.kt ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/caches 5 | /.idea/libraries 6 | /.idea/modules.xml 7 | /.idea/workspace.xml 8 | /.idea/navEditor.xml 9 | /.idea/assetWizardSettings.xml 10 | .DS_Store 11 | /build 12 | /captures 13 | .externalNativeBuild 14 | .cxx 15 | local.properties 16 | -------------------------------------------------------------------------------- /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | Dialogs Demo -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /.idea/kotlinc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 10 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Android Kotlin Dialogs 2 | 3 | This repository provides simple demos for various types of dialogs in Android using Kotlin. 4 | 5 | ## Contents 6 | 7 | 1. AlertDialog: A basic dialog example. 8 | 2. AlertDialog with Yes and No: Demonstrates a dialog with Yes and No options. 9 | 3. Date Picker: Shows how to implement a date picker. 10 | 4. Time Picker: Illustrates the implementation of a time picker. 11 | 5. Custom Dialog: A demonstration of creating a custom dialog using an XML layout. 12 | 13 | ## Getting Started 14 | 15 | Clone the repository and open it in Android Studio to explore and run the demo projects. 16 | 17 | ## Usage 18 | 19 | Each demo is self-contained and can be studied independently. Refer to the individual directories for specific implementations. 20 | 21 | ## Contributions 22 | 23 | Contributions are welcome! Feel free to raise issues and submit pull requests. 24 | 25 | ## License 26 | 27 | This project is licensed under the [MIT License](https://opensource.org/licenses/MIT). 28 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'com.android.application' 3 | id 'org.jetbrains.kotlin.android' 4 | } 5 | 6 | android { 7 | namespace 'com.dalemncy.demo.dialogs' 8 | compileSdk 34 9 | 10 | defaultConfig { 11 | applicationId "com.dalemncy.demo.dialogs" 12 | minSdk 24 13 | targetSdk 34 14 | versionCode 1 15 | versionName "1.0" 16 | 17 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 18 | } 19 | 20 | buildTypes { 21 | release { 22 | minifyEnabled false 23 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 24 | } 25 | } 26 | buildFeatures { 27 | viewBinding true 28 | } 29 | compileOptions { 30 | sourceCompatibility JavaVersion.VERSION_1_8 31 | targetCompatibility JavaVersion.VERSION_1_8 32 | } 33 | kotlinOptions { 34 | jvmTarget = '1.8' 35 | } 36 | } 37 | 38 | dependencies { 39 | 40 | implementation 'androidx.core:core-ktx:1.8.0' 41 | implementation 'androidx.appcompat:appcompat:1.6.1' 42 | implementation 'com.google.android.material:material:1.10.0' 43 | implementation 'androidx.constraintlayout:constraintlayout:2.1.4' 44 | testImplementation 'junit:junit:4.13.2' 45 | androidTestImplementation 'androidx.test.ext:junit:1.1.5' 46 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' 47 | } -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile -------------------------------------------------------------------------------- /app/src/androidTest/java/com/dalemncy/demo/dialogs/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.dalemncy.demo.dialogs 2 | 3 | import androidx.test.platform.app.InstrumentationRegistry 4 | import androidx.test.ext.junit.runners.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext 22 | assertEquals("com.dalemncy.demo.dialogs", appContext.packageName) 23 | } 24 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 15 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /app/src/main/java/com/dalemncy/demo/dialogs/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package com.dalemncy.demo.dialogs 2 | 3 | import android.app.DatePickerDialog 4 | import android.app.TimePickerDialog 5 | import androidx.appcompat.app.AppCompatActivity 6 | import android.os.Bundle 7 | import android.widget.DatePicker 8 | import android.widget.TimePicker 9 | import android.widget.Toast 10 | import androidx.appcompat.app.AlertDialog 11 | import com.dalemncy.demo.dialogs.databinding.ActivityMainBinding 12 | import com.dalemncy.demo.dialogs.dialogs.CustomDialog 13 | import java.util.Calendar 14 | 15 | class MainActivity : AppCompatActivity() { 16 | private lateinit var binding: ActivityMainBinding 17 | 18 | override fun onCreate(savedInstanceState: Bundle?) { 19 | super.onCreate(savedInstanceState) 20 | binding = ActivityMainBinding.inflate(layoutInflater) 21 | setContentView(binding.root) 22 | 23 | binding.simpleContentDialog.setOnClickListener { simpleContentDialog() } 24 | binding.yesOrNoDialog.setOnClickListener { yesOrNoDialog() } 25 | binding.datePickerDialog.setOnClickListener { datePickerDialog() } 26 | binding.timePickerDialog.setOnClickListener { timePickerDialog() } 27 | binding.customDialog.setOnClickListener { customDialog() } 28 | } 29 | 30 | private fun simpleContentDialog() { 31 | val dialogBuilder = AlertDialog.Builder(this) 32 | 33 | dialogBuilder.setMessage("This is a sample dialog.") 34 | .setCancelable(false) // Set this to true if you dialog can be closed via tapping outside. 35 | .setPositiveButton("Okay") { dialog, _ -> dialog.dismiss() } 36 | 37 | val alert = dialogBuilder.create() 38 | alert.setTitle("Sample Dialog") 39 | alert.show() 40 | } 41 | 42 | private fun yesOrNoDialog() { 43 | val builder = AlertDialog.Builder(this) 44 | builder.setTitle("Confirmation") 45 | builder.setMessage("Are you sure you want to proceed?") 46 | builder.setPositiveButton("Yes") { dialogInterface, _ -> 47 | // Handle the "Yes" response here 48 | Toast.makeText(this, "You said Yes", Toast.LENGTH_SHORT).show() 49 | dialogInterface.dismiss() 50 | } 51 | builder.setNegativeButton("No") { dialogInterface, _ -> 52 | // Handle the "No" response here 53 | Toast.makeText(this, "You said No", Toast.LENGTH_SHORT).show() 54 | dialogInterface.dismiss() 55 | } 56 | builder.setNeutralButton("Maybe") { dialogInterface, _ -> 57 | // Handle the "Maybe" response here 58 | Toast.makeText(this, "You said Maybe", Toast.LENGTH_SHORT).show() 59 | dialogInterface.dismiss() 60 | } 61 | builder.show() 62 | } 63 | 64 | private fun datePickerDialog() { 65 | val calendar = Calendar.getInstance() 66 | val year = calendar.get(Calendar.YEAR) 67 | val month = calendar.get(Calendar.MONTH) 68 | val day = calendar.get(Calendar.DAY_OF_MONTH) 69 | 70 | val datePickerDialog = DatePickerDialog(this, 71 | { _: DatePicker, selectedYear: Int, selectedMonth: Int, selectedDay: Int -> 72 | val selectedDate = "$selectedDay/${selectedMonth + 1}/$selectedYear" 73 | Toast.makeText(this, selectedDate, Toast.LENGTH_SHORT).show() 74 | }, year, month, day) 75 | 76 | datePickerDialog.show() 77 | } 78 | 79 | private fun timePickerDialog() { 80 | val calendar = Calendar.getInstance() 81 | val hour = calendar.get(Calendar.HOUR_OF_DAY) 82 | val minute = calendar.get(Calendar.MINUTE) 83 | 84 | val timePickerDialog = TimePickerDialog(this, 85 | { _: TimePicker, selectedHour: Int, selectedMinute: Int -> 86 | val selectedTime = String.format("%02d:%02d", selectedHour, selectedMinute) 87 | Toast.makeText(this, selectedTime, Toast.LENGTH_SHORT).show() 88 | }, hour, minute, true) 89 | 90 | timePickerDialog.show() 91 | } 92 | 93 | private fun customDialog() { 94 | val customDialog = CustomDialog(this) 95 | customDialog.show() 96 | } 97 | } -------------------------------------------------------------------------------- /app/src/main/java/com/dalemncy/demo/dialogs/dialogs/CustomDialog.kt: -------------------------------------------------------------------------------- 1 | package com.dalemncy.demo.dialogs.dialogs 2 | 3 | import android.app.Dialog 4 | import android.content.Context 5 | import android.os.Bundle 6 | import android.view.ViewGroup 7 | import android.view.Window 8 | import com.dalemncy.demo.dialogs.databinding.DialogCustomBinding 9 | 10 | class CustomDialog(context: Context) : Dialog(context) { 11 | 12 | private lateinit var binding: DialogCustomBinding 13 | 14 | override fun onCreate(savedInstanceState: Bundle?) { 15 | super.onCreate(savedInstanceState) 16 | binding = DialogCustomBinding.inflate(layoutInflater) 17 | setContentView(binding.root) 18 | makeFullWidth() 19 | 20 | binding.title.setText("This is the title") 21 | binding.save.setOnClickListener { 22 | // This closes the dialog. This is similar to "finish()" for activities. 23 | dismiss() 24 | } 25 | } 26 | 27 | private fun makeFullWidth() { 28 | val window: Window? = this.window 29 | window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) 30 | } 31 | } -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 15 | 18 | 21 | 22 | 23 | 24 | 30 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 13 | 14 |