ANDROID
Prepared By
Pranav Ashok
Android developer
CONTENTS
 Introduction
 Android Life Cycle
 Activity
 Fragment
 Layout
 Navigation Drawer
 Recycler view
 Shared preference
 Api integration
INTRODUCTION
 HISTORY OF ANDROID
Android Inc.founded in Palo Alto,california ,united states in October
2003 by Andy Rubin.(Rich Miner, Nick sears and Chris white)
 WHAT IS ANDROID?
It is a open source software platform and operating system for mobile
devices , Based on the Linux kernel . Developed by Google and later the
Open Handset Alliance (OHA).
INTRODUCTION
Android version history
Code name Version no. Release date API Level
No codename 1.0 September 23, 2008 1
No codename 1.1 February 9, 2009 2
Cupcake 1.5 April 27, 2009 3
Donut 1.6 September 15, 2009 4
Eclair 2.0 – 2.1 October 26, 2009 5-7
Froyo 2.2 – 2.1 May 20, 2010 8
Gingerbread 2.3 – 2.3.7 December 6, 2010 9-10
Honeycomb 3.0 – 3.2.6 February 22, 2011 11-13
Ice Cream Sandwich 4.0 – 4.0.4 October 18, 2011 14-15
Jelly Bean 4.1 – 4.3.1 July 9, 2012 16-18
KitKat 4.4 – 4.4.4 October 31, 2013 19-20
Lollipop 5.0 - 5.1.1 November 12, 2014 21-22
Mashmallow 6.0 – 6.0.1 October 5, 2015 23
Nougat 7.0 – 7.1.2 August 22, 2016 24-25
Oreo 8.0 August 21, 2017 26
ANDROID LIFE CYCLE
 The steps that an application goes through from starting to finishing
 onCreate()
 onRestart
 onStart()
 onResume()
 onStop()
 onPause()
 onDestroy
ANDROID LIFE CYCLE
 onCreate() - Called before the first components of the application starts.
 onRestart - Called after your activity has been stopped, prior to it being
started again.
 onStart() – Called when the activity is becoming visible to the user.
 onResume() - Called if the activity get visible again.
 onStop() - Called once the activity is no longer visible.
 onPause() - Called once another activity gets into the foreground.
 onDestroy - The final call you receive before your activity is destroyed.
ACTIVITY
 Basic component of most applications
 Most applications have several activities that start each other as needed
 Each is implemented as a subclass of the base Activity class
 The content of the window is a view or a group of views
 Example of views: buttons, text fields, scroll bars, menu items, check boxes,
etc.
 An activity is started by Context.startActivity(Intent intent) or
Activity.startActivityForResult(Intent intent, int RequestCode)
package com.twixt.uaeexportdirectory.view.activity;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
/**
* @author Pranav Ashok on 30-09-2017.
*/
public class Activity extends AppCompatActivity{
@Override
public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
setContentView(R.layout.activity_xml);
}
}
ACTIVITY
FRAGMENT
About Fragments
 New in Android 3.0 (Honeycomb, API 11)
 Intended to reuse layouts between tablets and phones But usable for many
more...
 A fragment is a modular section of an activity, which has its own lifecycle.
 A fragment's lifecycle is directly affected by the host activity's lifecycle
 Activity paused: fragments paused
 Activity destroyed: fragments destroyed
 Activity running: fragments can have different states
FRAGMENT LIFECYCLE
FRAGMENT LIFECYCLE
 onAttach() :This method will be called first, even before onCreate(), letting
us know that your fragment has been attached to an activity.
 onCreate() : The system calls this when creating the fragment.
 onCreateView() : The system calls this when it's time for the fragment to
draw its user interface for the first time.
 onViewCreated() : This will be called after onCreateView().
 onPause() : The system calls this method as the first indication that the user
is leaving the fragment (though it does not always mean the fragment is being
destroyed).
 onDestroyView() : It’s called before onDestroy().
package com.twixt.uaeexportdirectory.view.activity;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
/**
* @author Pranav Ashok on 30-09-2017.
*/
public class Activity extends AppCompatActivity{
@Override
public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle
persistentState) {
super.onCreate(savedInstanceState, persistentState);
setContentView(R.layout.activity_xml);
//type your code here
}
}
FRAGMENT
getFragmentManager().beginTransaction()
.add(R.id.activity_xml, new YourFragment(), Activity.class.getName())
.commit();
package com.twixt.uaeexportdirectory.view.activity;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.xtronlabs.uaeexportdirectory.R;
/**
* @author Pranav Ashok on 30-09-2017.
*/
public class YourFragment extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle
savedInstanceState) {
View view=inflater.inflate(R.layout.your_fragment_xml,container,false);
return view;
}
}
FRAGMENT
LAYOUT
 What is a Layout?
Your layout is the architecture for the user interface in an Activity.
It defines the layout structure and holds all the elements that appear
to the user.
 How to declare a Layout?
Option #1: Declare UI elements in XML
Option #2: Instantiate layout elements at runtime
LAYOUT
Types of Layouts
 RelativeLayout
 LinearLayout
 FrameLayout
 GridLayout
 TableLayout
 ConstraintLayout
NAVIGATION DRAWER
 For opening drawer (using java code)
drawer_layout_id.openDrawer(Gravity.LEFT);
drawerLayout.openDrawer(Gravity.RIGHT);
 For closing drawer (using java code)
drawer_layout_id.closeDrawers();
NAVIGATION DRAWER
RECYCLERVIEW
RECYCLERVIEW
 This widget is a container for displaying large data sets that can be scrolled
very efficiently by maintaining a limited number of views.
 You need to include gradle file in the dependencies (build.gradle module)
 Compile 'com.android.support:recyclerview-v7:25.+'
RECYCLERVIEW
RECYCLERVIEW
public Rv_Adapter rv_Adapter;
public RecyclerView r_v;
Rv_Adapter = new Rv_Adapter (getActivity());
r_v.setAdapter(rv_Adapter);
r_v.setLayoutManager(new LinearLayoutManager(getActivity()));
//in main xml page
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_id"
android:layout_width="match_parent"
android:layout_height="wrap_content“/>
SHARED PREFERENCE
 The shared preference is a class that provide a storage area to store a small
amount of temporary data you can save.
 Shared preference in Android are used to keep track of application and user
preferences.
 Android applications can store data in application preferences.
 In any application, there are default preferences that can accessed through
the PreferenceManager instance and its related
method getDefaultSharedPreferences(Context)
 With the SharedPreference instance one can retrieve the int value of the any
preference with the getInt(String key, intdefVal).
 We can modify the SharedPreference instance in our case using the <br
/>edit() and use the putInt(String key, intnewVal)
SHARED PREFERENCE
SharedPreferences sp =
getActivity().getSharedPreferences("sp_file",Context.MODE_PRIVATE);
String i=sp.getString (“key", “default string");
SharedPreferences s_p =
getActivity().getSharedPreferences(“sp_file",Context.MODE_PRIVATE);
SharedPreferences.Editor editor=s_p.edit();
editor.putString(“key", " your_string ");
editor.commit();
//default SharedPreference
SharedPreferences sp =
getActivity().PreferenceManager.getDeaultSharedPreferences(this);
API INTEGRATION
 Connect to the Network
<uses-permission android:name="android.permission.INTERNET" />
 Manage Network Usage
Minimize the amount of sensitive or personal user data that you transmit
over the network.
 Parsing XML Data
Integrate data in your xml fields
API INTEGRATION
Things to remember before integrating
 In manifests
<uses-permission android:name="android.permissionINTERNET" />
 In build.gradle(Module:app)
packagingOptions {
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
}
}
API INTEGRATION
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.6.0'
compile 'com.squareup.retrofit2:converter-jackson:2.1.0'
Things to remember before integrating
 In build.gradle(Module:app)
API INTEGRATION
 Create an Interface Class
public interface Interface_all_links_here {
@GET("aps_api/get_all_ads")
Call<Api_Responce[]> getDATA();
}
API INTEGRATION
 Create an Interface Class for processor responce
public interface ProcessResponceIntrphase<T> {
void processResponce(T responce);
}
API INTEGRATION
 Create an Rrequest Class for Api
API INTEGRATION
 Create an Rrequest Class for Api
public class Api_Request extends AbstractRequest<Api_Responce[]> {
public Api_Request(Context context,
ProcessResponceIntrphase<Api_Responce[]> responcec_Handeler) {
super(context, responcec_Handeler);
}
public void get_Data() {
Call<Api_Responce[]> call = _interface.getDATA();
call.enqueue(this);
}
}
API INTEGRATION
 Create an Response Class for Api
API INTEGRATION
 Main activity/fragement
TIPS FOR BEGINNER
 Accounts
 https://stackoverflow.com
 https://github.com
 References
 https://www.tutorialspoint.com/android
 https://developer.android.com/training/index.html
 https://www.javatpoint.com/android-tutorial
 https://www.youtube.com
Android

Android

  • 1.
  • 2.
    CONTENTS  Introduction  AndroidLife Cycle  Activity  Fragment  Layout  Navigation Drawer  Recycler view  Shared preference  Api integration
  • 3.
    INTRODUCTION  HISTORY OFANDROID Android Inc.founded in Palo Alto,california ,united states in October 2003 by Andy Rubin.(Rich Miner, Nick sears and Chris white)  WHAT IS ANDROID? It is a open source software platform and operating system for mobile devices , Based on the Linux kernel . Developed by Google and later the Open Handset Alliance (OHA).
  • 4.
  • 5.
    Android version history Codename Version no. Release date API Level No codename 1.0 September 23, 2008 1 No codename 1.1 February 9, 2009 2 Cupcake 1.5 April 27, 2009 3 Donut 1.6 September 15, 2009 4 Eclair 2.0 – 2.1 October 26, 2009 5-7 Froyo 2.2 – 2.1 May 20, 2010 8 Gingerbread 2.3 – 2.3.7 December 6, 2010 9-10 Honeycomb 3.0 – 3.2.6 February 22, 2011 11-13 Ice Cream Sandwich 4.0 – 4.0.4 October 18, 2011 14-15 Jelly Bean 4.1 – 4.3.1 July 9, 2012 16-18 KitKat 4.4 – 4.4.4 October 31, 2013 19-20 Lollipop 5.0 - 5.1.1 November 12, 2014 21-22 Mashmallow 6.0 – 6.0.1 October 5, 2015 23 Nougat 7.0 – 7.1.2 August 22, 2016 24-25 Oreo 8.0 August 21, 2017 26
  • 6.
    ANDROID LIFE CYCLE The steps that an application goes through from starting to finishing  onCreate()  onRestart  onStart()  onResume()  onStop()  onPause()  onDestroy
  • 8.
    ANDROID LIFE CYCLE onCreate() - Called before the first components of the application starts.  onRestart - Called after your activity has been stopped, prior to it being started again.  onStart() – Called when the activity is becoming visible to the user.  onResume() - Called if the activity get visible again.  onStop() - Called once the activity is no longer visible.  onPause() - Called once another activity gets into the foreground.  onDestroy - The final call you receive before your activity is destroyed.
  • 9.
    ACTIVITY  Basic componentof most applications  Most applications have several activities that start each other as needed  Each is implemented as a subclass of the base Activity class  The content of the window is a view or a group of views  Example of views: buttons, text fields, scroll bars, menu items, check boxes, etc.  An activity is started by Context.startActivity(Intent intent) or Activity.startActivityForResult(Intent intent, int RequestCode)
  • 10.
    package com.twixt.uaeexportdirectory.view.activity; import android.os.Bundle; importandroid.os.PersistableBundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; /** * @author Pranav Ashok on 30-09-2017. */ public class Activity extends AppCompatActivity{ @Override public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) { super.onCreate(savedInstanceState, persistentState); setContentView(R.layout.activity_xml); } } ACTIVITY
  • 11.
    FRAGMENT About Fragments  Newin Android 3.0 (Honeycomb, API 11)  Intended to reuse layouts between tablets and phones But usable for many more...  A fragment is a modular section of an activity, which has its own lifecycle.  A fragment's lifecycle is directly affected by the host activity's lifecycle  Activity paused: fragments paused  Activity destroyed: fragments destroyed  Activity running: fragments can have different states
  • 12.
  • 13.
    FRAGMENT LIFECYCLE  onAttach():This method will be called first, even before onCreate(), letting us know that your fragment has been attached to an activity.  onCreate() : The system calls this when creating the fragment.  onCreateView() : The system calls this when it's time for the fragment to draw its user interface for the first time.  onViewCreated() : This will be called after onCreateView().  onPause() : The system calls this method as the first indication that the user is leaving the fragment (though it does not always mean the fragment is being destroyed).  onDestroyView() : It’s called before onDestroy().
  • 14.
    package com.twixt.uaeexportdirectory.view.activity; import android.os.Bundle; importandroid.os.PersistableBundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; /** * @author Pranav Ashok on 30-09-2017. */ public class Activity extends AppCompatActivity{ @Override public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) { super.onCreate(savedInstanceState, persistentState); setContentView(R.layout.activity_xml); //type your code here } } FRAGMENT getFragmentManager().beginTransaction() .add(R.id.activity_xml, new YourFragment(), Activity.class.getName()) .commit();
  • 15.
    package com.twixt.uaeexportdirectory.view.activity; import android.app.Fragment; importandroid.os.Bundle; import android.support.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.xtronlabs.uaeexportdirectory.R; /** * @author Pranav Ashok on 30-09-2017. */ public class YourFragment extends Fragment{ @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { View view=inflater.inflate(R.layout.your_fragment_xml,container,false); return view; } } FRAGMENT
  • 16.
    LAYOUT  What isa Layout? Your layout is the architecture for the user interface in an Activity. It defines the layout structure and holds all the elements that appear to the user.  How to declare a Layout? Option #1: Declare UI elements in XML Option #2: Instantiate layout elements at runtime
  • 17.
    LAYOUT Types of Layouts RelativeLayout  LinearLayout  FrameLayout  GridLayout  TableLayout  ConstraintLayout
  • 19.
    NAVIGATION DRAWER  Foropening drawer (using java code) drawer_layout_id.openDrawer(Gravity.LEFT); drawerLayout.openDrawer(Gravity.RIGHT);  For closing drawer (using java code) drawer_layout_id.closeDrawers();
  • 20.
  • 21.
  • 22.
    RECYCLERVIEW  This widgetis a container for displaying large data sets that can be scrolled very efficiently by maintaining a limited number of views.  You need to include gradle file in the dependencies (build.gradle module)  Compile 'com.android.support:recyclerview-v7:25.+'
  • 23.
  • 24.
    RECYCLERVIEW public Rv_Adapter rv_Adapter; publicRecyclerView r_v; Rv_Adapter = new Rv_Adapter (getActivity()); r_v.setAdapter(rv_Adapter); r_v.setLayoutManager(new LinearLayoutManager(getActivity())); //in main xml page <android.support.v7.widget.RecyclerView android:id="@+id/rv_id" android:layout_width="match_parent" android:layout_height="wrap_content“/>
  • 26.
    SHARED PREFERENCE  Theshared preference is a class that provide a storage area to store a small amount of temporary data you can save.  Shared preference in Android are used to keep track of application and user preferences.  Android applications can store data in application preferences.  In any application, there are default preferences that can accessed through the PreferenceManager instance and its related method getDefaultSharedPreferences(Context)  With the SharedPreference instance one can retrieve the int value of the any preference with the getInt(String key, intdefVal).  We can modify the SharedPreference instance in our case using the <br />edit() and use the putInt(String key, intnewVal)
  • 27.
    SHARED PREFERENCE SharedPreferences sp= getActivity().getSharedPreferences("sp_file",Context.MODE_PRIVATE); String i=sp.getString (“key", “default string"); SharedPreferences s_p = getActivity().getSharedPreferences(“sp_file",Context.MODE_PRIVATE); SharedPreferences.Editor editor=s_p.edit(); editor.putString(“key", " your_string "); editor.commit(); //default SharedPreference SharedPreferences sp = getActivity().PreferenceManager.getDeaultSharedPreferences(this);
  • 28.
    API INTEGRATION  Connectto the Network <uses-permission android:name="android.permission.INTERNET" />  Manage Network Usage Minimize the amount of sensitive or personal user data that you transmit over the network.  Parsing XML Data Integrate data in your xml fields
  • 29.
    API INTEGRATION Things toremember before integrating  In manifests <uses-permission android:name="android.permissionINTERNET" />  In build.gradle(Module:app) packagingOptions { packagingOptions { exclude 'META-INF/DEPENDENCIES' exclude 'META-INF/NOTICE' exclude 'META-INF/LICENSE' exclude 'META-INF/LICENSE.txt' exclude 'META-INF/NOTICE.txt' } }
  • 30.
    API INTEGRATION compile 'com.squareup.retrofit2:retrofit:2.1.0' compile'com.squareup.okhttp3:logging-interceptor:3.6.0' compile 'com.squareup.retrofit2:converter-jackson:2.1.0' Things to remember before integrating  In build.gradle(Module:app)
  • 31.
    API INTEGRATION  Createan Interface Class public interface Interface_all_links_here { @GET("aps_api/get_all_ads") Call<Api_Responce[]> getDATA(); }
  • 32.
    API INTEGRATION  Createan Interface Class for processor responce public interface ProcessResponceIntrphase<T> { void processResponce(T responce); }
  • 33.
    API INTEGRATION  Createan Rrequest Class for Api
  • 34.
    API INTEGRATION  Createan Rrequest Class for Api public class Api_Request extends AbstractRequest<Api_Responce[]> { public Api_Request(Context context, ProcessResponceIntrphase<Api_Responce[]> responcec_Handeler) { super(context, responcec_Handeler); } public void get_Data() { Call<Api_Responce[]> call = _interface.getDATA(); call.enqueue(this); } }
  • 35.
    API INTEGRATION  Createan Response Class for Api
  • 36.
    API INTEGRATION  Mainactivity/fragement
  • 37.
    TIPS FOR BEGINNER Accounts  https://stackoverflow.com  https://github.com  References  https://www.tutorialspoint.com/android  https://developer.android.com/training/index.html  https://www.javatpoint.com/android-tutorial  https://www.youtube.com

Editor's Notes

  • #21 <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00e4c9"> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:background="@color/colorAccent" android:alpha=".6" android:layout_margin="5dp" android:layout_height="match_parent" /> </RelativeLayout> <RelativeLayout android:layout_width="200dp" android:layout_height="match_parent" android:layout_gravity="left" android:background="#6200ff"> </RelativeLayout> <RelativeLayout android:layout_width="200dp" android:layout_height="match_parent" android:layout_gravity="right" android:background="#ff6f00"> </RelativeLayout> </android.support.v4.widget.DrawerLayout>
  • #26  public class Adapter_for_rv extends RecyclerView.Adapter<Adapter_for_rv.ViewHolder> { Context context; public Adapter_for_rv(Context con) { context = con; } @Override public Adapter_for_rv.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { LayoutInflater layoutInflater = LayoutInflater.from(context); return new ViewHolder(layoutInflater.inflate(R.layout.rvlayout, parent, false)); } @Override public void onBindViewHolder(Adapter_for_rv.ViewHolder holder, int position) { //code -> action to be done } @Override public int getItemCount() { return length; } public class ViewHolder extends RecyclerView.ViewHolder { // declarations public ViewHolder(final View itemView) { super(itemView); // finding declared fields and actions here } } }
  • #34 package com.twixt.nd; import android.content.Context; import android.util.Log; import java.util.concurrent.TimeUnit; import okhttp3.OkHttpClient; import okhttp3.logging.HttpLoggingInterceptor; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; import retrofit2.Retrofit; import retrofit2.converter.jackson.JacksonConverterFactory; /** * @author pranav Ashok on 06-10-2017. */ public abstract class AbstractRequest<T> implements Callback<T> { private static final String base_url = "http://apps.cloudsoftct.com/"; protected Context context; protected Interface_all_links_here _interface; private ProcessResponceIntrphase<T> responcehandler; public AbstractRequest(Context con,ProcessResponceIntrphase<T> rhandler){ context= con; responcehandler =rhandler; HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(); loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY); Retrofit retrofit= new Retrofit.Builder() .baseUrl(base_url) .client(new OkHttpClient.Builder().addNetworkInterceptor(loggingInterceptor) .connectTimeout(300, TimeUnit.SECONDS) .readTimeout(300,TimeUnit.SECONDS) .build()) .addConverterFactory(JacksonConverterFactory.create()) .build(); _interface=retrofit.create(Interface_all_links_here.class); } @Override public void onResponse(Call<T> call, Response<T> response) { Log.e("response", " ok"); processResponse(response.body()); } @Override public void onFailure(Call<T> call, Throwable t) { Log.e("response ", t.getMessage()+" poe"); processResponse(null); } private void processResponse(T response){ if(responcehandler != null){ responcehandler.processResponce(response); } } }