Posts

Showing posts from 2011

Android Database Example

Database Class import java.util.ArrayList; import java.util.List; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteStatement; import android.util.Log; public class DatabaseHelper { private static final String DATABASE_NAME = "firstdatabase.db"; private static final int DATABASE_VERSION = 1; private static final String TABLE_NAME = "mytable"; private Context context; private SQLiteDatabase sqlDb; private SQLiteStatement insertStatement; private static final String INSERT = "insert into " + TABLE_NAME + "(name) values (?)"; public DatabaseHelper(Context context) { this.context = context; OpenHelper openHelper = new OpenHelper(this.context); this.sqlDb = openHelper.getWritableDatabase(); this.insertStatement = this.sqlDb.compileStatement(INSERT); } public long insert(Strin

Java Singleton Class

The Singleton class’s default constructor is made private,  which prevents the direct instantiation of the object by other classes using the new keyword. The Singleton is a useful Design Pattern for allowing only one instance of your class, but common mistakes can inadvertently allow more than one instance to be created.

JSON Object

JSON (JavaScript Object Notation) is a lightweight computer data interchange format.  It is a text-based, human-readable format for representing simple data structures and  associative arrays (called objects).  The JSON format is often used for transmitting structured data over a network connection  in a process called serialization. Its main application is in AJAX web application  programming, where it serves as an alternative to the traditional use of the XML format. Supported Data types are ( String, Number, Boolean,Array,  Object (Collection of key/value Pairs), null ). Example import net.sf.json.JSONObject; public class MyJSON { public static void main(String args[]){ JSONObject object=new JSONObject(); object.put("name","your Name"); object.put("Max.Marks",new Integer(100)); object.put("Min.Marks",new Double(35)); object.put("Scored",new Double(86.67)); object.put("nickname","your&qu

Change Apache Tomcat Port Number

 Tomcat runs on port 8080. But there can be situations where there are some other servers running on this same port, forcing you to change the port of one of the servers. Go to your tomcat installed path. C:\..............\Tomcat 6.0\conf\server.xml In the server.xml file; locate the following segment. <Connector port="8080" … /> By Changing this 8080 port number to your new port number.

Android Notes

Activity: Your applications presentation layer.  Every screen in your application will be an extension of activity.  It is nothing but a visible screen. (Layouts, Views) Intent: An inter-application message-passing framework. Intent is an abstract description of an operation  to be performed. Intent provides a facility for performing late runtime binding between the codes  in different applications. Intent Filter: An intent filter declares the capabilities of its parent component. Broadcast receiver: Application - receive and respond to a global event, such as the phone ringing or an  incoming text message. Broadcast Receivers will automatically start your application to  respond to an Incoming Intent, making them perfect for creating event-driven applications. Layouts: Template of view objects.Some Layouts are Absolute Layout, Frame Layout, Linear Layout,  Relative Layout. Android manifest.xml This XML file describes where your application begins, what its permissio

Android menu creation and implementation

To create a menu resource, create an yourmenu.xml  file inside your project's  res/menu/  directory yourmenu.xml   <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/menu_next" android:icon="@drawable/icon_next" android:title="Next" /> <item android:id="@+id/menu_back" android:icon="@drawable/icon_back" android:title="Back" /> </menu> Creation   @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.yourmenu, menu); return true; } If menu item selected.. @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menu_next: //your code here.. return true; default: return super.onOptionsItemSelected(item)

Android Progress Dialog using runnable

Following code will create a progress dialog and run using ruunable interface. final ProgressDialog dialog = ProgressDialog.show(Activity.this, "Your Message", "Status Message",true); Handler handler = new Handler(); Runnable runnable = new Runnable() { @Override public void run() { startActivity(new Intent(MyActivity.this, YourActivity.class)); dialog.dismiss(); } }; handler.postDelayed(runnable, 3000);

Android Button onClickListener

Button button = (Button) findViewById(R.id.your_button); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(firstActivity.this, "Message by Donor", Toast.LENGTH_SHORT).show(); } });

Android Intent

Intents are used as a message passing mechanism. It works within your application, between the applications.  One of the most common uses for intents is to start new activities. It is either explicitly (by specifying the class to load) or implicitly (by requesting that an action be performed on a piece of data). Explicit Intent startActivity(new Intent(FirstActivity.this,SecondActivity.class)); Implicit Intent Intent intent = new Intent (Intent.ACTION_DIAL, Uri.parse ("tel: 123-4567")); StartActivity (intent);

Android Custom List View Creation

import java.util.ArrayList; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.TextView; public class MyActivity extends Activity { public ArrayList<String> oneList = new ArrayList<String>(); public ArrayList<String> twoList = new ArrayList<String>(); private oneAdapter oneAdapter; private ListView oneListView; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.yourXml); } @Override protected void onResume() { super.onResume(); setContentView(R.layout.yourXml); listCreation(); oneListView = (ListView) findViewById(R.id.list_medic

SwapNumbersWithoutThirdVariable - JAVA

public class SwapNumbersWithoutThirdVariable { public static void main(String[] args) { int num1 = 10; int num2 = 20; System.out.println("Before Swapping"); System.out.println("Value of num1 is :" + num1); System.out.println("Value of num2 is :" +num2); //add both the numbers and assign it to first num1 = num1 + num2; num2 = num1 - num2; num1 = num1 - num2; System.out.println("After Swapping"); System.out.println("Value of num1 is :" + num1); System.out.println("Value of num2 is :" +num2); } }

What is .shtml?

SHTML  stands for Server-parsed HyperText Markup Language. It is a file extension identifying a particular type of HyperText Markup Language (HTML) file. Basic HTML files usually provide the text and formatting for web pages.  SHTML  files generally perform the same function, but they also allow the use of simple server commands.

Android Screen sizes and folder struture

Image
Low density Small screens QVGA 240x320 res/layout-small-ldpi res/layout-small-land-ldpi Low density Normal screens WVGA400 240x400 (x432) res/layout-ldpi res/layout-land-ldpi Medium density Normal screens HVGA 320x480 res/layout-mdpi res/layout-land-mdpi Medium density Large screens HVGA 320x480 res/layout-large-mdpi res/layout-large-land-mdpi High density Normal screens WVGA800 480x800 (x854) res/layout-hdpi res/layout-land-hdpi Xoom (medium density large but 1280x800 res) res/layout-xlarge res/layout-xlarge-land

Android layout border with rounded corners

Create the file layout_border.xml in your drawables directory and put in the following content. layout_border.xml <?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#FFFFFF"/> <stroke android:width="3dip" android:color="#B1BCBE" /> <corners android:radius="10dip"/> <padding android:left="0dip" android:top="0dip" android:right="0dip" android:bottom="0dip" /> </shape> Implementation you have to call the above xml in your border property of your (Relative or Linear or etc.,) layout: android:background="@drawable/layout_border"

Prototype.js - Useful Methods

Method Description $() If provided with a string, returns the element in the document with matching ID; otherwise returns the passed element. $$() Takes an arbitrary number of CSS selectors (strings) and returns a document-order array of extended DOM elements that match any of them. $A() Converts the single argument it receives into an Array object. $F() Returns the value of a form control. This is a convenience alias of Form.Element.getValue. $H() Converts objects into enumerable Hash objects that resemble associative arrays $R() Creates a new ObjectRange object. $w() Splits a string into an Array, treating all whitespace as delimiters. Try.these Accepts an arbitrary number of functions and returns the result of the first one that doesn't throw an error.

Blackberry Resource File Creation and Calling Method

(1) On the File menu , click New > Other. (2) In the New dialog box, expand the BlackBerry item . (3) Click BlackBerry Resource File . (4) Click Next. (5) In the Create a new BlackBerry Resource File dialog box, click the project folder for the BlackBerry device application. (6) In the File name field , type the name of the .rrc file or .rrh file. (7) Click Finish . Calling Method: public static ResourceBundle _res = ResourceBundle.getBundle (stringsResource.BUNDLE_ID,stringsResource.BUNDLE_NAME); private String valueText = ClassName._res.getString (stringsResource.stringValue); // stringsResource means fileName(strings)+Resource //  BUNDLE_ID and  BUNDLE_NAME both are default.

Blackberry SubMenu Creation and Calling Method

// creating menu and submenus SubMenu menu = new SubMenu(); SubMenu sMenu = new SubMenu(); menu.add(new SubMenuItem("MenuName", sMenu));  //menu is main Menu  //u have to call from where ever you want Ui.getUiEngine().pushScreen(menu);

Blackberry - SubMenu

class SubMenu extends PopupScreen implements ListFieldCallback { private static final int MAX_WIDTH = Font.getDefault().getAdvance( "max menu item text"); XYRect mRectangle = new XYRect(); Vector mSubMenuItems = new Vector(); ListField mListField; public SubMenu() { super(new SubMenuItemManager(), DEFAULT_CLOSE); int rowHeight = getFont().getHeight() + 2; mListField = new ListField() { protected boolean navigationClick(int status, int time) { runMenuItem(getSelectedIndex()); return super.navigationClick(status, time); } }; mListField.setRowHeight(rowHeight); add(mListField); mListField.setCallback(this); updateMenuItems(); } public void add(SubMenuItem subMenuItem) { mSubMenuItems.addElement(subMenuItem); subMenuItem.mMenu = this; updateMenuItems(); } private void updateMenuItems() { int rowCounts = mSubMenuItems.size(); mRectangle.width = getMaxObjectToStringWidth(mSubMenuItems); mRectangle.height = mListField.getRo

Blackberry Custom Image Field with active and normal Images

public class ImageField extends BitmapField { Bitmap mNormal; Bitmap mFocused; Bitmap mActive; int mWidth; int mHeight; public ImageField(Bitmap normal, Bitmap focused, Bitmap active, long align) { super(active, align | ImageField.FOCUSABLE); mNormal = normal; mFocused = focused; mActive = active; mWidth = mFocused.getWidth(); mHeight = mFocused.getHeight(); } protected void paint(Graphics graphics) { Bitmap bitmap = null; switch (getVisualState()) { case VISUAL_STATE_NORMAL: bitmap = mNormal; break; case VISUAL_STATE_FOCUS: bitmap = mFocused; break; case VISUAL_STATE_ACTIVE: bitmap = mActive; break; default: bitmap = mNormal; } graphics.drawBitmap(0, 0, bitmap.getWidth(), bitmap.getHeight(), bitmap, 0, 0); } public int getPreferredWidth() { return mWidth; } public int getPreferredHeight() { return mHeight; } protected void layout(int width, int height) { setExtent(mWidth, mHeight); } }

Blackberry Vertical Layout Size and Vertical Scrollbar

VerticalFieldManager myLayout = new VerticalFieldManager (VerticalFieldManager.FIELD_LEFT | VerticalFieldManager.FOCUSABLE | VerticalFieldManager.USE_ALL_WIDTH| VerticalFieldManager.VERTICAL_SCROLL|VerticalFieldManager. VERTICAL_SCROLLBAR){ protected void sublayout(int maxWidth, int maxHeight) { int setWidth = Display.getwidth -20; int setHeight = Display.getHeight -130; super.sublayout(setWidth, setHeight); } };

Blackberry Bar-Graph Field & Output

Image
Pass the values to Bar-Graph Field VerticalFieldManager graphLayout = new VerticalFieldManager( VerticalFieldManager.FIELD_HCENTER); // Graph BarGraphField _barGraph = new BarGraphField(values, colors, 10, 1, 10, BarGraphField.PADDING_MEDIUM, BarGraphField.PADDING_MEDIUM,true); _barGraph.setPadding(2, 10, 5, 10); graphLayout.add(_barGraph); Output:

Blackberry Bar-Graph (Class 2)

import net.rim.device.api.ui.Font; import net.rim.device.api.ui.Graphics; import net.rim.device.api.util.Arrays; /** * A field that can display data in a bar graph format. */ public class BarGraphField extends GraphField { /** * * Use no padding. */ public static final int PADDING_NONE = 0; /** * * Use low padding. */ public static final int PADDING_LOW = 4; /** * * Use medium padding. */ public static final int PADDING_MEDIUM = 8; /** * * Use high padding. */ public static final int PADDING_HIGH = 12; private int _scale, _increment, _barWidth, _barPadding, _scalePadding; private boolean _showScale; /** * * Constructs a bar graph using the default (PADDING_MEDIUM) padding without * scale labels. * * @param values * The values (data) to be graphed. * @param colors * The colors used to draw each bar. * @param scale * The maximum possible value of the graph, the top scale value. * @param increment * The increment a

Blackberry Bar-Graph (Class 1)

import net.rim.device.api.ui.Field; /** * The base class for which other graph fields are derived from. */ public abstract class GraphField extends Field { /* * The values (data) used to create the graph. */ protected int[] _values; /** * The colors used to display the data sets. */ protected int[] _colors; /** * The width of the field. */ protected int _width; /** * The height of the field. */ protected int _height; /** * Creates a plain empty graph field. */ GraphField() { super(); } /** * Retrieves the color at the specified index. * * @param index * The index of the color. * @return an integer in form 0x00RRGGBB */ public int getColor(int index) { return _colors[index]; } /** * Retrieves an array of colors. * * @return an array of integers in the form 0x00RRGGBB */ public int[] getColors() { return _colors; } /** * Retrieves the value at the specified index. * * @param index *