0% found this document useful (0 votes)
505 views

Practical No 19

The document describes an Android application that allows users to view contacts. It includes the XML layout file activity_main.xml, which contains a button and edit text field. It also includes the MainActivity Java file, which defines the button click handler to request read contacts permission and then query the contacts provider if granted, logging all contact names and the total count. The application displays a button that when clicked will retrieve and display names of all contacts on the device if permission is given.

Uploaded by

atharvabutte03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
505 views

Practical No 19

The document describes an Android application that allows users to view contacts. It includes the XML layout file activity_main.xml, which contains a button and edit text field. It also includes the MainActivity Java file, which defines the button click handler to request read contacts permission and then query the contacts provider if granted, logging all contact names and the total count. The application displays a button that when clicked will retrieve and display names of all contacts on the device if permission is given.

Uploaded by

atharvabutte03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Practical No 19

Q1.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="showContact"
android:text="Get Contact"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/editTextTextPersonName"
android:layout_width="162dp"
android:layout_height="48dp"
android:ems="10"
android:inputType="textPersonName"
android:singleLine="false"
android:text="Atharva Butte"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java
package com.example.practical19;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.ContentResolver;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void showContact (View v){
Button b = (Button) v;
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this,new String[]
{Manifest.permission.READ_CONTACTS},6);
}else {
ContentResolver contentResolver = getContentResolver();
Uri u = ContactsContract.Contacts.CONTENT_URI;
Cursor cursor = contentResolver.query(u,null,null,null,null);
if(cursor.getCount()>0) {
while (cursor.moveToNext()) {
int cno =
cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
String nm = cursor.getString(cno);
Log.d("MainActivity",nm);
}
Log.d("MainActivity","Total Contact: "+cursor.getCount());
}
}
}
}

You might also like