I need to be able to select multiple contacts in Android. The flow is like this :
User clicks on a button which opens the Contacts application.
However, instead of being able to select a single contact, I need to be able to select multiple contacts (in the same launch of the intent).
If a contact has multiple phone numbers, I need the user to be able to choose which phone number he wants to select.
This feature is already present in my Samsung Android Phone (Running 2.3 Gingerbread) when I click on "Contacts" in the Messaging app. See screenshot below :
There is not built in way of doing this, so you need to do most of the work by yourself. Luckily, it's not that hard.
Display
To Display your contacts you can use either a listview with the multi-select choice mode, or you can create a custom adapter and bind it to a regular listview. I don't think the listview with multi-select will let you put anything other than text for each row, but you'd have to dig deeper to find out.
I've used the custom adapter method for something very similar (except the multiple phone numbers part). It's pretty easy to do and I find custom adapters are really useful in the long run.
Custom Adapter Listview Tutorial
With a custom adapter setup, you can create data objects with all the information for a person including their Name and Phone Number(s). In the getView of your Custom Adapter, you can decide what/how and where to display each piece of information.
Gathering Information
You'll need to use the ContactContract API to get information for your contacts.
Reading Contact Info
Reading ALL phone numbers for a Contact
You will have to write this all yourself. You can use the ContactsContract provider to query for all contacts with phone numbers, and then for the selected contact you can query for all phone numbers for that contact. You can display the results in activities or dialogs as you see fit.
Unfortunately this code isn't supported for all versions of android
I know it's kinda late but wanted to share this!
I found some incomplete code in the net and after cracking my head with it I finally found the answer!
Basically you launch the picker and let it return the data in the extras =]
There was no full answer in the net so hope it helps to some soul out there!
Enjoy:
public void pickContact(View v){
try {
Intent phonebookIntent = new Intent("intent.action.INTERACTION_TOPMENU");
phonebookIntent.putExtra("additional", "phone-multi");
startActivityForResult(phonebookIntent, PICK_CONTACT);
// PICK_CONTACT IS JUST AN INT HOLDING SOME NUMBER OF YOUR CHOICE
} catch (Exception e) {
e.printStackTrace();
}
}
public String getData(String contact, int which)
{
return contact.split(";")[which];
}
public void onActivityResult(int reqCode, int resultCode, Intent data) {
final int URI = 0;
final int NUMBER = 1;
if (RESULT_OK != resultCode) return;
Bundle contactUri = data.getExtras();
if (null == contactUri) return;
ArrayList<String> contacts = (ArrayList<String>)contactUri.get("result");
Toast.makeText(getApplicationContext(), getData(contacts.get(0),NUMBER), Toast.LENGTH_SHORT).show();
}
Related
I'm creating an android app which has selection of company and based on that I show cars and based on selection of particular car, I want to show some details of car e.g. capacity, power, engine oil etc. Now I'm using array to store all this car related information. e.g.
private static final String[] car1={"624CC","33bhp","10W40"}
private static final String[] car2={"1600CC","120bhp","10W40"}
To check the section, I've used multiple if-else statements for each car. And depending on the selection, I show value form array.
if(bn.equals("car 1"))
{
cap.setText(""+car1[0]);
powr.setText(""+car1[1]);
oil.setText(" "+car1[2]);
}
else if(bn.equals("car 2"))
{
cap.setText(""+car2[0]);
powr.setText(""+car2[1]);
oil.setText(" "+car2[2]);
}
else if(bn.equals("car 3"))
{
cap.setText(""+car3[0]);
powr.setText(""+car3[1]);
oil.setText(" "+car3[2]);
}
Now the problem is as the number of if-else have increased,I'm getting error of "Code too large" in android studio.
I was wondering if is there any way to replace these multiple if else statements with single, generalised statement. As you can see, in all if else, the code is the same, its just that array name is different.
I'm aware that I can use SQLite db, but I'll have to add all the values to it for all the cars again. So was wondering if I can use the same array that I've created. Any solution/suggestion will be really helpful
Probably in place of string comparison of names you can store the index of the value selected and use switch case to find out which car is selected. This should reduce the code also make it much more readable.
This might be a stretch, but I was wondering if it is possible to add functionality to an application that is already created for the android device. Specifically, I would like to send a broadcast whenever the user tries to make a new search in the internet browser. There might be another way to act only when the user searches the browser, but I thought this would be the easiest. If this isn't possible (or is completely the wrong way of going about this), please let me know. Any help is appreciated.
You can probably query the Browser provider and get search data from it. Here is some basic code on how to do it:
Cursor cursor = this.context.getContentResolver().query(Browser.SEARCHES_URI, null, null, null, null);
if (cursor.moveToFirst()) {
while (!cursor.isAfterLast()) {
final int indexDate = cursor.getColumnIndex(Browser.SearchColumns.DATE);
final int indexTerm = cursor.getColumnIndex(Browser.SearchColumns.SEARCH);
String date = cursor.getString(indexDate);
String term = cursor.getString(indexTerm);
cursor.moveToNext();
}
}
cursor.close();
Keep in mind that it would be better if you run queries on a separate Thread using the Loader framework.
I do not think there is a broadcast sent when a new search is performed.
In an Android app I've got a couple contacts from my contacts list. They can be either emails, phone numbers, or even other things. I now want to check which type it is and bind specific actions to them.
For example, if it is a type vnd.android.cursor.item/email_v2, I want to send a POST message with just the email field, and if it is a type vnd.android.cursor.item/phone_v2 I want to send a POST message with just the phone field.
Any ideas how I could check this?
I guess the way to go would be using overloading:
You implement multiple methods with different input parameters but the same name, such as:
checkContact(email_v2 email){ do things with email }
checkContact(phone_v2 phone){ do things with phone }
checkContact(String s){do things with random string }
I think you get my point.
If you want a simple if-statement, though:
if (contact instanceof vnd.android.cursor.item/email_v2){ do send }
You could try checking the class constant CONTENT_ITEM_TYPE for your different types, something like:
contact.CONTENT_ITEM_TYPE.equals("vnd.android.cursor.item/email_v2");
I want to get an int with the number of unread emails in the accounts of the device.
I have seen that there is a new way to do this using the "Gmail Labels Public API"
http://android-developers.blogspot.in/2012/04/gmail-public-labels-api.html
I have read the documentation and downloaded the sample application and it really works.
But I have two problems: (
My intention is to get an int with the number of unread conversations, i try this:
String unread = GmailContract.Labels.NUM_UNREAD_CONVERSATIONS.toString();
but not works, always returns "numUnreadConversations"
How I can get an int to this value?
My second problem is this: The code works correctly with the default count of my device, but I want to get the value with all unread conversations of all device gmail accounts
eally appreciate any help
thanks and regards
With GmailContract.Labels.NUM_UNREAD_CONVERSATIONS.toString(); you're just converting the enum to string.
You need a Cursor object to iterate through labels and then acquire the number with cursor.getInt(GmailContract.Labels.NUM_UNREAD_CONVERSATIONS)
Check this code for iterating through accounts and labels.
Now that PhoneGap is version 2.0, is there a (potentially undocumented) way to have a contact picker?
The docs make it seem like I'd have to write my own in JavaScript by requesting ALL the user's contacts, then building my own in-app contact picker.
http://docs.phonegap.com/en/2.0.0/cordova_contacts_contacts.md.html#Contacts
I've found a one-off plug-in for Android, but that's not helpful if there's no plug-in for iPhone, cause then I'd still have to write my own. I'm looking for a device agnostic method that says "let the user go pick a contact, then send them back here with that contact info"
I don't know whether you can use this solution for Android as well but for iPhone you can use the .chooseContact() method.
Example:
Choose a contact
function contactChooser(){
//The chooseContact method will open a new window with all you contacts
navigator.contacts.chooseContact(
//After picking a name you will receive the id of the chosen contact
function(id){
//In an options variable you can set some filter parameters
//In this example we will use the Id to receive the data of the chosen contact
var options = {
filter: ""+id
}
//In the fields variable we're going to set the fields we want to receive
//'*' = every data. More field values are explained
// here: http://bit.ly/T8YyuE
var fields = ['*'];
navigator.contacts.find(fields, onPickContactSuccess, onPickContactError, options);
}, null);
}
function onPickContactSuccess(contacts){
//contacts contains all data you've requested
var _name = contacts[0].name
alert('Last: '+_name.familyName+' First: '+_name.givenName);
}