The application in this case is the Telegram and it works with a simple system of reference to users, channels, groups.
I would like that by clicking on the hyperlink (clickable textView) that is with the @Fulano user link, it would open the Telegram and direct it to the user in question.
If the Telegram was not installed, the corresponding link on Google Play was opened, so the user could install it.
I've seen this kind of thing happen in other applications, even in YouTube comments and in my smatphone's own browser. The option to "open with" appears.
----------------------------------------------- -------------------------------------------------- ------
Edit:
I tried this way, with the code suggested by the ramaral, this time had no running errors, but with the test device that owns the telegram, did not open the same but the camera .
Activity:
package genesysgeneration.hl;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView tvLink03;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvLink03=(TextView)findViewById(R.id.tvLink03);
PackageLinkMovementMethod.makerHyperlink(tvLink03);
tvLink03.setMovementMethod(new PackageLinkMovementMethod("org.telegram.messenger"));
}
}
Inherited class (PackageLinkMovementMethod):
package genesysgeneration.hl;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.URLSpan;
import android.view.MotionEvent;
import android.widget.TextView;
public class PackageLinkMovementMethod extends LinkMovementMethod{
private String packageName;
public PackageLinkMovementMethod(String packageName){
}
@Override
public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event){
if (event.getAction()==MotionEvent.ACTION_UP){
Context context = widget.getContext();
Intent laucnkIntent = context.getPackageManager().getLaunchIntentForPackage(packageName);
if (laucnkIntent==null){
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id="+ packageName));
context.startActivity(intent);
}else {
context.startActivity(laucnkIntent);
}
return true;
}
return super.onTouchEvent(widget, buffer, event);
}
public static void makerHyperlink(TextView textView){
SpannableStringBuilder ssb = new SpannableStringBuilder();
ssb.append(textView.getText());
ssb.setSpan(new URLSpan("#"), 0, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(ssb, TextView.BufferType.SPANNABLE);
}
}