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());
Related
I want to make an Interface for route-planning purposes that has 2 states
1 - Inputting start and target addresses
2 - Doing the same thing through GPS location services
I have made the Content layout through the XML file but since I need to switch between them dynamically I tried to set up the same thing via Java button events. But whenever I try to setup the TextInputLayout with an EditText inside through Java and try to compile and launch it on my Android emulator, which is a Pixel 2 API lvl 28 it gives me a java.lang.IllegalArgumentError with the message 'The style on this component requires your app theme to be Theme.AppCompat (or a descendant)'.
inputTop = new android.support.design.widget.TextInputLayout(getApplicationContext());
inputTop.setLayoutParams(new ConstraintLayout.LayoutParams(Constraints.LayoutParams.FILL_PARENT - 120, (int) convertDpToPx(getApplicationContext(), 45)));
inputTop.setX(convertDpToPx(getApplicationContext(), 174));
inputTop.setY(convertDpToPx(getApplicationContext(), 60));
inputTopInner = new EditText(getApplicationContext());
inputTopInner.setLayoutParams(new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
inputTopInner.setGravity(Gravity.CENTER);
inputTopInner.setInputType(InputType.TYPE_CLASS_TEXT);
inputTopInner.setTextColor(Color.rgb(151,151,151));
inputTopInner.setTextSize(12);
inputTopInner.setHint("Standort");
inputTopInner.setEms(10);
inputTop.addView(inputTopInner);
layout.addView(viewTop);
layout.addView(inputTop);
Anybody know how to make this run as it does when inputted into the XML file?
This is how we did for our project:
We kept Parent as LinearLayout and add views in that layout
LiearLayout parentView = findViewById(R.id.parentView);
TextInputLayout emailTextInputLayout = new TextInputLayout(this, null, R.style.Widget_MaterialComponents_TextInputLayout_OutlinedBox);
emailTextInputLayout.setHint("Please Enter Email Address");
emailTextInputLayout.setBoxBackgroundMode(TextInputLayout.BOX_BACKGROUND_OUTLINE);
emailTextInputLayout.setBoxCornerRadii(5, 5, 5, 5);
TextInputEditText edtEmail = new TextInputEditText(emailTextInputLayout.getContext());
emailTextInputLayout.addView(edtEmail);
parentView.addView(emailTextInputLayout);
TextInputLayout passTextInputLayout = new TextInputLayout(this, null, R.style.Widget_MaterialComponents_TextInputLayout_OutlinedBox);
passTextInputLayout.setHint("Please Enter Password");
passTextInputLayout.setBoxBackgroundMode(TextInputLayout.BOX_BACKGROUND_OUTLINE);
passTextInputLayout.setBoxCornerRadii(5, 5, 5, 5);
TextInputEditText edtPass = new TextInputEditText(passTextInputLayout.getContext());
passTextInputLayout.addView(edtPass);
parentView.addView(passTextInputLayout);
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 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"
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, "");
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