Android Xml Percent Symbol

Android XML Percent Symbol

The Android Asset Packaging Tool (aapt) has become very strict in its latest release and is now used for all Android versions. The aapt-error you're getting is generated because it no longer allows non-positional format specifiers.

Here are a few ideas how you can include the %-symbol in your resource strings.

If you don't need any format specifiers or substitutions in your string you can simply make use of the formatted attribute and set it to false:

<string formatted="false">%a + %a == 2%a</string>

In this case the string is not used as a format string for the Formatter so you don't have to escape your %-symbols. The resulting string is "%a + %a == 2%a".

If you omit the formatted="false" attribute, the string is used as a format string and you have to escape the %-symbols. This is correctly done with double-%:

<string>%%a + %%a == 2%%a</string>

Now aapt gives you no errors but depending on how you use it, the resulting string can be "%%a + %%a == 2%%a" if a Formatter is invoked without any format arguments:

Resources res = context.getResources();

String s1 = res.getString(R.string.str);
// s1 == "%%a + %%a == 2%%a"

String s2 = res.getString(R.string.str, null);
// s2 == "%a + %a == 2%a"

Without any xml and code it is difficult to say what exactly your problem is but hopefully this helps you understand the mechanisms a little better.

percentage symbol in strings.xml

Use

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="teststring">The new value is %1$s%%</string>

</resources>

In java

String value = "25";
String formattedString =
String.format(getResources().getString(R.string.teststring), value);
Log.i("",formattedString);

Trying to write a the symbol '%' in strings.xml

It seems that there was no reason to do String.format() when just retrieving the string would automatically handle the formatting for me.

So it just becomes:

@JvmStatic
fun formatPercentageText(context: Context, percentage: Float): String {
return context.resources.getString(
R.string.work_package_percent_complete,
percentage
)
}

Write % in strings.xml of android

You can use its code. Try this: %

Also check the similar question: Android XML Percent Symbol

Use Placeholder with % sign in string.xml for String.format()

I've run into this as well. There are two ways to escape a literal percent symbol in XML string resources.

For your case (a string with parameters, e.g. %1$d) you should use %% to escape the percent symbol literal.

If your string does not have parameters, then you should use \u0025 to escape the literal percent symbol.

If you use %%in a string without parameters, then getString() will resolve it as a literal %% instead of escaping it. If you use \u0025 in a string with parameters, then getString() will crash trying to treat that % as a format parameter.

Android string-array display Percent Sign

Apparently using an older version of eclipse and not having the latest SDK for android will produce this problem. I upgraded eclipse to 3.6.2 and made sure to install the latest version android sdk r10. So hope this helps someone and saves them time as I wasted a couple of hours trying to fix/find the answer.

So the answer to this question is you can use just a single % sign multiple times in a string array item without having to escape it, double up or use CDATA as long as you have the most recent versions of the SDK and Eclipse.

I just want a percent sign at end of my edittext like in google

you can try like this

 <EditText
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Medium Text"
android:drawableRight="@mipmap/ic_launcher"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/darker_grey"
android:textStyle="bold" />

put your image in

            android:drawableRight="@drawable/ic_launcher"

or you can do like this

 <FrameLayout
android:id="@+id/pwdLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/emailLayout">

<android.support.design.widget.TextInputLayout
android:id="@+id/textinputlayoutpwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/emailLayout"
android:layout_marginTop="10dp">

<EditText
android:id="@+id/signinpassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:hint="Password"
android:inputType="textPassword"
android:paddingRight="30dp"
android:singleLine="true"
android:textSize="20dp" />
</android.support.design.widget.TextInputLayout>

<TextView
android:id="@+id/loginpwdicon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:layout_marginRight="45dp"
android:layout_marginTop="10dp"
android:text="@string/lock_icon2"
android:textColor="@color/red"
android:textSize="25sp" />
</FrameLayout>

I used here icon image in textview. you can use here image view, image button or any other layout as for your requirements.



Related Topics



Leave a reply



Submit