Open Hyperlinks of Textview in WebView Android - java

The following is the text that I have to set in text view. I wanted to open webview on click of hyperlink. Rest of the text should not be clickable.
String value = "Check on this link: Go to Google";
binding.text.setText(value);
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/black"
android:autoLink="web"
android:textColorLink="#color/g_turquoise_blue" />
Thanks in advance.

This will help you if i got you correct
yourTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String url = "https://google.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
UPDATE:
Try this
/**
* Returns a list with all links contained in the input
*/
public static List<String> extractUrls(String text)
{
List<String> containedUrls = new ArrayList<String>();
String urlRegex = "((https?|ftp|gopher|telnet|file):((//)|(\\\\))+[\\w\\d:##%/;$()~_?\\+-=\\\\\\.&]*)";
Pattern pattern = Pattern.compile(urlRegex, Pattern.CASE_INSENSITIVE);
Matcher urlMatcher = pattern.matcher(text);
while (urlMatcher.find())
{
containedUrls.add(text.substring(urlMatcher.start(0),
urlMatcher.end(0)));
}
return containedUrls;
}
Example:
List<String> extractedUrls = extractUrls("Welcome to https://stackoverflow.com/ and here is another link http://www.google.com/ \n which is a great search engine");
for (String url : extractedUrls)
{
System.out.println(url);
}
Prints:
https://stackoverflow.com/
http://www.google.com/
source: Detect and extract url from a string?

Related

Make HTML anchor links start a new activity in Android Java

I have a String that contains HTML tags which I display in a TextView
Spanned text;
String htmlText = "<p dir=\"ltr\">It was a great day with Julius and Stanley</p>";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
text = Html.fromHtml(htmlText, Html.FROM_HTML_MODE_COMPACT);
else
text = Html.fromHtml(htmlText);
textView.setText(text);
Now I want those links to be clickable, and not just clickable, but to startup an Activity when clicked. The href attributes have numbers which I want to pass as a parameter to my Intent to start my Activity.
I use JSoup to extract the values of the href like this:
Document doc = Jsoup.parse(htmlText, "UTF-8");
Elements elements = doc.getElementsByTag("a");
for(int e = 0; e < elements.size(); e++){
Element element = elements.get(e);
String href = element.attr("href");
}
So I was hoping I can get a ClickEventListener on the links and start the activity like this:
element.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, NewActivity.class);
Bundle userParams = new Bundle();
userParams.putString("userId", href);
intent.putExtras(userParams);
startActivity(intent);
}
});
I know element is not a ViewGroup so it can't be done like my code shows, but is there any possible way of achieving this?
Search and I got an answer from here
String htmlText = "<p dir=\"ltr\">It was a great day with Julius and Stanley</p>";
CharSequence sequence = Html.fromHtml(htmlText);
SpannableStringBuilder strBuilder = new SpannableStringBuilder(sequence);
URLSpan[] urls = strBuilder.getSpans(0, sequence.length(), URLSpan.class);
for(URLSpan span : urls) {
makeLinkClickable(strBuilder, span);
}
textView.setText(strBuilder);
textView.setMovementMethod(LinkMovementMethod.getInstance());
And then the makeLinkClickable method to handle the links
protected void makeLinkClickable(SpannableStringBuilder strBuilder, final URLSpan span){
int start = strBuilder.getSpanStart(span);
int end = strBuilder.getSpanEnd(span);
int flags = strBuilder.getSpanFlags(span);
ClickableSpan clickable = new ClickableSpan() {
public void onClick(View view) {
String href span.getURL();
Intent intent = new Intent(context, NewActivity.class);
Bundle userParams = new Bundle();
userParams.putString("userId", href);
intent.putExtras(userParams);
startActivity(intent);
}
};
strBuilder.setSpan(clickable, start, end, flags);
strBuilder.removeSpan(span);
}

Get Chip Text on click chip

I am new to android and trying to play with chips. I have chip group like this in xml
<com.google.android.material.chip.ChipGroup
android:padding="10dp"
android:id="#+id/tags"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:chipSpacing="10dp">
</com.google.android.material.chip.ChipGroup>
I am dynamically adding chip like this in it.
Chip chip = new Chip(getLayoutInflater().getContext());
for(String genre : tags_text) {
chip.setText(genre);
tags.addView(chip);
}
Adding chip is working fine. Now I want get chip text of clicked chip. I am trying to get it like this
chip.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int chipsCount = tags.getChildCount();
String TagText ="";
int i = 0;
while (i < chipsCount) {
Chip chip = (Chip) tags.getChildAt(i);
if (chip.isChecked() ) {
TagText = chip.getText().toString();
}
i++;
};
Log.e("CHIPS", TagText+"-OK");
}
});
But I am getting empty text always. let me know if someone can help me for solve it.
Thanks!
You can use:
chip.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String text = ((Chip) view).getText().toString();
//...
}
});

checking number with regex if it is match or not

EditText view
<EditText
android:id="#+id/inputField"
android:layout_width="300dp"
android:layout_height="50dp"
android:hint="#string/input_placeholder"
android:textColorHint="#color/colorAccent"
android:inputType="number"
android:layout_centerHorizontal="true"
android:textColor="#color/colorPrimary"
android:textSize="25sp"
android:padding="10dp"
android:layout_marginTop="100dp"
android:gravity="center"/>
class for hold number
package com.example.kassammustapha.samplecode;
public class regexHolder{
String patten_one()
{
return "[2550]{1}[7]{1}[1]{1}[2-9]{1}\\d{6}";
}}
mainActivity
the problem is when user enter the number from editText from the view, i receive the number and convert it into string then i test with regular expression but it won't work i dont what is the problems.
regexHolder operatorPatterns = new regexHolder();
final Pattern tigoOne = Pattern.compile(operatorPatterns.patten_one());
Button mBtn =findViewById(R.id.loginBtn);
mBtn.setOnClickListener(
new View.OnClickListener()
{
EditText texEdit = findViewById(R.id.inputField);
TextView viewText = findViewById(R.id.operatorDisplay);
String content = texEdit.getText().toString();
Matcher tigoMatcher = tigoOne.matcher(content);
public void onClick(View view)
{
if (tigoMatcher.matches())
{
String message = "valid";
viewText.setText(message);
}else{
String message = "not valid";
viewText.setText(message);
}
}
}
);
First you need to fix your regex. From your attempted regex and your comments, I am guessing that you want this:
(?:255|0)71[2-9]\\d{6}
Start with either 255 or 0, followed by 71, followed by a digit that is in the range 2-9, followed by 6 other digits.
Secondly, fix your on click listener.
These two lines:
String content = texEdit.getText().toString();
Matcher tigoMatcher = tigoOne.matcher(content);
Will be run the instant you create the listener, at which point the text edit is empty. You need to move these lines to the onClick method:
new View.OnClickListener()
{
EditText texEdit = findViewById(R.id.inputField);
TextView viewText = findViewById(R.id.operatorDisplay);
public void onClick(View view)
{
String content = texEdit.getText().toString();
Matcher tigoMatcher = tigoOne.matcher(content);
if (tigoMatcher.matches())
{
String message = "valid";
viewText.setText(message);
}else{
String message = "not valid";
viewText.setText(message);
}
}
}

Android sent string from one activity to another not working

I want to ask something that maybe simple, but I still can't find the problem.
I sent a string from one activity to another activity, but its show nothing.
I already checked it several times, and I think there's nothing wrong with my code.
Here's My first java code ( FoodFilter.class)
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String name = ((TextView) view.findViewById(R.id.filter_type))
.getText().toString();
String cost = ((TextView) view.findViewById(R.id.item_list))
.getText().toString();
String description = ((TextView) view.findViewById(R.id.category_name))
.getText().toString();
// Starting single contact activity
Intent in = new Intent(getApplicationContext(),
FoodFilterDetail.class);
in.putExtra(TAG_NAME, name);
in.putExtra(TAG_EMAIL, cost);
in.putExtra(TAG_PHONE_MOBILE, description);
startActivity(in);
}
});
This is the FoodFilterDetail.class
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.foods_filterdetail);
TextView txtProduct = (TextView) findViewById(R.id.filterItemList);
Intent i = getIntent();
// getting attached intent data
String product = i.getStringExtra("TAG_NAME");
// displaying selected product name
txtProduct.setText(product);
}
Here's my xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:layout_marginLeft="7dip"
android:layout_marginRight="7dip" >
<TextView
android:id="#+id/filterItemList"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="15dip"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:background="#android:color/transparent"
android:text="Sort By"
android:textSize="20sp"
android:textStyle="bold" />
</RelativeLayout>
I get the data on FoodFilter.class from json parse and it works fine. I can show all the data on ListView, but when I send the string to FoodFilterDetail.class it show nothing and there's no error or warning in my code.
Is there anyone can help me to fix this? I've been searching the problem for whole day.
Thanks Before
use i.getStringExtra(TAG_NAME); instead of i.getStringExtra("TAG_NAME");
create one variable in FoodFilter.java
public static final String TAG_NAME = "name";
use this
in FoodFilter.java
in.putExtra(TAG_NAME, name);
and in FoodFilterDetail.java
String product = i.getStringExtra(FoodFilter.TAG_NAME);
Do the same for all other variable
I am assuming that you are defining TAG_NAME like this in the first activity:
public static final String TAG_NAME = "some_random_key";
Then you are setting it as the key for Intent like this:
in.putExtra(TAG_NAME, name);
And in the second activity you are trying to access it like this:
String product = i.getStringExtra("TAG_NAME");
Which will not work since the key is not "TAG_NAME" but "some_random_key". So to access it in the second activity you need to define TAG_NAME in the second activity as well just like you did in the first activity:
public static final String TAG_NAME = "some_random_key";
And then access the string like:
String product = i.getStringExtra(TAG_NAME);

Android open URL onclick certain button

How i can send people to my google play on click certain button ?
i already did this in activity_menu.xml
<Button
android:id="#+id/ratebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/morebutton"
android:layout_alignBottom="#+id/morebutton"
android:layout_toLeftOf="#+id/morebutton"
android:layout_toRightOf="#+id/aboutButton"
android:background="#drawable/more"
android:text="#string/rateBtn"
android:textColor="#color/White"
android:textColorHint="#color/White"
android:textSize="18sp" />
but in MenuActivity.java i couldn't know what should i do ?
Any help? Thanks
findViewById(R.id.ratebutton).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String url = "market://details?id=<package_name>";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
Button rate = (Button)findViewById(R.id.rate);
rate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i2=new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=yourpackagename"));
startActivity(i2);
}
});
// Enter package name of application in place of yourpackagename
In my signin.xml file I put this code:
<Button
android:id="#+id/privacypolicylink"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginBottom="50dp"
android:background="#android:color/transparent"
android:onClick="performSingUpMethod"
android:text="PolĂ­tica de Privacidad"
android:textColor="#A9A9A9"
android:textSize="15dp"
android:textStyle="bold" />
In my signin.java file I used this:
findViewById(R.id.privacypolicylink).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String url = "https://example.net/privacy_policy";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
It works for me. It displays a text in the grey color that I want it. The text is underlined and it is a link. The is the way it looks:
When I click on it, I see this:
Then I can open the link using a web browser on the phone and everything works correctly the way I needed it.
Declare button inside your activity:
Button btnRateButton;
Initialize btnRateButton inside onCreate method:
btnRateButton = (Button) findViewById(R.id.ratebutton);
Set on click listener:
btnRateButton.setOnClickListener(this);
Write onclick method:
#Override
public void onClick(View v) {
Intent i = null;
final String myPackage = getPackageName();
if(v.getId() == R.id.btnRateButton) {
i = new Intent(Intent.ACTION_VIEW,Uri.parse("market://details?id=" + myPackage));
startActivity(i);
}
}
Note that for above code to work, you need to have Play Store app installed on your device.

Categories