How to make text underline and remove text at the same time - java

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, "");

Related

ClickableSpan onClick not working android java

I am developing an android SMS application using java and I want to check if the received message contains a phone number. If yes, I want to make the phone number bold and underlined and when user clicks on it, I want to open a popup for options like Call, Add To Contacts etc. I have done everything except for the part where I want to show the popup when user clicks on the phone number. I tried using ClickableSpan but the onClick method never triggers. Here is what I have done so far:
String finalMsg;
String ifNumberExists = Util.extractNumber(message.getMessageBody());
String boldNum = "<b><u>" + ifNumberExists + "</u></b>";
if (message.getMessageBody().contains(ifNumberExists)) {
finalMsg = message.getMessageBody().replace(ifNumberExists, boldNum);
} else {
finalMsg = message.getMessageBody();
}
SpannableString spannableString = new SpannableString(boldNum);
spannableString.setSpan(new ClickableSpan() {
#Override
public void onClick(#NonNull View widget) {
Toast.makeText(viewMessageThreadActivity, "Text: ", Toast.LENGTH_SHORT).show();
}
}, 0, boldNum.length() - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView messageBodyLabel = rowView.findViewById(R.id.message_body_label);
messageBodyLabel.setText(Html.fromHtml(finalMsg));
messageBodyLabel.setClickable(true);
messageBodyLabel.setMovementMethod(LinkMovementMethod.getInstance());
you are creating spannableString, but enver uses that, TextView have set Html.fromHtml(finalMsg) as a content. set this spannableString as text to get desired result (and onClick working)
messageBodyLabel.setText(spannableString.toString());

How to overwrite a text in TextView using a button?

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);
?

How to extract all the URLs from the text in android

I want to get all the URLs from the given text using Patterns.WEB_URL.matcher(qrText);
What I want to do:
I am scanning a QR code,
open the link in webView if the link contains link which contians the word "veridoc"
showing in textView if the text scanned is not link or another link that does not contain the word "veridoc"
What I have tried:
private void initialize() {
if (getIntent().getStringExtra(Constants.KEY_LINK) != null) {
qrText = getIntent().getStringExtra(Constants.KEY_LINK);
webMatcher = Patterns.WEB_URL.matcher(qrText);
}
if (qrText.contains("veridoc") && webMatcher.matches()) {
//if qr text is veridoc link
Log.e("veridoc link", qrText);
setupWebView(qrText, false);
} else if (webMatcher.matches()) {
//if qr text is link other than veridoc
Log.e("link", qrText);
openInBrowser(qrText);
finish();
} else if (qrText.contains("veridoc") && webMatcher.find()) {
//if qrText contains veridoc link + other text.
String url = webMatcher.group();
if (url.contains("veridoc")) {
Log.e("veridoc link found", url);
setupWebView(url, true);
} else
showQRText(qrText);
} else {
//the qrText neither is a link nor contains any link that contains word veridoc
showQRText(qrText);
}
}
}
In the above code,
setupWebView(String strUrl, boolean isTextAndUrlBoth) setup webview and load url etc.
openInBrowser(String url) opens the provided URL in the browser.
showQRText(String text) shows the provided text in textView with formatting.
The Issue
When the text contains some text and more than 1 link, String url = webMatcher.group(); always fetches the first link in the text.
What I want
I want all the links from the text, and find out that which links contain the word "veridoc". After that I would like to call the method setupWebView(url, true); .
I am using following link and text for Example
name: Something
Profession: Something
link1: https://medium.com/#rkdaftary/understanding-git-for-beginners-20d4b55cc72c
link 2: https://my.veridocglobal.com/login
Can anyone help me to find all the links present in the text?
You can loop on find to find the different websites and setup arraylists with that
Matcher webMatcher = Patterns.WEB_URL.matcher(input);
ArrayList<String> veridocLinks = new ArrayList<>();
ArrayList<String> otherLinks = new ArrayList<>();
while (webMatcher.find()){
String res = webMatcher.group();
if(res!= null) {
if(res.contains("veridoc")) veridocLinks.add(res);
else otherLinks.add(res);
}
}
Given a sample input like :
String input = "http://www.veridoc.com/1 some text http://www.veridoc.com/2 some other text http://www.othersite.com/3";
Your ArrayLists will contains :
veridocLinks : "http://www.veridoc.com/1", "http://www.veridoc.com/2"
otherLinks : "http://www.othersite.com/3"

Two extra lines added to edit text when retrieving html String

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);

Click on link inside TextView class

How can I access to click on 2nd link inside TextView? So far I tried:
AndroidElement d = (AndroidElement) driver.findElement(By.xpath("//android.widget.TextView[#text='Notice']"));
d.click();
And using:
driver.findElement(By.Id("com.optimum.rdvr.mobile:id/toc_text"));
just clicks on 1st link. And I need to get 2nd link. I want to click on "Mobile Privacy Notice" Any one has suggestion?
You can use Linkify
String termsOfUse = getResources().getString(R.string.terms_of_use);
String privacyNotice = getResources().getString(R.string.privacy_notice);
legalDescription.setText(
String.format(
getResources().getString(R.string.message),
termsOfUse,
privacyNotice )
);
//add the links
Pattern privacyNoticeMatcher = Pattern.compile(privacyNotice);
Linkify.addLinks(legalDescription, privacyNoticeMatcher , "privacy:");
Pattern termsOfUseMatcher = Pattern.compile(termsOfUse);
Linkify.addLinks(legalDescription, termsOfUseMatcher , "termsOfUse:");
via Multiple Clickable links in TextView on Android

Categories