Thursday, 21 August 2014

Difference between JVM and DVM (JVM vs. DVM)

Hello Friends,

Let us have a look at difference between JVM and DVM (Dalvik Virtual Machine) 

1. DVM is the software that runs the applications on Android devices. It is open source.

2. JVM is Stack based whereas DVM is Register based.

Stack-based machines require more instructions(i.e. larger instruction set) than register-based machines for the same task. Whereas, each instruction in the register-based machines are larger.

3. Mobile Devices have limited memory and power , low CPU speed as compared to Desktops. 
So,  DVM is more efficient in terms of memory usage and performance.

 4. While running multiple instances of the DVM, DVM is supposed to be more efficient. In mobile phones, each application runs on its own sand box and thus each active application requires its own DVM instance.

Single instance of JVM is shared with multiple applications.

5.  Java compiler complies Java source code into .class files. Then dx (dexer) tool, which is a part of the Android SDK,  processes the .class files into .DEX files that contains Dalvik bytecode.

DVM runs .Dex file (Dalvik Executable File) whereas JVM runs .class files.

With dx tool, all the classes of the application are packed into one file. Also, all the classes in the same DEX file share field, method etc if they are same and these classes are loaded by the same class loader instance.

Thus dx tool eliminate all the redundant information that is present in the classes. 

6. JVM supports multiple operating systems. DVM only supports Android operating system.

7. In case if DVM, executable is APK whereas in JVM, executable is JAR.

8. In case of JVM, each class has its own constant pool. In the dex conversion,  all of the separate constant pools are collapsed into a single shared constant pool which is used by all classes in an application. Thus, storage space to store the shared constant pool is also conserved.

Tuesday, 12 August 2014

Android Studio Icon Generator: How to Generate Icons for different screen densities using Android Studio

Hello Friends, today we will see how to generate Icons for different screen density Android devices using Android Studio.

While developing android app, if we need to use any icon in the app (including launcher icons), we need them in all different screen densities as below:

mdpi: 48*48
hdpi: 72*72
xhdpi: 96*96
xxhdpi: 144*144

Given an icon of any resolution,  Android Studio comes with in built facility to auto generate icons for all above screen resolutions (thereby avoiding need for Android Developer to learn any photo editing tool or take help of expert in this field) and it also puts them in their respective folders ( drawable-mdpi , drawable - hdpi etc.) , thus saving developer's time as well.

Below are steps you need to follow to auto generate icons for your app.
(Note:I assume you have Android Studio installed and using it for your android app development. Also steps 1 to 6 are for creating a new project in Studio, if you already have project created, you can directly refer from step 7).

1. Launch Android Studio and click on File > New Project


2. Input Application Name (e.g. Test Application)  and Company Domain (e.g. testapps.com) and also specify Project Location where you wish to save your project.


3. Then click Next button. Now Check Phone and Tablet as Platform and select Minimum SDK version.


4. Then click Next button. Select Blank Activity.

5. Click Next Button and Specify Activity Name, Layout Name and Title.

6. Click on Finish Button. A New Project in Android Studio has been successfully created with Project Name as TestApplication and Module name as app.

7. Now Right Click on module name ('app' in our case) under Project (TestApplication in our case).
Then select New >  Image Asset.

8. Asset Studio Icon Generator screen appears now.

9. This screen now gives you option to select Image (of any resolution from local PC/laptop) .

For this browse Image file from your computer. You can see preview also on the same screen. You may also design your icons further  by using Padding, Scaling, Shape and Background Color features.

You can also update Resource name field for your icon to match its reference in resource files.

If you don't have a ready-made icon for your needs, you can still design one using Clipart or Text option under 'Foreground'.

Once selected all the options as desired and previewed the icons, Click  Next button.

10. Android Studio now confirms Target module and Res Directory where your icons will be saved.
Click Finish button.
11. You may confirm whether icons have been successfully generated and placed in their respective directory by checking contents of drawable-hdpi, drawable-xhdpi etc.

So we are done creating icons for difference screen densities and placing them in appropriate folder!!!

Hope this post was useful.

We will meet again soon with something new and interesting. Till then, take care.

Thursday, 24 July 2014

Android Studio Shortcuts

Here is the category-wise list of shortcuts for Android Studio IDE.

Using them in day to day programming with Android Studio will definitely save your time!!.

Search/Find/Replace:


Ctrl + F Find in current file
F3 Find Next in current file
Shift + F3 Find Previous in current file
Ctrl + R Replace in current file
Ctrl + Shift + F Find in Path (Whole Project, Module or Directory)
Ctrl + Shift + R Replace in Path
Alt + F7 Find Usages
Ctrl + Alt + F7 Show Usages in a Popup


Code Assistance:


Alt + Enter Quick Fix
Ctrl + Space Assistance in code completion
Ctrl + Q Show Documentation for selected element

Ctrl + P

Show Parameters for selected method

Alt + Insert

Generate Code (Constructor, Getter/setter, equals(), hashcode() etc..)

Ctrl + O

Select Methods to override/implement

Ctrl + Alt + O

Optimize imports

Ctrl + Alt + T

Surround with (if/else, try/catch, do/while etc..)

Shift + F6

Rename

Ctrl + D

Duplicate selected piece of code (*It is not Delete Line)

Ctrl + F4

Close current editor window

Ctrl + F12

Displays File Structure (methods and instance variables for a
class)

Ctrl + Shift + F12

Maximize or restore current editor window

Ctrl + Alt + S

Displays Settings dialog

Ctrl + Alt + Shift + S

Displays Project Structure dialog


Navigation:



Ctrl + N

Go to class

Ctrl + Shift + N

Go to File

F4

Go to Source (goes to implementation for method, declaration
for variable)

Ctrl + G

Go to Line

Ctrl + B

Go to Declaration of class, method or variable

Ctrl + E

Recent Files popup

Alt + Up Arrow

Navigate to previous method in editor window

Alt + Down Arrow

Navigate to next method in editor window

Alt + Left Arrow

Navigate to previous editor window

Alt + Right Arrow

Navigate to next editor window
Ctrl + U
Go to parent of selected element
Ctrl + H
Displays Type hierarchy

Ctrl +Shift + H

Displays Call hierarchy


Add/Remove Comments:



Ctrl + /

Toggle line comment (comment/uncomment using '//' )

Ctrl + Shift + /

Toggle block comment (comment/uncomment using '/*....*/' )



Debug/Run:



Shift + F9

Debug

Shift + F10

Run

Alt + Shift + F9

Select configuration and Debug

Alt +Shift + F10

Select configuration and Run


Deletion:


Ctrl+Y Deletes current line
Ctrl+Del Deletes word next to cursor position
Ctrl+Backspace Deletes word previous to cursor position


Indentions:


Alt + Ctrl + i Correct indentions of selected lines
Tab Increase Indent of selected lines
Shift+Tab Decrease Indent of selected lines



General Editing:


Ctrl + C or Ctrl + Insert Copy
Ctrl + X or Shift + Del Cut
Ctrl + V or Shift + Insert Paste
Ctrl + Y Delete current line(*It is not Redo)




Eclipse Shortcuts

Below is the category-wise list of commonly used shortcuts in Eclipse IDE. 

Learning and using them in day to day programming while using Eclipse will definitely make you a smarter developer and save your time as well!!


Working With Files:


Ctrl + N Create New Project, File etc.
Ctrl + S Save File
Ctrl + Shift + S Save All Files
Ctrl + W or Ctrl + F4 Close File
Ctrl + Shift + W or 
Ctrl + Shift + F4
Close All Files


Search/Find/Replace:



Ctrl + F
Displays Find/
Replace dialog
Ctrl + H
Displays search dialog
(file/java/C/C++/git search)
Ctrl + K
Find Occurrences of
selected text in file


Navigation:


Ctrl + F8 Switch Perspective
Ctrl + Shift + R
Open any Resource
(file, folder, project etc.)
Ctrl + E Displays list of open editors. Allows search within list
F12 Move focus to editor window
Alt + Left arrow   or
Ctrl+Page Up
Switch to previous editor window
Alt +Right arrow  or
Ctrl+Page Down
Switch to next editor window
Ctrl + F10 To get options of show/hide line no,show quick diff, toggle/disable breakpoint
Ctrl + Left arrow moves cursor left by one element in current line
Ctrl + Right arrow moves cursor right by one element in current line
Ctrl + Up arrow Scroll up in editor
Ctrl + Down arrow Scroll down in editor
Ctrl +Shift + Left arrow Extend selection to left by one element
Ctrl +Shift + Right arrow Extend selection to right by one element
Ctrl +Shift + Up arrow Moves cursor to previous method
Ctrl+ Shift+ Down arrow Moves cursor to next method
Ctrl + Shift + P
For a selected bracket,moves cursor to matching
opening/closing bracket
Alt + Up arrow Moves selected line up in file
Alt + Down arrow Moves selected line down in file


Assistance while coding:


F3 Goes to implementation of selected class/method
F4 Opens type hierarchy window for selected class
Ctrl + L Go to a particular line
Ctrl + O Displays structure of current class 
(methods,instance variables etc.)
Ctrl + T Displays inheritance tree of selected class or method
Ctrl + M Maximize or restore current editor window
Ctrl + Shift + T Search for a particular file
Ctrl + Space Assistance in code completion
Ctrl + Shift + O Adds missing import statements
Alt + Shift + R Rename selected element
(project/source folder/resource/
package/compilation unit/method/variables etc.)


Add/Remove Comments:


Ctrl + / Toggle comment(comment/uncomment 
selected piece of code using '//')
Ctrl + Shift + / Comment selected piece of code 
using block comment (/*.....*/)
Ctrl + Shift+ \ Removes block comment around selection
Alt + Shift + J Generate Element Comment
 

Debug/Run:


F11 Debug
Ctrl + F11 Run
F5 (Inside Debug perspective) Step Into
F6 (Inside Debug perspective) Step Over
F7 (Inside Debug perspective) Step Out
F8 (Inside Debug perspective) Jump to next 
breakpoint or watchpoint

Breakpoint:


Ctrl + Shift + B Toggle breakpoint for a selected line


Deletion:


Ctrl + D Deletes current line
Ctrl + Del Deletes word next to cursor position
Ctrl + Backspace Deletes word previous to cursor position


Indentions:


Ctrl + i Correct indentions of selected lines
Tab Increase indent of selected lines
Shift + Tab Decrease indent of selected lines


General Editing:



Ctrl  +  C   Copy
Ctrl  +  X   Cut
Ctrl  +  V   Paste
Ctrl  +  Z   Undo
Ctrl  +  Y   Redo
Ctrl  +  A  Select All