Friday 18 July 2014

Set values to Dimen folder for different screen of Android

 Please use below list for all resolution support.

values-sw720dp 10.1” tablet 1280x800 mdpi

values-sw600dp 7.0”     tablet 1024x600 mdpi

values-sw480dp 5.4” 480x854 mdpi 
values-sw480dp 5.1” 480x800 mdpi 

values-xhdpi         4.7”   1280x720 xhdpi 
values-xhdpi         4.65” 720x1280 xhdpi 

values-hdpi 4.0” 480x800 hdpi
values-hdpi 3.7” 480x854 hdpi

values-mdpi 3.2” 320x480 mdpi

values-ldpi 3.4” 240x432 ldpi
values-ldpi 3.3” 240x400 ldpi
values-ldpi 2.7” 240x320 ldpi


Friday 13 June 2014

Coding Standards.


Classes and interfaces The first letter should be capitalized, and if several words are linked together to form the name, the first letter of the inner words should be uppercase (a format that's sometimes called "camelCase"). For classes, the names should typically be nouns.

For example:
Dog
Account
PrintWriter

For interfaces, the names should typically be adjectives like

Runnable
Serializable

Methods The first letter should be lowercase, and then normal camelCase rules should be used. In addition, the names should typically be verb-noun pairs. 

For example:
getBalance
doCalculation
setCustomerName

Variables Like methods, the camelCase format should be used, starting with a lowercase letter. Sun recommends short, meaningful names, which sounds good to us. 

Some examples:
buttonWidth
accountBalance
myString

Constants Java constants are created by marking variables static and final. They should be named using uppercase letters with underscore characters as separators: MIN_HEIGHT


  • Use access modifier with proper requirement.
  • For constant declaration use Enum with example
  • prefix use based on class name
  • layout name give based on meaning and use.
  • use inheritance and polymorphism 
  • Use JavaDoc for every Class,Method,Object etc... description  

JavaBeans Standards

First, JavaBeans are Java classes that have properties. For our purposes, think of properties as private instance variables. Since they're private, the only way they can be accessed from outside of their class is through methods in the class. The methods that change a property's value are called setter methods, and the methods that retrieve a property's value are called getter methods. The JavaBean naming rules that you'll need to know for the exam are the following:

JavaBean Property Naming Rules

■ If the property is not a boolean, the getter method's prefix must be get. For example, getSize()is a valid JavaBeans getter name for a property named "size." Keep in mind that you do not need to have a variable named size (although some IDEs expect it). The name of the property is inferred from the getters and setters, not through any variables in your class. What you return from getSize() is up to you.

■ If the property is a boolean, the getter method's prefix is either get or is. For example, getStopped() or isStopped() are both valid JavaBeans names for a boolean property.

■ The setter method's prefix must be set. For example, setSize() is the valid JavaBean name for a property named size.

■ To complete the name of a getter or setter method, change the first letter of the property name to uppercase, and then append it to the appropriate prefix (get, is, or set).

■ Setter method signatures must be marked public, with a void return type and an argument that represents the property type.

■ Getter method signatures must be marked public, take no arguments, and have a return type that matches the argument type of the setter method for that property.

Second, the JavaBean spec supports events, which allow components to notify each other when something happens. The event model is often used in GUI applications when an event like a Button click is multicast to many other objects that may have things to do when the mouse click occurs. The objects that receive the information that an event occurred are called listeners. For the exam, you need to know that the methods that are used to add or remove listeners from an event must also follow JavaBean naming standards:

 JavaBean Listener Naming Rules

■ Listener method names used to "register" a listener with an event source must use the prefix add, followed by the listener type. For example, addActionListener() is a valid name for a method that an event source will have to allow others to register for Action events.

■ Listener method names used to remove ("unregister") a listener must use the prefix remove, followed by the listener type (using the same rules as the registration add method).

■ The type of listener to be added or removed must be passed as the argument to the method.

■ Listener method names must end with the word "Listener".

Examples of valid JavaBean method signatures are

public void setMyValue(int v)
public int getMyValue()
public boolean isMyStatus()
public void addMyListener(MyListener m)
public void removeMyListener(MyListener m)

Examples of invalid JavaBean method signatures are

void setCustomerName(String s) // must be public
public void modifyMyValue(int v) // can't use 'modify'
public void addXListener(MyListener m) // listener type mismatch

Source File Declaration Rules

Before we dig into class declarations, let's do a quick review of the rules associated
with declaring classes, import statements, and package statements in a source file:
■ There can be only one public class per source code file.

■ Comments can appear at the beginning or end of any line in the source code file; they are independent of any of the positioning rules discussed here.

■ If there is a public class in a file, the name of the file must match the name of the public class. For example, a class declared as public class Dog { } must be in a source code file named Dog.java.

■ If the class is part of a package, the package statement must be the first line in the source code file, before any import statements that may be present.

■ If there are import statements, they must go between the package statement (if there is one) and the class declaration. If there isn't a package statement, then the import statement(s) must be the first line(s) in the source code file. If there are no package or import statements, the class declaration must be the first line in the source code file.

■ import and package statements apply to all classes within a source code file. In other words, there's no way to declare multiple classes in a file and have them in different packages, or use different imports.

■ A file can have more than one nonpublic class.
 
 
 Reference from SCJP book

Thursday 12 June 2014

 

Declaring Constructors, Methods, and Variables in an enum with example in Android OR Java


Note: Every enum has a static method, values(), that returns an array of the enum's values in the order they're declared.

The key points to remember about enum constructors are

 

■ You can NEVER invoke an enum constructor directly. The enum constructor is invoked automatically, with the arguments you define after the constant value. For example, BIG(8) invokes the CoffeeSize constructor that takes an int, passing the int literal 8 to the constructor. (Behind the scenes, of course, you can imagine that BIG is also passed to the constructor, but we don't have to know—or care—about the details.)

■ You can define more than one argument to the constructor, and you can overload the enum constructors, just as you can overload a normal class constructor. We discuss constructors in much more detail in Chapter 2. To initialize a CoffeeType with both the number of ounces and, say, a lid type, you'd pass two arguments to the constructor as BIG(8, "A"), which means you have a constructor in CoffeeSize that takes both an int and a String. And finally, you can define something really strange in an enum that looks like an anonymous inner class (which we talk about in Chapter 8). It's known as a constant specific class body, and you use it when you need a particular constant to override a method defined in the enum. Imagine this scenario: you want enums to have two methods—one for ounces and one for lid code (a String). Now imagine that most coffee sizes use the same lid code, "B", but the OVERWHELMING size uses type "A". You can define a getLidCode() method in the CoffeeSize enum that returns "B", but then you need a way to override it for OVERWHELMING. You don't want to do some hard-to- maintain if/then code in the getLidCode() method, so the best approach might be to somehow have the OVERWHELMING constant override the getLidCode() method.

This looks strange, but you need to understand the basic declaration rules:

 

    enum CoffeeSize {
        BIG(8), HUGE(10), OVERWHELMING(16) {
         // start a code block that defines
         // the "body" for this constant
         public String getLidCode() {
              // override the method
              // defined in CoffeeSize
               return "A";
         }
        };
        // the semicolon is REQUIRED when more code follows
        CoffeeSize(int ounces) {
            this.ounces = ounces;
        }

        private int ounces;

        public int getOunces() {
            return ounces;
        }

        public String getLidCode() {
            return "B";
        }
    }


protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("ENUM", "BIG.getLidCode() = "+CoffeeSize.BIG.getLidCode());
        Log.d("ENUM", "BIG.getOunces() = "+CoffeeSize.BIG.getOunces());
        Log.d("ENUM", "BIG.ounces = "+CoffeeSize.BIG.ounces);
        Log.d("ENUM", "BIG.ordinal() = "+CoffeeSize.BIG.ordinal());
       
       
        Log.d("ENUM", "HUGE.getLidCode() = "+CoffeeSize.HUGE.getLidCode());
        Log.d("ENUM", "HUGE.getOunces() = "+CoffeeSize.HUGE.getOunces());
        Log.d("ENUM", "HUGE.ounces = "+CoffeeSize.HUGE.ounces);
        Log.d("ENUM", "HUGE.ordinal() = "+CoffeeSize.HUGE.ordinal());
       
       
        Log.d("ENUM", "OVERWHELMING.getLidCode() = "+CoffeeSize.OVERWHELMING.getLidCode());
        Log.d("ENUM", "OVERWHELMING.getOunces() = "+CoffeeSize.OVERWHELMING.getOunces());
        Log.d("ENUM", "OVERWHELMING.ounces = "+CoffeeSize.OVERWHELMING.ounces);
        Log.d("ENUM", "OVERWHELMING.ordinal() = "+CoffeeSize.OVERWHELMING.ordinal());


}


OutPut :-

 BIG.getLidCode() = B
 BIG.getOunces() = 8
 BIG.ounces = 8
 BIG.ordinal() = 0


 HUGE.getLidCode() = B
 HUGE.getOunces() = 10
 HUGE.ounces = 10
 HUGE.ordinal() = 1


 OVERWHELMING.getLidCode() = A
 OVERWHELMING.getOunces() = 16
 OVERWHELMING.ounces = 16
 OVERWHELMING.ordinal() = 2


Reference from SCJP book

Wednesday 16 March 2011

ListView Data Select And Delete

New Android Project Select

Project Name : ListviewSample

Build Target : Android 2.2

Application Name :  ListviewSample

Package Name :  com.ovte.listviewsample

Create Activity : ListviewSample

Min SDK Version : 8

Open AndroidManifest.xml File

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.ovte.listviewsample"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name="ListviewSample"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-sdk android:minSdkVersion="3" />
</manifest> 


Open ListviewSample.java File

package com.ovte.listviewsample;

import java.util.ArrayList;
import java.util.Arrays;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

public class ListviewSample extends ListActivity {

    /** We want to display these names as our list items */
    String[] names = {
 "Piyush","Ankit","Dipak","Hitesh","Renuka","Dharmesh","Mitesh","Mihir","Lalit","Kiran","Rohit","Krunal"
    };
   
    private ArrayAdapter<String> adapter;        // The list adapter
    private String selectedItem;                // Stores the selected list item
    private final Context context = this;        // This context
   
   
    /** Called when the activity is first created. */
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
       
        // Create the listener for normal item clicks
        OnItemClickListener itemListener = new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View v, int position, long rowid) {

                Toast.makeText(
                        getApplicationContext(),
                        "You have clicked on " + parent.getItemAtPosition(position).toString() + ".",
                        Toast.LENGTH_SHORT).show();
            }
        };
       
        // Create the listener for long item clicks
        OnItemLongClickListener itemLongListener = new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long rowid) {
               
                // Store selected item in global variable
                selectedItem = parent.getItemAtPosition(position).toString();
       
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setMessage("Do you want to remove " + selectedItem + "?");
                builder.setCancelable(false);
                builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                   
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                       
                        adapter.remove(selectedItem);
                        adapter.notifyDataSetChanged();
               
                        Toast.makeText(
                                getApplicationContext(),
                                selectedItem + " has been removed.",
                                Toast.LENGTH_SHORT).show();
                    }
                });
                builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                   
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });

                // Create and show the dialog
                builder.show();
               
                // Signal OK to avoid further processing of the long click
                return true;
            }
        };
       
        // Convert array to ArrayList
        ArrayList<String> list = new ArrayList<String>(Arrays.asList(names));
       
        // Create new ArrayAdapter with the ArrayList as underlying data source
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,list);
       
        // Assign adapter to the list
        setListAdapter(adapter);
       
        // Assign both click listeners
        getListView().setOnItemClickListener(itemListener);
        getListView().setOnItemLongClickListener(itemLongListener);
    }
}

Output 

  Application Run Then Default Display This Type
 

    Click On ListView Then Selected Value Display


 
Long Press on Value When Delete Dialog Display