javaandroidkotlinmodulegetstring

Get text from string with kotlin in android studio


I have a kotlin module with code like this.

class AppOpenManager(private val appLanding: AppLanding) : 
    Application.ActivityLifecycleCallbacks, LifecycleObserver{

    //.........OTHER CODE

    companion object {
        private const val TEXT_HEADER = getString(R.string.textheader)
    }

    //.........OTHER CODE
    
}

However I can't get string resource from this code,

private const val TEXT_HEADER = getString(R.string.textheader)

string.xml :

<resources>
    <string name="textheader">Lorem Ipsum</string>
</resources>

Please help so I can fetch string values ​​into kotlin module from resources string.xml Thank you for the help.


Solution

  • use applications context:

    private const val TEXT_HEADER = 
    getApplicationContext().getResources().getString(R.string.textheader);