Posts

Showing posts from December, 2019

Private constructor

The private constructor is used in singleton classes. A Singleton class is used to create an instance only one for that class. A private constructor used to prevent the creation of more than one object for the class. Example program: package com.javatutorial; public class SingleTonEx { public static void main(String[] args) { SingleTonClass obj1 = new SingleTonClass(); //Compiler error because of private constractor (The constructor SingleTonClass() is not visible) obj1.display(); SingleTonClass obj2 = SingleTonClass.getInstance(); obj2.display(); } } class SingleTonClass{ private static SingleTonClass obj; //Here private constractor avoid to create object for this class private SingleTonClass() { } //Create a instance only once for this class public static SingleTonClass getInstance() { if(obj == null) { obj = new SingleTonClass();

Retrofit with Dagger 2 for Android

Image
Retrofit Retrofit is type-safe REST client for Android and Java which aims to make it easier to consume RESTful web services. URL Manipulation: //ex: api/users?@GET("api/users?") Call<UsersModel> getAllUser(); //ex: api/1/users@GET("api/{id}/users") Call<List<User>> groupList2(@Path("id") int groupId); //ex: api/1/users@GET("api/{id}/users") Call<List<User>> groupList1(@Path("id") int groupId); //ex: api/1/users?sort@GET("api/{id}/users") Call<List<User>> groupList(@Path("id") int groupId, @Query("sort") String sort); //Passing multiple query params@GET("group/{id}/users") Call<List<User>> groupList(@Path("id") int groupId, @QueryMap Map<String, String> options); //Request body@POST("users/new") Call<User> createUser(@Body User user); //FORM ENCODED AND MULTIPART@FormUrlEncoded@POST("user/edit

Dagger 2 for Dependency Injection in Android

Image
Hello Friends, Here we see about dagger 2 dependency injection setup and implementation What is Dependency: What is dagger: Dagger is a compile-time framework for dependency injection. A fast dependency injector for Android and Java. Following annotations used for Dagger 2: @Module and @Provides: define classes and methods which provide dependencies @Inject: request dependencies. Can be used on a constructor, a field, or a method @Component: enable selected modules and used for performing dependency injection Step 1: Gradle dependencies getting from below website https://github.com/google/dagger#gradle dependencies { api 'com.google.dagger:dagger:2.x' annotationProcessor 'com.google.dagger:dagger-compiler:2.x' api 'com.google.dagger:dagger-android:2.x' api 'com.google.dagger:dagger-android-support:2.x' // if you use the support libraries annotationProcessor 'com.google.dagger:dagger-android-processor:2.x&#

Git comments for command line

Git Version control system Git  is a distributed version-control system for tracking changes in source code during software development. It is designed for coordinating work among programmers SCM Git is S oftware C onfiguration M anagement SVN is a V ersion C ontrol S ystem ( VCS ) tool Git is a Revision control system, a tool to manage your source code history. GitHub is a hosting service for Git repositories. GUI Tools for Git: Source Tree - https://www.sourcetreeapp.com/ Android Studio has VCS (For Android developer) Tortoise Git -   https://tortoisegit.org/ Check Git version git --version Add login credentials git config --global user.name "Your username" git config --global user.email "your git email: git config --global user.password "your git password" Check your git information git config --global --list Initialize a local directory git init Clone repository git clone "Repository URL" Clone from a specif

Super Keyword in Java

The super keyword refers to the superclass (parent) class object super keyword can be used to refer to the parent class instance variable. super keyword can be used to invoke the parent class method. super keyword can be used to invoke parent class constructor. For Practice: public class SuperKeyExample { public static void main(String[] args) { NewClass ncObj = new NewClass(); ncObj.show(); } } class SuperClass { int a = 10; SuperClass() { System.out.println("Super/Base class constructor"); } void show() { System.out.println("Base class member method"); } } class NewClass extends SuperClass { int a = 20; NewClass() { super();//implitly super keyword used here System.out.println("New Class constructor"); } void show() { super.show(); //invoke the base class method using super keyword System.out.println(a); System.out.println(super.a); //Access the base class variable using super keywo

Final keyword in Java

It's used in variables, methods, and classes 1) Final variables - can't be reassigned the value 2) Final Method - Can't be override 3) Final Class - Can't be extends 1) Final Variable public class FinalKeyTutorial { final static int name = 2; public static void main(String[] arg) { name = 3; //Compiler error System.out.println(name); } } 2) Final Method public class StaticKeyword { public static void main(String[] args) { B b = new B(); b.show(); } } class A { final void show() { System.out.print("baseclasse"); } } class B extends A { void show() { System.out.print("sub class"); } } 3) Final Class The best example is String public class FinalKeyTutorial { public static void main(String[] args) { FinalEx f =new FinalEx(); f.a = 20; } } final class FinalEx{ int a = 10; } Happy coding...😉

Static Keyword in Java

Image
- Mainly used for Memory Management - Static variables are stored in non-heap memory, Non-static variables are stored in heap memory - Usages         1. Block         2. Variable         3. Method         4. Nested class Static Block - It's executed exactly once when the class is first loaded - It's executed before the main method at the time of classloading. Example: public class StaticKeyword { static { System.out.println("Static block initialized."); } public static void main(String[] args) { System.out.println("Main Method"); } } Response: Static block initialized. Main Method Static Variable - It's used for a constant variable or a method that is same for every instance of a class - It can be used to refer to the common property of all objects - It can be created at class-level only. (Not allowed to create a static local variable in member function in java) Example: public class StaticK

Thread in Android

Thread A thread is the unit of execution within a process. Java provides two ways to create a thread programmatically. Way 1) Extending the java.lang.Thread class.    new ExampleThread().start();     //Step 1 - Extend the Thread base class     class ExampleThread extends Thread{         @Override         public void run() {             for (int i = 0; i < 10; i++) {                 try {                     Thread.sleep(1000);                 } catch (InterruptedException e) {                     e.printStackTrace();                 }         }     } Way 2) Implementing the java.lang.Runnable interface. new Thread(new ExampleRunnable()).start();    //Step 2- Implement Runnable class     class ExampleRunnable implements Runnable {         @Override         public void run() {             for (int i = 0; i < 10; i++) {                 try {                     Thread.sleep(1000);                 } catch (InterruptedException e) {      

Shortcuts for Android Studio

Here we can see some useful shortcuts for Android/Java Developer when using android studio IDE (Windows). Double Tab the Shift - Search any class/resource files CTRL + I - Add an unimplemented methods CTRL + ALT + L - Format/Align the code CTRL + F9 - Build the project CTRL + F10 - Build and Run the project CTRL + O - Override methods  CTRL  +N  - Jump to the search the file with line number (Eg: MainActivity:50 (Line number)) CTRL  +G  - Goto Line/column (line/column - 10:40)  CTRL  +E  - Recent files CTRL  + ALT  +F7  - Search the usages of the selected class in the class itself CTRL+SHIFT+N  - Find File CTRL + SHIFT + NumPad + (+)  - Expand all CTRL + SHIFT + NumPad + (-)  -Collapse all CTRL + R  - Find and replace CTRL + F  -  Find CTRL+ SHIFT+A  - quick command search CTRL +N  - Find Class (capable of finding internal classes as well) CTRL+B  - Goto the class definition CTRL+LeftClick  - g

Sqlite browser for Android development

Image
Get the SQLite DB file from Android studio Below android studio 3.5 Tool window bar -> " Device File explorer "  Above Android 3.5 You can find "Device File Explorer" in the bottom right corner of the screen open directory in  data/data/your-application-package/databases In the new architecture, 3 files are created in the databases directory database-name database-name-shm database-name-wal You have to export all these 3 files in the same directory then open the first one file (that is with database-name only ) in any SQLite browser. Offline SQLite browser Download DB Browser for SQLite free software for as per your operating system then open the "DB Browser for SQLite.exe" executable file You can import the database file and view the data as per the below sample screenshot Download:  https://sqlitebrowser.org/dl/ Stetho tool for Android debugging http://facebook.github.io/stetho/ http://gmariotti.blog
Android Architecture (MVVM) with Room database (Java) Source code link: https://github.com/AndroidManikandan5689/android_architecture_mvvm_Java

Android Studio Live Templates

Image
Auto-Generated Live Templates in Android studio Auto generating live templates are useful when your writing code in Android Studio IDE.  You can enable this feature from the following the path  Editor >> Live Templates >> AndroidLog Some templates for android/java developers: psfs -     public static final String psf -     public static final psfi -     public static final int psvm -     public static void main(String[] args) {} fixme -     // FIXME: 02-12-2019 geti -     public static MainActivity getInstance() {     return ;   } - Instert singlton method get instance key -     private static final String KEY_ = ""; - key for the bundle fori -        for (int i = 0; i < ; i++) {} foreach - ifn - if null statement inn - if not null inst - instance lazy - perform lazy initializer lst - fetch last element of an array rgs - get a string from resource rouit - Run on ui thread ritar - Iterate element of array in reverse orde