0

I used this code for a button, after that I am in the Contacts Activity:

btnPhonebook.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent pb = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
            startActivityForResult(pb, 1);
        }
    });

After that, there is a contact list with many contacts. Now I want that, whenever I clicked to a contact, a dialog is displayed. How can I do that. Could anyone help me because currently I have no issue how to make it. I have tried with this code but it did not work.

Dialog dialog = new Dialog(this);
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Choose a phone number");

            ListView lp = new ListView(this);
            lp.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, _listPhoneNumber));

            builder.setView(lp);
            dialog = builder.create();

Also I put the above code in onActivityResult() method.

Thanks in advance.

detno29
  • 2,113
  • 6
  • 19
  • 21

5 Answers5

0

Instead of using the Built in Intent to display contacts.. why don't you build the List yourself and then do what you plan to do.

OR

Did you set your dialog to .show()?

JoxTraex
  • 13,423
  • 6
  • 32
  • 45
0

It is very simple,

progDailog = ProgressDialog.show(loginAct,"Process ", "please wait....",true,true);

new Thread ( new Runnable()
{
     public void run()
     {
      // your loading code goes here
     }
}).start();

 Handler progressHandler = new Handler() 
 {

     public void handleMessage(Message msg1) 
     {

         progDailog.dismiss();
         }
 }
Lucifer
  • 29,392
  • 25
  • 90
  • 143
0

You cannot show Dialog in native Contacts app.

However you can fetch Contacts by yourself and show them in a ListView with CheckBox and proceed to next step.

Here is a simplest ListView to show contacts. you can edit it by yourself.

Community
  • 1
  • 1
Adil Soomro
  • 37,609
  • 9
  • 103
  • 153
0

I Have did this in my application how we can get contact detail from listview , using this Data you can Display your dialog.

    lv.setClickable(true);
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView <? > arg0, View arg1, int position, long arg3) {

        Object o = lv.getItemAtPosition(position);
        Toast.makeText(getApplicationContext(), o.toString(), Toast.LENGTH_LONG).show();
        String infoString = o.toString();
        String arr[] = infoString.split(",");
        String names[] = arr[1].split("=");
        id = Integer.parseInt(names[1]);
        System.out.println("info" + id);

        db = dh.getReadableDatabase();

        String select = "select * from '" + dh.tablename + "' WHERE adb_id='" + id + "' ";
        Cursor c = db.rawQuery(select, null);

        if (c.moveToFirst()) {

            name.setText(c.getString(1));
            address.setText(c.getString(2));
            contact.setText(c.getString(3));    
        }    
    }
});
Jesse
  • 8,605
  • 7
  • 47
  • 57
AMD
  • 1,662
  • 18
  • 39
-1

If you want to use dialog in contact then you have to make your own custom layout for contact.. See below code for example... on button click

public static final int NUMBER_SELECT = 1;
Intent intent = new Intent(clsBlockNumbers.this,CallLog_Activity.class);
startActivityForResult(intent,NUMBER_SELECT);

in the same Activity write/make onActivityResult

protected void onActivityResult(int requestCode, int resultCode, Intent data)
        {
            switch(requestCode) {
            case NUMBER_SELECT: 
                if (resultCode == RESULT_OK) {
                    String number = data.getStringExtra("SelectedNumber");
                    if(number == null)
                    {
                        Toast.makeText(this, "No Record found: ", Toast.LENGTH_LONG).show();
                    }
                    else
                    {
                        //Your code
                    }
                    break;
                }

            }

        }

CallLog_Activity.java

package com.demo;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;


public class CallLog_Activity extends Activity implements OnItemClickListener
{
    ArrayList<String> strAyyNumber,strAyyType,listNumber, strType, strAyyName ;

    private CallLogListAdapter adapter ;
    CallLog callLog;
    String noType;
    ListView listCallLog;
    private String[] listCallLog_arr={};
    Cursor cursor;
    String strArr;
    TextView tv, tv1, txtEmptyMsg;


    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.calllog_list);

        callLog = new CallLog();

        listCallLog = (ListView)findViewById(R.id.list);

        strAyyNumber = new ArrayList<String>();
        strAyyType = new ArrayList<String>();
        strAyyName = new ArrayList<String>();
        listNumber = new ArrayList<String>();
        strType = new ArrayList<String>();

        System.out.println("In Call log list activity");            
        try
        {

            final String[] projection = null;
            final String selection = null;
            final String[] selectionArgs = null;
            final String sortOrder = "DATE DESC";
            Cursor cursor = this.getContentResolver().query(
                    Uri.parse("content://call_log/calls"),
                    projection,
                    selection,
                    selectionArgs,
                    sortOrder);
            if (cursor != null) 
            {
                //Loop through the call log.
                while (cursor.moveToNext()) 
                { 
                    //Common Call Log Items
                    String callNumber = cursor.getString(cursor.getColumnIndex(android.provider.CallLog.Calls.NUMBER));
                    strAyyNumber.add(callNumber);

                    String callType = cursor.getString(cursor.getColumnIndex(android.provider.CallLog.Calls.TYPE));
                    strAyyType.add(callType);

                    String callName = cursor.getString(cursor.getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME));
                    strAyyName.add(callName);



                }               

                for(int i=0;i<strAyyNumber.size();i++)
                {
                    String no = strAyyNumber.get(i).toString();//.concat("\n").concat(strAyyType.get(i).toString());
                    Log.d("No length ", "No length ::" + no.length());
                    listNumber.add(no);
                }


                listCallLog_arr = listNumber.toArray(new String[listNumber.size()]);

                Log.d("size", "list listCallLog_arr"+ listCallLog_arr.length);

                if(!listNumber.isEmpty())
                {
                    listCallLog.setVisibility(View.VISIBLE);
                    adapter = new CallLogListAdapter(CallLog_Activity.this,R.layout.calllog_list_row, listCallLog_arr,strAyyNumber,strAyyType,strAyyName);
                    listCallLog.setAdapter(adapter);
                }
                else
                {   
                    txtEmptyMsg = (TextView)findViewById(R.id.txtEmptyMsg);
                    txtEmptyMsg.setVisibility(View.VISIBLE);
                    txtEmptyMsg.setText("No Record found Press Back to Continue");
                }
                listCallLog.setOnItemClickListener(this);

            }
            listCallLog.setOnItemClickListener(this);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }


    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View v, int position, long id)
    {

        String o = arg0.getItemAtPosition(position).toString();
        Intent returnIntent = new Intent();

        StringBuffer sb = new StringBuffer(o);
        sb.reverse().setLength(10);

        Log.d("Item click", "String buffer"+ sb);

        String revercenum = sb.toString().trim();

        Log.d("Item click", "revercenum "+ revercenum);

        StringBuffer sb1 = new StringBuffer(revercenum);
        sb1.reverse();
        Log.d("Item click", "sb1 "+ sb1);

        String revercenum1 = sb1.toString().trim();
        revercenum1.replace("+", "");
        Log.d("Item click", "revercenum 1"+ revercenum1);

        returnIntent.putExtra("SelectedNumber",revercenum1.replace("+", ""));
        setResult(RESULT_OK,returnIntent);      
        finish();
    }
}

colllog_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<ListView
android:id="@+id/list"
android:layout_width="fill_parent" android:layout_height="fill_parent" 
android:visibility="gone"
/>

<TextView android:id="@+id/txtEmptyMsg"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:textStyle="bold"
        android:textSize="25dp"
        android:text=""
        android:visibility="gone"
    /> 

</LinearLayout>

colllog_list_row.xml

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal">


        <LinearLayout 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">


                <TextView
                    android:id="@+id/txtCallLogName"
                    android:layout_width="wrap_content" android:layout_height="fill_parent" 
                    android:textSize="20dp" android:layout_margin="10dp" 
                    android:layout_weight="1" />

                <TextView
                    android:id="@+id/txtCallLogNumber"
                    android:layout_width="wrap_content" android:layout_height="fill_parent" 
                    android:textSize="20dp" android:layout_weight="1"
                    android:layout_marginLeft="10dp" android:layout_marginBottom="10dp" 
                    android:layout_marginTop="5dp" android:layout_marginRight="10dp"    />

        </LinearLayout>

        <TextView
            android:id="@+id/txtCallLogType"
            android:layout_width="wrap_content" android:layout_height="fill_parent" 
            android:textSize="12dp"  android:layout_marginBottom="10dp" 
            android:layout_marginTop="10dp" android:layout_marginRight="10dp"
            android:layout_alignParentRight="true"  />

</RelativeLayout>

CallLogListAdapter.java

package com.Demo;

import java.util.ArrayList;

import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class CallLogListAdapter extends ArrayAdapter<Object>
{
    private static final String TAG = "CallLogListAdapter";
    private LayoutInflater inflater = null;
    private int resource;
    private Activity activity;
    CallLog callLog ;
    String[] strTemp;
    ArrayList<String> arrayItem = new ArrayList<String>();
    ArrayList<String> ayyType = new ArrayList<String>();
    ArrayList<String> tempType = new ArrayList<String>();
    ArrayList<String> ayyName = new ArrayList<String>();
    ArrayList<String> tempName = new ArrayList<String>();
    ArrayList<String> tempName1 = new ArrayList<String>();

    ArrayList<String> tempNo = new ArrayList<String>();

    String strType, strName, strName1, strNo ;
    String[] tmpName, tmpName1, tmpNo;

    public CallLogListAdapter(Activity activity, int resorce, String[] strTemp, ArrayList<String> arryListNumber, ArrayList<String> arryListType, ArrayList<String> arryListName) 
    {
        super(activity, resorce,strTemp);
        this.resource = resorce;
        this.activity = activity;
        this.strTemp = strTemp;
        Log.d("in adapter", "In Adapter");
        arrayItem = arryListNumber;
        ayyType = arryListType;
        ayyName = arryListName;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        Log.d("in adapter", "In get View");

        ViewHolder holder;

        if (convertView == null)
        {

            LayoutInflater layoutInflater = LayoutInflater.from(getContext());
            convertView  = layoutInflater.inflate(resource, parent, false);
            holder = new ViewHolder();

            holder.txtName = (TextView)convertView.findViewById(R.id.txtCallLogName);
            holder.txtNumber = (TextView)convertView.findViewById(R.id.txtCallLogNumber);
            holder.txtType = (TextView)convertView.findViewById(R.id.txtCallLogType);

            holder.txtName.setVisibility(View.VISIBLE);
            try
            {
                for(int i=0;i<ayyName.size();i++)
                {
                    strName = ayyName.get(i);
                    Log.d("in get view in  ", "Name is: **"+ strName);
                    tempName.add(strName);  
                }

                tmpName = tempName.toArray(new String[tempName.size()]);
                if(tmpName[position] == null)
                {
                    holder.txtName.setVisibility(View.GONE);                
                }
                else
                {
                    holder.txtName.setVisibility(View.VISIBLE);
                    holder.txtNumber.setTextSize(12);
                    holder.txtName.setText(""+ tmpName[position]);
                }


            }
            catch (NullPointerException e) 
            {
                e.printStackTrace();
            }


            for(int i=0;i<arrayItem.size();i++)
            {
                strNo = arrayItem.get(i);//.toString();
                Log.d("in get view in  ", "Number is: **"+ strNo);
                tempNo.add(strNo);
            }

            String[] tmpNo = tempNo.toArray(new String[tempNo.size()]);

            Log.d("in get view ", "Number is String[]** : "+ tmpNo[position]);
            holder.txtNumber.setText(""+ tmpNo[position]);

            for(int i=0;i<ayyType.size();i++)
            {
                strType = ayyType.get(i).toString();

                if(strType.equalsIgnoreCase("1"))
                {
                    strType = "Incoming Call";
                }
                else if(strType.equalsIgnoreCase("2"))
                {
                    strType = "Outgoing Call";

                }
                else if(strType.equalsIgnoreCase("3"))
                {
                    strType = "Missed Call";
                }
                tempType.add(strType);
            }

            String[] tmpType = tempType.toArray(new String[tempType.size()]);

            holder.txtType.setText(""+ tmpType[position]);
            convertView.setTag(holder);

        } else {
             holder=(ViewHolder)convertView.getTag();
        }
        return convertView;
    }

    public static class ViewHolder
    {
        private TextView txtNumber, txtType, txtName;
    }

}

I hope it may be help you.. :)

anddev
  • 3,144
  • 12
  • 39
  • 70
  • Thanks very much for your code. But I still do not understand: Where and What are the private CallLogListAdapter adapter and CallLog callLog; in the CallLogActivity.java Thanks. – detno29 Feb 06 '12 at 08:22
  • Oh it is now ok, let me check it out... Thanks so much – detno29 Feb 06 '12 at 08:33
  • You are most welcome.. If it will really helps you then make it correct so that other can use it. – anddev Feb 06 '12 at 08:36
  • sorry, i saw that this code display the callLog- Uri.parse("content://call_log/calls"), how can I get the phonebook list? – detno29 Feb 06 '12 at 08:37
  • Yes it is the example of CallLog list if you want to use contacts then it must be different URI for that.. You have to find it out.. :( – anddev Feb 06 '12 at 08:38
  • You can refere this http://stackoverflow.com/questions/866769/how-to-call-android-contacts-list – anddev Feb 06 '12 at 08:41
  • That is not a code snippet, that a full working program, save yourself the trouble and **ONLY** post what is necessary. Also just so you know that doesn't answer the question – JoxTraex Feb 06 '12 at 16:44