Monday, 7 September 2015

Dialog Boxes in Android

What is Dialog:-

A dialog is a small window that prompts the user to make a decision or enter additional information. A dialog does not fill the screen and is normally used for modal events that require users to take an action before they can proceed.

in Android Application Development There are Five Types of Dialog Boxes There are
1.Alert Dialog
2.Progress Dialog
3.Custom Dialog
4.Date Picker Dialog
5.Time Picker Dialog

1.Alert Dialog 
Some times in your application, if you wanted to ask the user about taking a decision between yes or no in response of any particular action taken by the user, by remaining in the same activity and without changing the screen, you can use Alert Dialog.
activity_main.xml
----------------------------
<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:paddingLeft="@dimen/activity_horizontal_margin"

    android:background="#7af0f0">



    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Exit"

        android:id="@+id/button"

        android:layout_gravity="center_horizontal" />

</LinearLayout>
MainActivity.Java
---------------------------
package com.android.anil.alertdialogbox;



import android.app.AlertDialog;

import android.content.DialogInterface;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.widget.Button;

import android.widget.Toast;



public class MainActivity extends AppCompatActivity {



    Button exitbtn;



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



        exitbtn=(Button)findViewById(R.id.button);

        exitbtn.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);

                builder.setTitle("Exit");

                builder.setMessage("are you sure do you want exit the app");

                builder.setIcon(R.mipmap.ic_launcher);

                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

                    @Override

                    public void onClick(DialogInterface dialog, int which) {

                        finish();

                        Toast.makeText(getApplicationContext(), "App is Closed", Toast.LENGTH_LONG).show();



                    }

                });

                builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {

                    @Override

                    public void onClick(DialogInterface dialog, int which) {

                        Toast.makeText(getApplicationContext(), "App is Not Closed", Toast.LENGTH_LONG).show();



                    }

                });



                AlertDialog alertDialog =builder.create();

                alertDialog.show();

            }

        });

    }



}
 Output is:
--------------------













Wednesday, 2 September 2015

Marshmallow Android 6.0 with new Features

Google Finally announced that the next generation Android M is “Marshmallow”. This news was announced by Dave Burke. The Google will soon allow developers to upload apps base on Android 6.0 to be uploaded on the PlayStore. 

Android 6.0 is mainly aimed at stability and performance improvements, and will bring new features such as – Fingerprint Scanner, Android Pay , MultiWindow , Tap on Now, New app Permissions Model, Support for the USB Type-C, Doze for Battery . 

Here i describe few features that has been introduced in Marshmallow : 

1) Support Fingerprint: API for Fingerprint support will be provided and if developer want they can incorporate the API into their app. 

2) App Permission: How many time you want to restrict the permission that are given to apps. Now in Android M the new API allows users to decide what permission they want to give to any particular app. 

3) Google Now: Now searching easy is Easy on Android M. It is a voice assisted search that allows you to search anything without leaving the app currently running. I think that one is best facility. 

Main Feature 
4) Doze for Battery : This battery saving feature of Marshmallow that will allow the phone battery to last as much as two and half time longer than Lollipop. It will detect if the phone is not touched or moved for a long period of time it will go to sleep, hence saving precious battery.

Implicit Intent in Android

activity_main.xml
---------------------
<RelativeLayout

    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=".MainActivity">



    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Google"

        android:id="@+id/button"

        android:layout_alignParentTop="true"

        android:layout_centerHorizontal="true"

        android:layout_marginTop="74dp" />

</RelativeLayout>
MainActivity.Java
-------------------------
package com.android.anil.implicitintent;



import android.content.Intent;

import android.net.Uri;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.widget.Button;

import java.net.URI;

public class MainActivity extends AppCompatActivity {

    Button googlebtn;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        googlebtn=(Button)findViewById(R.id.button);

        googlebtn.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                Intent in=new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));

                        startActivity(in);

            }

        });

    }



}

Go to Android Manifest.xml and be sure to add the <uses-permission tag >
<manifest xlmns:android...>

 <uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>





Output is:
---------------




Tuesday, 1 September 2015

Explicit Intent in Android

activity_main.xml
---------------------
<RelativeLayout 

    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=".MainActivity">


   <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Call Activity 2"

        android:id="@+id/button"

        android:layout_alignParentTop="true"

        android:layout_centerHorizontal="true"

        android:layout_marginTop="156dp" />

</RelativeLayout>
MainActivity.Java
-----------------
ackage com.android.anil.explicitintent;



import android.content.Intent;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.widget.Button;



public class MainActivity extends AppCompatActivity {

Button gosecond;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        gosecond=(Button)findViewById(R.id.button);

        gosecond.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                Intent in=new Intent(MainActivity.this,SecondActivity.class);

                startActivity(in);

           }

        });

    }

}
Output is:
-------------
 

Android Intents

Android Intent is the message that is passed between components such as activities, content providers, broadcast receivers, services etc.
It is generally used with startActivity() method to invoke activity, broadcast receivers etc.
The dictionary meaning of intent is intention or purpose. So, it can be described as the intention to do action.
The LabeledIntent is the subclass of android.content.Intent class.
Android intents are mainly used to:

  • Start the service
  • Launch an activity
  • Display a web page
  • Display a list of contacts
  • Broadcast a message
  • Dial a phone call etc.
Types of Intents In Android
1. Explicit Intent
2. Implicit Intent
3. Sticky Intent
4. Pending Intent
5. Intent Filter
Developers are Mostly Using Two Primary Intents
1. Explicit Intent
2. Implicit Intent

 Explicit Intent  
specifies the component. In such case, intent provides the external class to be invoked.

Navigation Between the Two Activities  is Called Explicit Intent

Syntax:-
Intent  in=new Intent(Currentclass.this,TargetActivity.class);
startActivity(in);

Implicit Intent 
doesn't specifiy the component. In such case, intent provides information of available components provided by the system that is to be invoked.

it's calling webpage is called Implicit Intent
Syntax:-
Intent  in=new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.google.com"));
 startActivity(in);






Activity Life Cycle

Android Activity Life Cycle

                                           An Activity is usually a single screen that the users sees on the device at a one time. Any application that has been developed by anyone it typically contains multiple activities.

* Most management of the life cycle is done automatically by the system via the activity Stack

* Activity manager is responsible for creating , destroying and managing activities.

For Example when the user starts an application for the first time, the activity manager will create
its activity and put it into the screen. later, when  the user navigating to different screens . The activity manager will move to the previous activity to a holding place .This way if the user wants to go back to older activity , it can be started more quickly , older activities that the user has not used in a while will be destroyed in order to free  more space fort the currently one .

In Activity Life Cycle we are having following methods:

                 1. onCreate()
                 2. onStart()
                 3. onRestart()
                 4. onResume()
                 5. onPause()
                 6. onStop()
                 7. onDestroy()


1. onCreate(); Called when the activity is first created. this is where you should do all  of your normal static setup. create views bind data to lists etc, this method also provides you with a bundle containing the activities previous state.

2. onStart(): Called when the activity become visible to the user.

3. onRestart(); called after your activity has been stopped. prior to it being started again

4. onResume(); Called when the activity starts interacting with the user. At this point your activity is at the top of the stack.

5. onPause(): Called when the system is about to start resuming a previous activity. this is typically used to commit unsaved changes to persistent data, stop Animations and graphics that may be consuming CPU .

6. onStop(): Called when the activity is no longer visible to the user. and another activity has been resumed and is covering this one.

7. onDestroy(): Called when your finishing activity.


The activity first time loads the methods are called as bilow:
onCreate()
onStart()
onResume()

When try to click back button or finish the activity the following methods are called:
onPause()
onStop()
onDestroy()

When you click on home button the activity goes to background and below methods are called:
onPause()
onStop()

After hitting home button again if your opening the previous activity which is in background the following methods are called:
onRestart()
onStart()
onResume();
activity_main.xml
----------------------------
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    android:paddingBottom="@dimen/activity_vertical_margin" 

    tools:context=".MainActivity">





    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:textAppearance="?android:attr/textAppearanceLarge"

        android:text="Activity Life Cycle"

        android:id="@+id/textView"

        android:layout_alignParentTop="true"

        android:layout_centerHorizontal="true" />

</RelativeLayout>


 MainActivity.Java
-----------------------
package com.android.anil.activitylifecycle;



import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.widget.Toast;



public class MainActivity extends AppCompatActivity {



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



        Toast.makeText(getApplicationContext(),"OnCreated() Method",Toast.LENGTH_LONG).show();

    }



    @Override

    protected void onStart() {

        super.onStart();

        Toast.makeText(getApplicationContext(),"OnStart() Method",Toast.LENGTH_LONG).show();



    }



    @Override

    protected void onResume() {

        Toast.makeText(getApplicationContext(),"OnResume() Method",Toast.LENGTH_LONG).show();



        super.onResume();

    }



    @Override

    protected void onRestart() {

        super.onRestart();

        Toast.makeText(getApplicationContext(),"OnRestart() Method",Toast.LENGTH_LONG).show();



    }



    @Override

    protected void onDestroy() {

        super.onDestroy();

        Toast.makeText(getApplicationContext(),"OnDestory() Method",Toast.LENGTH_LONG).show();



    }



    @Override

    protected void onPause() {

        super.onPause();

        Toast.makeText(getApplicationContext(),"OnPause() Method",Toast.LENGTH_LONG).show();



    }



    @Override

    protected void onStop() {

        super.onStop();

        Toast.makeText(getApplicationContext(),"OnStop() Method",Toast.LENGTH_LONG).show();



    }

}

Output is:
-------------------
















Encryption Decryption Example Android Using AES Alogirthm

activity_main.xml ------------------------- <?xml version="1.0" encoding="utf-8"?> <LinearLayou...