Thursday, 24 August 2017

Android Call Logs And Make A New Call






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;
    }
}

Thursday, 10 August 2017

Android Rotate Image







1) Manifests.xml


        <activity android:name=".MainActivity">
           <!-- <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>-->
        </activity>

        <activity android:name=".Splashscreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>


2) Splashscreen.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="language.rotateimage.Splashscreen">


    <TextView android:text="@string/app_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:textStyle="bold"
        android:textSize="45sp"
        android:id="@+id/txt"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="28dp" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/android_icon" />

    <!--SplashScreen Xml File-->

</RelativeLayout>

3) Splashscreen.java
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class Splashscreen extends AppCompatActivity {

    ImageView imageView ;
    Animation animation_1 ;

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

        imageView = (ImageView)findViewById(R.id.imageView);
        animation_1 = AnimationUtils.loadAnimation(Splashscreen.this,R.anim.rotate);
         imageView.startAnimation(animation_1);
        animation_1.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                imageView.startAnimation(animation_1);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                imageView.startAnimation(animation_1);
                finish();
                Intent i = new Intent(getBaseContext(),MainActivity.class);
                startActivity(i);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    /*Todo : Thanks For Watching*/

}

4) MainActivity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="language.rotateimage.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Welcome"
        android:textColor="#000000"
        android:textSize="35sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="219dp" />

</RelativeLayout>


4) MainActivity.java

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;


public class MainActivity extends AppCompatActivity {


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

    }

}



5) anim(Folder)


i) rotate.xml


<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true">
    <rotate android:fromDegrees="0"
        android:toDegrees="360"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="1000"
        android:repeatMode="reverse"
        android:repeatCount="2"
        android:interpolator="@android:anim/cycle_interpolator"></rotate>
</set>

 




Android Studio - Get Current Latitude And Longitude

1. Add this dependencies in your gradle file     implementation 'com.google.android.gms:play-services-location:18.0.0' 2. Add this p...