Wrong argument count, format string resource_name requires 3 but format call supplies 2 Android string

52 Views Asked by At

I have a resource which is in an html format since i want to display it like that

<string name="calculator_bottom_sheet_body" translatable="false">
        <![CDATA[<b>Basic requirements</b><br /><br />
You must be between 18 - 70 in order to proceed:
        <ul>
            <li> requirement 1</li>
            <li> requirement 2</li>
            <li> requirement 3 and</li>
            <li> requirement 4</li>
        </ul> <br /><br />

<b>Money requirement:</b><br /><br />
From €500 to €15.000<br /><br />

<b>Duration:</b><br /><br />
From 6 to 84 months<br /><br />
        If you like to continue press the button below.]]>
    </string>

and from the Fragment i retrieve the string like that

val body = resources.getString(R.string.calculator_bottom_sheet_body)

That works just fine. However i want the €500, €15.000 values to variables like €%s, €%s. So after i convert the line

From €%s to €%s<br /><br />

i need to call the resource from the fragment like

val body = resources.getString(R.string.calculator_bottom_sheet_body, minAmount, maxAmount)

And thats where i get this error

Wrong argument count, format string calculator_bottom_sheet_body requires 1 but format call supplies 2

Does anybody know whats the problem??

2

There are 2 best solutions below

0
Ajay Makwana On

I think you need to update your string from

From €%s to €%s<br /><br />

to

From €%1$s to €%2$s<br /><br />

and use it like

val body = String.format(context.getResources().getString(R.string.calculator_bottom_sheet_body), minAmount, maxAmount)

Hope this helps.

0
Erselan Khan On

As per the documentation,

https://developer.android.com/guide/topics/resources/string-resource#formatting-strings

you have to define the arguments positions like this:

<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>


var text = getString(R.string.welcome_messages, username, mailCount)