How to put ListView contents to EditText?

I am new to Android programming and I am planning to create an app that will show contacts and call from it.

I have shown my contacts through a listview and what I am wishing to do is that when I click on a certain contact, it would pop up into my edittext and then I would click my call button. (picture shown) Screenshot of contacts and the edittext

updates Screenshot with call button

Is there any way for it?

activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/idLayout">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:layout_marginTop="20dp"
        android:text="@string/idContacts"
        android:textAlignment="center"
        android:textSize="15pt" />

    <EditText
        android:id="@+id/number"
        android:layout_width="match_parent"
        android:layout_height="51dp"
        android:layout_marginBottom="10dp"
        android:inputType=""
        android:labelFor="@+id/number"
        android:textSize="25sp" />

    <Button
        android:id="@+id/callBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:text="Call" />

    <ListView
        android:id="@+id/idList"
        android:layout_width="400dp"
        android:layout_height="188dp"></ListView>

</LinearLayout>

and this is my MainActivity.java

package com.example.spyk.testmessageandcall;

import android.Manifest;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {

    ListView listview;
    EditText numberTxt;
    private static final int PERMISSIONS_REQUEST_READ_CONTACTS = 100;
    Cursor c;
    ArrayList<String> contacts;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listview = (ListView) findViewById(R.id.idList);

        int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS);

        if (permissionCheck == PackageManager.PERMISSION_GRANTED){
            showContacts();
        }
        else{
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.READ_CONTACTS},
                    PERMISSIONS_REQUEST_READ_CONTACTS);
        }

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                this, android.R.layout.simple_list_item_1,contacts);
        listview.setAdapter(adapter);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            }
        });
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions,
                                           int[] grantResults){
        if(requestCode == PERMISSIONS_REQUEST_READ_CONTACTS){
            if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
                showContacts();
            }
            else{
                Toast.makeText(this, "Names will display if you grant permission", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void showContacts(){
        c = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null, ContactsContract.Contacts.DISPLAY_NAME + " ASC ");

        contacts = new ArrayList<String>();
        while (c.moveToNext()){

            String contactName = c.getString(c.getColumnIndex( ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String phNumber = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            contacts.add("Name: "+contactName + "\n" + "Phone #: " +phNumber);
        }
        c.close();
    }

}

Edit

What I need is when I click on a contact, only its number will be on the edittext, here is its variable:

phNumber, variable of the phone number

Upvotes: 1

Views: 64

Answers (1)

Radesh
Radesh

Reputation: 13555

just type this in onItemClick for change text of editText

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
             String[] splits = contacts.get(i).split(":");

             numberTxt.setText(splits[splits.length-1])
        }
    });

and for call in button click use this

startActivity(new Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", numberTxt.getText().toString(), null)));

Upvotes: 1

Related Questions