ACTION_CALL intent, with Linkify? - android

I have a couple Activities with TextViews that contain phone numbers. What I need is that when the user clicks on one, the phone dials the number. And by "dial", I mean dial, not bring up the dialer with the number preloaded, waiting for the user to click "Dial".
In other words, I want Intent.ACTION_CALL, not Intent.ACTION_DIAL.
Now keep in mind, I'm entirely new to Android development, and I'm just beginning to figure out how to put things together.
My first attempt was simple. In my XML:
<TextView
android:id="#+id/textPhoneNumber"
android:text="#string/the_phone_number"
android:autoLink="phone"
android:clickable="true"
android:onClick="clickPhone"
/>
Then in my java:
public void clickPhone(View view)
{
String phoneNumber = getString(R.string.the_phone_number);
Uri uri = Uri.parse("tel:" + phoneNumber);
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(uri);
startActivity(callIntent);
}
Having read more about how autolink is supposed to work, that's clearly more work than should be necessary, but it did work...
Until I had to do the same with a phone number that included letters. You know the kind: 1-800-BUY-MY-STUFF.
The problem, Linkify.PHONE_NUMBERS doesn't recognize strings containing letters as phone numbers, so my string would not be rendered as a link. The onClick would still file, though, and I'd still get the behavior I wanted.
So, I went digging into what autoLink actually did, and read up a bit on Linkify, and removed my onCLick and tried to use a custom pattern:
Pattern matchEverything = Pattern.compile("^.*$");
TextView textPhoneNumber = (TextView)findViewById(R.id.textPhoneNumber);
Linkify.addLinks(textPhoneNumbermatchEverything, "tel:");
Since my TextView contained only a phone number, I figured I'd keep the Regex simple. And this worked fine.
Except that I think I am getting Intent.ACTION_DIAL instead of Intent.ACTION_CALL.
What's the simplest way to get phone number to display in an Activity as a clickable link, and to have clicking that link dial the phone, instead of just bringing up the dialer?

You can just skip the whole linkify business by simulating the same behavior:
Use SpannableString to underline the text in TextView:
String udata="1-800-BUY-MY-STUFF";
SpannableString content = new SpannableString(udata);
content.setSpan(new UnderlineSpan(), 0, udata.length(), 0);
mTextView.setText(content);
then in XML, set the TextView's text color to #05c5cf
After that it's a matter of calling your clickPhone method.

Related

android SPAN_EXCLUSIVE_EXCLUSIVE not working properly

I am trying to set span on a SpannableStringBuilder using flag SPAN_EXCLUSIVE_EXCLUSIVE and I am facing problem on further editing the text to which I am setting span.
Expected behaviour
1: Original text.
2: Text added before.
3: Text added after with space.
Unexpected Behaviour on adding text after styled text
I don't want the added text to be styled, and want to know what am I doing wrong.
EDIT 1:
The issue is happening on Moto X Play, but is not reproduced on Nexus 5X. Still testing on other devices.
You just probably add text not the way you should. Use .insert() and .append() methods of SpannableStringBuilder to add additional text.
I just tried what you try to achieve and here is the result:
TextView hratkyTextView = (TextView) findViewById(R.id.spannableHratkyTextView);
final StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD); // Span to make text bold
// "Test text" part (in bold)
SpannableStringBuilder builder = new SpannableStringBuilder("Test text");
builder.setSpan(bss, 0, builder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
// Prepending "before" (non-bold)
builder.insert(0, "before");
// Appending " after_with_space" to the end of the string
builder.append(" after_with_space");
hratkyTextView.setText(builder);
Result:
Nexus 7 Emulator running MainActivity with this code
TL;DR: Using some IMEs, like Gboard, when adding a char directly after a word (without space) the IME will replace the whole word tric with trick instead of just appending the c.
Detailed asnwer: How IMEs work with editors.
How some IMEs dictate commands to editors
IMEs communicate with editors (e.g. EditText) through InputConnection interface where they can send commands following user input, and get current text.
Gboard IME works in the following way:
gets text before and after cursor
detects the currently "composing" word and asks the editor to highlight and remember it (usually results in the word being underlined - check screenshot below)
Being aware of the currently composing word enables many features like suggesting words or auto-correcting spelling.
Whenever a char is inputted by the user, Gboard will ask the editor to set the currently composing text to a new value, i.e. replace trick by tricky
After a space is inputted, Gboard will do a final replace of currently composing region, eventually auto-correcting spelling
Currently composing region is reset to the next word.
This unfortunately breaks what we would normally expect from SPAN_EXCLUSIVE_EXCLUSIVE.

How to unhyperlink links programmatically?

I have a large body of text that includes web urls and emails. I created a SpannableString object and used Linkify class to set up hyperlinks for the web urls and emails. However, perceived phone numbers also get linked. So even though I have instances of 5-digit numbers such as "12345", this numeric sequence also are being linked. When clicked, a prompt comes up asking to call the number. Linkify thnks it is a phone number. How can I go back through and un-link specific text that I know should not be linked?
Here is how I have linked everything
final SpannableString s = new SpannableString(context.getText(R.string.message));
Linkify.addLinks(s, Linkify.WEB_URLS);
textView.setText(s);
textView.setMovementMethod(LinkMovementMethod.getInstance());
Thank you in advance.

problem with linkify in android app

I have a phone number in a text view (thats the only thing I have there). I am using :
Linkify.addLinks(textView, Linkify.ALL);
However, the phone number is not being recognized by linkify. The number is of the format:
(123) 456-7890. I have also tried 1234567890 and 123.456.7890. Nothing works. Any help ?
thanks.
In your XML file of your Text View add an attribute android:phoneNumber="true"
Try this in the Click of the Text View :
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("tel:"
+ your_phone_number)));
Automatically the Text will have the property of Phone number.
Since you are trying something that is custom refer to this doc
http://developer.android.com/resources/articles/wikinotes-linkify.html
it will help you.

SearchView widget - any way to SELECT ALL text inside?

I'm using SearchView widget (new in Honeycomb). I can set initial text by setQuery, that works.
But is there some way to select all text inside (similar to EditText.selectAll)?
I'd like to give user preset text from previous search, yet easy way to type new text without need to delete old.
You just need to capture the inner EditText, then do anything you want.
EditText edit = (EditText)searchView.findViewById(R.id.search_src_text);
edit.selectAll();
A little late but i had a similar issue.
I'd like to give user preset text from previous search, yet easy way to type new text without need to delete old.
I found a solution that worked for me. You just need to access to the inner EditText and then select all the text in usual way.
To do this try something like the following.
Please note that my code is in c# not in Java but its similar.
int id = searchView.Context.Resources.GetIdentifier("android:id/search_src_text", null, null);
EditText editText = searchView.FindViewById<EditText>(id);
editText.SelectAll();
In Java it should be something like this:
int id = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
EditText editText = (EditText) searchView.findViewById(id);
editText.selectAll();
I have just testet it on API level 15! But i think it works on lower levels, too.
What you want to do works well on PCs, but but is not very convenient on touch devices.
I'd use suggestion provider to allow user to reuse recent searches, possibly with query refinement enabled.

Change Layout Of Text Created In Java [Android]

Ok right , i asked how to create a random number from 1-100 for android and i came to this
TextView tv = new TextView(this);
int random = (int)Math.ceil(Math.random()*101);
tv.setText("Your Number Is..."+ random );
What this does is create the default kinda "hello world" style text view and says "Your Number Is.... [Then Random Number]
My problem is that i cant change the layout of this text , because it is not defined in XML, if someone could tell me how to change the style , or like make the random number into a string so i could use it for any Textview layout that would be great ..
Thanks :)
If by change the style you mean the text color, text size, and you want to change them programmatically, have a look at the setTextColor and setTextSize methods.
More info here
If you want more advanced formatting to set programmatically, see this link.
The below example demonstrates how to make your text bold and italic.
tv.setText("Your Number Is..."+ random, TextView.BufferType.SPANNABLE );
Spannable myText = (Spannable) tv.getText();
myText.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC),0,myText.length(),0);
Edit:
Try the below for the android:textSize="100dp" and android:gravity="center" :
tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 100);
tv.setGravity(Gravity.CENTER);
Putting it into a string is easy.
String randomAsAString = Integer.toString(random)
You can then use the XML properties of the TextView to change its formatting, such as android:textSize="30dp" or android:textColor="#900".
By the way, if you're happy with the answer to your previous question, you should go back and mark an answer as "Accepted". That gives 'reputation' points to the person whose answer you accepted and closes the question so that people don't think you're still waiting for a better answer. You can read more about reputation in the FAQ.
Edit:
You can't reference the string entirely in xml while still giving it a random number. This is because the "#string/some_string" format only allows unchangeable strings. The execption to this is using parameters, e.g. setting the string as
<string name="random_number">The random number is %d</string>
Then you could call up that string using something like
yourTextView.setText(this.getString(R.string.random_number, random))
As for your other question about setting a background to a textView, that's also easy.
yourTextView.setBackgroundDrawable(R.drawable.....)
You should take advantage of Eclipse's autocomplete feature... it makes finding these commands a lot easier. For example, simply type the name of your TextView followed by a period, pause half a second for the list of options to come up, then "setB" and it should then filter the list to the three setBackground Drawable/Resource/Color options.
tv.setText(Html.fromHtml("Your number is: <b>" + random + "</b>"));
For basic HTML text-styling tags.
You could also do something like this.
Define your string in strings.xml like:
<string name="your_number_is">Your number is <xliff:g id="number">%s</xliff:g>.</string>
Create a TextView in a layout xml:
<TextView android:id="#+id/your_number_is"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/your_number_is"
android:gravity="center"
android:textSize="100dip"
/>
Then your code would look like:
TextView tv = (TextView) findViewById(R.id.your_number_is);
int random = (int)Math.ceil(Math.random()*101);
tv.setText(getString(R.string.your_number_is, random));
This will make it a lot easier when you later on would like to change your text or maybe localize your app.
if you have thead troubles use this:
new Thread(){
public void run(){
TextView v = (TextView)findViewById(R.id.mytext);
v.setText("TEST");
}
}.start();

Categories

Resources