Slide 1

Slide 1 text

Android Puzzlers Traps, Pitfalls and Corner Cases @cyrilmottier ? ? ? ? ? ? ? ? ?

Slide 2

Slide 2 text

It all began with…

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Small applications with curious behavior Android puzzlers

Slide 5

Slide 5 text

Small applications with curious behavior Android puzzlers 1. What does it print?

Slide 6

Slide 6 text

Small applications with curious behavior Android puzzlers 1. What does it print? 2. The solution revealed

Slide 7

Slide 7 text

Small applications with curious behavior Android puzzlers 1. What does it print? 2. The solution revealed 3. The moral

Slide 8

Slide 8 text

A simple setup apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' android { compileSdkVersion 28 defaultConfig { minSdkVersion 28 targetSdkVersion 28 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt') } } } dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.21" } common.gradle

Slide 9

Slide 9 text

A simple setup AndroidManifest.xml

Slide 10

Slide 10 text

A simple setup minSdkVersion 28 targetSdkVersion 28 compileSdkVersion 28 Running on Android 9 (Pie) / API 28 Warnings/errors/lint turned off Focusing on vanilla-Android

Slide 11

Slide 11 text

Inflation result

Slide 12

Slide 12 text

MainActivity.kt layout/view_inflate.xml class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val parent = FrameLayout(this).apply { tag = "parent" } val result1 = layoutInflater.inflate(R.layout.view_inflate, parent) val result2 = layoutInflater.inflate(R.layout.view_inflate, null) trace("${result1.tag} and ${result2.tag}") } }

Slide 13

Slide 13 text

MainActivity.kt layout/view_inflate.xml class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val parent = FrameLayout(this).apply { tag = "parent" } val result1 = layoutInflater.inflate(R.layout.view_inflate, parent) val result2 = layoutInflater.inflate(R.layout.view_inflate, null) trace("${result1.tag} and ${result2.tag}") } }

Slide 14

Slide 14 text

A child and child class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val parent = FrameLayout(this).apply { tag = "parent" } val result1 = layoutInflater.inflate(R.layout.view_inflate, parent) val result2 = layoutInflater.inflate(R.layout.view_inflate, null) trace("${result1.tag} and ${result2.tag}") } } MainActivity.kt layout/view_inflate.xml

Slide 15

Slide 15 text

B parent and child A child and child class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val parent = FrameLayout(this).apply { tag = "parent" } val result1 = layoutInflater.inflate(R.layout.view_inflate, parent) val result2 = layoutInflater.inflate(R.layout.view_inflate, null) trace("${result1.tag} and ${result2.tag}") } } MainActivity.kt layout/view_inflate.xml

Slide 16

Slide 16 text

B parent and child C throws NullPointerException A child and child class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val parent = FrameLayout(this).apply { tag = "parent" } val result1 = layoutInflater.inflate(R.layout.view_inflate, parent) val result2 = layoutInflater.inflate(R.layout.view_inflate, null) trace("${result1.tag} and ${result2.tag}") } } MainActivity.kt layout/view_inflate.xml

Slide 17

Slide 17 text

B parent and child C throws NullPointerException D Answer D A child and child class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val parent = FrameLayout(this).apply { tag = "parent" } val result1 = layoutInflater.inflate(R.layout.view_inflate, parent) val result2 = layoutInflater.inflate(R.layout.view_inflate, null) trace("${result1.tag} and ${result2.tag}") } } MainActivity.kt layout/view_inflate.xml

Slide 18

Slide 18 text

B parent and child C throws NullPointerException D Answer D A child and child class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val parent = FrameLayout(this).apply { tag = "parent" } val result1 = layoutInflater.inflate(R.layout.view_inflate, parent) val result2 = layoutInflater.inflate(R.layout.view_inflate, null) trace("${result1.tag} and ${result2.tag}") } } MainActivity.kt layout/view_inflate.xml

Slide 19

Slide 19 text

attaches child to parent & returns parent null null parent parent returns child root returns child attaches child to parent & returns parent Output

Slide 20

Slide 20 text

Favor the 3-args inflate method instead fun LayoutInflater.inflate( resource: Int, root: ViewGroup, attachToRoot: Boolean ): View Be explicit

Slide 21

Slide 21 text

null null parent parent false true false true returns child root attachToRoot returns child returns child attaches child to parent & returns parent Output

Slide 22

Slide 22 text

null null parent parent false true false true returns child root attachToRoot returns child returns child attaches child to parent & returns parent Output

Slide 23

Slide 23 text

Padding precedence

Slide 24

Slide 24 text

MainActivity.kt class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) trace( "start=${text.paddingStart} top=${text.paddingTop} " + "end=${text.paddingEnd} bottom=${text.paddingBottom}" ) } }

Slide 25

Slide 25 text

MainActivity.kt class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) trace( "start=${text.paddingStart} top=${text.paddingTop} " + "end=${text.paddingEnd} bottom=${text.paddingBottom}" ) } } layout/activity_main.xml

Slide 26

Slide 26 text

MainActivity.kt layout/activity_main.xml drawable/padded_rect.xml class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) trace( "start=${text.paddingStart} top=${text.paddingTop} " + "end=${text.paddingEnd} bottom=${text.paddingBottom}" ) } }

Slide 27

Slide 27 text

class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) trace( "start=${text.paddingStart} top=${text.paddingTop} " + "end=${text.paddingEnd} bottom=${text.paddingBottom}" ) } } B start=9 top=5 end=10 bottom=6 C start=2 top=4 end=3 bottom=1 D start=11 top=5 end=7 bottom=5 A start=5 top=5 end=5 bottom=5 MainActivity.kt layout/activity_main.xml drawable/padded_rect.xml

Slide 28

Slide 28 text

B start=9 top=5 end=10 bottom=6 C start=2 top=4 end=3 bottom=1 D start=11 top=5 end=7 bottom=5 A start=5 top=5 end=5 bottom=5 MainActivity.kt class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) trace( "start=${text.paddingStart} top=${text.paddingTop} " + "end=${text.paddingEnd} bottom=${text.paddingBottom}" ) } } layout/activity_main.xml drawable/padded_rect.xml

Slide 29

Slide 29 text

background Drawable padding supportsRtl = false Lower Lower Higher

Slide 30

Slide 30 text

background Drawable padding padding[Start|End] Lower Lower Higher supportsRtl = false

Slide 31

Slide 31 text

background Drawable padding padding[Start|End] Lower Lower Higher padding[Left|Top|Right|Bottom] supportsRtl = false

Slide 32

Slide 32 text

padding[Left|Top|Right|Bottom] padding[Horizontal|Vertical] background Drawable padding padding[Start|End] Lower Lower Higher supportsRtl = false

Slide 33

Slide 33 text

padding[Left|Top|Right|Bottom] padding[Horizontal|Vertical] padding background Drawable padding padding[Start|End] Lower Lower Higher supportsRtl = false

Slide 34

Slide 34 text

padding[Left|Top|Right|Bottom] padding[Horizontal|Vertical] padding background Drawable padding padding[Start|End] padding[Left|Top|Right|Bottom] padding[Horizontal|Vertical] padding padding[Start|End] background Drawable padding supportsRtl = true Lower Higher supportsRtl = false

Slide 35

Slide 35 text

Activity lifecycle

Slide 36

Slide 36 text

MainActivity.kt class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) trace("onCreate") finish() trace("finish") } override fun onStart() { super.onStart() trace("onStart") } override fun onResume() { super.onResume() trace("onResume") } override fun onPause() { trace("onPause") super.onPause() } override fun onStop() { trace("onStop") super.onStop() } override fun onDestroy() { trace("onDestroy") super.onDestroy() } }

Slide 37

Slide 37 text

B onCreate, finish, onStart, onResume, onPause, onStop, onDestroy C onCreate, onStart, onResume, onPause, onStop, onDestroy D onCreate, finish, onDestroy A onCreate MainActivity.kt class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) trace("onCreate") finish() trace("finish") } override fun onStart() { super.onStart() trace("onStart") } override fun onResume() { super.onResume() trace("onResume") } override fun onPause() { trace("onPause") super.onPause() } override fun onStop() { trace("onStop") super.onStop() } override fun onDestroy() { trace("onDestroy") super.onDestroy() } }

Slide 38

Slide 38 text

B onCreate, finish, onStart, onResume, onPause, onStop, onDestroy C onCreate, onStart, onResume, onPause, onStop, onDestroy D onCreate, finish, onDestroy A onCreate MainActivity.kt class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) trace("onCreate") finish() trace("finish") } override fun onStart() { super.onStart() trace("onStart") } override fun onResume() { super.onResume() trace("onResume") } override fun onPause() { trace("onPause") super.onPause() } override fun onStop() { trace("onStop") super.onStop() } override fun onDestroy() { trace("onDestroy") super.onDestroy() } }

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

“The following diagram shows the important state paths of an Activity. – d.android.com

Slide 41

Slide 41 text

“The following diagram shows the important state paths of an Activity. – d.android.com

Slide 42

Slide 42 text

Drawable tags

Slide 43

Slide 43 text

MainActivity.kt class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val classNames = arrayOf( R.drawable.level_list, R.drawable.layer_list, R.drawable.shape ).map { getDrawable(it)::class.java.simpleName } trace(classNames.joinToString(", ")) } }

Slide 44

Slide 44 text

MainActivity.kt drawable/level_list.xml class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val classNames = arrayOf( R.drawable.level_list, R.drawable.layer_list, R.drawable.shape ).map { getDrawable(it)::class.java.simpleName } trace(classNames.joinToString(", ")) } }

Slide 45

Slide 45 text

MainActivity.kt drawable/level_list.xml drawable/layer_list.xml class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val classNames = arrayOf( R.drawable.level_list, R.drawable.layer_list, R.drawable.shape ).map { getDrawable(it)::class.java.simpleName } trace(classNames.joinToString(", ")) } }

Slide 46

Slide 46 text

MainActivity.kt drawable/level_list.xml drawable/layer_list.xml drawable/shape.xml class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val classNames = arrayOf( R.drawable.level_list, R.drawable.layer_list, R.drawable.shape ).map { getDrawable(it)::class.java.simpleName } trace(classNames.joinToString(", ")) } }

Slide 47

Slide 47 text

B LevelDrawable, LayerListDrawable, ShapeDrawable C LevelListDrawable, LayerDrawable, GradientDrawable D throws InflateException A LevelListDrawable, LayerListDrawable, ShapeDrawable MainActivity.kt drawable/level_list.xml drawable/layer_list.xml drawable/shape.xml class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val classNames = arrayOf( R.drawable.level_list, R.drawable.layer_list, R.drawable.shape ).map { getDrawable(it)::class.java.simpleName } trace(classNames.joinToString(", ")) } }

Slide 48

Slide 48 text

B LevelDrawable, LayerListDrawable, ShapeDrawable C LevelListDrawable, LayerDrawable, GradientDrawable D throws InflateException A LevelListDrawable, LayerListDrawable, ShapeDrawable MainActivity.kt drawable/level_list.xml drawable/layer_list.xml drawable/shape.xml class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val classNames = arrayOf( R.drawable.level_list, R.drawable.layer_list, R.drawable.shape ).map { getDrawable(it)::class.java.simpleName } trace(classNames.joinToString(", ")) } }

Slide 49

Slide 49 text

The general rule ValarMorghulisDrawable

Slide 50

Slide 50 text

The general rule

Slide 51

Slide 51 text

The general rule LevelListDrawable TransitionDrawable RippleDrawable AdaptiveIconDrawable ColorDrawable VectorDrawable AnimatedVectorDrawable ScaleDrawable ClipDrawable RotateDrawable InsetDrawable BitmapDrawable NinePatchDrawable AnimatedImageDrawable xceptions

Slide 52

Slide 52 text

Exceptions The general rule

Slide 53

Slide 53 text

Exceptions StateListDrawable AnimatedStateListDrawable LayerDrawable AnimationDrawable GradientDrawable

Slide 54

Slide 54 text

Exceptions StateListDrawable AnimatedStateListDrawable LayerDrawable AnimationDrawable GradientDrawable ShapeDrawable PaintDrawable PictureDrawable

Slide 55

Slide 55 text

Exceptions StateListDrawable AnimatedStateListDrawable LayerDrawable AnimationDrawable GradientDrawable ShapeDrawable PaintDrawable PictureDrawable

Slide 56

Slide 56 text

Exceptions StateListDrawable AnimatedStateListDrawable LayerDrawable AnimationDrawable GradientDrawable ShapeDrawable PaintDrawable PictureDrawable YourDrawable

Slide 57

Slide 57 text

Fragment lifecycle

Slide 58

Slide 58 text

TracingFragment.kt class TracingFragment : Fragment() { override fun onAttach(context: Context?) { super.onAttach(context) trace("onAttach") } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) trace("onCreate") } override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { trace("onCreateView") return super.onCreateView(inflater, container, savedInstanceState) } override fun onDestroyView() { trace("onDestroyView") super.onDestroyView() } override fun onDestroy() { trace("onDestroy") super.onDestroy() } override fun onDetach() { trace("onDetach") super.onDetach() } }

Slide 59

Slide 59 text

MainActivity.kt class MainActivity : FragmentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val fragment = TracingFragment() supportFragmentManager.beginTransaction() .add(android.R.id.content, fragment, "fragment:puzzler") .commit() supportFragmentManager.beginTransaction() .detach(fragment) .commit() } override fun onSaveInstanceState(outState: Bundle?) { // For the simplicity of this puzzler, // let’s consider state is never ever saved } }

Slide 60

Slide 60 text

B onAttach, onCreate, onCreateView, onDestroyView C onCreate, onDestroy D Nothing A onAttach, onCreate, onCreateView, onDestroyView, onDestroy, onDetach MainActivity.kt class MainActivity : FragmentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val fragment = TracingFragment() supportFragmentManager.beginTransaction() .add(android.R.id.content, fragment, "fragment:puzzler") .commit() supportFragmentManager.beginTransaction() .detach(fragment) .commit() } override fun onSaveInstanceState(outState: Bundle?) { // For the simplicity of this puzzler, // let’s consider state is never ever saved } }

Slide 61

Slide 61 text

B onAttach, onCreate, onCreateView, onDestroyView C onCreate, onDestroy D Nothing A onAttach, onCreate, onCreateView, onDestroyView, onDestroy, onDetach MainActivity.kt class MainActivity : FragmentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val fragment = TracingFragment() supportFragmentManager.beginTransaction() .add(android.R.id.content, fragment, "fragment:puzzler") .commit() supportFragmentManager.beginTransaction() .detach(fragment) .commit() } override fun onSaveInstanceState(outState: Bundle?) { // For the simplicity of this puzzler, // let’s consider state is never ever saved } }

Slide 62

Slide 62 text

add remove/replace attach/detach the Fragment and more… creates/destroys the Fragment’s associated View turns Fragment’s View visibility to GONE/VISIBLE Fragment actions attach detach show hide FragmentTransaction commands

Slide 63

Slide 63 text

Notification channels

Slide 64

Slide 64 text

MainActivity.kt private const val CHANNEL_ID = "channel" class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val manager = getSystemService(NotificationManager::class.java) val aChannel = NotificationChannel( CHANNEL_ID, // id "A name", // name NotificationManager.IMPORTANCE_DEFAULT // importance (3) ) aChannel.lightColor = Color.RED manager.createNotificationChannel(aChannel) manager.deleteNotificationChannel(CHANNEL_ID) val anotherChannel = NotificationChannel( CHANNEL_ID, // id "Another name", // name NotificationManager.IMPORTANCE_HIGH // importance (4) ) anotherChannel.lightColor = Color.GREEN manager.createNotificationChannel(anotherChannel) with(manager.getNotificationChannel(CHANNEL_ID)) { trace("$name, $importance, 0x${Integer.toHexString(lightColor)}") } } }

Slide 65

Slide 65 text

MainActivity.kt private const val CHANNEL_ID = "channel" class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val manager = getSystemService(NotificationManager::class.java) val aChannel = NotificationChannel( CHANNEL_ID, // id "A name", // name NotificationManager.IMPORTANCE_DEFAULT // importance (3) ) aChannel.lightColor = Color.RED manager.createNotificationChannel(aChannel) manager.deleteNotificationChannel(CHANNEL_ID) val anotherChannel = NotificationChannel( CHANNEL_ID, // id "Another name", // name NotificationManager.IMPORTANCE_HIGH // importance (4) ) anotherChannel.lightColor = Color.GREEN manager.createNotificationChannel(anotherChannel) with(manager.getNotificationChannel(CHANNEL_ID)) { trace("$name, $importance, 0x${Integer.toHexString(lightColor)}") } } } B Another name, 4, 0xff00ff00 C Another name, 3, 0xff00ff00 D Another name, 3, 0xffff0000 A A name, 3, 0xffff0000

Slide 66

Slide 66 text

MainActivity.kt B Another name, 4, 0xff00ff00 C Another name, 3, 0xff00ff00 D Another name, 3, 0xffff0000 A A name, 3, 0xffff0000 private const val CHANNEL_ID = "channel" class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val manager = getSystemService(NotificationManager::class.java) val aChannel = NotificationChannel( CHANNEL_ID, // id "A name", // name NotificationManager.IMPORTANCE_DEFAULT // importance (3) ) aChannel.lightColor = Color.RED manager.createNotificationChannel(aChannel) manager.deleteNotificationChannel(CHANNEL_ID) val anotherChannel = NotificationChannel( CHANNEL_ID, // id "Another name", // name NotificationManager.IMPORTANCE_HIGH // importance (4) ) anotherChannel.lightColor = Color.GREEN manager.createNotificationChannel(anotherChannel) with(manager.getNotificationChannel(CHANNEL_ID)) { trace("$name, $importance, 0x${Integer.toHexString(lightColor)}") } } }

Slide 67

Slide 67 text

fun NotificationManager .deleteNotificationChannel( channelId: String )

Slide 68

Slide 68 text

It rather “hides”: the only possible deletion is app uninstall Doesn’t really delete fun NotificationManager .deleteNotificationChannel( channelId: String )

Slide 69

Slide 69 text

Name/description Always modifiable Mainly for i18n purposes & user-defined names/descriptions.

Slide 70

Slide 70 text

Name/description Importance Always modifiable Mainly for i18n purposes & user-defined names/descriptions. Modifiable under certain conditions Only if new importance is lower than the current one & none of the channel’s settings have been altered by the user.

Slide 71

Slide 71 text

Name/description Importance Group Always modifiable Mainly for i18n purposes & user-defined names/descriptions. Modifiable under certain conditions Modifiable under certain conditions Only if the channel has no group yet. Only if new importance is lower than the current one & none of the channel’s settings have been altered by the user.

Slide 72

Slide 72 text

Name/description Importance Group Other fields Always modifiable Mainly for i18n purposes & user-defined names/descriptions. Modifiable under certain conditions Only if new importance is lower than the current one & none of the channel’s settings have been altered by the user. Modifiable under certain conditions Only if the channel has no group yet. Never Changing seamlessly user-alterable settings would result in a bad UX.

Slide 73

Slide 73 text

Preferences read/write

Slide 74

Slide 74 text

MainActivity.kt class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val prefs = getSharedPreferences("test", Context.MODE_PRIVATE) prefs.edit() .putString("key", "hell") .apply() prefs.edit() .putString("key", null) .apply() val value = prefs.getString("key", "heaven") trace("Go to $value!") } }

Slide 75

Slide 75 text

B Go to hell! C Go to null! D Either A, B or C A Go to heaven! MainActivity.kt class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val prefs = getSharedPreferences("test", Context.MODE_PRIVATE) prefs.edit() .putString("key", "hell") .apply() prefs.edit() .putString("key", null) .apply() val value = prefs.getString("key", "heaven") trace("Go to $value!") } }

Slide 76

Slide 76 text

B Go to hell! C Go to null! D Either A, B or C A Go to heaven! MainActivity.kt class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val prefs = getSharedPreferences("test", Context.MODE_PRIVATE) prefs.edit() .putString("key", "hell") .apply() prefs.edit() .putString("key", null) .apply() val value = prefs.getString("key", "heaven") trace("Go to $value!") } }

Slide 77

Slide 77 text

apply() is atomic async to disk

Slide 78

Slide 78 text

async to disk apply() is atomic sync to memory

Slide 79

Slide 79 text

sync to memory async to disk apply() is

Slide 80

Slide 80 text

No content

Slide 81

Slide 81 text

“ defValue - String: Value to return if this preference does not exist.

Slide 82

Slide 82 text

¯\_(ツ)_/¯ Well… not exactly

Slide 83

Slide 83 text

Null values are always considered as “not existing” so expect “defValue”

Slide 84

Slide 84 text

LinearLayout weight

Slide 85

Slide 85 text

MainActivity.kt class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) linear_layout.addOnLayoutChangeListener { v, _, _, _, _, _, _, _, _ -> trace( "LL: ${linear_layout.width}px, " + "TV1: ${text_view_1.width}px, " + "TV2: ${text_view_2.width}px" ) } } }

Slide 86

Slide 86 text

layout/activity_main.xml MainActivity.kt class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) linear_layout.addOnLayoutChangeListener { v, _, _, _, _, _, _, _, _ -> trace( "LL: ${linear_layout.width}px, " + "TV1: ${text_view_1.width}px, " + "TV2: ${text_view_2.width}px" ) } } }

Slide 87

Slide 87 text

class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) linear_layout.addOnLayoutChangeListener { v, _, _, _, _, _, _, _, _ -> trace( "LL: ${linear_layout.width}px, " + "TV1: ${text_view_1.width}px, " + "TV2: ${text_view_2.width}px" ) } } } B LL: 500px, TV1: 300px, TV2: 200px C LL: 400px, TV1: 233px, TV2: 167px D LL: 400px, TV1: 300px, TV2: 200px A LL: 400px, TV1: 267px, TV2: 133px layout/activity_main.xml MainActivity.kt

Slide 88

Slide 88 text

class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) linear_layout.addOnLayoutChangeListener { v, _, _, _, _, _, _, _, _ -> trace( "LL: ${linear_layout.width}px, " + "TV1: ${text_view_1.width}px, " + "TV2: ${text_view_2.width}px" ) } } } B LL: 500px, TV1: 300px, TV2: 200px C LL: 400px, TV1: 233px, TV2: 167px D LL: 400px, TV1: 300px, TV2: 200px A LL: 400px, TV1: 267px, TV2: 133px layout/activity_main.xml MainActivity.kt

Slide 89

Slide 89 text

No content

Slide 90

Slide 90 text

So we might end up in some very extreme & weird cases…

Slide 91

Slide 91 text

Locale resolution

Slide 92

Slide 92 text

build.gradle apply from: puzzler android { defaultConfig { resConfigs "es" } }

Slide 93

Slide 93 text

MainActivity.kt class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // User’s locale preferences are set to: en_US, fr_FR, es_ES trace("${LocaleList.getDefault()}, " + "${Locale.getDefault()}, ${text_view.text}" ) } }

Slide 94

Slide 94 text

MainActivity.kt class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // User’s locale preferences are set to: en_US, fr_FR, es_ES trace("${LocaleList.getDefault()}, " + "${Locale.getDefault()}, ${text_view.text}" ) } } layout/activity_main.xml

Slide 95

Slide 95 text

MainActivity.kt class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // User’s locale preferences are set to: en_US, fr_FR, es_ES trace("${LocaleList.getDefault()}, " + "${Locale.getDefault()}, ${text_view.text}" ) } } layout/activity_main.xml values/strings.xml Bonjour monde !

Slide 96

Slide 96 text

MainActivity.kt layout/activity_main.xml values/strings.xml Bonjour monde ! values-es/strings.xml ¡Hola mundo! class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // User’s locale preferences are set to: en_US, fr_FR, es_ES trace("${LocaleList.getDefault()}, " + "${Locale.getDefault()}, ${text_view.text}" ) } }

Slide 97

Slide 97 text

B [en_US,fr_FR,es_ES], fr_FR, Bonjour monde ! C [en_US,fr_FR,es_ES], en_US, Bonjour monde ! D Resources.NotFoundException A [fr,es], fr, Bonjour monde ! MainActivity.kt layout/activity_main.xml values/strings.xml Bonjour monde ! values-es/strings.xml ¡Hola mundo! class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // User’s locale preferences are set to: en_US, fr_FR, es_ES trace("${LocaleList.getDefault()}, " + "${Locale.getDefault()}, ${text_view.text}" ) } }

Slide 98

Slide 98 text

B [en_US,fr_FR,es_ES], fr_FR, Bonjour monde ! C [en_US,fr_FR,es_ES], en_US, Bonjour monde ! D Resources.NotFoundException A [fr,es], fr, Bonjour monde ! MainActivity.kt layout/activity_main.xml values/strings.xml Bonjour monde ! values-es/strings.xml ¡Hola mundo! class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // User’s locale preferences are set to: en_US, fr_FR, es_ES trace("${LocaleList.getDefault()}, " + "${Locale.getDefault()}, ${text_view.text}" ) } }

Slide 99

Slide 99 text

User settings de_DE fr_FR es_ES App resources default es

Slide 100

Slide 100 text

User settings de_DE fr_FR es_ES App resources default es Resolution path de de_DE Children of de fr_FR fr Children of fr es_ES es Children of es Default

Slide 101

Slide 101 text

User settings de_DE fr_FR es_ES App resources default es Resolution path de de_DE Children of de fr_FR fr Children of fr es_ES es Children of es Default

Slide 102

Slide 102 text

User settings de_DE fr_FR es_ES App resources default es Resolution path de de_DE Children of de fr_FR fr Children of fr es_ES es Children of es Default

Slide 103

Slide 103 text

User settings de_DE fr_FR es_ES App resources default es Resolution path de de_DE Children of de fr_FR fr Children of fr es_ES es Children of es Default

Slide 104

Slide 104 text

User settings de_DE fr_FR es_ES App resources default es Resolution path de de_DE Children of de fr_FR fr Children of fr es_ES es Children of es Default es

Slide 105

Slide 105 text

User settings en_US fr_FR es_ES App resources default es Resolution path English is always considered as supported if in user settings

Slide 106

Slide 106 text

User settings en_US fr_FR es_ES App resources default es Resolution path en en_US Children of en fr_FR fr Children of fr es_ES es Children of es Default English is always considered as supported if in user settings

Slide 107

Slide 107 text

User settings en_US fr_FR es_ES App resources default es Resolution path en en_US Children of en fr_FR fr Children of fr es_ES es Children of es Default English is always considered as supported if in user settings

Slide 108

Slide 108 text

User settings en_US fr_FR es_ES App resources default es Resolution path en en_US Children of en fr_FR fr Children of fr es_ES es Children of es Default English is always considered as supported if in user settings default

Slide 109

Slide 109 text

That’s all folks! @cyrilmottier • cyrilmottier.com