Skip to content

Commit 66a6ebc

Browse files
committed
add : single choice dialog example added
1 parent 751bd89 commit 66a6ebc

5 files changed

Lines changed: 197 additions & 3 deletions

File tree

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
<activity android:name=".customview.CustomViewActivity" />
4848
<activity android:name=".animation.CrossFadeAnimationActivity" />
4949
<activity android:name=".animation.ShapeRotationActivity" />
50+
<activity android:name=".dialog.SingleChoiceDialogActivity" />
5051
</application>
5152

5253
</manifest>

app/src/main/java/com/mindorks/example/jetpack/compose/MainActivity.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import com.mindorks.example.jetpack.compose.card.CardExampleActivity
2626
import com.mindorks.example.jetpack.compose.clickable.ClickableExampleActivity
2727
import com.mindorks.example.jetpack.compose.customview.CustomViewActivity
2828
import com.mindorks.example.jetpack.compose.dialog.AlertDialogActivity
29+
import com.mindorks.example.jetpack.compose.dialog.SingleChoiceDialogActivity
2930
import com.mindorks.example.jetpack.compose.image.ImageActivity
3031
import com.mindorks.example.jetpack.compose.layout.*
3132
import com.mindorks.example.jetpack.compose.materialdesign.*
@@ -149,6 +150,12 @@ class MainActivity : AppCompatActivity() {
149150
buttonText = "Alert Dialog"
150151
)
151152
Divider(color = Color.Black)
153+
ButtonComponent(
154+
context = context,
155+
intent = Intent(context, SingleChoiceDialogActivity::class.java),
156+
buttonText = "Single Choice Dialog"
157+
)
158+
Divider(color = Color.Black)
152159
ButtonComponent(
153160
context = context,
154161
intent = Intent(context, MaterialAppBarActivity::class.java),
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
package com.mindorks.example.jetpack.compose.dialog
2+
3+
import android.os.Bundle
4+
import androidx.appcompat.app.AppCompatActivity
5+
import androidx.compose.foundation.Text
6+
import androidx.compose.foundation.layout.*
7+
import androidx.compose.foundation.lazy.LazyColumnFor
8+
import androidx.compose.foundation.selection.selectable
9+
import androidx.compose.foundation.shape.RoundedCornerShape
10+
import androidx.compose.material.*
11+
import androidx.compose.runtime.*
12+
import androidx.compose.ui.Modifier
13+
import androidx.compose.ui.graphics.Color
14+
import androidx.compose.ui.platform.setContent
15+
import androidx.compose.ui.text.style.TextAlign
16+
import androidx.compose.ui.unit.dp
17+
import androidx.compose.ui.window.Dialog
18+
import java.util.*
19+
20+
21+
//The state should be at the lower most common parent of the
22+
// composables, which are going to interact.
23+
val dialogState by lazy { mutableStateOf(false) }
24+
val selectedCountry by lazy { mutableStateOf("") }
25+
26+
val countriesList = getCountries().values.toList()
27+
28+
class SingleChoiceDialogActivity : AppCompatActivity() {
29+
override fun onCreate(savedInstanceState: Bundle?) {
30+
super.onCreate(savedInstanceState)
31+
setContent {
32+
CountriesDialog()
33+
SingleChoiceDialogActivityContent()
34+
}
35+
}
36+
}
37+
38+
@Composable
39+
fun SingleChoiceDialogActivityContent() {
40+
Column {
41+
Button(
42+
onClick = {
43+
dialogState.value = true
44+
},
45+
modifier = Modifier.padding(16.dp).fillMaxWidth(),
46+
shape = RoundedCornerShape(8.dp),
47+
backgroundColor = Color.Gray
48+
) {
49+
Text(
50+
text = "Show Countries List",
51+
textAlign = TextAlign.Center,
52+
color = Color.White
53+
)
54+
}
55+
Divider(color = Color.Black)
56+
Text(
57+
text = selectedCountry.value,
58+
textAlign = TextAlign.Center,
59+
color = Color.Black
60+
)
61+
}
62+
}
63+
64+
@Composable
65+
fun CountriesDialog() {
66+
SingleSelectDialog(
67+
dialogState = dialogState,
68+
title = "Choice your Country",
69+
optionsList = countriesList,
70+
submitButtonText = "Select",
71+
onSubmitButtonClick = { selectedCountry.value = countriesList[it] },
72+
onDismissRequest = { dialogState.value = false }
73+
)
74+
}
75+
76+
@Composable
77+
// Generally it's a good habit that we create 2 composables, one which accepts
78+
// state and one which maintain its own state. That way,
79+
// the developer has more control which approach he/she want to follow.
80+
fun SingleSelectDialog(
81+
dialogState: MutableState<Boolean>,
82+
title: String,
83+
optionsList: List<String>,
84+
defaultSelected: Int = -1,
85+
submitButtonText: String,
86+
onSubmitButtonClick: (Int) -> Unit,
87+
onDismissRequest: () -> Unit
88+
) {
89+
if (dialogState.value) {
90+
SingleSelectDialog(
91+
title = title,
92+
optionsList = optionsList,
93+
defaultSelected = defaultSelected,
94+
submitButtonText = submitButtonText,
95+
onSubmitButtonClick = onSubmitButtonClick,
96+
onDismissRequest = onDismissRequest
97+
)
98+
}
99+
}
100+
101+
@Composable
102+
fun SingleSelectDialog(
103+
title: String,
104+
optionsList: List<String>,
105+
defaultSelected: Int = -1,
106+
submitButtonText: String,
107+
onSubmitButtonClick: (Int) -> Unit,
108+
onDismissRequest: () -> Unit
109+
) {
110+
val selectedOption = mutableStateOf(defaultSelected)
111+
Dialog(onDismissRequest = { onDismissRequest.invoke() }) {
112+
Surface(
113+
modifier = Modifier.preferredWidth(300.dp),
114+
shape = RoundedCornerShape(10.dp)
115+
) {
116+
Column(modifier = Modifier.padding(10.dp)) {
117+
Text(text = title)
118+
Spacer(modifier = Modifier.preferredHeight(10.dp))
119+
LazyColumnFor(
120+
items = optionsList,
121+
modifier = Modifier.preferredHeight(500.dp)
122+
) {
123+
val selected = if (selectedOption.value == -1) {
124+
""
125+
} else {
126+
optionsList[selectedOption.value]
127+
}
128+
129+
RadioButton(it, selected) { selectedValue ->
130+
selectedOption.value = optionsList.indexOf(selectedValue)
131+
}
132+
}
133+
Spacer(modifier = Modifier.preferredHeight(10.dp))
134+
Button(
135+
onClick = {
136+
onSubmitButtonClick.invoke(selectedOption.value)
137+
onDismissRequest.invoke()
138+
},
139+
shape = MaterialTheme.shapes.medium
140+
) {
141+
Text(text = submitButtonText)
142+
}
143+
}
144+
145+
}
146+
}
147+
}
148+
149+
150+
@Composable
151+
fun RadioButton(text: String, selectedValue: String, onClickListener: (String) -> Unit) {
152+
Row(Modifier
153+
.fillMaxWidth()
154+
.selectable(
155+
selected = (text == selectedValue),
156+
onClick = {
157+
onClickListener(text)
158+
}
159+
)
160+
.padding(horizontal = 16.dp)
161+
) {
162+
//The Default Radio Button in Jetpack Compose doesn't accept text as an argument. So have Text Composable to show text.
163+
RadioButton(
164+
selected = (text == selectedValue),
165+
onClick = {
166+
onClickListener(text)
167+
}
168+
)
169+
Text(
170+
text = text,
171+
style = MaterialTheme.typography.body1.merge(),
172+
modifier = Modifier.padding(start = 16.dp)
173+
)
174+
}
175+
}
176+
177+
// Dummy Util function for the sake of example
178+
fun getCountries(): Map<String, String> {
179+
val countriesMap = hashMapOf<String, String>()
180+
val isoCountries = Locale.getISOCountries()
181+
isoCountries.forEach {
182+
val locale = Locale("", it)
183+
countriesMap[locale.country.toLowerCase(Locale.getDefault())] = locale.displayCountry
184+
}
185+
return countriesMap.toList().sortedBy { (_, value) -> value }.toMap()
186+
}

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ buildscript {
99
jcenter()
1010
}
1111
dependencies {
12-
classpath "com.android.tools.build:gradle:4.2.0-alpha14"
12+
classpath 'com.android.tools.build:gradle:4.2.0-alpha15'
1313
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1414

1515
// NOTE: Do not place your application dependencies here; they belong
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Sun Nov 01 17:34:25 IST 2020
1+
#Mon Nov 02 18:36:12 IST 2020
22
distributionBase=GRADLE_USER_HOME
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-rc-4-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-bin.zip
44
distributionPath=wrapper/dists
55
zipStorePath=wrapper/dists
66
zipStoreBase=GRADLE_USER_HOME

0 commit comments

Comments
 (0)