25 Jun 2015

How to import all packages in android studio

Go to File ->Settings ->Editor->Auto Import->Java and make the below things:
Select Insert imports on paste value to All
Do tick mark on Add unambigious imports on the fly option and "Optimize imports on the fly*

Auto import packages in android studio

Read More !

23 Jun 2015

Change or add theme to Android Studio?


For Change :-
File->Settings->Editor->Colors & Fonts-> In scheme name select Darcula and apply to see a awesome dark background theme editor.

For Add :-

You can download new themes from http://ideacolorthemes.org/
Once you have downloaded the .jar file, go to File -> Import Settings... and choose the file downloaded.
Android Studio theme change

Read More !

19 Jun 2015

Where to place Assets folder in Android Studio

Root-Module
--.idea
--app
----bulid
----src
------main
--------assets
----------abc.html
----------AnotherFont.otf
--------java
----------Source code here
--------res
------AndroidManifest.xml
----Build.gradle

In android studio, right click on the app icon, select folder and navigate to the Assets Folder.



On the next screen just click Finish.

And It will create the assets folder in the main target source set.


Read More !

18 Jun 2015

This version of the rendering library is more recent than your version of Android Studio. Please update Android Studio.

I just created Demo Project inside of Android Studio and I'm getting following message:
Summary:
"This version of the rendering library is more recent than your version of Android Studio. Please update Android Studio."



And, I found that Preview Android Versions should be changed from API 22 : Android M(Preview) to any Non-Preview Android Version.





Read More !

15 Jun 2015

Why java does not support multiple inheritance?

Multiple inheritance can be really difficult to understand. When you have a multiple inheritance with two classes which have methods in conflicts, how do you handle this ?
Of course solutions exist (in C++ for example) but the creators of Java decided it was way to complicated and not really in the Java philosophy (make development a lot easier).
From sun.com :
Multiple inheritance--and all the problems it generates--was discarded from Java. The desirable features of multiple inheritance are provided by interfaces--conceptually similar to Objective C protocols.
An interface is not a definition of a class. Rather, it's a definition of a set of methods that one or more classes will implement. An important issue of interfaces is that they declare only methods and constants. Variables may not be defined in interfaces.

Multiple Inheritance is :
  • hard to understand
  • hard to debug (for example, if you mix classes from multiple frameworks that have identically-named methods deep down, quite unexpected synergies can occur)
  • easy to mis-use
  • not really that useful
  • hard to implement, especially if you want it done correctly and efficiently
  1. Assume that class A is having a method fun(). class B and class C derives from class A.
  2. And both the classes B and C, overrides method fun().
  3. Now assume that class D inherits both class B, and C. (just Assumption)
  4. Create object for class D.
  5. D d = new D();
  6. and try to access d.fun();  will it call class B's fun() or class C's fun()?

Therefore, it can be considered a wise choice to not include Multiple Inheritance into the Java language.
Problem with multiple inheritance: Diamond problem.
Example:
This is the ambiguity existing in diamond problem.It is not impossible to solve this problem, but it creates more confusion and complexities to the programmer while reading it. It causes more problem than it tries to solve.

Read More !

13 Jun 2015

error : Could not save application settings: java.io.IOException: java.lang.AssertionError: Unexpected content storage modificat

I have a message saying "Could not save application settings: java.io.IOException: java.lang.AssertionError: Unexpected content storage modification" when running the android sudio.


Simply renaming/deleting the .AndroidStudio hidden folder in Windows, under your home directory. C:\Users\YOURUSERNAME.AndroidStudio. I renamed mine and I'm back up and running.
Example : 
C:\Users\amardeep.vijay and deleting the .AndroidStudio folder.
Read More !

5 Jun 2015

Why we use OOPS concepts in java

Object oriented programming is a concept that was created because of the need to overcome the problems that were found with using structured programming techniques. While structured programming uses an approach which is top down, OOP uses an approach which is bottom up. Traditionally, programming has placed an emphasis on logic and actions.

Object oriented programming has taken a completely different direction, and will place an emphasis on objects and information. With object oriented programming, a problem will be broken down into a number of units. These units are called objects. The foundation of OOP is the fact that it will place an emphasis on objects and classes.

Objects will be defined, and they will interact inside the system in a number of different ways. There are a number of advantages to be found with using the OOP paradigm, and some of these are simple maintenance, an advanced analysis of complicated programs, and re-usability.

There are a number of programming languages that use OOP, and some of these are Java, C++, and Ada. One concept that you will want to become familiar with is data modeling. Before you can construct an object oriented system, you will first need to find the objects within the system and determine the relationships they have. This process is called data modeling.

OOPs stands for Object Oriented Programming. The concepts in oops are similar to anyother programming languages. Basically, it is program agnostic. The different OOps concepts are :
Polymorphism
Inheritance
Abstraction
Encapsulation
Aggreagation
Composition
Association

Q1) What is polymorphism?
Ans) The abiltiy to define a function in multiple forms is called Polymorphism. In java, c++ there are two type of polymorphism: compile time polymorphism (overloading) and runtime polymorphism (overriding).
Mehtod overriding: Overriding occurs when a class method has the same name and signature as a method in parent class. When you override methods, JVM determines the proper methods to call at the program’s run time, not at the compile time.

Overloading: Overloading is determined at the compile time. It occurs when several methods have same names with:
Different method signature and different number or type of parameters.
Same method signature but different number of parameters.
Same method signature and same number of parameters but of different type
Example of Overloading

int add(int a,int b)
float add(float a,int b)
float add(int a ,float b)
void add(float a)
int add(int a)
void add(int a) //error conflict with the method int add(int a)
class BookDetails {
String title;
setBook(String title){}
}
class ScienceBook extends BookDetails{
setBook(String title){} //overriding
setBook(String title, String publisher,float price){} //overloading
}

Q2) What is inheritance?
Ans) Inheritance is the property which allows a Child class to inherit some properties from its parent class. In Java this is achieved by using extends keyword. Only properties with access modifier public and protected can be accessed in child class.

public class Parent {
public String parentName;
public String familyName;

protected void printMyName() {
System.out.println(“ My name is “+ chidName+” “ +familyName);
}
}
public class Child extends Parent {
public String childName;
public int childAge;
//inheritance
protected void printMyName() {
System.out.println(“ My child name is “+ chidName+” “ +familyName);
}
}
In above example the child has inherit its family name from the parent class just by inheriting the class. When child object is created printMyName() present in child class is called.

Q3) What is multiple inheritance and does java support?
Ans) If a child class inherits the property from multiple classes is known as multiple inheritance. Java does not allow to extend multiple classes, to overcome this problem it allows to implement multiple Interfaces.

Q4) What is an abstraction ?
Ans) Abstraction is a way of converting real world objects in terms of class. Its a concept of defining an idea in terms of classes or interface. For example creating a class Vehicle and injecting properties into it. E.g
public class Vehicle {
public String colour;
public String model;
}

Q5) What is Encapsulation?
Ans) The encapsulation is achieved by combining the methods and attribute into a class. The class acts like a container encapsulating the properties. The users are exposed mainly public methods.The idea behind is to hide how thinigs work and just exposing the requests a user can do.

Q6) What is Association?

Ans) Association is a relationship where all object have their own lifecycle and there is no owner. Let's take an example of Teacher and Student. Multiple students can associate with single teacher and single student can associate with multiple teachers but there is no ownership between the objects and both have their own lifecycle. Both can create and delete independently.

Q7) What is Aggregation?
Ans) Aggregation is a specialize form of Association where all object have their own lifecycle but there is ownership and child object can not belongs to another parent object. Let's take an example of Department and teacher. A single teacher can not belongs to multiple departments, but if we delete the department teacher object will not destroy. We can think about "has-a" relationship.

Q8) What is Composition ?
Ans) Composition is again specialize form of Aggregation and we can call this as a "death" relationship. It is a strong type of Aggregation. Child object dose not have their lifecycle and if parent object deletes all child object will also be deleted. Let's take again an example of relationship between House and rooms. House can contain multiple rooms there is no independent life of room and any room can not belongs to two different house if we delete the house room will automatically delete.
Read More !

4 Jun 2015

Android Studio shortcuts like Eclipse

Yes, like Eclipse. For Android studio there is a list of shortcuts. Here are a few that I know.
Add unimplemented methods: CTRL + I
Override methods: CTRL + O
Format code: CTRL + ALT + L
Show project: ALT + 1
Show logcat: ALT + 6
Hide project - logcat: SHIFT + ESC

Build: CTRL + F9
Build and Run: CTRL + F10
Collapse all: CTRL + SHIFT + NumPad +
Expand all: CTRL + SHIFT + NumPad -
Find and replace: CTRL + R
Find: CTRL + F

By changing the keymaps settings you can use the same keyboard short cuts as in Eclipse (Or your favourite IDE)
File -> Settings -> KeyMap
Android Studio -> Preferences -> KeyMap (Mac)

change keymaps settings to eclipse so that you can use the short cut keys like in eclipse.
Eclipse shortcut in Android studio


Read More !

2 Apr 2015

Install an APK file in the Android emulator.

Windows:
  1. Execute the emulator (SDK Manager.exe->Tools->Manage AVDs...->New then Start)
  2. Start the console (Windows XP), Run -> type cmd, and move to the platform-tools folder ofSDK directory.
  3. Paste the APK file in the 'platform-tools' folder.

  4. Then type the following command.
    adb install [.apk path]

    Example:
    adb install C:\Users\Name\MyProject\build\xyz.apk

Linux:
In Linux the adb file is found in platform-tools directory under the SDK root directory
Mac:

PATH=$PATH:"adb location"  
Example : PATH=$PATH:/users/jorgesys/eclipse/android-sdk-mac_64/tools
Then run adb.

Read More !

20 Feb 2015

Why XML used in Android

XML stands for Extensible Markup Language. It is a markup language much like HTML but the the difference between XML and HTML is, XML was designed to transport and store data while HTML was designed to display data.XML is easy to parse and manipulate data pro-grammatically. It is a verbose(expressed in more words) and having a easily human readable format.

XML is easy to parse and manipulate programmatically, it's basically a tree structure and most UI creation tools already use it. It really has nothing to do with decoupling business logic because you can define Java code in Android using a Model-View-Controller pattern just as well.The reason that XML was chosen for android is because of its familiarity and the number of IDE (Integrated Development Environment like eclipse) tools that natively support it.

The auto-generated R.java file is a helper for the IDE so that you can get the benefit of autocomplete when you want to access a resource. It also stops you from making stupid mistakes since the compiler will complain if you try to access a resource you haven't defined. If you were using a simple properties file you wouldn't know until runtime that the 'key' you are using is missing.

There are several XML files used:
    Layout XML:
    -Used to define the actual UI (user interface) of your application.
    -It holds all the elements or the tools that you want to use in your application.

    Manifest XML:
    -Used to define all the components of your application.
    -It includes the names of your packages, your classes(Activities), services, permissions that your application needs.
   
    String XML:
    -Used to replace the hard coded strings with a single string.
    -Like, you can replace "Welcome to my application" with a string named "welcome" and you can refer to that string.

    Styles XML:
    -Used to define different styles and looks for the UI of your application.

    Drawable XML:
    -Used to provide various graphics to the elements of your application.

Read More !