Skip to content

Instantly share code, notes, and snippets.

@ercnksgl
ercnksgl / Customize.kt
Last active September 14, 2022 11:55
Customize Object
import android.content.Context
import androidx.annotation.ColorRes
import androidx.core.content.ContextCompat
/**
* Set font types and color to customize text.
*/
data class Customize(
var hexColor: Int? = null,
val isItalic: Boolean = false,
@ercnksgl
ercnksgl / TextCustomizer.kt
Last active September 15, 2022 07:34
Text customizer
import android.text.Spanned
import androidx.core.text.HtmlCompat
class TextCustomizer {
class Builder(private val allText: String) {
private val parts = arrayListOf<Pair<String, Customize>>()
fun addTextToReplace(part: String, customize: Customize)= apply {
@ercnksgl
ercnksgl / activity_main.xml
Created September 15, 2022 18:43
Custom component MainActivity
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="match_parent"
tools:context="com.ercnksgl.customcomponent.MainActivity">
<com.ercnksgl.customcomponent.custom.CustomEditText
android:id="@+id/customEditText"
@ercnksgl
ercnksgl / attrs.xml
Created September 15, 2022 18:48
Custom component attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CustomTextView">
<attr name="android:title" />
<attr name="startIcon" format="reference"/>
<attr name="customEditTextStartIconStyle" format="reference" />
<attr name="customEditTextStyle" format="reference" />
</declare-styleable>
</resources>
@ercnksgl
ercnksgl / themes.xml
Created September 15, 2022 18:54
Custom component themes.xml
<resources xmlns:tools="http://schemas.android.com/tools">
.
.
.
.
<style name="EditTextStyle" parent="Widget.AppCompat.EditText">
<item name="android:background">@drawable/bg_edittext</item>
<item name="android:textColor">@color/color_red_300</item>
@ercnksgl
ercnksgl / custom_edittext_layout
Created September 15, 2022 18:59
Custom component edittext 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"
tools:layout_height="wrap_content"
tools:layout_width="match_parent"
tools:parentTag="androidx.constraintlayout.widget.ConstraintLayout">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/titleTextView"
@ercnksgl
ercnksgl / CustomEditText.kt
Created September 15, 2022 19:06
Custom component CustomEditText
package com.ercnksgl.customcomponent.custom
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.Space
import androidx.annotation.DrawableRes
import androidx.appcompat.widget.AppCompatEditText
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
@ercnksgl
ercnksgl / DateUtilities.kt
Last active September 16, 2022 08:48
Relative time differences function
fun getRelativeTimeDifference(resources: Resources, time: LocalDateTime): String {
val now = LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()
val timeDifference = now - time.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()
return when {
timeDifference < DateUtils.MINUTE_IN_MILLIS -> resources.getString(R.string.just_now)
timeDifference < DateUtils.HOUR_IN_MILLIS -> {
val minute = (timeDifference / DateUtils.MINUTE_IN_MILLIS).toInt()
resources.getString(R.string.minutes_ago, minute)
}
timeDifference < DateUtils.DAY_IN_MILLIS -> {
@ercnksgl
ercnksgl / MessageType.kt
Created September 18, 2022 11:11
The enum to customize the view.
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import com.ercnksgl.enumtestapp.R
enum class MessageType(
@StringRes val titleResId: Int,
@DrawableRes val iconResId: Int,
@ColorRes val cardColorResId: Int,
@ColorRes val titleColorResId: Int
@ercnksgl
ercnksgl / Result.kt
Created September 19, 2022 18:12
Sealed Result Object
import com.ercnksgl.sealedtest.data.network.response.ApiError
sealed class Result<out T : Any> {
data class Success<out T : Any>(val data: T) : Result<T>()
data class Error(val error: ApiError) : Result<Nothing>()
}