Wednesday, January 16, 2013

Create Text file in Android


Hi, To day I'm going to write "how to create a file (text file) in Android".

In Eclipse File ---> New ---> Android Project



Type the ProjectName as “CreateTextFile”  and click “Next”



Choose the SDK version for this project. I choose Android 4.0.3

Click “Next"



Type the package Name as “niro.CreateTextFile” and click “Finish”

In the Package Explorer our project will display.


Expand our project “CreateTextFile” and open the "main.xml" file which is in res/layout folder to design our UI.

In this application I add two TextView, two EditText and a button.

Type the following code in "main.xml"

main.xml

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="File Name" />

    <EditText
        android:id="@+id/fileName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </EditText>

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Type your Message here" />

    <EditText
        android:id="@+id/msgBody"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </EditText>

    <Button
        android:id="@+id/saveButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Save" />

</LinearLayout>




We need to set permission to write a file in SD card.

To set permission, Open  “AndroidManifest.xml” file  and add the following code

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Then open the “CreateTextFileActivity.java” file to write the code.

CreateTextFileActivity.java

package niro.CreateTextFile;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class CreateTextFileActivity extends Activity {
       final int ACTIVITY_CHOOSE_FILE = 1;
       String msg;
       String title;

       /** Called when the activity is first created. */
       @Override
       public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.main);
              Button btn = (Button) this.findViewById(R.id.saveButton);
              final EditText etTitle = (EditText)this.findViewById(R.id.fileName);
              final EditText etBody = (EditText) this.findViewById(R.id.msgBody);

              btn.setOnClickListener(new OnClickListener() {
                     public void onClick(View v) {
                           title = etTitle.getText().toString() + ".txt";
                           msg = etBody.getText().toString();
                           createFileOnSD(title, msg);
                     }
              });
       }

       public void createFileOnSD(String fileName, String body) {
              try {
                   File root = new File(Environment.getExternalStorageDirectory(),
                                  "MyFolder");
                     if (!root.exists()) {
                           root.mkdirs();
                     }
                     File gpxfile = new File(root, fileName);
                     FileWriter writer = new FileWriter(gpxfile);
                     writer.append(body);
                     writer.flush();
                     writer.close();
                     Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show();
              } catch (IOException e) {
                     e.printStackTrace();
                     // importError = e.getMessage();
                     // iError();
              }
       }


After save this program, we need to run this program. I test this application in my phone instead of emulator.


Then type the File Name as “test” and Type the message as “This is the body” and click Save button


Now our application has created a folder, name "MyFolder", and write a text file, name "test.txt" in that folder.


























Open the "test.txt" file



When we go back to home screen, our application will display on the screen.



Monday, January 7, 2013

My First Android Application (Hello World)


In the previous post I had written about “install android development environment on windows 7”. Today I’m going to create my first android application.

First launch the eclipse by double clicking on the eclipse.exe file located in the eclipse folder.

Then we have to choose the Workspace,this is the folder where we store our projects, I choose D:\Example as my Workspace.


Then click “OK” to launch eclipse.


To create new android project  
 
File ---> New ---> Android Project


Type the Project Name as “Hello World” and click Next.

In the next window some platform versions will appear, which were installed early. We need to choose a platform version from list to this project. I choose 2.3.3


Click “Next” to go next window.

In this window we need to give a package name. We need to have at least a period (.) in the package name. I type the package name as “niroshan.HelloWorld”.


Click “Finish” to close this window.

Now the Eclipse IDE is look like this.


In the Package Explorer (located on the left of the Eclipse IDE), Expand the Hello World project.

Double click “main.xml” file which is in the res/layout folder. This defines the User Interface of our project.

The default view is the layout view. To modify the UI, click the main.xml tab located at the bottom.


From here I’m going to start code writing. In this first application I’m going to add a Text view and a Button.  So I add the following code in bold to the  main.xml file.

<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"/>

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello Niroshan, This is my first Android Application"/>

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is a clickable Button"/>

</LinearLayout>


Press ctrl+s to save the changes.

Now it is the time to test our application. There are many ways to run android application in Eclipse IDE.

1. Select the project name in Eclipse and press F11

2. Select the project name in Eclipse ---> click “Run” in menu bar ---> Run As
     ---> Android Application

3. Right click on the project name in Eclipse ---> Run As ---> 
    Android Application

When I tried to run my project, an error message was appeared. 


That is, there is no compatible target. Because, This application’s (Hello World) target platform is 2.3.3, but we have not create Virtual Device for this version yet. We have only created Virtual Device for 2.1 in the previous post. So we need to create an Android Virtual Device for 2.3.3 version.

Click “Yes” to create Virtual Device.




Click “New” and fill as follows.



Click “Create AVD”


Now we have created another Virtual Device name Emulator_2.3.3

Then select Emulator_2.3.3 , click “Start” and click “Launch” to launch the Virtual Device.



Our application will run on the Emulator.


When we click the Home button, Home Screen will appear.



When we click the application launcher icon, it will display the installed applications on the device.  “Hello World” application also in here.


This is my first Android Application.





Sunday, January 6, 2013

Install Android development environment on windows 7


What is Android

Android is a mobile operating system. It is developed by Google. Android is free and open; hence, most of the Android code are released under the open-source Apache License, which means that anyone who wants to use Android can do so by downloading the full Android source code. Nowadays many touch screen mobile devices such as smart phones and tablet computers are using Android operating system. 


Android has many versions and Each Android version has a code name.

Android Version
Code Name
1.1

1.5
Cupcake
1.6
Donut
2.0/2.1
Eclair
2.2
Froyo           
2.3
Gingerbread
3.0
Honeycomb
4.0
Ice Cream Sandwich
4.1/4.2
Jelly Bean


Install Android development environment on windows 7

For Android development we can use a Mac, windows PC or Linux machine. Here, I'm using windows 7. Here are the steps to installing Android development environment on windows 7.

1. Install Java SDK.
         We can download Java SDK from the following link

2. Download and unzip Eclipse.
         We can download Eclipse from here
         I choose 32-bit version of Eclipse for Windows.
         Once the Eclipse IDE is downloaded, unzip its content into a folder, say D:\Project.

3. Download and unzip Android SDK. 
         We can download Android SDK from here
         Once the Android SDK is downloaded, unzip its content into a folder, say D:\Project\SDK

4. Run “SDK Manager” from Android SDK to download the platform versions.
         Double click “SDK Manager” from SDK folder.
         We have to choose some packages to install and check the Accept radio button.


When we click “Install” button, the packages will be downloaded from internet and will be installed.



Once we have downloaded the various platform tools, we need to create an Android Virtual Device. 

Double click “AVD Manager” from SDK folder.






  Click “New” button to create a virtual device. We can create more than one device.
  Fill the text box as follows and finally click “Create AVD” button.



To start the virtual device we have to select one device and click “Start” button.





Then the virtual device will appear on the monitor. This will take some time.






5. Install Android Development Tools (ADT) plug-in for Eclipse and point to the Android SDK.

To install the ADT, first launch Eclipse by double clicking on the eclipse.exe file located in the eclipse folder.

When eclipse is started we have to choose a workspace, this is the folder where we store our projects. We can change this workspace later. I choose D:\AndroidProject. 






Once Eclipse is up and running, do the following.


1.      Help -----> Install New Software…

Install window will appear.





2.      Click “Add”  then type


Name:             Android
Location:         https://dl-ssl.google.com/android/eclipse/

Click “Ok”





3.      Tic the check box “Developer Tools” on the install window and click “Next”.



4.    Installation details will appear on the install window. Then click “Next”.


5.   Tic the radio button to accept the license agreements and click “Finish” to continue.

Now Eclipse will download tolls from internet and install them. This will take some time.

6.      Once ADT is installed, we will be prompted to restart Eclipse.

Now we have to point this ADT plug-in to Android SDK. To do this,

            Window ------> Preferences and select Android tab on the left panel.





Then click Browse and point the folder where the SDK was installed, that is where “SDK Manager.exe” is located, and click “Apply”.

If the SDK is found, the target details (platforms we chose to download previously) will automatically be populated into the list.  




Click “OK” to close window.

            Now we are ready to build Apps for Android.