I save my formatted edit text in sqlite database in html then after retrieving it two extra lines are added to the edit text
So I started like so:-
edt.setText(Html.fromHtml("<b>"+myString+"</b>"));//making it bold
Editable e = edt.getText();//convert to Editable
String text = Html.toHtml(e);//convert to String
Then after I inserted text into the database .I retrieved it back to edt this way:-
Spanned sp = Html.fromHtml(text);//convert text to spanned
edt.setText(sp);//setting to the edittext
It was retrieved successfully however 2 extra lines where added to edt at the end of text each time why you think that occurs?.
try this
edt.setText(Html.fromHtml("<b>"+myString+"</b>"));//making it bold
Editable e = edt.getText();//convert to Editable
/* String text = Html.toHtml(e);//convert to String
Spanned sp = Html.fromHtml(text); //convert text to spanned */
edt.setText(e);//setting to the edittext
I finally found a solution using code from an answer in this question Remove extra line breaks after Html.fromHtml()
.Its all about removing extra html whitspaces but using it for spannable
String like so :-
Spanned sp = Html.fromHtml(subnote);
int h = sp.length();
// loop back to the first non-whitespace character
while(--h >= 0 && Character.isWhitespace(sp.charAt(h))) {
}
sp= (Spanned) sp.subSequence(0, h+1);
edt.setText(sp);
Related
It has being parsed Ms Word documents with Aspose Words for Android below code. All of paragraphs in the document have inline character styled texts seperatelly. I've text and style of them but are there any way to get start position of them in its paragraph string like String.indexOf() ? It may be convert to string, but style control is not possible in this case.
Document doc = new Document(file); // Get word document.
NodeCollection paras = doc.getChildNodes(NodeType.PARAGRAPH, true); // get all paragraphs.
for (Paragraph prg : (Iterable<Paragraph>) paras) {
for (Run run : (Iterable<Run>) prg.getChildNodes(NodeType.RUN, true)){
boolean defaultPrgFont = run.getFont().getStyle().getName().equals("Default Paragraph Font");
// Get different styled texts only.
if (!defaultPrgFont){
// Text in different styled according to paragraph.
String runText = run.getText();
// Style of the different styled text.
String runStyle = run.getFont().getStyle().getName()
// Start position of the different styled text in its paragraph.
int runStartPosition; // ?
}
}
}
You can calculate length of text in runs before the styled run. Something like this.
Document doc = new Document("C:\\Temp\\in.docx"); // Get word document.
NodeCollection paras = doc.getChildNodes(NodeType.PARAGRAPH, true); // get all paragraphs.
for (Paragraph prg : (Iterable<Paragraph>) paras) {
int runStartPosition = 0;
for (Run run : (Iterable<Run>) prg.getChildNodes(NodeType.RUN, true)){
boolean defaultPrgFont = run.getFont().getStyle().getName().equals("Default Paragraph Font");
// Get different styled texts only.
if (!defaultPrgFont){
// Text in different styled according to paragraph.
String runText = run.getText();
// Style of the different styled text.
String runStyle = run.getFont().getStyle().getName();
System.out.println(runStartPosition);
}
// Position is increased for all runs in the paragraph.
// Note that some runs might represent field codes and are not normally displayed.
runStartPosition += run.getText().length();
}
}
I'm working on an app that generates passwords randomly using the array. The password is in TextView. Everything is good unless I want to generate a new password second time. How can I "delete" the old text (password) from the TextView and replace it with the new one using the same button?
Here are the variables that I'm using:
EditText dlugosc;
String haslo = "";
String pustak = "";
TextView haslo0;
And this is a code that I use to generate a password:
(znaki is the name of array)
dlugosc = findViewById(R.id.password_len);
haslo0 = findViewById(R.id.password);
String yui = dlugosc.getText().toString();
int x = Integer.parseInt(yui);
for(int i = 0; i < x; i++){
int Index = generator.nextInt(znaki.length);
haslo = znaki[Index] + haslo;
}
I have already tried doing an if structure:
if (haslo0 != null){
haslo0.setText(pustak);
haslo0.setText(haslo);
}
else
haslo0.setText(haslo);
But it doesn't help :(
When I want to have 7 chars in the password and click the button first time, the result is correct e.g. PKAjzQL. But when I click the button second time, the result is nBzcRjQPKAjzQL instead of nBzcRjQ.
Why are you appending the old string haslo behind the newly generated one in haslo = znaki[Index] + haslo;
Probably that's why you are getting output like that. Can you please try just setting newly generated password into the textview like
haslo = znaki[Index];
And then try to set text in the text view using haslo0.setText(haslo);
How can I "delete" the old text (password) from the TextView and replace it with the new one using the same button?
The problem is not to "delete" the old text, the problem is that you have to clear the list for example, every time user clicks on the Button you clear the list doing : znaki.clear(), then it will only show the new password generated.
If you see your output :
First output :
PKAjzQL --> This is correct
Second output :
nBzcRjQPKAjzQL --> this is the new output + the old one
Can you give the code of the OnClickButton? And why are you setting the same TextView with diferents Strings when you click?
haslo0.setText(pustak);
haslo0.setText(haslo);
?
I'm testing the functions of text editor and I would like to be able to select text (either a word or a string between 'p' tags) and then edit it by clicking a button (B for bold or I for Italic). Much like the editor used here at Stackoverflow. This will add an html B tag so that it shows as bold.
I know the following Xpath leads to a string of text but I can't find how to select this text or how to select a word in this text.
This is part of the page source (text editor is in an iframe)
<html>
<head>
<body>
<p>
This is a line of text to test Bold
</p>
<p>
This is a line of text to test Italic
</p>
</body>
Where this
driver.findElement(By.xpath("/html/body/p[1]")
leads to a text:
This is a line of text to test Bold
I would like to select the word 'Bold' (or the entire sentence if this is easier) and then click a button above the text to make it Bold
Try this
Actions a = new Actions(driver);
a.doubleClick(driver.findElement(By.xpath("/html/body/p[1]")).build().perform();
What you could do is to use an <html> Tag within JLabels for instance.
What I have in mind looks like the following:
boldButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String formattedText = "<html><b>" + text + "</b></html>";
label.setText(formattedText);
}
}
italicButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String formattedText = "<html><i>" + text + "</i></html>";
label.setText(formattedText);
}
}
Unforunately, I do not know how your text editor looks like, but this is the way I would approach the problem.
Anyways, in a larger scale you can still use HTML tags to format the text (e.g., in a TextArea). In order to get the highlighted/selected items you might want to have a look at this thread how to get highlighted text in jtextarea.
So you could try something like this:
// retrieve selected text from text area
String selectedText = textArea.getSelectedText();
// get whole text
StringBuffer text = new StringBuffer(textArea.getText());
// remove the selected text
text.remove(textArea.getSelectionStart(), textArea.getSelectionEnd());
// make the selected text, e.g., bold.
selectedText = "<b>" + selectedText + "</b>";
// add the bold text to the whole text
text.insert(textArea.getSelectionStart(), selectedText);
//eventually apply outer <html> tags
String result = "<html>" + text.toString() + "</html>";
// set new text (with bold element) to the text area
textArea.setText(result);
Please note that I have not tried this, so you might tweak some things in order to make it work.
My requirement is to add ""string"" and ""string"" tags only not formatting in the screen using shortcuts ctrl+b and ctrl+i
I used the below code to add italic and bold tags
static void addBold1() {
int end = 0;
String text = ta_description.getText();
String selectionStart = ta_description.getSelectedText();
int index = ta_description.getText().indexOf(selectionStart, ta_description.getSelectionStart());
int len = selectionStart.length();
end = index + len;
ta_description.insert("<b>", index);
ta_description.insert("</b>", end + 3);
}
Where ta_description is the textarea. With this i can add bold tags when the textarea is opened but when i reopen the screen bold tags are not coming. I dont need editor and text pane. I need to solve this using jtextarea. Suggest some idea.
This is achieved by using cdata tags at the start and end as like below
<![CDATA[ <b> பெங்களூர் 28-4-2014</b> <i> தினத்தந்தி</i>]]>
How to insert a text in EditText at the cursor position? editText.setText erases the current text and sets the new one, which is not what I want. editText.setText(editText.getText() + "my text"); inserts the text at the end, that's also not the case.
Its very easy to do this :
int start =editText.getSelectionStart(); //Get cursor position with this function
String str = "my text";//String you want to insert
editText.getText().insert(start, str); //This will get the text and insert the String str at the current position.
Hope this Helps!
are you perhaps talking about settings the hint text ?
editText.setHint("Enter Text Here");
in XML
android:hint="Enter Text Here"
If this is not what you are looking for then please alaborate the problem a bit more
Add \n before the text you want to add...
var.setText(var.getText() + "\nnewtext");