At Last, I can find a solution for my request. As @basant_matharu mentioned at the comment, I extent TextView element that you can see that in below code:
public class LinkEnabledTextView extends TextView {
// Pattern for gathering *140*1# from the Text
Pattern ussdPattern = Pattern.compile("(\\*[0-9]+[\\*[0-9]+]*#)");
private TextLinkClickListener mListener;
private ArrayList<Hyperlink> listOfLinks;
public LinkEnabledTextView(Context context, AttributeSet attrs) {
super(context, attrs);
listOfLinks = new ArrayList<Hyperlink>();
}
public void setText(String text) {
SpannableString linkableText = new SpannableString(text);
gatherLinks(listOfLinks, linkableText, ussdPattern);
for (Hyperlink linkSpec : listOfLinks) {
// this process here makes the Clickable Links from the text
linkableText.setSpan(linkSpec.span, linkSpec.start, linkSpec.end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
// sets the text for the TextView with enabled links
super.setText(linkableText);
}
public void setOnTextLinkClickListener(TextLinkClickListener newListener) {
mListener = newListener;
}
private void gatherLinks(ArrayList<Hyperlink> links, Spannable s, Pattern pattern) {
Matcher m = pattern.matcher(s);
while (m.find()) {
int start = m.start();
int end = m.end();
Hyperlink spec = new Hyperlink();
spec.textSpan = s.subSequence(start, end);
spec.span = new InternalURLSpan(spec.textSpan.toString());
spec.start = start;
spec.end = end;
links.add(spec);
}
}
public interface TextLinkClickListener {
public void onTextLinkClick(View textView, String clickedString);
}
/**
* Class for storing the information about the Link Location
*/
public class InternalURLSpan extends ClickableSpan {
private String clickedSpan;
public InternalURLSpan(String clickedString) {
clickedSpan = clickedString;
}
@Override
public void onClick(View textView) {
mListener.onTextLinkClick(textView, clickedSpan);
}
}
class Hyperlink {
CharSequence textSpan;
InternalURLSpan span;
int start;
int end;
}
}
In this class overridden setText() method for recognize any patterns like USSD codes. Also There is an interface named TextLinkClickListener that help us to call recognizes USSD code:
public interface TextLinkClickListener {
public void onTextLinkClick(View textView, String clickedString);
}
Use custom class instead of TextView:
<com.example.test.custom_textview.LinkEnabledTextView
android:id="@+id/txtMessage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="@android:color/black"
android:autoLink="all"/>
And In your activity(for example):
LinkEnabledTextView textView = (LinkEnabledTextView) findViewById(R.id.txtMessage);
textView.setOnTextLinkClickListener(this);
textView.setText(text);
Everywhere you want to call USSD code, you should write below code and call LinkEnabledTextView listener:
public void onTextLinkClick(View textView, String clickedString) {
String ussdCode = clickedString.substring(0, clickedString.indexOf("#")) + Uri.encode("#");
startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + ussdCode)));
}
Oh, be aware write call permissiion in Manifest file:
<uses-permission android:name="android.permission.CALL_PHONE" />