Showing posts with label code. Show all posts
Showing posts with label code. Show all posts

31 Jul 2017

Android app code to illustrate sqlite database

package com.ani;

import android.app.*;import android.os.*;import android.content.*;import android.app.DatePickerDialog.*;import android.app.TimePickerDialog.*;
import android.view.View.*;import android.view.*;import android.widget.*;import android.database.sqlite.*;import android.database.*;
import java.text.*;import java.util.*;import android.widget.*;import android.graphics.Color;

public class HomePage extends Activity 

{
SQLiteDatabase db;LinearLayout ll;Context ctx;
private static String DB="TEST.db",TBL="MY_TABLE";
private static boolean screenChange=false;
@Override
protected void onSaveInstanceState(Bundle b)
{
    super.onSaveInstanceState(b);
String str="Screen Change="+String.valueOf(screenChange)+"....";
    Toast.makeText(ctx,str+"You are changing orientation...",Toast.LENGTH_SHORT).show();
screenChange=true;
    
}

@Override
public void onCreate(Bundle b)
        {
super.onCreate(b);
ctx=getApplicationContext();
if(!screenChange)
{
String str="Screen Change="+String.valueOf(screenChange);
Toast.makeText(ctx,str+"Trying to Drop Previous & Create New Table...",Toast.LENGTH_SHORT).show();

killTable();makeTable();str=" Screen Change="+String.valueOf(screenChange)+"....";
Toast.makeText(ctx,str,Toast.LENGTH_SHORT).show();

showData();

Toast.makeText(ctx,str+"Trying to Put Data in Table...",Toast.LENGTH_SHORT).show();
putData();

Toast.makeText(ctx,str+"Trying to Show Data in Table...",Toast.LENGTH_SHORT).show();
showData();
}
}

public void makeTable()
{
try {
db=openOrCreateDatabase(DB,Context.MODE_PRIVATE,null);
db.execSQL(  "CREATE TABLE IF NOT EXISTS " + TBL +" (ID INTEGER PRIMARY KEY ,NAME TEXT,PLACE TEXT);"  );
db.close();
Toast.makeText(ctx,"Created Table successfully...",Toast.LENGTH_SHORT).show();
   }
catch(Exception e)
   {
Toast.makeText(ctx,"Error in Creating Table...",Toast.LENGTH_SHORT).show();
   }
}

public void killTable()
{
try {
db=openOrCreateDatabase(DB,Context.MODE_PRIVATE,null);
db.execSQL(  "DROP TABLE " + TBL );
db.close();
Toast.makeText(ctx,"Killed Table...",Toast.LENGTH_LONG).show();
   }
catch(Exception e)
   {
Toast.makeText(ctx,"Error encountered while Dropping Table...",Toast.LENGTH_LONG).show();
   }
}
public void putData()
{
try {
db=openOrCreateDatabase(DB,Context.MODE_PRIVATE,null);
db.execSQL(  "INSERT INTO " + TBL +" (NAME,PLACE) VALUES ('ANIMESH','KOLKATA')"  );
db.close();
Toast.makeText(ctx,"Successfully Inserted Records in Table...",Toast.LENGTH_SHORT).show();
   }
catch(Exception e)
   {
Toast.makeText(ctx,"Error in Inserting Records in Table...",Toast.LENGTH_SHORT).show();
   }
}
public void showData()
{
try {
db=openOrCreateDatabase(DB,Context.MODE_PRIVATE,null);
String qry="SELECT * FROM "+TBL;
Cursor allrows=db.rawQuery(qry,null);

if(allrows.moveToFirst())
{
do
 {
String id=allrows.getString(0),name=allrows.getString(1),place=allrows.getString(2);
String msg="Record No." + id + "  ->   Name: " + name + " , Place: " + place; 
Toast.makeText(ctx,msg,Toast.LENGTH_SHORT).show();
 }
while(allrows.moveToNext());



}
else
{
Toast.makeText(ctx,"No Records as yet...",Toast.LENGTH_SHORT).show();
}


db.close();
Toast.makeText(ctx,"Successfully shown Records in Table...",Toast.LENGTH_SHORT).show();
   }
catch(Exception e)
   {
Toast.makeText(ctx,"Error in Retrieving Records in Table...",Toast.LENGTH_SHORT).show();
   }
}

Make android java code

Let's make Main.Java

This Main class file of  android Java must be an extension of Activity class. It's instantiated by Android system when app is run by user.

public class Main extends Activity
{
 protected void onCreate ()
  {
  }
}
Above is a skeleton which says
Let's have a publicly available code resembling inbuilt code of Activity and let's only change a portion of it called onCreate.
    This function is called when an object of this code is instantiated in RAM memory of Android mobile. What is created is an empty screen shown to user with a top title as Activity label defined in manifest.

We can place many more text and buttons etc in this empty screen by putting code in this custom function.

But what code. So see below.
TextView tv = new TextView (this)

Above statement allocates a textview object in RAM memory.

tv.setText("Hi dude");
This puts Hi dude in its data variable for containing text.

setContentView(tv);
This above statement let's the empty screen window object point to the newly allocated tv object so that contents of tv view object are displayed on mobile screen.

30 Jul 2017

Make AndroidManifest.xml

We need one androidManifest.xml file to describe icon and name of app to be displayed in mobile to user. Also this file will tell which .class file to execute first out of all classes stored in Dex file. We can call it as main class file. Let's make it.

AndroidManifest. xml
manifest xmlns:android = "URI" version=1.0 encoding=utf-8

Above statement tells that all elements inside tags in this XML if prefixed by word 'android' then replace Android by URI. URI can be HTTP:/schema/apk... Etc.

application android:icon="@drawable/myicon" android:label="@string/app name".

Above statement tells what icon to use and what label for this app to be displayed to users. Android URI will be prefixed to icon and label elements. When aapt tool makes apk it will only consider those elements which have this prefix.

activity android:name="Main" android:label="My App"

Above statement uses activity element and says that Main.class code must be executed first and title label should be My App.

intent-filter
action android:name="android.intent.action.MAIN"
category android:name = "android. intent. category.LAUNCHER"
intent-filter

Here optional Activity related commands can be placed.
While Activity refers to screen of mobile placed before user.

So things like making it full screen without title bar or allowing it's auto resizing or panning thereby denying scrolling on pop-up of soft keyboard can be done here.

'activity windowSoftInputMode ="adjustSize|adjustPan" /
'activity Orientation="Portrait"/>
Window feature flag full screen.

activity
application
manifest

In above statement two elements <action>,<category> are defined within <intent-filter> element. User actions of clicking touching mobile screen suggest there intentions. When there intention is fired from launching an app by app launcher service in mobile then the category of intention is stored as LAUNCHER.
Now is the user deleting uninstalling or running this app. If running then action is MAIN.

This manifest will be used two time.
First time when generating R.java and R.class etc.
Second time to generate apk by aapt tool.