Saturday, 17 March 2012

Android Simple Program to add two numbers

A program to demonstrate usage of EditText, Textview and Button widgets in android this program is very useful for beginners and give the basic overview on how to use the Api's, using Droiddraw i was able to design the User Interface with two textbox(EditText) and Label(TextView) and button as shown in below Pic.




The xml code that is to be saved in main.xml file is

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget35"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<EditText
android:id="@+id/txtbox1"
android:layout_width="145px"
android:layout_height="40px"
android:text="EditText"
android:textSize="18sp"
android:layout_x="75px"
android:layout_y="54px"
>
</EditText>
<EditText
android:id="@+id/txtbox2"
android:layout_width="144px"
android:layout_height="40px"
android:text="EditText"
android:textSize="18sp"
android:layout_x="75px"
android:layout_y="109px"
>
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="106px"
android:layout_height="36px"
android:text="Button"
android:layout_x="88px"
android:layout_y="224px"
>
</Button>
<TextView
android:id="@+id/lbl1"
android:layout_width="141px"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_x="76px"
android:layout_y="166px"
>
</TextView>
</AbsoluteLayout>


and below is the Java Source code which contains the logic to add two numbers and the refrence to id of the controls can be found in above XML



import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class fit extends Activity {
Button button1;
EditText txtbox1,txtbox2;
TextView tv;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtbox1= (EditText) findViewById(R.id.txtbox1);
button1 = (Button) findViewById(R.id.button1);
tv = (TextView) findViewById(R.id.lbl1);
txtbox2= (EditText) findViewById(R.id.txtbox2);
button1.setOnClickListener(new clicker());
}
class clicker implements Button.OnClickListener
{
public void onClick(View v)
{
String a,b;
Integer vis;
a = txtbox1.getText().toString();
b = txtbox2.getText().toString();
vis = Integer.parseInt(a)+Integer.parseInt(b);
tv.setText(vis.toString());
}
}
}





in above code we have OnClickListener which is similar to ActionListener in java swing values are taken from the EditText(textbox) and added and the result is showed in TextView(label).

Saturday, 10 March 2012

good guide website for android developers

hey, it is very important to have a good website which guides us at each step. so did i, i found out the best website to resolve all my questions about android.


http://stackoverflow.com/

is the best website for all your questions about android.


this website is the most viewed website for any android beginner. its better you take the advantage of these websites. happy coding.

Thursday, 8 March 2012

Android Tutorial: Creating and Using an SD Card in the Emulator

The first issue I ran into was trying to load files into the Android emulator. For instance, if you want to load MP3 files on a “real” Android phone, you’ll probably insert an SD card into the phone. The emulator can emulate this, but you need to jump through a few hoops:

1.
    First, you would have created an sd card image file during the creation of avd (android virtual device).

2.
    If you want to access the image, the emulator must be running with the image loaded. I’m going to assume you’re using the Eclipse tools. In there you need to specify the image on the command line. Open the “Run configurations…” dialogue and configure the image as follows (don’t forget to put in the correct directory)


3.
     Next start the emulator and verify that the SD card is loaded. The best way to do this is in the DDMS tool. To access it: click on the open perspective button:

If the DDMS isn’t in the list, click “Other…” and choose it from there. Open the file explorer and you should be able to open the sdcard:



4.
  now you can select any file that you want to move for eg, i want to move an mp3 file to the sd card. so select the mp3 file that you want to move just drag and drop the file on the "sdcard" directory in the file explorer. the file will be pushed into the sd card in the emulator.

 if you cannot follow the above point then do as follows
Add files to the sdcard: There are two options: either you can use the two icons in the top right corner of the DDMS tools () or you can use the command line tools. The following command pushes the file to your sdcard:
adb push myfile.mp3 /sdcard/


5.
And that’s it. the file should appear in the DDMS file explorer. Next time, we’ll try to actually do something with the file we places on the Android emulator.

important point is that when you push or add any files to the sd card in the emulator, its better to restart the emulator. this ensures that files have been moved and the emulator will configure the sd card and scan it for your files.



Hello World - Your First Android Application "Create a New Android Project"

As you are done with the initial setup and the installation of the softwares to build your own android apps. now, we'll start building the basic and the first app.




Create a New Android Project

After you've created an AVD you can move to the next step and start a new Android project in Eclipse.
  1. In Eclipse, select File > New > Project.... If the ADT Plugin for Eclipse has been successfully installed, the resulting dialog should have a folder labeled "Android" which should contain "Android Project". (After you create one or more Android projects, an entry for "Android XML File" will also be available.)
  2. Select "Android Project" and click Next.
  3. Fill in the project details with the following values:
    • Project name: HelloAndroid
    • Build Target: Select a platform version that is equal to or lower than the target you chose for your AVD.
    • Application name: Hello, Android
    • Package name: com.example.helloandroid (or your own private namespace)
    • Create Activity: HelloAndroid
    Click Finish.
    Here is a description of each field: 

    Project Name
    This is the Eclipse project name — the name of the directory that contains the project files.
     
    Application Name
    This is the human-readable title for your application — the name that appears on the Android device.
     
    Package Name
    This is the package namespace (following the same rules as for packages in the Java programming language) that you want all your source code to reside under. This also sets the package name under which the stub Activity is generated. Your package name must be unique across all packages installed on the Android system; for this reason, it's important to use a standard domain-style package for your applications. The example above uses the "com.example" namespace, which is a namespace reserved for example documentation — when you develop your own applications, you should use a namespace that's appropriate to your organization or entity.
    Create Activity
    This is the name for the class stub that is generated by the plugin. This is a subclass of Android's Activity class. An Activity is simply a class that can run and do work. It can create a UI if it chooses, but it doesn't need to. As the checkbox suggests, this is optional, but an Activity is almost always used as the basis for an application.
     
    Other fields: The checkbox for "Use default location" allows you to change the location on disk where the project's files are generated and stored.
Your Android project is now ready. It should be visible in the Package Explorer on the left. Open the HelloAndroid.java file, located inside HelloAndroid > src > com.example.helloandroid). It should look this : i.e copy and past the below code in the HelloAndroid.java file.



package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloAndroid extends Activity {
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       TextView tv = new TextView(this);
       tv.setText("Hello, Android");
       setContentView(tv);
   }
}

now change the main.xml file to the below code as shown.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout> 
 
 now you are good to run your first android application.
 the configuration to run your android application is given below.

The Eclipse plugin makes it easy to run your applications:
  1. Select Run > Run.
  2. Select "Android Application".
The Eclipse plugin automatically creates a new run configuration for your project and then launches the Android Emulator. Depending on your environment, the Android emulator might take several minutes to boot fully, so please be patient. When the emulator is booted, the Eclipse plugin installs your application and launches the default Activity. You should now see something like this:


 so, i guess you executed your first android application.

any further problems, pls write down your problems in the comments.
keep following for more android applications and solutions.

Android setup installation guide

The screen shots to install the required softwares to begin with the android app development is given below ( for windows OS).

Here, I have put up the screen shots for other version of eclipse but you go for the eclipse (indigo 3.7.1).



after installing the jvm, extract eclipse to your c drive and copy android sdk - windows file to the c drive. follow the screen shots.



click on the eclipse icon and choose the workspace






























Friday, 24 February 2012

Android for beginners and the initial setup

Welcome guys, here i am going to guide you how exactly you can start building android apps and clear all your basic ideas.

first of all we know that we do require the right software that are required for development of the android apps.
the software and the beginning setup that you should follow to start with developing apps. all the best guys.


yeah, these are the list of software's that you need.

1. Eclipse ( i prefer 'eclipse indigo 3.7.1 for java developers' cause its the latest version).

2. jdk which is suitable for your OS. you can find it here ("http://www.oracle.com/technetwork/java/javase/downloads/index.html").

3.  ADT (android development tools) that is basically like eclipse plugin for executing android applications. i dont know the formal definition but this certainly an important thing for developing apps. you can find it ("http://developer.android.com/sdk/eclipse-adt.html"). download the latest version for better performance.

4.  The important thing is that androis sdk. that you require to connect it inorder to see your output of the program which helps you to setup virtual devices on your computer screen. basically called as emulator. you can find it here. for windows its better to download the zip folder and extract it.

so, now you are good to develop apps.

for further much more help. The screenshots will be uploaded, where you can
get the idea to install all these software's.