1) Android Manifest.XML File
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="log.call.calllog">
<uses-permission android:name="android.permission.WRITE_CALL_LOG"/>
<uses-permission android:name="android.permission.READ_CALL_LOG"/>
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
<uses-permission android:name="android.permission.CALL_PHONE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
2) MainActivity.XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--Todo : MainActivity.XML File -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/linear_call"
android:layout_marginTop="5dp"
android:background="#E708F5">
<Button
android:layout_width="150dp"
android:layout_height="40dp"
android:background="#F7DC6F"
android:id="@+id/call_logs"
android:layout_marginLeft="25dp"
android:text="Call Logs"
android:textStyle="bold"/>
<Button
android:layout_width="150dp"
android:layout_height="40dp"
android:text="Calls"
android:id="@+id/call"
android:layout_marginLeft="10dp"
android:layout_marginRight="25dp"
android:background="#F7DC6F"
android:textStyle="bold"/>
</LinearLayout>
<!--For Change Screen -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/linear_call"
android:id="@+id/change_frame"
android:orientation="horizontal">
</LinearLayout>
</RelativeLayout>
3) MainActivity.JAVA
public class MainActivity extends Activity{
Button call, call_logs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
call_logs = (Button)findViewById(R.id.call_logs);
call = (Button)findViewById(R.id.call);
call_logs.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadFragment(new Call_Log());
}
});
call.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadFragment(new Call());
}
});
}
private void loadFragment(Fragment fr) {
// create a FragmentManager
FragmentManager fm = getFragmentManager();
// create a FragmentTransaction to begin the transaction and replace the Fragment
FragmentTransaction fragmentTransaction = fm.beginTransaction();
// replace the FrameLayout with new Fragment
fragmentTransaction.replace(R.id.change_frame, fr);
fragmentTransaction.commit(); // save the changes
}
}
4) Call_Log.XML
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="log.call.calllog.Call_Log">
<!-- TODO: Update blank fragment layout -->
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment"
android:id="@+id/tv_call_logs"
android:textSize="12dp"
android:textColor="#000000"
android:textStyle="bold"/>
</ScrollView>
</FrameLayout>
5) Call_Log.JAVA
public class Call_Log extends Fragment {
TextView call_logs;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_call__log, container, false);
ContentResolver resolver = getActivity().getContentResolver();
Context context = getActivity();
call_logs = (TextView) v.findViewById(R.id.tv_call_logs);
StringBuffer sb = new StringBuffer();
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_CALL_LOG) !=
PackageManager.PERMISSION_GRANTED)
{
final int REQUEST_CODE_ASK_PERMISSIONS = 123;
ActivityCompat.requestPermissions(getActivity(), new String[]{"android.permission.READ_CALL_LOG"}, REQUEST_CODE_ASK_PERMISSIONS);
ActivityCompat.requestPermissions(getActivity(), new String[]{"android.permission.WRITE_CALL_LOG"}, REQUEST_CODE_ASK_PERMISSIONS);
}
Cursor cur = context.getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, android.provider.CallLog.Calls.DATE + " DESC");
int number = cur.getColumnIndex( CallLog.Calls.NUMBER );
int type = cur.getColumnIndex(CallLog.Calls.TYPE);
int date = cur.getColumnIndex(CallLog.Calls.DATE);
int duration = cur.getColumnIndex( CallLog.Calls.DURATION);
sb.append( "Call Details : \n");
while ( cur.moveToNext() ) {
String phNumber = cur.getString( number );
String callType = cur.getString(type);
String callDate = cur.getString(date);
Date callDayTime = new Date(Long.valueOf(callDate));
String callDuration = cur.getString( duration );
String dir = null;
int dircode = Integer.parseInt(callType);
switch (dircode) {
case CallLog.Calls.OUTGOING_TYPE:
dir = "OUTGOING";
break;
case CallLog.Calls.INCOMING_TYPE:
dir = "INCOMING";
break;
case CallLog.Calls.MISSED_TYPE:
dir = "MISSED";
break;
}
sb.append( "\nPhone Number:--- "+phNumber +"\nCall Type:--- " + dir +" \nCall Date:--- " + callDayTime+" \nCall duration in sec :--- "+callDuration );
sb.append("\n----------------------------------");
}
cur.close();
call_logs.setText(sb);
return v;
}
}
6) Call.XML
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="log.call.calllog.Call">
<!-- TODO: Update blank fragment layout -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="97dp"
android:text="Android Call"
android:textStyle="bold"
android:textColor="#000000"
android:textSize="20dp"/>
<EditText
android:id="@+id/et_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="48dp"
android:ems="10"
android:inputType="phone"
android:hint="Phone Number"
android:gravity="center"/>
<Button
android:id="@+id/btn_call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/et_num"
android:layout_centerHorizontal="true"
android:layout_marginTop="37dp"
android:text="Call"
/>
</RelativeLayout>
</FrameLayout>
6) Call.JAVA
public class Call extends Fragment {
Button btn_call;
EditText et_num;
String phn_no;
private static final int MAKE_CALL_PERMISSION_REQUEST_CODE = 1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_call, container, false);
btn_call = (Button)v.findViewById(R.id.btn_call);
et_num = (EditText)v.findViewById(R.id.et_num);
if (checkPermission(Manifest.permission.CALL_PHONE)) {
btn_call.setEnabled(true);
} else {
btn_call.setEnabled(false);
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CALL_PHONE}, MAKE_CALL_PERMISSION_REQUEST_CODE);
}
btn_call.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
phn_no = et_num.getText().toString();
if (!TextUtils.isEmpty(phn_no)) {
if (checkPermission(Manifest.permission.CALL_PHONE)) {
String dial = "tel:" + phn_no;
// startActivity(new Intent(Intent.ACTION_CALL, Uri.parse(dial)));
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+phn_no));//change the number
startActivity(callIntent);
} else {
Toast.makeText(getActivity(), "Permission Call Phone denied", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getActivity(), "Enter a phone number", Toast.LENGTH_SHORT).show();
}
}
});
return v;
}
private boolean checkPermission(String permission) {
return ContextCompat.checkSelfPermission(getActivity(), permission) == PackageManager.PERMISSION_GRANTED;
}
}