Showing posts with label android. Show all posts
Showing posts with label android. Show all posts

19 Aug 2017

Step 1. Convert your Windows Laptop into an Android App Development Environment

What is a development Environment, by the way.....?

A development environment is nothing but some settings on your laptop's operating system.

What settings are needed....?

Setting :-

When I type  in command-prompt......

                  C:\....\>  AppToMobile
.....& press <ENTER> , then I will find my App in my mobile to run & test it.

What follows now is how to achieve these settings.

  • Download development Kits :-  We need to download a Java Development Kit called JDK for Windows. And Android Development Kit called ADK. I have downloaded JDK 1.6 because its old & old is gold. Its lesser in size and complexity and has been tested and is stable with known bugs only. Similarly try downloading older version of Android SDK haveing GUI version of SDK Manager so that you can download essentials. Avoid the slow moving Android Studio. Using SDK Manager download Android-23 platform build tools.
  • Configure Kits :- Set Windows PATH variable by Rt.Clicking My Computer -> Properties ->Advanced System Settings or reach through Control-Panel. PATH should include the javac.exe file in JDK directory, android.bat file of ADK.
  • Download Build-Tool :-  We dont need it but it is very convenient if we have one. Gradle is preferred , though Ant is fast. These programs configure the ADK command-line tools for easy usage for our convenience. I downloaded gradle old version 2.2.1 as it can use old java & old android.
  • For a detailed illustration kindly see this link: https://letsmakeandroidapp.wordpress.com
  • SCREENSHOTS of my devlopment environment:-





Step 2. Create Android Mobile App Project Structure into root directory and sub directories

In Windows, create a directory/folder called AndroidApps  in  'D' drive......D:\AndroidApps\.
This will be root directory of our Project. What is our Project? Say our project is to design as many apps as we can for Android-Mobiles.

<Project Directory>
D:\AndroidApps\>
  • appNo.1
    • src
      • main
        • java
          • com
            • pkg
              • App.java
        • res
          • drawable
            • ic_launcher.png
          • layout
            • main.xml
          • values
            • strings.xml
            • attrs.xml
        • assets
          • fonts
        • AndroidManifest.xml
      • androidTest
  • appNo.2
  • appNo.3

To create above directory structure in root directory, we issue a one-line command at MS Dos Shell Command Prompt :-

D:\>android create project --name AndroidApps  --path D:\AndroidApps --package srivastav.animesh  --activity AppNo.1 --target android-23 --gradle --gradle-version 1.5.0

What is gradle in above command?
gradle is a command-line tool which automates the task of compiling the java text code files *.java  into  *.class byte code and then to *.dex dalvik language of androids. Then packaging it with images, audio, etc resources into a *.apk file and install in to mobile.

Now what is gradle version 1.5.0?
This is a android plug .... so we have connected android with gradle by the inserting a plug of gradle version 1.5.0 into the switchboard of Android installed in our Windows Operating System.

But why the version is 1.5.0?
This plug version is compatible with java version 1.6 which is compiling android java codes.

Now, although we have created an android project directory structure, its not required to use gradle to build apk. We can use other tool called 'Ant' or we can manually do it ourselves by using installed android's commands.



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.

29 Jul 2017

Generate android app apk on shell or command prompt

Generation of Android app apk at command prompt of Windows will be as follows:

1. Install Java in form of jdk and jre in folders c:\jdk and c:\jre. Only install version 6 standard edition as it is small size compatible with Android.
Next install Android in folder.   c:\android.
Run Android package manager and put Android API level 23 platform tools for version 6 of Android lollipop in directory
c:\android\platform\android-23
.This will have a file Android.jar containing all class files as a library jar for making a Dex file run and execute as app on Android lollipop mobile.
Set path environment variable in  advance system settings of Windows. Include c:/jdk;c:/jre;c:/Android/tools etc in path variable.

2. Lets use shortcut mypro for my project. Mypkg means my package. Make directory folder  c:\mypro\com\mypkg.
Note that com.pkg is a package where dot or period separates com and pkg sub directories.
In case your package doesn't have this dot or period, then during compilation no error but when you install in mobile, it will say invalid apk.
Put myapp.java containing your code in this directory.
Put needed image icon files in  c:\mypro\res\drawable.
Put AndroidManifest.XML file in c:\mypro.

3. Now we generate R class for res directory. R.drawable class for subdir drawable and R.attr class for attributes to gather information from AndroidManifest.XML file and other layout XML style XML string xml files.
For this we use aapt command.

aapt p -S res -J ./com/pkg -I android.jar
Where
    aapt=Android app pkging tool
    S= Sources in res directory
    J= Java generated files
    I = Include dir or jar file
          For target Android mobile
          Say lollipop.
This  generates R.Java, having R.attr.class,etc classes in it.

4.  Now goto dir c:\mypro\com\mypkg. Give command
             javac    *.java
to get myapp.class
,R.class, R.attr.class, etc in same directory.

5. Now we make a Dex file from these classes.

dx -dex --output=classes.dex .

Notice the dot above represents current directory.
So all binaries including .class and .jar and .apk all will be compiled to Dex file.
Therefore remove all other jar and apk files. delete them prior executing above command. Also don't keep Android.jar here.

6.Now make apk out of Dex and resources.
First delete all class files as we have incorporated already in dex. Also delete any previous jar or apk. Goto directory
c:/mypro.
aapt p -I android.jar -S res -F myapp.apk  classes.dex

Where F=file to be created