Skip to content

Instantly share code, notes, and snippets.

View navczydev's full-sized avatar
💭
Mobile Dev

Nav Singh navczydev

💭
Mobile Dev
View GitHub Profile
with(binding) {
rgChoices.setOnCheckedChangeListener { _, index ->
Toast.makeText(
this@MainActivity,
"Checked index $index",
Toast.LENGTH_SHORT
).show()
when (index) {
R.id.rb_choice1 -> {
<RadioGroup
android:id="@+id/rg_choices"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/textView">
<RadioButton
android:id="@+id/rb_choice1"
// rgChoices - RadioGroup's id
binding.rgChoices.clearCheck()
binding.rgChoices.check(-1) // -1 to clear the selection
val clipboardManager = context.getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
// When setting the clip board text.
val clipData = ClipData.newPlainText(/* label = */ "Sample copy data",/* text = */ context.getString(R.string.clip_preview))
.apply {
description.extras = PersistableBundle().apply {
// only available for Android13 or higher
putBoolean(ClipDescription.EXTRA_IS_SENSITIVE, true)
// use raw string for older versions
// android.content.extra.IS_SENSITIVE
}
<queries>
<intent>
<action android:name="android.media.action.IMAGE_CAPTURE" />
</intent>
</queries>
private fun launchCameraIntent() {
val takePhotoIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
// Ensure that there's a camera activity to handle the intent
// Android11 or higher needs the <query> tag to be added in the Android Manifest file.
// It depend on the device, if by-default camera package is visible or not
takePhotoIntent.resolveActivity(packageManager)?.also {
// Create the File where the photo should go
val photoFile: File? = try {
createImageFile()
} catch (ex: IOException) {
adb shell dumpsys package queries
// create ActivityResultContract
val getContent =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
result?.let { nonNullableResult ->
when (nonNullableResult.resultCode == RESULT_OK) {
true -> {
val intent = nonNullableResult.data
intent?.data?.let { uri ->
// process data
}
@Composable
fun PhotoPickerResultComposable() {
var result by rememberSaveable { mutableStateOf<Uri?>(null) }
val launcher =
rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) {
result = it.data?.data
}
Column(
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
Intent(MediaStore.ACTION_PICK_IMAGES).apply {
type = "image/*"
// only videos
type = "video/*"
}