Android make phone numbers clickable, autodetect - java

When I am using android on websites and reading emails, I notice that I can click on addresses to load into google maps, or click on phone numbers to call, or click on emails and send an email.
These elements on the web are formatted in a variety of ways, so there is some built in function that detects these sort of things.
How do I allow this within my app? I have a page which displays contact information in plain text and I would like the user to just be able to click.
Do I Absolutely need to create clicklisteners for each textview or is there a system function I just need to enable?

Use
android:autoLink="phone"
in textView in the xml layout file

Android has a utility expressly for this purpose: Linkify
TextView noteView = (TextView) findViewById(R.id.noteview);
noteView.setText(someContent);
Linkify.addLinks(noteView, Linkify.ALL);
See also: https://android-developers.googleblog.com/2008/03/linkify-your-text.html

import android.text.util.Linkify;
Linkify.addLinks(text, Linkify.PHONE_NUMBERS);

You can use it in TextView like this,
Set android:autoLink="phone" as below,
<TextView
android:layout_width="fill_parent"
android:id="#+id/text"
android:layout_height="wrap_content"
android:autoLink="phone"
android:gravity="center"
android:linksClickable="true"
android:text="#string/txtCredits" />
However,
For some reason above code does not work all time. So, add below code also,
TextView textView = (TextView) findViewById(R.id.text);
textView.setMovementMethod(LinkMovementMethod.getInstance());

android:autoLink="phone"
was working for me on all phones... except Samsung.
Therefore, I chose the following option. Transformed phone number texts to support click to call:
+49 / 30 123456789
and then used this static helper method to add web link support to my TextViews
public static void linkifyTextViews(#NonNull TextView... textViews) {
for (TextView textView : textViews) {
Linkify.addLinks(textView, Linkify.WEB_URLS);
textView.setMovementMethod(LinkMovementMethod.getInstance());
}
}

If you want to detect different patterns like emails, contact numbers, weblink and set a separate on click implementations for these patterns I suggest you to use CustomClickableEmailPhoneTextview
Sample Code to use the library.
CustomPartialyClickableTextview customPartialyClickableTextview= (CustomPartialyClickableTextview) findViewById(R.id.textViewCustom);
/**
* Create Objects For Click Patterns
*/
ClickPattern email=new ClickPattern();
ClickPattern phone=new ClickPattern();
ClickPattern weblink=new ClickPattern();
/**
* set Functionality for what will happen on click of that pattern
* In this example pattern is email
*/
email.setOnClickListener(new ClickPattern.OnClickListener() {
#Override
public void onClick() {
Toast.makeText(MainActivity.this,"email clicked",Toast.LENGTH_LONG).show();
}
});
/**
* set Functionality for what will happen on click of that pattern
* In this example pattern is phone
*/
phone.setOnClickListener(new ClickPattern.OnClickListener() {
#Override
public void onClick() {
Toast.makeText(MainActivity.this,"phone clicked",Toast.LENGTH_LONG).show();
}
});
/**
* set Functionality for what will happen on click of that pattern
* In this example pattern is weblink
*/
weblink.setOnClickListener(new ClickPattern.OnClickListener() {
#Override
public void onClick() {
Toast.makeText(MainActivity.this,"website clicked",Toast.LENGTH_LONG).show();
}
});
/**
* set respective regex string to be used to identify patter
*/
email.setRegex("\\b[A-Z0-9._%+-]+#[A-Z0-9.-]+\\.[A-Z]{2,4}\\b"); // regex for email
phone.setRegex("[1-9][0-9]{9,14}"); // regex for phone number
weblink.setRegex("^(https?|ftp|file)://[-a-zA-Z0-9+&##/%?=~_|!:,.;]*[-a-zA-Z0-9+&##/%=~_|]"); // regex for weblink
/**
* add click pattern to the custom textview - first parameter is tag for reference second parameter is ClickPattern object
*/
customPartialyClickableTextview.addClickPattern("email",email);
customPartialyClickableTextview.addClickPattern("phone",phone);
customPartialyClickableTextview.addClickPattern("weblink",weblink);

Related

Android activity: Generating a string value with an flow-based node system

I want to create an activity in which you can insert/remove/move/connect nodes between each others and based on them to generate a string value that would be later sent through Bluetooth to an other device.
Something like this
And the resulting string should look like:
`"do[i<0-2>]:
{case[i]:
{0:"Hello ",1:"World",2:"!"}
}"
My problem is that I have no idea how to start creating the view where the nodes will be placed and the nodes themselves
I think that the "workspace" should be just a simple empty view where you can pan and zoom in/out
But for the nodes I have no idea where to start because they need to be able to have multiple inputs/outputs... maybe I need to create a custom veiw/component but like i said :( i don't know how to start
Thanks for the help in advance!
EDIT:
I have decided to use Google's Blockly to generate the string, I have customized the block the way I need to generate the string, but I can't figure it out how to get the "code" generated as a string so I can use it later... does anyone has an idea?
Blockly for Android uses a CodeGenerationRequest.CodeGeneratorCallback to pass the code string back to the application.
See this example from the TurtleActivity:
private final CodeGenerationRequest.CodeGeneratorCallback mCodeGeneratorCallback =
new CodeGenerationRequest.CodeGeneratorCallback() {
#Override
public void onFinishCodeGeneration(final String generatedCode) {
// Sample callback.
Log.i(TAG, "generatedCode:\n" + generatedCode);
Toast.makeText(getApplicationContext(), generatedCode,
Toast.LENGTH_LONG).show();
mHandler.post(new Runnable() {
#Override
public void run() {
String encoded = "Turtle.execute("
+ JavascriptUtil.makeJsString(generatedCode) + ")";
mTurtleWebview.loadUrl("javascript:" + encoded);
}
});
}
};

How to change android:hint dynamically?

I want to change android:hint depending on each model.
(Android device might have different sdcard path. I want to set this value.)
How can I get the element or change the attribute?
Such as
Preferences.java
public class Preferences extends PreferenceActivity {
private void setDeviceSDCardPath () {
String defaultPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
pref = getElementById(R.preferences.pref_id); // <--- invalid, but I want to do like this
pref.hint = "ex.) " + defaultPath; // <-- also invalid
}
}
preferences.xml
<PreferenceCategory android:title="#string/title">
<EditTextPreference
android:id="#+id/pref_id"
android:key="pref_key"
android:title="#string/pref_title"
android:summary="#string/pref_summary"
android:hint="/storage/sdcard"
android:defaultValue="/storage/sdcard/Download/"/>
</PreferenceCategory>
your_editText.setHint("Your New hint");
Check this doc
myTextView.setHint("My conditional hint");
See the javadoc for EditTextPreference:
http://developer.android.com/reference/android/preference/EditTextPreference.html
It is a subclass of DialogPreference and shows the EditText in a
dialog. This EditText can be modified either programmatically via
getEditText(), or through XML by setting any EditText attributes on
the EditTextPreference.
When you got the EditText object by using getEditText() on your EditTextPreference object you can simply use setHint(String message) method. I believe this should work:
EditTextPreference editPref = (EditTextPreference) findPreference(R.id.pref_id);
editPref.getEditText().setHint(message);
yout_editText.setSummary("some description to your preference edit text");
in xml: android:defaultValue or android:summary

Getting listview ID name

I'm passing a listview in to an onselect but theres a couple of ways it's called from different listviews. So i'm trying to work out which listview is being clicked.
I thought I could do the following however the string thats returned is like com.myapp.tool/id/32423423c (type thing) instead of lvAssets.
Here is what I've got:
#Override
public void onNumberRowSelect(ListView listview, clsNameID stat) {
if(listview.getAdapter().toString().equals("lvGenericAssets")){
} else if(listview.getAdapter().toString().equals("lvAssets")){
} else {
Functions.ShowToolTip(getApplicationContext(),
listview.getAdapter().toString());
}
}
As Emil Adz said in first, you can get the id of your list by calling list.getId();
Then use String idList = getResources().getResourceEntryName(id); and you will be able to get the name of the id you have given to your list
Why wont you just use: list.getId(); if you defined it in the XML file then you should define there an id for you ListView.
If you are doing this from code then you can use the list.setId(); to first set it's id.
Another thing you can do is to add a Tag to your listView: list.setTag("list1");
and latter on distinct this listView using the Tag: list.getTag();

GWT - how to get the tabText of selected tab?

I want to get the tab text when I click on a tab. I do this:
tabPanel.addSelectionHandler(new SelectionHandler<Integer>() {
#Override
public void onSelection(SelectionEvent<Integer> event) {
//get the tabtext here
}
});
But I only get the index.
Assuming you are using TabPanel and you haven't provided a custom Widget for the TabBar, you could do this:
tabPanel.addSelectionHandler(new SelectionHandler<Integer>() {
#Override
public void onSelection(SelectionEvent<Integer> event) {
String tabHtml = tabPanel.getTabBar().getTabHTML(event.getSelectedItem());
}
});
Of course, you will get the underlying HTML of the tab, that generally is a <div>tab text</div>. The text you put in the add() methods are wrapped in either a Label, or an HTML widget, whether you have chosen to display the tab text as HTML.
Of course this is not handy, generally you need to store somewhere the tab text (in a TabPanel extension I'd guess, or a model) at insertion time (overriding the add(...)s) and retrieve it when needed (by adding a simple getter for them).
You can get the selected tab by following.
tabPanel.getElement().getTitle();

Inserting EditText & Spinner data to SQLite?

I'm trying to figure out how to capture data from a form using EditText & Spinner's and insert it into a SQLite database. I am able to write the hard coded attributes but when I try to use R.id.fieldName it throws an error due to being an Integer vice a String.
public class PetAdd extends Activity {
DBAdapter db = new DBAdapter(this);
private OnClickListener btnPetAddListener = new OnClickListener() {
#Override
public void onClick(View arg0) {
db.open();
long id;
id = db.insertPet("name", "type", "breed", "sex", "notes");
/**id = db.insertPet(R.id.petName, R.id.SpinnerPetType, R.id.petBreed, R.id.SpinnerPetGender, R.id.EditTextPetAddOptions);*/
db.close();
}
};
I'm still trying to learn all this stuff and my brain is fried from looking at a plethora of online tutorials, examples and Google documentation. If anyone can show me how to do this or direct me to a barney style tutorial that breaks it down for me to understand what's going on, it'd be greatly appreciated.
R.id.fieldName is a numeric reference to the item in your Activity (provided it's part of your layout).
You'll need to call findViewById(R.id.fieldName) to get a refererene to it. You'll also need to cast it to the correct type of view (in your case EditText) and then call getText().toString() on the whole thing.
Putting it all together...
EditText myField = (EditText)findViewById(R.id.userName); //assuming you have a field named userName in your XML
String userNameValue = myField.getText().toString();
Oh, and welcome to Stack... don't forget to mark answers as correct and up-vote them when they're helpful.
If you use R.id.name you are in fact using internally generated int that Android uses. You need the raw data your spinner has.
I suggest you play with getItem and getItemId in your Spinner. If you are using a SimpleAdapter you can expect to get the ID of your item with getItemId.
The implementation of getItem is up to you. I usually use BaseAdapter or in the case of Spinners, ArrayAdapter, which has several convenient methods.
And with the EditText you need to call getText() to the EditText.

Categories