Editable text to string - android

How can I convert editable text into string in Android ? Any solution?

If I understand correctly, you want to get the String of an Editable object, right? If yes, try using toString().

Based on this code (which you provided in response to Alex's answer):
Editable newTxt=(Editable)userName1.getText();
String newString = newTxt.toString();
It looks like you're trying to get the text out of a TextView or EditText. If that's the case then this should work:
String newString = userName1.getText().toString();

This code work correctly only when u put into button click because at that time user put values into editable text and then when user clicks button it fetch the data and convert into string
EditText dob=(EditText)findviewbyid(R.id.edit_id);
String str=dob.getText().toString();

Related

How can I add a backspace to a textView Android

I read the oracle escape sequence and realize that if I want to make a backspace in a textView I need to use "\b", the same way as we do for inserting a new line (/n). I've tried this line of code:
textView.setText("Hellos\bWorld");
Then, when I run the app, the textView shows this:
Hellos World
Intead of what I expected:
HelloWorld
I wish you can help me, how I can make a backspace within a textView. Any suggestion will be welcome.
Simplest method remove space in string is replace() method. It accepts two arguments 1st what word/char you want to search in string and 2nd what you want to replace with.
String dummy = "Hellos World";
String newText = dummy.replace("s ","");
textView.setText(newText);
//output > HelloWorld
String regex = "\\s*\\bis\\b\\s*";
String str = "Hellos World";
str = str.replaceAll(regex, "");
textView.setText(str);
\b doesn't work the way you are thinking. Basically,
\b allows you to perform a "whole words only". It matches the empty string at the beginning or end of a word.
So you can match \bword\b with \b. So to remove the character you want you either need to use substring or replace particular character.

How do I cut blank lines from EditText using XML that the unnecessary lines aren't shown in TextView

I have an EditText where I put a message in. Let's say I write a message like "Hi" followed by lot of blank lines, then the ChatBubble wraps in the blank lines as well. How can I remove them without converting the Message Object to a String? Is there a way using XML only?
You could solve your problem by first taking the text that you enter in the EditText and save it to a String variable, like so:
String message = String.valueOf(editText.getText())
Then you could simply replace any new lines (or any text that you are looking for) in that String variable.
newMessage = message.replace("\n", "")
Replace the "\n" with whatever you want to replace. Using the newMessage variable, create your ChatBubble and display the text you want. Hope it helps!

Hide only part of text in TextView

Good Day i want to hide some specified or certain part of text in textview!Important: Im not talking about hide the full textview with TextView.setVisibility(View.Gone) I'm not talking about transparent of TEXT in textview!im not talking about hiding full text in textview!So please help me to hide some text.
Example: lets say i have a textview with this text (10-Sporting Goods)
I want to hide the (10-) and show only Sporting Goods text.Any help will be appreciated!Thank you very much beforehand!
Although even i would appreciate for your case to strongly go with DroidWorm/Gabriella approach , just for the information of all the other folks who may see this in future.
If you really wish to hide just a portion of your textview which has the entire string in itself, you should use a SpannableString , as below:-
tvHello = (TextView) findViewById(R.id.tvHello);
SpannableString customText = new SpannableString("10-Sporting Good");
customText.setSpan(new RelativeSizeSpan(.1f), 0, 3, 0);
tvHello.setText(customText);
This code will technically HIDE the 10- from 10-Sporting Good without using a substring.
You could try to get the whole text like
String text = textView.getText().toString();
and then make substring of it like this:
String wantedSubstr = text.substring(4); //for example - everything from the 4th index to the end
then set this substring as text of your textView like this:
textView.setText(wantedSubstr);
There is one the possible solution of it is that..First you have to find the index(position) of "-" and than split the string according to it therefore use below code
String text = textView.getText().toString();
int position=text.indexOf('-');
String wantedSubstr = text.substring(position+1);
textView.setText(wantedSubstr);
Will there always been "10_" in front of it? Or will there always be 3 characters before the text you want? Or will there always be a "-" or "_" before the text you want?
If so, you could just do a simple method which takes the substring and then updates the textview. If so I can help you write a simple method
You cannot hide part of textView, instead you can make a substring of the specific string and setText using it.
Do it like:
String originalString = "10-Sporting Goods";
String subString = originalString.substring(3);
textView.setText(asubstring);

Android EditText inserting

How do I insert characters into the middle of an EditText field?
I'm making a calculator that can take a string expression like "3*(10^2-8)". I'm using an EditText field to make the string using XML like so:
EditText
android:id="#+id/entry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/label"
android:text="#string/testString1"
android:background="#android:drawable/editbox_background"
and then in my activity I have, say:
entry = (EditText)findViewById(R.id.entry);
entry.setText("blablahblah");
entry.setSelection(3);
Now I have an EditText field with the cursor blinking after the third character in the string. How do I insert a character there, so it correctly says "blahblahblah"?
The method getText() of the EditText widget returns an object that implements the Editable interface. On this object you can call the insert() method to insert text at a certain position.
I found this out by reading the documentation, but never used this myself. But for your needs, to insert a character at the selected position in the EditText, the following should work:
Editable text = entry.getText();
text.insert(entry.getSelectionStart(), "h");
Let's say you have a String called str and it contains "blablahblah" and you want to make it "blahblahblah" you can do the following:
String newString = str.substring(0, 3) + "h" + str.substring(3);
Take the first 3, add the new letter, place everything else. So you could take the String from the EditText, change it like this and then put the new String as the new value of the EditText.

How to extract HTML styled text from an EditText in Android?

I am using HTML.fromHTML(...) to style the text of an EditText in Android. I need to pass the styled text back as a result to another activity. However, when I use an intent to pass the contents of the EditText I am unable to figure out how to retain the HTML style of the original text.
As an example, suppose that the original text in the EditText is:
Today is the 21st
When I extract the text of using edittext.getText() and send it back as a result the resulting text is:
Today is the 21st
Is there a way to extract the HTML styled string from the EditText?
Use this to get the HTML of the styled text. You can use the HTML in EditText, TextView or WebView
String htmlString=Html.toHtml(edittext.getText());
You can send the HTML text itself and then call Html.fromHTML in the activity to which you are passing this text. fromHTML is meant to be used for text which has to be displayed on the screen
This won't work if your text is not spanned:
edittext.setText("");
//error here:
String htmlString = Html.toHtml((Spanned) edittext.getText());
You need to cast it by creating an instance first:
String htmlString = Html.toHtml(new SpannableString(edittext.getText()));

Categories

Resources