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
How to Remove Strings from a Compiled Binary (.So)
Customize Android Facebook Login Button
Android: Speech Recognition Without Using Google Server
Blue Dot and Circle Is Not Shown on Mylocation Using Android Fused Location API
How to Draw Free Hand Polygon in Google Map V2 in Android
Didn't Find Class "Com.Google.Firebase.Provider.Firebaseinitprovider"
Android Create Folders in Internal Memory
Notifydatasetchange Not Working from Custom Adapter
How to Create Turn-By-Turn Gps Navigation App on Android/iOS Using Google Maps
What Is the Relation Between App_Platform, Android:Minsdkversion and Android:Targetsdkversion
How to Set Up Android Sdk for Command Line Development on Linux
Detect a New Android Notification
Android Background Image Size in Pixel
Out of Memory Error with Bitmap
Why Doesn't Logcat Show Anything in My Android
Android Studio Says "Cannot Resolve Symbol" But Project Compiles
How to Set the Edittext Keyboard to Only Consist of Numbers on Android
Ffmpeg for a Android (Using Tutorial: "Ffmpeg and Android.Mk")