Friday, September 26, 2014

HelloAndroid: app to learn the basics of Android

Android is so easy to learn and you enjoy when learning it. Lets create a simple app, which includes the following:

It contains two pages
a)First page with Title, Username label, text to enter user name, password label, text to enter password, submit button.
b) On clicking submit button, it should take me to second page which shows the name entered by user in first page, saying "Hello xxx", a back button to go back to first activity.


Activity is a page which is associated with one .java file and one .xml file.

.xml file contains the components which are visible to user(we can add components dynamically from .java file as well)
.java file contains the life cycle methods, component listeners, etc.

So lets create our first page:

1. Creating First Activity: LoginActivity
By default, when an android app is created, eclipse creates one activity xml file: activity_main.xml and one java file: MainActivity.java. Delete both these files.

a) Creating layout file: login_layout.xml
Right click on res/layout folder and select New > Android XML File
Give the name of the file as "login_layout"
click on "Finish"

Now go to text representation of the file, instead of Graphical layout.

Add the below content, inside LinearLayout tag: (all the corresponding strings must be defined in res/values/strings.xml file)


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" android:gravity="center">

    <TextView
        android:id="@+id/txtLoginTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="@string/loginTitle" >
    </TextView>
    
    <LinearLayout
        android:id="@+id/ll1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/txtUserName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_weight="0.4"
            android:text="@string/userNameLabel" >

        </TextView>

        <EditText
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="text" android:layout_weight="0.6">
        </EditText>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/txtPassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="@string/passWordLabel" >
        </TextView>
    </LinearLayout>

    <Button
        android:id="@+id/btnSubmit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="@string/submitButton"/>

</LinearLayout>


b) creating LoginActivity.java file:

package com.example.activitynavigationapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.example.activitynavigationapp.R;

public class LoginActivity extends Activity implements OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
this.setContentView(R.layout.login_layout);

super.onCreate(savedInstanceState);

Button submitBtn = (Button) findViewById(R.id.btnSubmit);

submitBtn.setOnClickListener(this);
}

@Override
public void onClick(View v) {
Intent intent = new Intent(this, UserActivity.class);

this.startActivity(intent);

finish();
}

@Override
public void onDestroy() {
System.out.println("onDestroy() is called...");

super.onDestroy();
}
}

2. Create second activity: UserActivity

a)user-layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" android:gravity="center">

    <TextView
        android:id="@+id/txtWelcome"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="@string/welcomeText" >
    </TextView>

    <Button
        android:id="@+id/btnGoBack"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="@string/goBack" />

</LinearLayout>

b) UserActivity.java

package com.example.activitynavigationapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class UserActivity extends Activity implements OnClickListener {

@Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.user_layout);

super.onCreate(savedInstanceState);

Button goBackButton = (Button) findViewById(R.id.btnGoBack);

goBackButton.setOnClickListener(this);
}

@Override
public void onClick(View view) {
Intent intent = new Intent(this, LoginActivity.class);
this.startActivity(intent);
}
}

3) AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.activitynavigationapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.activitynavigationapp.LoginActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.activitynavigationapp.UserActivity"
            android:label="@string/app_name" />
    </application>

</manifest>

4) strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">ActivityNavigationApp</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="loginTitle">Please Login Here</string>
    <string name="userNameLabel">Username</string>
    <string name="passWordLabel">Password</string>
    <string name="submitButton">Submit</string>
    <string name="welcomeText">Welcome Guest</string>
    <string name="goBack">Go Back</string>

</resources>

Output:




Wednesday, September 24, 2014

Creating HelloAndroid application in Android with Eclipse

To follow this post, you must have Android Development Eclipse environment in your system. If it is not done yet, the follow this post: AndroidConfiguration

Steps to Create HelloWorld Application:
1. File > New > Android Application Project


2. Give the values like in below screenshot:

Here "Application Name": This is going to be the application name in the android device
"Project Name": This is the project in the eclipse workspace
"Package Name": package for java classes

Minimum Required SDK: From which device, you want to support your app
"Target SDK": To which device you are trying to create the app
"Compile With": All files are compiled against the selected version Android API

3. Click Next. Leave the default options and Click on Next

4. This describes the app logo images for different kind of devices. Leave out the defaults and click on Next

5. Create Activity:
Activity is a page shown on the device. Leave defaults and click "Next"

6. Blank Activity:
Give a name to the activity.
Layout contains the elements to be placed in the activity(page)

Leave out the defaults and click on "Finish"

Thats it...Now if you right click on the created project, Run As > Android Application, it will open the emulator and installs your created application and launches it. (Launching an emulator takes time, depending your system configuration. So please be patient till it gets opened)



Issues and fixes to the command problems faced during the Android Apps Development

Issue 1: “no system images installed for this target” while creating a new AVD

Solution: system-images/ folder might not be present in extracted “sdk” directory.

  • In eclipse, open “Window” and then select “Android SDK Manager”
  • This shows the updates available for Android SDK.
  • Here select “ARM EABI v7a System Image” and click on “Install Paackages”

After this, restart the eclipse, which should solve the issue.

Issuse 2: Avoid message "Can’t connect to the Internet. Swipe for some tips." when the AVD is opened


Solution: There are few issues with API level 20 and this is one of them. Install API Level 19 and use it, instead of API Level 20. That should solve the problem.

Issue 3: When "EditText" component is used, getting following message and not able to build the xml properly.
raised during rendering: java.lang.System.arraycopy([CI[CII)V Exception details are logged in Window > Show View > Error Log

Solution: This occurs due to the installation of "Android 4.4W version". If we remove this, then the above error message should go away.
To uninstall this package, Open Windows > Android SDK Manager > Select the installed packages under "Android 4.4W" and delete those packages.
Restart eclispe.

Configuring Android development Environment

In Windows:

Please follow the below steps:

1. Android is java based programming language. So to develop android applications, jdk must be installed in your system.

To see if jdk is installed or not in your system, open command prompt and type "java".
If you get below output, then jdk is not installed.
'java' is not recognized as an internal or external command,
operable program or batch file.

If jdk is not installed in your system, then go to "http://www.oracle.com/technetwork/java/javaee/downloads/java-ee-sdk-6u3-jdk-6u29-downloads-523388.html", download respective jdk and install it by doubling clicking on downloaded .exe file.

2. Go to android developer link: http://developer.android.com/sdk/installing/index.html
Download the "Eclipse ADT" bundle.
This bundle includes,
  • Eclipse
  • Android SDK(Software Development Kit)
  • ADT(Android Developer Tools)

3. Extract the downloaded ADT bundle and keep it in D: drive (you can extract it any drive)

4. When you extract, you get two subfolders: eclipse and sdk.
We need to set paths for "ddms" and "adb" applications, which will be available under ".../sdk/tools" and ".../sdk/platform-tools" respectively.

So add these two paths to "PATH" environment variable, with below in command prompt (change the paths accordingly, depending on the extracted path here)

set PATH=%PATH%;D:\softwares\adt-bundle-windows-x86_64-20140702\sdk\platform-tools;D:\softwares\adt-bundle-windows-x86_64-20140702\sdk\tools;

Thats it...You are done with the configuration of android development environment.

Now in the extracted folder, open "eclipse" folder and click on "eclipse" application, which opens the eclipse IDE, with predefined Android plugins.