Skip to content

Instantly share code, notes, and snippets.

View mirokolodii's full-sized avatar

Myroslav Kolodii mirokolodii

View GitHub Profile
@mirokolodii
mirokolodii / SearchBox.kt
Created December 1, 2023 21:26
Search box with icons and edit text
@Composable
fun SearchBox(
onBackClick: () -> Unit,
) {
var searchText by remember { mutableStateOf("") }
Row(
horizontalArrangement = Arrangement.spaced(16.dp),
) {
Icon(
@mirokolodii
mirokolodii / CustomViewPreviewData.kt
Created November 19, 2023 07:22
Custom View preview data
private val previewItems = listOf(
mapOf(
"Dimension" to "34x12x8",
),
mapOf(
"Display" to "6.4 inches",
"Resolution" to "1080x2340"
),
mapOf(
"OS" to "Android 10",
@mirokolodii
mirokolodii / CustomViewPreview.kt
Last active November 19, 2023 07:47
Custom View preview
@Preview
@Composable
fun CustomViewPreview(
@PreviewParameter(ViewStateProvider::class) params: PreviewParams,
) {
AndroidView(
modifier = Modifier
.fillMaxWidth()
.background(Color.White)
.padding(8.dp),
@mirokolodii
mirokolodii / view_custom.xml
Last active November 15, 2023 07:46
Custom view layout
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/background_info_view"
android:padding="16dp"
tools:parentTag="androidx.constraintlayout.widget.ConstraintLayout"
>
@mirokolodii
mirokolodii / CustomView.kt
Last active November 15, 2023 07:38
Android custom view
class OrderView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null
) : ConstraintLayout(context, attrs) {
private val binding = ViewOrderBinding.inflate(LayoutInflater.from(context), this)
init {
setPadding(context.dpToPx(16))
background = context.drawable(R.drawable.background_info_view)
@mirokolodii
mirokolodii / Test.kt
Created October 24, 2019 06:57
Switch to Kotlin 2.9
val nonNullInt = 1
val nullInt: Int? = null
if (nullInt == nonNullInt) {
println("Both are equal")
} else {
println("Not equal") // Result is 'Not equal'
}
@mirokolodii
mirokolodii / Test.kt
Last active October 24, 2019 06:51
Switch to Kotlin #2.8
var nonNullInt = 1
val nullInt: Int? = null
// 1. Error
nonNullInt = nullInt // Error: type mismatch
// 2. Valid
if (nullInt != null) {
/* Here is used a smart cast (one of Kotlin compiler's feature),
which casts Int? to Int automatically */
nonNullInt = nullInt
}
@mirokolodii
mirokolodii / Test.java
Last active October 24, 2019 06:53
Switch to Kotlin #2.7
Integer nullInt = null;
if (someInt.equals(23)) { // NullPointerException because of attempt to call
// a method on a null
// Rest of code
}
@mirokolodii
mirokolodii / Test.java
Created October 18, 2019 07:00
Switch to Kotlin #2.6
Integer intObj = null;
int primitiveInt;
primitiveInt = intObj; // NullPointerException during unboxing
@mirokolodii
mirokolodii / Test.kt
Created October 18, 2019 06:35
Switch to Kotlin #2.5
var nullableStr: String? = null
var nonNullStr: String = "a string"
nullableStr = nonNullStr // valid
nonNullStr = nullableStr // error, should be checked for nullability first
If (nullableStr != null) {
nonNullStr = nullableStr // valid
}