Skip to content

Instantly share code, notes, and snippets.

View diegohkd's full-sized avatar

Diego Almeida de Oliveira diegohkd

View GitHub Profile
@diegohkd
diegohkd / City.kt
Last active April 16, 2020 13:29
Spinner with dropdown items that can be disabled and then not selectable
data class City(val name: String, val isEnabled: Boolean) {
override fun toString(): String {
return name
}
}
@diegohkd
diegohkd / MainActivity.kt
Last active April 11, 2020 22:11
Toolbar menu item with animation for notifications counter using Action View. Sample project: https://github.com/diegohkd/AndroidKotlinLab/tree/master/ToolbarMenuItemWithAnimation
class MainActivity : AppCompatActivity() {
...
private var notificationsActionView: NotificationsActionView? = null
...
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.main_menu, menu)
return true
}
override fun onPrepareOptionsMenu(menu: Menu?): Boolean {
@diegohkd
diegohkd / DialogFragment+withToolbar.kt
Last active April 10, 2020 14:17
DialogFragment with toolbar and a menu in it
class MyDialogFragment : DialogFragment() {
...
private fun setupToolbar() {
setupToolbarCloseButton(toolbar, R.string.my_dialog_title)
setupToolbarMenu()
}
private fun setupToolbarMenu() {
toolbar.inflateMenu(R.menu.menu_my_dialog)
toolbar.setOnMenuItemClickListener { menuItem ->
@diegohkd
diegohkd / DialogFragment+slideAnimation.kt
Created April 10, 2020 14:09
DialogFragment with slide animations. Since the animation is applied again if Activity resumes again after showing this dialog, here there's a workaround to remove enter animation after it already happened once
...
import androidx.fragment.app.DialogFragment
import android.os.Handler
...
class MyDialogFragment : DialogFragment() {
...
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setupSlideFromTopAnimation()
@diegohkd
diegohkd / DialogFragment+FullscreenWithCustomStatusBarColor.kt
Last active April 25, 2024 13:04
Show DialogFragment in full screen with status bar, change status bar background color and update status bar components color depending on background color to make them more readable
...
import androidx.fragment.app.DialogFragment
...
class MyDialogFragmentDialog : DialogFragment() {
...
private val statusBarColorRes = R.color.colorPrimaryDark
private var previousSystemUiVisibility: Int? = null
...
override fun getTheme(): Int = R.style.FullscreenDialog
...
@diegohkd
diegohkd / DialogFragment+FullWidth.kt
Last active July 4, 2022 12:21
Show DialogFragment with full width set
fun DialogFragment.setupWidthToMatchParent() {
dialog?.window?.setLayout(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
}
@diegohkd
diegohkd / DialogFragment+GravityTop.kt
Last active April 10, 2020 13:40
Show DialogFragment at the top of screen
fun DialogFragment.setupToShowAtTheTop() {
dialog?.window?.setGravity(Gravity.CENTER_HORIZONTAL or Gravity.TOP)
}
@diegohkd
diegohkd / android_draw_ui.md
Last active April 28, 2020 10:44
This is a simple code to help understand how Android draws the UI
<?xml version="1.0" encoding="utf-8"?>
<example.com.exampledrawview.CustomLinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

    <example.com.exampledrawview.CustomTextView