Android Kaki

Build beautiful, usable products using required Components for Android.

Stay code samples for Android Studio to avoid wasting your coding time | by Tomáš Repčík | July 2023

Please do not waste your time repeating the identical code samples and spend it constructing the app.

photograph taken by ilgmyzin ABOVE depart

Android Studio (and another IDE developed by JetBrains) gives many useful autocomplete shortcuts to fill out declarations, technique calls, and many others. Some are for particular key phrases in Kotlin/Android. Keep away from repeating some actions simply since you keep away from utilizing context helpers within the IDE. The IDE is right here that will help you be extra productive.

Some pure Kotlin patterns:

  • constconst val
  • void, fun0, fun1, fun2– created a operate with required variety of parameters
  • ifn / inn if ( … == null) / if ( … != null)
  • todo // TODO:
  • fixme// FIXME:

Some Android-specific Jetpack Compose:

  • comp – create editor operate
  • paddpModifier.padding(.dp)
  • weightModifier.weight()
  • prev – preview editor operate
  • prevCol – create a group of drafting previews

For round composable with Container, Column or Row, set the cursor to composable, press Alt+Enter, choose ‘Encompass with Widget’. You possibly can set your personal keyboard shortcuts as you want. I’ve it arrange for Alt+i, but it surely’s fully as much as you.

You possibly can obtain one thing comparable with ctrl + alt + tpossibly even wrap your code into if-else or try-catch.

There are various extra templates, however in case you begin coding, they may pop up routinely as a result of they’re normally very apparent. Should you begin writing courses, or interfaces, for loops, and extra, you will get a context helper for key phrase completion.

Nonetheless, there may be extra room for enchancment.

Easy methods to add a brand new template

Programmers are lazy, and I’m no exception. I see plenty of programmers looking for a bit of code someplace of their code, the web or at some passion mission. If it has a repeating sample, take a minute or two to create it and by no means repeat writing that piece of code from scratch each time.

Open Android Studio and go to File → Settings → Editor → Stay Templates. By clicking the plus signal within the prime proper nook, you’ll be able to add your template group or template to an current catalog.

I encourage you to discover all of the templates accessible, as a result of you’ll discover different helpful templates in addition to those I named above or you’ll get inspiration on how one can simplify your work much more. once more.

Should you add a brand new Stay Template, the context ought to be recognized within the backside left nook by a yellow warning signal. Be at liberty to create a template, not only for Kotlin. It may be Java, Dart, Gradle or anything.

Stay Template instance

Abbreviation is the textual content utilized in your code to name the template. It may be something handy and memorable for you. I wish to maintain it easy with a capital begin letter since most Kotlin variables are written in camelCase.

Pattern textual content is your area to enter the shape. Customized enter might be outlined by $…$. Should you identify the identical, the primary enter will probably be shared throughout different situations. So in case you begin writing first $STATE$ as within the instance above, it will likely be replicated throughout all the pattern.

Superior conduct

By clicking edit variable, you’ll get a desk with customized enter out of your template. To declare default worth you should use double quotes for enter like "defaultValue".

Manifestation used to rapidly add extra performance to a customized area. You possibly can add the identify of the category, the date, the consumer, the enter options, or no matter. Please notice that it can’t be used to change current enter from you, however it might take enter from different fields already outlined.

$END$ is a particular placeholder that the pointer will leap after the top of the sample.

Some highlights from the expression:

  • blockCommentStart / blockCommentStop
  • capital / camelCase
  • class identify – class identify introduction
  • day — to format dates inside feedback
  • file identify — the identify of the file wherein the template is positioned
  • technique identify — the strategy, wherein the stay template is used
  • trace Variable identify — primarily based on enter, it can counsel a variable identify
  • an increasing number of

Extra might be discovered right here with explanations and examples.

Listed here are some concepts that I typically use in my programming as a result of they’re repeated all through the code. Code might be positioned straight as a template straight into Android Studio’s settings. Please create customized variations and acronyms to name them.

MutableStateFlow and StateFlow

personal val _$StateFlow$: MutableStateFlow<$StateType$> = MutableStateFlow($StateDefault$)
val $StateFlow$ = _$StateFlow.asStateFlow()


StateName can use ViewModel’s LowerCase identify for MVI structure.

class $VmName$ViewModel @Inject constructor($Parameters$) : ViewModel() {

personal val _$StateName$State: MutableStateFlow<$StateType$> = MutableStateFlow($StateDefault$)
val $StateName$State = _$StateName$State.asStateFlow()


Coroutine operate scope to ViewModel

Technique, scoped to a coroutine context outlined by a dispatcher. Dispatcher as a variable utilizing completeSmart expression to indicate you, accessible dispatcher, is Foremost, IO, or Default.

personal enjoyable $MethodName$() = viewModelScope.launch(Dispatchers.$Dispatcher$) {
Log.i(TAG, "$MethodName$: invoked")

Module class with constraints in Hilt

In my code, I reference the interface as a template, so the interface and the present implementation normally have a standard identify. The module is created as a camelCase expression model for the parameter.

summary class $Module$DI {

summary enjoyable gives$Module$($ModuleCamelCase$: $Module$): $Module$Template


Binding in Hilt

Identical as earlier than, however just for one dependency.

summary enjoyable gives$Module$($ModuleCamelCase$: $Module$): $Module$Template

Module class with Provision in Hilt

Identical implementation as earlier than for Binding, however now for Provisioning.

object $Module$DI {

enjoyable gives$Module$($ModuleCamelCase$: $Module$): $Module$Template = $END$

Supplied in Hilt

enjoyable gives$Module$($ModuleCamelCase$: $Module$): $Module$Template = $END$

ApplicationContext shortcut might be helpful for @ApplicationContext context: Context by way of utilizing Hilt.

Knife definition for the Room database

I do all of the fundamentals, however typically I do not want all of them.

interface $Kind$Dao {
@Question("SELECT * FROM $Kind$")
enjoyable getAll(): Record<$Kind$>

@Question("SELECT * FROM $Kind$ WHERE id = :id")
enjoyable getById(id: Int): $Kind$?

@Insert(onConflict = OnConflictStrategy.REPLACE)
enjoyable addItem(merchandise: $Kind$)

@Replace(onConflict = OnConflictStrategy.REPLACE)
enjoyable updateItem(merchandise: $Kind$)

enjoyable deleteItem(merchandise: $Kind$)

Take a look at class for unit testing

Instantiate total take a look at class with mockito rule for future mock implementation. The primary take a look at is simply to ensure, the unit beneath take a look at is initialized correctly and the second is about for testing.

class $TestClassName$Take a look at {

val mockitoRule: MockitoRule = MockitoJUnit.rule()

@Earlier than
enjoyable setUp() {


enjoyable tearDown() {


@Take a look at
enjoyable `Preliminary take a look at`() {
val sut: $TestModule$ = $TestModule$($Parameters$)

@Take a look at
enjoyable `$TestDescription$`() {
val sut: $TestModule$ = $TestModule$($Parameters$)

Unit take a look at

@Take a look at
enjoyable `$TestDescription$`() {
val sut: $TestModule$ = $TestModule$($Parameters$)


personal var $variable$: $variableType$

To export templates and maintain them for one more laptop or share them with others, you’ll be able to go to File → Handle IDE Settings → Export Settings. The identical goes for imports.

It could take some time to get used to a number of the keyboard shortcuts. It takes some time to arrange too. Nonetheless, it might change into a brand new behavior, saving you plenty of time sooner or later.

Should you appreciated the article, remember to clap it and subscribe for extra content material. Thank!

Tomáš Repčík

Android improvement

John Wick: Chapter 4 (FREE) FULLMOVIE The Super Mario Bros Movie avatar 2 Where To Watch Creed 3 Free At Home Knock at the Cabin (2023) FullMovie Where To Watch Ant-Man 3 and the Wasp: Quantumania Cocaine Bear 2023 (FullMovie) Scream 6 Full Movie
Updated: July 5, 2023 — 8:58 am

Leave a Reply

Your email address will not be published. Required fields are marked * © 2023 Android kaki