Pages

Saturday, 1 March 2014

Android: Using Buttons with Click Events

In this tutorial we will create two buttons and on pressing one of the button, TextView displays the text as which button is pressed.

Output will be as:



Xml file: res---layout
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:orientation="vertical" >  
  <Button   
    android:id="@+id/button1"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:text="Button 1"  
    />  
  <Button   
    android:id="@+id/button2"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:text="Button 2"  
    />  
  <TextView   
    android:id="@+id/textview1"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:text=""  
    />  
 </LinearLayout>  

First, Layouts are used to define how your views(i.e Buttons, TextViews, etc) will be positioned on the screen. For these, different layouts are used such as LinearLayout, RelativeLayout, GridLayout, etc.
Here we are using LinearLayout, in LinearLayout views can be either placed in vertical or horizontal format.
So android:orientation="vertical" under LinearLayout defines that the views will be placed vertically.
Next we have used Buttons and TextView, for any view id,width and height are the basic attributes.
Id of the view is further used in java file as a reference to the view defined in xml.
Width and Height defines the width and height of a view.Either you can put width and height in dp for e.g android:width="20dp" or you can use wrap_content,fill_parent or match_parent. fill_parent and match_parent does almost same thing that it will fit the screen and wrap_content means that it will be wrapped in minimum possible space.

Java file: src---your package name
 import android.os.Bundle;  
 import android.app.Activity;  
 import android.view.Menu;  
 import android.view.View;  
 import android.view.View.OnClickListener;  
 import android.widget.Button;  
 import android.widget.TextView;  
 public class MainActivity extends Activity implements OnClickListener {  
      Button b1,b2;  
      TextView tv1;  
      @Override  
      protected void onCreate(Bundle savedInstanceState) {  
           super.onCreate(savedInstanceState);  
           setContentView(R.layout.activity_main);  
           b1=(Button) findViewById(R.id.button1);//referencing button defined in xml file  
           b2=(Button) findViewById(R.id.button2);  
           tv1=(TextView) findViewById(R.id.textview1);//referencing textview  
           b1.setOnClickListener(this); //setting on click event on button  
           b2.setOnClickListener(this);  
      }  
      @Override  
      public boolean onCreateOptionsMenu(Menu menu) {  
           // Inflate the menu; this adds items to the action bar if it is present.  
           getMenuInflater().inflate(R.menu.main, menu);  
           return true;  
      }  
      @Override  
      public void onClick(View v) {  
           // TODO Auto-generated method stub  
           switch(v.getId()){//v.getId() for getting id of the pressed button  
           case R.id.button1:  
                //button1 pressed  
                tv1.setText("Button 1 pressed");//set text of textview  
                break;  
           case R.id.button2:  
                tv1.setText("Button 2 pressed");  
                break;  
           }  
      }  
 }  
A java file placed under src---your package name must extend an Activity.
Here as we have to implement click event so we will have to implement OnClickListener and then in onClick method we can add the functionality that will be performed on a click event.

Manifest file: (AndroidManifest.xml)
 <?xml version="1.0" encoding="utf-8"?>  
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
   package="com.example.example"  
   android:versionCode="1"  
   android:versionName="1.0" >  
   <uses-sdk  
     android:minSdkVersion="8"  
     android:targetSdkVersion="17" />  
   <application  
     android:allowBackup="true"  
     android:icon="@drawable/ic_launcher"  
     android:label="@string/app_name"  
     android:theme="@style/AppTheme" >  
     <activity  
       android:name="com.example.example.MainActivity"  
       android:label="@string/app_name" >  
       <intent-filter>  
         <action android:name="android.intent.action.MAIN" />  
         <category android:name="android.intent.category.LAUNCHER" />  
       </intent-filter>  
     </activity>  
   </application>  
 </manifest>  

Whenever a new Activity is created it should be declared in a manifest file.
An Activity should include a name and intent filters,intent filters are basically used to define the role of activity as in our case when our application is launched so the activity that needs to be displayed first should be mentioned as a launcher activity for e.g android:name="android.intent.category.LAUNCHER" under the category.Also the action names can be used to navigate between different activities which we will see in our further tutorials.

So that's it we have created an application having buttons with click events. If you have any doubts feel free to ask me in the comments below.

Running Android Application

In this tutorial we will see how to run an android application.

So for that we have two methods:
  1.   Either we can run on Android Virtual Device(AVD).
  2. Or we can run our application on an actual android device.
Running on Android Virtual Device(AVD)

Before running an android application on a virtual device we have to create one.

Steps for creating Android Virtual Device:
  1. Go to Windows->Android Virtual Device Manager.
  2. Click on New
  3. Add details for Android Virtual Device.
  4. Click OK

Now after creating a virtual device just start it from Android Virtual Device Manager and when you will run your application, eclipse will automatically install the application on your virtual device.

Running on Actual Android Device.
  1. Go to Settings in your phone.
  2. Locate Development and tick USB Debugging.
  3. Connect your phone through USB.
  4. Click Run from Eclipse.
  5. Select appropriate device from Android Device Chooser and click OK.
  
Eclipse will then automatically install the application in your device.

Friday, 28 February 2014

Android Creating Project

In this tutorial we will see how to create a New Android Project.

Note: Steps are for creating project using Eclipse.

Steps:

  1.   Go to File---New---Android Application Project
  2. Enter Application Name and other details. 


 3. Configure Launcher icon, press next to use default.
 
4.  Enter your Activity name (name of java file) and your layout name (name of xml file) 
5. Click on finish

Android Development Basics

Android application development is mainly dependent on xml and java. Where xml is used to create application’s UI and java is used to handle events and other functionalities of applications.

XML file:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:paddingBottom="@dimen/activity_vertical_margin"  
   android:paddingLeft="@dimen/activity_horizontal_margin"  
   android:paddingRight="@dimen/activity_horizontal_margin"  
   android:paddingTop="@dimen/activity_vertical_margin"  
   tools:context=".MainActivity" >  
   <TextView  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="Hello World " />  
 </RelativeLayout>  

This is the layout of basic xml file for printing Hello World.

So to begin with android application development you require basic knowledge of xml but even if you don’t have any knowledge of xml don’t worry in our further tutorials I will try to explain every possible terms used in android xml file.

Xml files reside under res---layout

Java file:
 import android.os.Bundle;  
 import android.app.Activity;  
 import android.view.Menu;  
 public class MainActivity extends Activity {  
      @Override  
      protected void onCreate(Bundle savedInstanceState) {  
           super.onCreate(savedInstanceState);  
           setContentView(R.layout.activity_main);  
      }  
 }  
This is the basic structure of your applications java file. So it is must to have basic knowledge of java to begin with android development.

Java files reside under src--- your package name

One more important part of your application is Android Manifest file
A manifest file includes some basic information needed by system before running any application.

Now before moving on to real application development you people need to download eclipse and setup the environment.

A good tutorial on setting up environment can be found here