0

I explain my goal of this post.

Actualy, I want to rewrite how android works basicaly, with strings in app. At this moment, i can change dynamicaly it from rewritting the android ressources & context, but when i refer directly a string in xml ( @string/name ), the getString function in Ressources class isnt called. But when i do pragmaticaly a getString(R.string.my_string) in my Fragment / Activity, getString from Ressources class is called.

Actualy, i have this and i works good only when i use getString(R.string.my_string) :


class MyRessources(
    res: Resources?,
    private val context: Context?
) : Resources(res?.assets, res?.displayMetrics, res?.configuration) {

    /** {@inheritDoc} */
    override fun getString(id: Int): String {

        var identifier = id

        val entryName = getResourceEntryName(id)

        context?.let {

            val name = entryName.plus("__test")

            identifier = getIdentifier(name, "string", getResourcePackageName(id))

            if (identifier == 0) {
                identifier = id
            }
        }

        return super.getString(identifier)
    }
}

There is a simple way, with not much code, to do overwrite of that @string behavior in xml ?

Brodalee
  • 21
  • 5

1 Answers1

0

it works for you in Fragment, because you are probably creating in there your custom MyRessources instance and obtaining String through it

new MyResources(resources, context).getString(...)

there is no option for overriding @string/resname, this will always use original resources of an app

if you want to override some Strings only for some tests (e.g. for debug) then consider creating separated flavor for this purpose and/or read THIS topic

snachmsm
  • 17,866
  • 3
  • 32
  • 74