0% found this document useful (0 votes)
22 views16 pages

Android With Java

Uploaded by

Ayush Kushwaha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views16 pages

Android With Java

Uploaded by

Ayush Kushwaha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

ANDROID WITH JAVA

Java is a programming language. Java is a OOP’S language.

XML- Designing

JAVA- Programming/Business Logics

FIREBASE/SQLITE/APIs-

Firebase- Database

JAVA- PROGRAMMING- 16/08-JDK

ANDROID STUDIO- SOFTWARE /IDE- 2GB

Introduction of JAVA

Java is a general purpose, high level, object oriented programming language.

JAVA with JSP – web development

Android with JAVA – mobile application

Desktop application/stand alone application

Java was developed by team of sun microsystem whose leader was James Gosling in 1991. Java
is the successor version of C++. Code was written in C and C++ programming languages. Java
supports all the concept of OOP that is Class, object, inheritance, polymorphism, constructor,
encapsulation, abstraction etc. The all concept of java makes the java most easier, portable,
and secure programming language.

Features of JAVA:

 Compiled and interpreted : Java source code firstly compiled and converted into
byte code that calls class file(.class) then class files interpreted and displays the
output of program.
 Platform independent : Most important feature of java is Platform independent.
Java follows the concept “Write once and run anywhere”. Code written in one
machine can easily run in any other machines.
 Secure : Java is the most secured programming language. Many concept of C
programming like goto, pointer is not present in java. Because java does not
provide direct access of memory without authorization.
 Object Oriented Programming: Java provides concept of code-reusability,
integrity and many other concept of OOP. That makes codes re-usable and
managed. Code written once can be used in many way.

OOPs : In object oriented programming, you need to put each instruction under
the block of class. One java program may contain multiple classes, out of all one
class must should contain Main function.
The main function is the place from where interpreter starts the execution.
Class: Class is the collection of data member(variable) and member
function(method).
Class is the collection of multiple methods that behaves same.
Naming Convention :
 All classes starts with uppercase letter.(Pascal naming conventions).
class MathematicalOperation
 All packages is written in lowercase letters. Some pre-defined packages of java
is : util , lang , io , etc.
 All methods of java is written in Camel case naming convention.
Void addNumber.
Syntax:
class class_name
{
//data_mambers(variables)
//functions
}

Syntax of simple java program :

class MyFirstClass

{
//main methods
public static void main(String arg[])
{
System.out.print(“Hello world of Java”);
}
}
User Input in Java : To take a value input from user, we need some pre-defined
functions. These pre-defined functions provide option to initialize a variable at
run time by asking a value from user on console screen.
All user input pre-defined functions are stored in Scanner class. And scanner
class is a part of util package.
To add a pre-defined functions are stored in program import is used:
Import java.util.*;//imports all classes of util package.
Import java.util.Scanner;//imports only Scanner class of util package.
To access any user input functions, firstly you need to create object of Scanner
class.
//how to create object of Scanner class:
Class_name obj_name=new class_name(parameters);
Scanner sc=new Scanner(System.in);

nextInt() - Input a integer value


nextFloat() – Input a float value
nextDouble() – Input a double type value
next() – Input one word string
nextLine() – Input multi-word string
next().charAt(0) – Input a character type value

Lang Package
Math Class – min(), max(), pow(), sqrt(), floor(), ceil(), round()

Function Parameter Return type


min() 2 double
max() 2 double
sqrt() 1 double
ceil() 1 double
floor() 1 double
round() 1 double
pow() 2 double
User Defined Function : User defined function is a block of code written by
developer for future use.
Functions is a block of code used to perform a pre-defined specific task.
Functions have pre-written code that can be call and use multiple times
whenever you need.
Syntax to create a UDF :
Return_type function_name(argument_list)
{
//statements;
}
Types of UDF :
1. No return type and no arguments
2. Return type and no arguments
3. No return type with arguments
4. Return type with arguments

No return type and no arguments :

no return type – void

no argument – ()

//create a UDF to add two numbers by using No return type and no arguments

void addNum() //no return type and no arguments

int n1=50,n2=70;

System.out.print(“Addition = ”+(n1+n2));

No return with arguments:

Void function_name(argument_list) //no return type with argument

//statement;

}
Return type and no arguments :

return type = output

int addNumber()

Return type with arguments :

int addNumber(int a, int b)

Polymorphism :
Polymorphism is a concept of OOP to use same name function with different working.

In a class we can not create same name function two times, but polymorphism provides a
concept to use same function multiple times in same class.

Polymorphism is collection of Poly+Morphism. Poly means many and Morphism means form.
To use same functions in many form is the purpose of polymorphism.

There are two types of polymorphism :

1. Compile time polymorphism


Method overloading
2. Run time polymorphism
Method overriding
1. Method overloading : method overloading is performed at the compile time of program.
Method overloading is a concept to use same name function in same class multiple
times. But all functions can be different in two ways:
a. By different number of argument
b. By different type of argument
Method overloading is performed within same class.

Ex:

a. By different number of argument :

class CLS1

void add(int a,int b)//method overloading

void add(int a,int b,int c)//method overloading

b.By different type of argument

class CLS1

void getMin(int a,int b)

void getMin(float a,float b)//method overloading

{
}

void getMin(double a,double b)

Method overriding : compile time polymorphism - polymorphism

Constructor : constructor is a method of any class that has same name as the
class. A single class may have more than one overloaded constructor.

 Constructor is always public, if class of constructor will not be public then it will
restrict object creation of the class.
 We do not need to call constructor explicitly, it is automatically called by
controller when we create the object of the class.
 Constructor do not have any return type not even void.
class ConstructEx
{
public ConstructEx()//constructor
{
}
}

In java, two types of Constructor :


1. Default Constructor
2. Parameterized Constructor

Every class has an implicit constructor. That constructor is used to initialize default value to
the instance variable of class. Implicit constructor runs in the absence of explicit constructor.

class Calculator
{
Calculator() //default constructor
{
}
}

Class Calculator
{
Calculator(int a,int b)//parameterized constructor
{

}
Calculator(double c,double d)//parameterized constructor
{

}
Calculator()//default constructor
{

}
}
Note: Constructor contains some instructions, that is executed each time
when object of class is created.
Constructor is mainly used to initialize instance variables of class.

Real life example : class, object, polymorphism, constructor, inheritece

Parameterized constructor : Parameterized constructor is a method of class


that has same name as the class and have some arguments also

Class Student

Student (int roll, String name)

If you are declaring a parameterized constructor in a class then you must have to pass actual
parameters at the time of object creation.
Note : Parameterized constructor is used to take value from the caller and initialize value of it’s
parameters to take the instance variable.

Pre-defined function of string :


 toLowerCase()
 toUpperCase()
 replace()
 trim()
 indexOf()
 substring()
 equalsTo()
 equalsToIgnoreCase()
 toString()
 length()
 concate()
 charAt()
ANDROID : 1 Activity : 2 files : a.) .XML – Designing(UI) b.) .java – Coding (Business logic)
Activity Life Cycle : in android each activity can be pass through 6 life stages. These stages of
activity can be managed by using some pre-defined function. There are mainly 6 pre-defined
function is present in the lifecycle of activity. These function is automatic called based on the
current life stages of activity.

onCreate() : when application is created

onStart() : when activity is visible to the user

onResume() : when user start interaction with the activity

onPause() : when activity is partially visible to the user

onStop() : when application runs in the background

onDestroy() : when activity is ended

XML : Pre-defined elements

Naming Convention : Pascal Naming

Button : Button

Text(String Message) : TextView

Form Control(TextBox) : EditText

Element Properties : camel case

textSize

textColor

textFontStyle

Layout of the Activity :

LinearLayout

RelativeLayout

ConstraintLayout

FrameLayout
Message Print :

Logcat : developer

Toast : application

Toast – class

makeText(this,””,duration) – function

show() – that enables the toast to be displayed

syntax :

Toast.makeText(this,”My name is abhishek”,Long).show();

TextView Print

Bind click event to the button :

1. Call a function onclick of button


Create a method on .java page of activity :
Public void showMsg(View v)
{

}
2. Bind onClickListener event to the button

For identifying text box on Java page

1. Apply a id to the element.


2. Get a reference of element by using id(on .java page)
EditText name = findViewsById(R.id.txtname);

Exception Handling : Exception is the run time error which caused termination of the
app. Exceptions are some unwanted situation of programs that may interrupt the
execution of program.
Exceptions are generated because wrong entries in program.
 Firstly try block is executed, if there is no Exception in try block then catch block
does not executed. Catch is executed only in case try throws an exception.
 Multiple catch may present with one try block, if you want to handle different
type of Exceptions differently. Catch should be present just after the block of try.

Syntax :

try

//statement

catch(Exception_class_obj)

catch(Exception_class_obj)

catch(Exception_class_obj)

 A finally block may present with each try block. This finally block executes in each cases
either try throws an error or not.

Intent : Intent is used to transfer from one activity to another activity.

Implicit intent is used to transfer control from one activity to another within same application.

Explicit Intent is used to transfer control from one activity to another in different applications.
Ex : open camera on click of button, open flashlight on click of button, open gallery, open
whatsapp.

Types of Intent :

1. Implicit Intent
2. Explicit Intent
Implicit Intent :

To open next activity from current activity in same application, implicit intent is used. Intent
class is used to create object of implicit intent.

Intent obj=new Intent(Context : File_name.this, java_file_name.class);

Ex : Move to login activity from Register activity :

Intent intent=new Intent(Register.this,login.class);

To finally start the activity “startActivity()” method is used. Which takes parameter of a intent
class obj :

startActivity(intent_class_obj);

ex : startActivity(intent)

Transfer from Activity to another with loaded data :

To pass some values with intent putExtra() function is used.

In putExtra function a value can be passed in form of key and value.

Inheritence : Inheritence is a concept of Object Oriented Programming that provide re-usability


of codes. By using inheritance some member of a pre-written class can be easily access in a new
class.

The class that is inheriting the members is known as child class or derived class or sub class and
which members are inherited is known as parent class or base class or super class.

By using inheritance all public and protected members can be accessed in child class but private
members(instance variable, methods)can not be accessed in child class.

In java “extends” keyword is used for inheritence.

Types of inheritance :

1. Single inheritence
2. Multiple inheritence(Interface)
3. Multi-level inheritence
4. Hierarchical inheritance
class Math //parent/base/super class

//variables

//methods

class Arithmetic extend Math //derived class/child /sub class

In java a single class can not inherit more than class, because java does not support multiple
inheritence. So in this case, concept of interface is used.

Adapter :

Adapter works as a bridge between a sample view and a layout. It is used to create a view that
can be add in any layouts.

To bind views in ViewPager adapters are inherited by FragmentStateAdapter class.

FragmentStateAdapter is a abstract class. Abstract class may have abstract any non-abstract
type methods.

When a class have any one abstract method then it must should be declared as abstract class.
When a class inherits a abstract class, it have to implement each abstract method of parent
class.

Abstract methods are those methods which have only declaration not definition. Abstract
methods are defined within child class. Abstract methods are declared with abstract keyword.

Foreach Loop : Foreach loop is used to access each element of a collection. Foreach can
only be applied on a collection like Array, list, ArrayList, HashMap etc.

Foreach loop access each element from start index to the last.

Syntax :

For(data_type variable_name : collection)


{

Ex:

public static void main(String[] args)

int []marks=new int[]{20,40,50,29};

for(int i:marks)//foreach loop

System.out.println(i);

System.out.prntln(“******************************************”);

Adapter for RecyclerView:

To create a Adapter for recyclerView, java class is inherited from the Abstract class called
RecyclerView.Adapter.

Class RecycleAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder>

//override three methods of RecyclerView.Adapter

onCreateView(){

//inflate a design and return the design

}
onBindViewHolder(){

//set different values on each item of recyclerView

getItemCount(){

return total_item_of_recyclerView;

class ViewHolder extends RecyclerView.ViewHolder{

public ViewHolder(View itemView)

return (super.itemView)

//get reference of all recyclerView items

You might also like