Thursday, 17 December 2015

How to find, where your database stored in Android Studio


I am going to tell you where you can find Database which is stored in android studio. You often need to see your Database file which is created by you. Also you want to see your data has been updated or inserted or not.

You have to understand run your application in emulator-:

Step 1-: Run your application in emulator.

Step 2-: If your emulator started, Now go to "Tools" -> "Android" -> "DDMS (monitor included)". You can also open the DDMS from:



Step 3-: Select project package name from your emulator and then select package Explorer(you can see in rectangle mark)




Step 4-: You will be able to see internal storage of Emulator.


Step 5-: Expand "Data" Directory you will see one more "Data" directory, again expand data folder.



Step 6-: You can see your project package name inside second data folder. Expand you project package name you will see database folder.



Step 7-: Expand database folder you will see the Database File.




Step 8-: You can pull you database file from that directory in you system.


Step 9-: Open FireFox Browser Type belwo url you will be able to see ADD-ONS SQLite Manager.
https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/



Step 10-: Click on "+ Add to Firefox" after installation restart Firefox, Now you will be able to see database icon in Firefox. Click on SQLite Manager icon which is mark in circle or you can open "Tools" -> "SQLite Manager"




Step 11-: Now you will be able to SQLite Manager. You can open your database file which has been pull from DDMS.




Get Position when Click on ListView and Click on Button in ListView

I am going to create customized ListView. In each row item put a Button so when user click on button it will show position of the item in ListView. In the same way when user click on ListView it will show position of the item in ListView.

Set the listener on ListView-:

listView.setOnItemClickListener(onItemClickListener);
Define the listener

private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {

        @Override

        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                                long arg3) {
            Toast.makeText(MainActivity.this, "This is click=" + position, Toast.LENGTH_LONG).show();
        }

    };

Set the OnClickListener on Button which is entail in ListView Row Item layout-:

eventButton.setOnClickListener(eventButtonClickListener);

Define the OnClickListener :

private OnClickListener eventButtonClickListener = new OnClickListener() {     
     @Override                                                                  
     public void onClick(View v) {                                              
         View parentRow = (View) v.getParent();                                 
         ListView listView = (ListView) parentRow.getParent();                  
         final int position = listView.getPositionForView(parentRow);           
                                                                                
     }                                                                          
 };                                                                             


Sunday, 22 November 2015

Material Design theme style alert dialogs

Material design is a complete guide for visual, motion, and interaction design across platforms and devices. By using Material theme you can provide a best look and feel to your application. So now here I am going to create Demo for creating Alert Dialog which will show Material design look and feel. This demo also give same look and feel in below API's by using android.support.v7.app.AlertDialog..

For more information follow Developer Guide.

You could handle click with setOnClickListener(). Add the following line of codes in showAlertDialog in MainActivity.java file.

private void showAlertDialog(){
        AlertDialog.Builder builder =
                new AlertDialog.Builder(this);
        builder.setTitle("Write Title");
        builder.setMessage("This is Alert Dialog for material support");
        builder.setPositiveButton("OK", null);
        builder.setNegativeButton("Cancel", null);
        builder.show();
    }


Activity defining these attributes in AppTheme's style.

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

    </style>

</resources>

See the result given below:

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#2196F3</color>
    <color name="colorPrimaryDark">#1565C0</color>
    <color name="colorAccent">#FF4081</color>
</resources>

Now write a code to customize dialog using style. Add below code in your file.
styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="alertDialogTheme">@style/AppCompatAlertDialogStyle</item>
    </style>


    <style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="colorAccent">#FFCC00</item>
        <item name="android:textColorPrimary">#FFFFFF</item>
        <item name="android:background">#5fa3d0</item>
    </style>
</resources>

See your result:




Wednesday, 18 November 2015

Android animation while switching activities

Android user interface displayed through an activity. So that if you want transition between the activities can be animated using method overridePendingTransition.



FirstActivity.java
package helloworld.test.com.activitytransition;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class FirstActivity extends AppCompatActivity implements View.OnClickListener {

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

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.startTransitionRightToLeft:
                startActivity(new Intent(FirstActivity.this, SecondActivity.class));
                //transition from right to left
                overridePendingTransition(R.anim.right_in, R.anim.left_out);
                break;
            case R.id.startTransitionBottomToTop:
                startActivity(new Intent(FirstActivity.this, SecondActivity.class));
                //transition from bottom to top
                overridePendingTransition(R.anim.bottom_in, R.anim.top_out);
                break;

        }

    }
}
activity_first.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".FirstActivity">

    <Button
        android:id="@+id/startTransitionRightToLeft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="@string/right_to_left_animation" />
    <Button
        android:id="@+id/startTransitionBottomToTop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="@string/bottom_to_top_animation" />
</LinearLayout>
right_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="500"
        android:fromXDelta="100%p"
        android:toXDelta="0" />

</set>
left_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="500"
        android:fromXDelta="0"
        android:toXDelta="-100%p" />

  >
</set>
bottom_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="500"
        android:fromYDelta="100%p"
        android:toYDelta="0" />

</set>
top_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="500"
        android:fromYDelta="0"
        android:toYDelta="-100%p" />
   
</set>
SecondActivity.java
package helloworld.test.com.activitytransition;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

public class SecondActivity extends AppCompatActivity implements View.OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.startTransitionLeftToRight:
                finish();
                //transition from left to right
                overridePendingTransition(R.anim.left_in, R.anim.right_out);
                break;
            case R.id.startTransitionTopToBottom:
                finish();
                //transition from top to bottom
                overridePendingTransition(R.anim.top_in, R.anim.bottom_out);
                break;
        }
    }
}

activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
    android:fitsSystemWindows="true"
    tools:context="helloworld.test.com.activitytransition.SecondActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_second" />


</android.support.design.widget.CoordinatorLayout>
content_second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="helloworld.test.com.activitytransition.SecondActivity"
    tools:showIn="@layout/activity_second">
    <Button
        android:id="@+id/startTransitionLeftToRight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="@string/left_to_right_animation" />
    <Button
        android:id="@+id/startTransitionTopToBottom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="@string/top_to_bottom_animation" />
</LinearLayout>
left_in.xml

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

    <translate
        android:duration="600"
        android:fromXDelta="-100%"
        android:toXDelta="0" />



</set>
right_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="600"
        android:fromXDelta="0%"
        android:toXDelta="100%" />

</set>
top_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="500"
        android:fromYDelta="-100%p"
        android:toYDelta="0" />

</set>
bottom_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="500"
        android:fromYDelta="0"
        android:toYDelta="100%p" />

</set>


Runtime User Permission For Contacts

Runtime permissions
Here I am showing only for read contact permission.
Android 6.0 M, application will not be accepted any permission at installation time.
Apps may request permission to access information or use device capabilities at any time after installation. When a user needs to perform an action in an app, such as using the device camera, the app may request permission at that moment.

Users may also allow or deny the permissions of any app from Android Settings anytime after installation.
Android's permission system is one of the biggest security concern all along since those permissions are asked for at install time.

How to make your application support new Runtime Permission.
Set you android application compileSdkVersion and targetSdkVersion to 23. For that use below line
android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 23
    }
Add a permission into AndroidManifest.xml
<uses-permission android:name="android.permission.READ_CONTACTS" />

Read Contact Permission:

If permission has already been granted, showContactDetails will be suddenly called. Otherwise launch a permission request dialog like below.

In order to check permission before going to read contacts -

@Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.readContactBtn:
                requestContactsPermission();
                break;
        }
    }

Once user click on button readContact will be called then it will check permission granted or not.

public void requestContactsPermission() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
            before = ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.READ_CONTACTS);
            if (before) {
                showMessageOKCancel("You need to allow access to Contacts",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                ActivityCompat.requestPermissions(RuntimePermission.this, PERMISSIONS_READ_CONTACT, REQUEST_CONTACTS);
                            }
                        });
                return;
            } else {
                Log.d("this is else block", "Block Number");
                ActivityCompat.requestPermissions(this, PERMISSIONS_READ_CONTACT, REQUEST_CONTACTS);
                return;
            }
        }
        showContactDetails();
    }

// show message dialog when user deny permission 
    private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
        new AlertDialog.Builder(RuntimePermission.this)
                .setMessage(message)
                .setPositiveButton("OK", okListener)
                .setNegativeButton("Cancel", null)
                .create()
                .show();
    }

Once user select ALLOW or DENY onRequestPermissionsResult will always be called to inform a result which we can check from the 3rd parameter, grantResults:

@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case REQUEST_CONTACTS:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // Permission Granted
                    showContactDetails();
                } else {
                    // Permission Denied
                    Toast.makeText(RuntimePermission.this, "READ_CONTACTS Denied", Toast.LENGTH_SHORT).show();
                    after = ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_CONTACTS);
                    boolean goToSettings = !(after || before);
                    if (goToSettings)
                        openApplicationInfoSetting();
                }
                break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

//open application setting screen for enabling permission
    private void openApplicationInfoSetting() {
        new AlertDialog.Builder(RuntimePermission.this)
                .setMessage("Allow App access to your contact. \n\nTo enable this, click App settings Below and activate Contact under the permission menu.")
                .setPositiveButton("APP SETTINGS", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        Intent intent = new Intent();
                        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                        Uri uri = Uri.parse("package:" + getPackageName());
                        intent.setData(uri);
                        startActivity(intent);
                    }
                })
                .setNegativeButton("NOT NOW", null)
                .create()
                .show();
    }

let's explain about shouldShowRequestPermissionRationale() method return value.

Step 1-When it will return true
  • If the app has requested this permission previously and the user denied the request.
Step 2-When it will return false
  • The app didn't request for this permission yet.
  • If the app has requested this permission previously and the user denied the request with Don't ask again.
  • If a device policy prohibits the app from having that permission
In the same way you can check permission for writing contacts(insert contact).

MainActivity.java

package com.demo.test.readcontactruntimepermission;

import android.Manifest;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

public class RuntimePermission extends AppCompatActivity implements View.OnClickListener {
    private static String[] PERMISSIONS_READ_CONTACT = {Manifest.permission.READ_CONTACTS};
    private static final int REQUEST_CONTACTS = 11;
    boolean before, after;

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

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.readContactBtn:
                requestContactsPermission();
                break;
        }
    }

    public void requestContactsPermission() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
            //
            before = ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.READ_CONTACTS);
            if (before) {
                showMessageOKCancel("You need to allow access to Contacts",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                ActivityCompat.requestPermissions(RuntimePermission.this, PERMISSIONS_READ_CONTACT, REQUEST_CONTACTS);
                            }
                        });
                return;
            } else {
                Log.d("this is else block", "Block Number");
                ActivityCompat.requestPermissions(this, PERMISSIONS_READ_CONTACT, REQUEST_CONTACTS);
                return;
            }
        }
        showContactDetails();
    }

    //open application setting screen for enabling permission
    private void openApplicationInfoSetting() {
        new AlertDialog.Builder(RuntimePermission.this)
                .setMessage("Allow App access to your contact. \n\nTo enable this, click App settings Below and activate Contact under the permission menu.")
                .setPositiveButton("APP SETTINGS", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        Intent intent = new Intent();
                        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                        Uri uri = Uri.parse("package:" + getPackageName());
                        intent.setData(uri);
                        startActivity(intent);
                    }
                })
                .setNegativeButton("NOT NOW", null)
                .create()
                .show();
    }

    // show message dialog when user deny permission
    private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
        new AlertDialog.Builder(RuntimePermission.this)
                .setMessage(message)
                .setPositiveButton("OK", okListener)
                .setNegativeButton("Cancel", null)
                .create()
                .show();
    }

    private void showContactDetails() {
        Log.i("CONTACT",
                "Going to read contact");
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case REQUEST_CONTACTS:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // Permission Granted
                    showContactDetails();
                } else {
                    // Permission Denied
                    Toast.makeText(RuntimePermission.this, "READ_CONTACTS Denied", Toast.LENGTH_SHORT).show();
                    after = ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_CONTACTS);
                    boolean goToSettings = !(after || before);
                    if (goToSettings)
                        openApplicationInfoSetting();
                }
                break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
}