How to Use String Resources In a ViewModel - Android Studio Tutorial

preview_player
Показать описание
Whenever possible, you should be using string resources to make sure your app can easily be translated to other languages. However to read a string resource, you need access to the context which you don't have in ViewModels. In this video I show you how you can solve this problem.

Get limited 20% discount on all my courses using the code HAPPY-EASTER:

💻 Let me personally review your code and provide individual feedback, so it won't backfire and cost you a fortune in future:

Subscribe to my FREE newsletter for regular Android, Kotlin & Architecture advice!

Join this channel to get access to perks:

Get the initial source code for this video here:

Get the final source code here:

Join my Discord server:

You like my free content? Here you can buy me a coffee:
Рекомендации по теме
Комментарии
Автор

Nice one!
TLDR: Return resource id instead of String from ViewModel using a wrapper class, then extract the string from UI layer where context is available.

irounik
Автор

This was impressive, Philip. Power to you for constantly improving and thanks for sharing your knowledge!

evanparrish
Автор

You don't know how much I have learned from you. Your tips and tricks are awesome 🙏

mithilmehta
Автор

This looks really interesting. I always just passed down the resId and let the view handle the rest. But your solution looks even cleaner. I'll give it a try!

leonawroth
Автор

Wow, I've just faced that issue today, and here is your upload! The universe has heard me 😅
Thanks for your videos, you're doing a great job, keep it up!

andres_tomillas
Автор

I really want to become like you in coding . I am so glade to find youtuber like you who share his knowledge with us

tracker_x
Автор

Thank you so much Philipp! You are a great tutor!

foivosstamopoulos
Автор

I was looking for an answer to this problem for a long time and finally thanks to you the problem was solved 👍👍

legato
Автор

I wish I knew this months ago!! Thanks for this, Philipp!

RexTorres
Автор

If you need to show error message, you can create sealed class/interface UiEvent, add its child for every type of error in hierarchy, and then map it in UI layer with when operator and using local context. So you won't need resources in your view model or domain layer.

yuriynekhodov
Автор

I am a fluttter developer but I learn alot from this channel

edgaremmanuel
Автор

Despite the code and the nice approach, it shows your commitment and love to make content on Youtube even when you got cold. Keep up the good work! ;)

mohsenrzna
Автор

I was using a data class to hold either a messageStr or a messageResId and sending that as error text to my UI layer from viewmodel. The thought of using sealed class didn't occur to me.

Many thanks for this helpful video.

prabhatpandey
Автор

The timing on this one is crazy, I was just trying and failing to solve this problem :D well, that problem is now solved :) you are the GOAT!

danju
Автор

Hi phillip could you help me to understand how can use navigation components for feature multi module with arguments and DeepLinks?

tutorialexpress
Автор

Hello phillip, thank u so much for your efforts, can i know what's the Channel (include send&receive functions) class in this case ?

abdelazizyasser
Автор

Hi Philipp. Thanks for your best practice sharing. Can we collect this message in MainActivity and trigger showing snackbar in our setContent {...} for all composable screens?

mesutemrecelenk
Автор

I follow with an implementation very close to yours, but I make an extension of the View class, which allows me to receivereceive the UiText as a parameter, and I set the value internally using the context of the vie itself

samilindo
Автор

Very good content! Using an interface wrapper for the resources injected in the ViewModel would also work right? Then you can easily provide it and also mock it for unit tests.

Renaro
Автор

Philipp you absolute gigachad, you should do a video on how to do layouts/UIs with Jetpack Compose, but not like the million other tutorials out there that position a few little text fields around and change the background color.

That's not useful for people trying to learn this for the first time and interested in writing an actual real-world app.

So just like the positioning of different elements and putting them together to create real-world app UIs, showing different functionalities of the Modifier, etc. I haven't seen anything good like this, and even the single class on Udemy that seems to be just on layouts doesn't look very extensive and is short.

clamum