Posts

Showing posts from May, 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);