-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #213 from myofficework000/poll_ui_demo
added poll ui demo
- Loading branch information
Showing
5 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
app/src/main/java/com/example/jetpack_compose_all_in_one/demos/polls/PollScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.example.jetpack_compose_all_in_one.demos.polls | ||
|
||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material3.Button | ||
import androidx.compose.material3.RadioButton | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.livedata.observeAsState | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.text.TextStyle | ||
import androidx.compose.ui.text.font.FontWeight | ||
import androidx.compose.ui.unit.dp | ||
import androidx.compose.ui.unit.sp | ||
import androidx.constraintlayout.compose.ConstraintLayout | ||
|
||
@Composable | ||
fun PollScreen(viewModel: QuizViewModel) { | ||
val radioOptions = listOf("Mango","Banana","Apple","Peach") | ||
var selectedOption by remember { mutableStateOf(radioOptions[0]) } | ||
val attemptedTime = viewModel.attemptedTime.observeAsState() | ||
|
||
ConstraintLayout(modifier = Modifier.fillMaxSize()) { | ||
val (questionStatement, attemptTime, radioGroup, submitButton) = createRefs() | ||
|
||
Text(text = "Question: Question statement here", | ||
modifier = Modifier | ||
.padding(8.dp) | ||
.constrainAs(questionStatement) { | ||
top.linkTo(parent.top) | ||
start.linkTo(parent.start) | ||
}, | ||
style = TextStyle(fontWeight = FontWeight.Bold, fontSize = 20.sp) | ||
) | ||
|
||
Text(text = "attempted ${attemptedTime.value} times", | ||
modifier = Modifier | ||
.padding(5.dp) | ||
.constrainAs(attemptTime) { | ||
top.linkTo(questionStatement.bottom) | ||
end.linkTo(parent.end) | ||
}) | ||
|
||
Column(modifier = Modifier.constrainAs(radioGroup){ | ||
top.linkTo(attemptTime.bottom, 20.dp) | ||
start.linkTo(parent.start,20.dp) | ||
}) { | ||
radioOptions.forEach { fruitName -> | ||
Row(verticalAlignment = Alignment.CenterVertically) { | ||
RadioButton( | ||
selected = (fruitName == selectedOption), | ||
onClick = { selectedOption = fruitName } | ||
) | ||
Text( | ||
text = fruitName, | ||
modifier = Modifier.padding(start = 8.dp) | ||
) | ||
} | ||
} | ||
} | ||
Button(onClick = { viewModel.submitPoll() }, modifier = Modifier.constrainAs(submitButton){ | ||
top.linkTo(radioGroup.bottom,50.dp) | ||
start.linkTo(parent.start) | ||
end.linkTo(parent.end) | ||
}) { | ||
Text("Submit") | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
app/src/main/java/com/example/jetpack_compose_all_in_one/demos/polls/QuizViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.example.jetpack_compose_all_in_one.demos.polls | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class QuizViewModel @Inject constructor():ViewModel() { | ||
|
||
private var _attemptedTime = MutableLiveData(0) | ||
var attemptedTime:LiveData<Int> = _attemptedTime | ||
|
||
fun submitPoll() = _attemptedTime.postValue(_attemptedTime.value!!+1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters