13 Apr 2016

git - simple guide

1. Setup :- 
(a) git-osx-installer
(b) git-for-windows
(c) git-for-linux


2. Create a new local repository :-
create a new directory, open it and perform a
git init :- to create a new git repository.

3. Checkout a repository :-
Create a working copy of a local repository:
git clone /path/to/repository
For a remote server, use:
git clone username@host:/path/to/repository

4. Add & commit :-
Add one or more files to staging (index):
git add *
Commit changes to head (but not yet to the remote repository):
git commit -m "Commit message"

5. Pushing changes :-
Your changes are now in the HEAD of your local working copy. To send those changes to your remote repository, execute :
git status :- List the files you've changed and those you still need to add or commit:
git pull origin master : - if conflict , resolve conflict and again follow step no 4. then push the code.
git push origin master :- push the code to your remote repository.

6. Branching :-
Branches are used to develop features isolated from each other. The master branch is the "default" branch when you create a repository.
Create a new branch and switch to it: git checkout -b branchname
Switch from one branch to another: git checkout branchname
List all the branches in your repository, and also tell you what branch you're currently in: git branch
Delete the feature branch: git branch -d branchname
Push the branch to your remote repository, so others can use it: git push origin branchname
Push all branches to your remote repository: git push --all origin
Delete a branch on your remote repository: git push origin :branchname

7. Update & merge :-
Fetch and merge changes on the remote server to your working directory: git pull
To merge a different branch into your active branch: git merge branchname
if conflict, resolve conflict and again follow step no 4. then push the code in your remote repository.

Read More !

6 Apr 2016

Get screen dimensions in pixels

If you want the display dimensions in pixels you can use getSize :
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

If you're not in an Activity you can get the default Display via WINDOW_SERVICE:
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();

Before getSize was introduced (in API level 13), you could use the getWidth and getHeight methods that are now deprecated:
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth(); // deprecated
int height = display.getHeight(); // deprecated

For the use case you're describing however, a margin/padding in the layout seems more appropriate.
Another ways is: DisplayMetrics.
A structure describing general information about a display, such as its size, density, and font scaling. To access the DisplayMetrics members, initialize an object like this:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
Log.d("ApplicationTagName", "Display width in px is " + metrics.widthPixels);

We can use widthPixels to get information for:
"The absolute width of the display in pixels."
Example:

Log.d("ApplicationTagName", "Display width in px is " + metrics.widthPixels);

Read More !

28 Mar 2016

How to generate key hash for facebook in android

1. Using OpenSSL and command prompt
  • Download openssl from Download
  • Extract it. Create a folder- OpenSSL in C: / and copy all files here
  • Find “debug.keystore” file path. Most likely it will be inside “C:\Users\\.android” folder. In my system it is under “C:\Program Files\Java\jdk1.7.0_67\bin”
  • Open command prompt (Run-> cmd->start) and go to java /bin folder (cd “C:\Program Files\Java\jdk1.7.0_67\bin” command will do it for you)
Now you run the below command.

C:\\Program Files\\Java\\jdk1.6.0_30\\bin>keytool -exportcert 
 -alias androiddebugkey -keystore "C:\\Users\\.android\\debug.keystore" | "C:\\OpenSSL\\bin\\openssl" sha1 -binary |"C:\\OpenSSL\bin\\openssl" base64
Enter keystore password:android

2. Using a method call from android code
Using below code snippet, you can get the hash code.
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Add code to print out the key hash
    try {
        PackageInfo info = getPackageManager().getPackageInfo(
                "com.facebook.samples.hellofacebook",
                PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
            }
    } catch (NameNotFoundException e) {
       
Check your logcat output for a message.
Read More !

16 Jan 2016

Android start or stop adb from command line

Sometimes your android emulator might fails to connect Android Studio DDMS tool and ask for adb to start manually. In that case you can start or stop the adb using the command prompt.

Open the cmd from Start ⇒ run ⇒ cmd and execute these commands.Before you execute the commands in  CMD make sure that you added the adb tool to your Environment Variables path.

Set path in environment variables :-
"C:\Users\AmarAndroidTech\AppData\Local\Android\Sdk\platform-tools"

Killing adb :- adb kill-server
Starting adb :- adb start-server

Read More !

11 Jan 2016

Naming Conventions in java

1. Use full English descriptions for names. Avoid using abbreviations. For example, use names  like firstName,lastName, and middleInitial rather than the shorter versions fName, lName,  and mi.
2. Avoid overly long names (greater than 15 characters). For example, setTheLengthField should  be shortened tosetLength.
3. Avoid names that are very similar or differ only in case. For example, avoid using the names  product, products, and Products in the same program for fear of mixing them up.
For Ex : -
double tax1;       // sales tax rate (example of poor variable name)
double tax2;       // income tax rate (example of poor variable name)
double salesTaxRate;      //no comments required due to
double incomeTaxRate;     //self-documenting variable names
Variable Naming Conventions :-
Choose meaningful names that describe what the variable is being used for. Avoid generic names like number or temp whose purpose is unclear. Compose variable names using mixed case letters starting with a lower case letter. For example, use salesOrder rather than SalesOrder or sales_order.
Use plural names for arrays. For example, use testScores instead of testScore. Exception: for loop  counter variables are often named simply i, j, or k, and declared local to the for loop whenever possible.
for (int i = 0; i < MAX_TEMPERATURE; i++){
   boilingPoint = boilingPoint + 1;
}

Constant Naming Conventions :- 
Use ALL_UPPER_CASE for your named constants, separating words with the underscore character.  For example, use TAX_RATE rather than taxRate or TAXRATE. Avoid using magic numbers in the  code. Magic numbers are actual numbers like 27 that appear in the code that require the reader to figure out what 27 is being used for. Consider using named constants for any number other than 0 and 1. 

Method Naming Conventions :-
1.Compose method names using mixed case letters, beginning with a lower case letter and starting each subsequent word with an upper case letter. 

2.Begin method names with a strong action verb (for example, deposit). If the verb is not  descriptive enough by itself, include a noun (for example, addInterest). Add adjectives if necessary to clarify  the noun (for example, convertToEuroDollars). 

3.Use the prefixes get and set for getter and setter methods. Getter methods merely return the  value of a instance variable; setter methods change the value of a instance variable. For example, use the method names getBalance and setBalance to access or change the instance variable balance. 

4.If the method returns a boolean value, use is or has as the prefix for the method name. For  example, use isOverdrawn or hasCreditLeft for methods that return true or false values. Avoid the  use of the word not in the boolean method name, use the ! operator instead.For example, use  !isOverdrawn instead of isNotOverdrawn.

  

Parameter Naming Conventions :-
With formal parameter names, follow the same naming conventions as with variables, i.e. use mixed case, begin with a lower case letter, and begin each subsequent word with an upper-case letter. 

1.Consider using the prefix a or an with parameter names. This helps make the parameter distinguishable from local and instance variables. Occasionally, with very general purpose methods, the names chosen may be rather generic (for example, aNumber). However, most of the time the parameter names should succinctly describe the type of value being passed into the method. 
Example:- 
public void deposit(long anAccountNumber, double aDepositAmount)
  ... 
}
public boolean isNumberEven(int aValue)
  ... 
}
Read More !

8 Jan 2016

Android Directory Structure

Within an Android project structure, the most frequently edited folders are:
  • src - Java source files associated with your project. This includes the Activity "controller" files as well as your models and helpers.
  • res - Resource files associated with your project. All graphics, strings, layouts, and other resource files are stored in the resource file hierarchy under the res directory.
  • res/layout - XML layout files that describe the views and layouts for each activity and for partial views such as list items.
  • res/values - XML files which store various attribute values. These include strings.xml, dimens.xml, styles.xml, colors.xml, themes.xml, and so on.
  • res/drawable - Here we store the various density-independent graphic assets used in our application.
  • res/mipmap-xhdpi - Series of folders for density specific images to use for various resolutions.
The most frequently edited files are:

  • AndroidManifest.xml - This is the Android application definition file. It contains information about the Android application such as minimum Android version, permission to access Android device capabilities such as internet access permission, ability to use phone permission, etc.
  • res/layout/activity_main.xml - This file describes the layout of the activity's UI. This means the placement of every view object on one app screen.
  • src/.../MainActivity.java - The Activity "controller" that constructs the activity using the view, and handles all event handling and view logic for one app screen.
Other less edited folders include:
  • gen - Generated Java code files, this library is for Android internal use only.
  • assets - Uncompiled source files associated with your project; Rarely used.
  • bin - Resulting application package files associated with your project once it’s been built.
  • libs - Contains any secondary libraries (jars) you might want to link to your app.

Read More !

1 Jan 2016

Seeing java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation

Seeing java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation

You have a third-party library reference defined twice. Check your app/build.gradle for duplicate libraries (i.e. commons-io library defined for 1.3 and another one using 2.4).
Read More !