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");
Related
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 have a text in which there is indicator tag that indicate from where i will make text underline, I want to make text underline from that indicator also want to remove indicator so that it wont appear in string, here is what I'm trying:
String consent = "By clicking this, you confirm you understand the services provided by your Health at Hand doctor and give <clickable>consent</clickable> to proceed.";
int i1 = consent.indexOf(">");
int i2 = consent.indexOf("</");
consentCheck.setMovementMethod(LinkMovementMethod.getInstance());
consentCheck.setText(consent, CheckBox.BufferType.SPANNABLE);
consent = consent.replace("</clickable>", "");
consent = consent.replace("<clickable>", "");
Spannable mySpannable = (Spannable)consentCheck.getText();
mySpannable.setSpan(clickableSpan, i1+1, i2 , Spannable.SPAN_INCLUSIVE_INCLUSIVE);
If you want to display the text in TextView you could replace "clickable" tags with "u" tags and then use Html.fromHtml() in setText:
String consent = "By clicking this, you confirm you understand the services provided by your Health at Hand doctor and give <clickable>consent</clickable> to proceed.";
consent = consent.replace("<clickable>", "<u>").replace("</clickable>", "</u>");
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
tvConsent.setText(Html.fromHtml(consent, Html.FROM_HTML_MODE_LEGACY));
} else {
tvConsent.setText(Html.fromHtml(consent));
}
EDIT:
If u want to edit "consent" part freely you could split the text into three parts and then edit each part independently
String consent = "By clicking this, you confirm you understand the services provided by your Health at Hand doctor and give <clickable>consent</clickable> to proceed.";
String start = "<clickable>";
String end = "</clickable>";
String part1 = consent.substring(0, consent.indexOf(start));
String part2 = consent.substring(consent.indexOf(start)+start.length(),consent.indexOf(end));
String part3 = consent.substring(consent.indexOf(end), consent.length()).replace(end, "");
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);
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.
I'm looking for an easy way to make some letters in a label bold.
I have a string like this:
String r = "y = "+output0+" "+output1+"sin(x) "+output2+"cos(x)";
and a label:
Label s = new Label(r);
and I need to make the "y" and "sin(x), cos(x)" bold. I tried using HTML, but it didn't work (maybe i was using it wrong). If I set font for that label, then the whole label is bold (including those outputs) which is not what I need.
You can use html style in JLabels and in some other Java components. If you start your text with <html> and end it with </html>, the html code in your JLabel will be rendered.
This should resolve your issue:
JLabel myLabel = new JLabel();
// The following line is required to make this JLabel's text not bold as JLabel's text is bold be default.
myLabel.setFont(myLabel.getFont().deriveFont(Font.PLAIN));
myLabel.setText("<html><strong>y</strong> = " + output0 + " " + output1 + "<strong>sin(x)</strong> " + output2 + "<strong>cos(x)</strong></html>");
Try using HTML (you have to start the string with <html> and end it with </html>):
new Jlabel("<html>Normal text. <strong>This is bold.</strong></html>")
Explanation! <html> tells Java to render HTML (Hyper Text Markup Language), and <strong> tells HTML that the text inside is of strong importance, normally represented in bold.