I am using EditText control in android, and I want to know if there is any way get the defaul string of the control(I mean the one that is in String.xml).
I use this when i want to modify its string.
e.setText( e.getText().toString + "something").
Now, the problem is that sometimes i get unnecessary information like :
" Name: JhonName: JhonName: JhonName: JhonName: Jhon "
When I just wanna show:
" Name: Jhon "
If i didnt explain properly, let me know :)
e.getText().toString() is return the string of your EditText....
now for the first time when you execute your code ..the above method will not return anything.. that's why your EditText will like "Name: John"..ok
when second time this code executes... the above method will return "Name: John"
and after that you adding "Something" so that why it happens...
just replace you code with this...
e.setText("something")
You dont need to write editText.setText(editText.getText().toString()+"something"), this will definately add "something" in the previous string. You should only write editText.setText("Something"), if you want only to print "something".
I hope that you know about Tag and Hint attributes of edit text. You can set tag and hint in both the ways XML as well as Java. So set the default (prefix) value in hint, append that value whenever you are settext to edit text.
for ex.:
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Name : " />
e.setText(e.getHint() + "John");
Related
In android, a String resource can be created in XML by using
<string name="name">data</string>
to create a new line, \n may be used. However, if the user personally inputs data into an EditText and includes a \n, the writing is saved to a String and when the writing is displayed again in a TextView the result would be something such as:
Line 1\nLine2
How come the \n doesn't apply to user written Strings?
The end goal for me is to be able to have the user type a \n into a String to make sure the String is displayed in multiple lines.
How would this be achieved? Thanks in advance!
Use this :
TextView view = your text view;
view.setSingleLine(false);
view.setText("first line" + "\n" + "second line" + "\n" + "third line");
How are you doing setText() in the textView.
Try this...
TextView view = your text view;
view.setSingleLine(false);
view.setText("first line\n"+"second line\n"+"third line");
with
System.getProperty("line.separator")
you get a String which results in a new line.
I would replace \n by this expression.
The text entered in an input field is used as an actual text. That means, when the user enters the Hello \n World and the code calls getText().toString(), what will actually be returned is: Hello \\n World that means, it's the actual slash symbol and not the "carriage return" symbol.
I don't believe android has any built-in API to give you the actual code typed by the user, that means, that you have to write that code yourself. If all you need is the \n as a carriage return it is simple:
getText().toString().replace("\\n", "\n");
I am writing my data in a database and getting it in android to set it in a textview but when I write for example:
"hello,
how are you?"
which means each line on a row if the row in the phone completes I get an extra line space and I don't want this need to remove it how can I do that?
thanks.
EDIT:
how can I place a line space in an arabic database: I added \n didn't work "\n" didn't work '\n' didn't work!! any ideas?
even if the database wasn't arabic that didn't work so can I place the \n in the database in a way that the android program takes it as a line space?
Edit2
Is there any other method? other than the one that I've mentioned in my answer? thanks.
check to see if you insert to your DB with that extra line.
showing more code would help us understand whats causing your issue...
but if you want to brute force it you could always use substring() for each or your elements or
`String input = EditTextinput.getText().toString();
input = input.replace(" ", "");`
but like #Nitin Sethi said more code especially the entry to your DB would give us a better understanding of what's going on
so what I did is that I've added the word "line space" in my database and I added
in my android code:
String input = //here I take the cde from cursor;
input = input.replace("line space", "\n");
and it worked perfectly but is there another way?
i've got a database with a field in var_char(2000).
in this field there's text with some new line, like a normal text written:
hello
i am davide
bye
i put this text in a textview but i see the text like a unique line (hello i am davide bye), without newlines.
in iphone it is all normal and i've done nothing particular... but here no.
how can i?
i've tried with replace \n or replace \r\n o other things but without success.
Also with Html.fromHtml()
the singleLine(false) is deprecated, and it doesn't work.
also text doesn't work. it see the newline as a space
Try setting android:singleLine="false" to your textView.
Edit:
If this does not work check whether the string has a new line character using below code
char[] chararray= mString.toCharArray();
for(char temp:chararry){
int value = temp;
System.out.println(value);
}
Decimal value of Newline is either 10 or 13. While space character is 32.
Edit2 : I think TextView does not go to next line for \r which is 13.
So do
mString = mString.replaceAll("\r","\n");
did you try changing the datatype in sqlite?, your varchar to just text? if not, try doing so maybe it'll help
Try to disable the multi-line feature of the text view.
If you created the text view from an XML file, add this attribute to the text view :
<TextView
[...]
android:singleLine="false"
[...] />
If you created the text view programmatically, try to use the following method instead :
TextView myText = [...]
myText.setSingleLine ( false );
For the below code I intended to get the system date and display it as per the formatting of the current locale, it's just that for the R.string.date. In emulator it always shows up as a long number (something like 821302314) instead of "Date: " which I has already externalized in the string.xml. Can anyone help to have a look why this is so?
final TextView mTimeText = (TextView)findViewById(R.id.mTimeText);
//get system date
Date date = new Date();
java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
mTimeText.setText(R.string.date + " " + dateFormat.format(date));
layout.xml
<TextView
android:id="#+id/mTimeText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/date"
/>
strings.xml
<string name="date">Date:</string>
R.string.date is indeed an int, you're missing the call to getText() or getString():
mTimeText.setText(getText(R.string.date) + " " + dateFormat.format(date));
Even better, don't build the string in your code, but use a template with getString(int resId, Object... formatArgs):
mTimeText.setText(getString(R.string.date, dateFormat.format(date)));
and in your string.xml:
<string name="date">Date: %s</string>
Yes, you will get the ID of the String if you use R.string.date.
As stated in the docs
You can use either getString(int) or getText(int) to retrieve a string. getText(int) will retain any rich text styling applied to the string.
Example:
this.getString(R.string.date);
Read about it here: getString
To get string value from xml, you should call this.getString(R.id.nameOfString). In your case this would be mTimeText.setText(this.getString(R.string.date) + " " + dateFormat.format(date));
To override all "R.string.*" to "getString(R.string.)"* i wrote a little regex.
This regex also ignores the strings who already have a "getString" in front.
((?!getString\() R\.string\.[a-zA-Z1-9_]+)
You just have to press Strg+Shift+R in Android Studio to open the replace terminal and insert the regex above as "Find" and as "Replacement" the regex below.
getString\( $1 \)
Don't forget to set "regular expression" checkbox.
For me this worked perfectly. But the "Find Regex" got one problem it only finds R.string when it starts with a whitespace. I don't know how to solve this because if i delete the whitespace ill find also the R.string that allready have the "getString".
May some can help to improve the regex or has a better way to achieve this.
i am trying to display STRING my car name is "abc" in textview.
it displays &qoute;abc&qoute; after running application.
i have tried decoding into " and then assigning it to TextView but it always convert " to &qoute;
can any one guide me how to solve this issue?
any help would be appriciated.
Escape them; use " to have quotes in XML values.
For e.g. try: android:text="" hi ""
I have done it escaping the "
Example:
<string name="pref_about_text">Estado del Tránsito versión \"%s\"\nCreado por:</string>
For me none of the solutions presented here earlier did not worked.
Sharing my method to do this:
<string name="filter">\"Filtruj\"</string>