TextView display wrong with escape character - android

I have a random message that contains escape characters. I found that TextView displays wrong spaces for escape characters "\t".
My sample project has an Activity that has a layout with only one TextView.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/tv_hello"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="#string/text_hello"
android:maxLines="5"
/>
</FrameLayout>
I define text in file strings.xml
<string name="text_hello">Click Change: 13894\t18258\nClick Change: 1604\t2190\nClick Change: 9250\t11962</string>
The corresponding result is
I change the text like below
<string name="text_hello">ABC: 13894\t18258\nDEF: 1604\t2190\nXYZ: 9250\t11962</string>
The corresponding result will changes
As sample, TextView displays wrongly space. Can anyone has a solution to fix this issue?

You can use
\u0020
or
instead of \t in string.xml file.

Related

How to resolve hint character wrapping on autosizing TextView with empty text on android?

I want to use autosizing TextView for dynamic text content.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="350dp"
android:autoSizeTextType="uniform"
android:hint="hint"
android:text="some dynamic text" />
</LinearLayout>
This works so far:
But there is an issue if the dynamic text content is empty. The TextView wraps each character of the hint content to a new line:
I expect hint to behave like text. How to resolve this without hacks like setting text content to hint content if the former is empty.
The same event happened to me.
I defined this and it fixed it.
app:autoSizeStepGranularity="1dp"
I'd definitely set the minimum and maximum sizes together.
android:hint="no message"
app:autoSizeMaxTextSize="20sp"
app:autoSizeMinTextSize="1sp"
app:autoSizeStepGranularity="1sp"
app:autoSizeTextType="uniform"
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/show"
android:layout_width="match_parent"
android:layout_height="350dp"
android:hint="no message"
/>
</RelativeLayout>
MainActivity.java
TextView shows = findViewById(R.id.show);
TextViewCompat.setAutoSizeTextTypeWithDefaults(shows,
TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);
if(message.isEmpty()){
shows.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
}else{
shows.setText(message);
shows.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
}
if you add this code please run the code.

underlying text in string in android

this is my string.xml
<string name="terms_condition"> Accept the <u>Terms & Conditions</u></string>
this is mytextview
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/terms_condition"
android:padding="25dp"
/>
</android.support.constraint.ConstraintLayout>
i am using this code for display underline text but there is no space between two string. how keep space between two text and how to reduce underline? in below screen you can see underline problem.
<string name="terms_condition"> Accept the\n<u>Terms & Conditions</u></string>. I think this should work.

Getting error in linear layout

I am new to Android. I am reading official documentation of Android about linear layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="15dp"
android:paddingRight="15dp"
tools:context="com.example.hassan_microsoft.a2ndday18122017.MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:hint="#string/to"/>
At this line => android:hint="#string/to"
it is giving error and not working as i want it to be worked.Picture1
this preview (Picture1) should be the result but mine is giving out as this one as below (Picture2). Picture2 What can be the problem.? Thanks.
Try this make your EditText hight to android:layout_height="wrap_content"
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:hint="#string/to"
/>
And also
You need Create a new string resource value in value res/values/String.xml like below code
<string name="to">To</string>
You need to declare the value of #string/to in your strings.xml
Go to app -> res -> values -> strings.xml
Create a new string value called to with the string value, it should look like this::
Sample App
<string name="to">To</string>
A simpler alternative would be to just change your hint value to :
android:hint="To"

program is working fine but eclipse is showing warning for textview

i would like to know why eclipse is showing warning "[I18N] Hardcoded string "TextView", should use #string resource" in the xml code below .Actually i am trying to get the text written by user in an edit Text in an activity to this current activity.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.01"
android:text="TextView" />
</LinearLayout>
The reason that you are receiving a warning, is due to the fact that you are trying to hardcode a string which is not good convention in Android programming due to possible redundancy:
<TextView
...
android:text="TextView" />
You should rather create a reference to a string in the .../res/values/strings.xml file like so:
<TextView
...
android:text="#string/TextView" />
.. and define it in your strings.xml file:
<string name="TextView">TextView</string>
Hope this helps.
As it says, you are using a "hard-coded" string which is less efficient than using the String resource. Simply remove
android:text="TextView"
if you don't want the warning to show. If you want it there then disregard the warning or add it to the String resource file. The Text property isn't needed. If you are expecting user input then you should change it to an EditText anyway unless you have a reason for using TextView
<EditText
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.01" />
Then if you want it to display something such as "Enter input here" in the View then you can add android:hint"Text to display". But this will give you the same warning if you don't add it to the strings.xml and use android:hint="#string/nameInStringsFile".
But these warnings are just that. Suggesting possibly more efficient methods or ways of implementing whatever you are doing.
Change the XML to the following to remove the warning
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.01" />
</LinearLayout>
The reason you are seeing the warning is because you had set the text to "TextView" inside the XML layout file. It is Android best practice to put all Strings inside the strings.xml file in your res/values folder which creates string resources. One you have a string in a resource file you can reference it from your layout file using the syntax "#string/string_name".

in my xml file I get a caution notice on my text entry and I'd love to know how to fix it

caution [I18N] Hardcoded string"Would you like to give Zohan a call?", should use #string resource
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Would you like to give Zohan a call?"
android:layout_marginTop="200dp"
android:layout_marginLeft="20dp"
android:textSize="18dp"/>
You should not hard code your strings. Instead you should put your strings in values/strings.xml.
Ctrl+1 on the error it will show you the way to fix it.
Refer to the android string documentation: http://developer.android.com/guide/topics/resources/string-resource.html#String
You should store your texts in a resource properties file and link them to your XML file using the # notation.
See this link for more information.

Categories

Resources