Android Tutorials

Taking text input in Android via EditText

Objectives :

  • How to take input in Android?
  • How to take text input in Android?
  • How to use EditText in Android?
  • How to take multiline input in Android?

If you wish to take text input in Android, you can use <EditText> to take the text input in Android.

Here is the small example showing different uses of EditText. EditText can be also used to take multiline input as that of textarea.

[sourcecode language=”xml”]

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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="First Name" />

<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:id="@+id/EditTextFirstName"
android:hint="First Name" />

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Cell No" />

<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="Cell No." />

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Email ID" />

<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="Email Name" />

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

<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:lines="5" />

</LinearLayout>

[/sourcecode]

Multiline Input in Android

If you need, you can use the same <EditText> to take multiline input as per the code snippet given below :

[sourcecode language=”xml”]
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:lines="5" />
[/sourcecode]

Here, android:inputType=”textMultiLine” will convert the single line text input into multi line input and android:lines=”5″ will give 5 (five) visible lines input.

Working with Layouts in Android

Objectives :

  • Which layouts does Android have?
  • How to implement a layout in Android?
  • How to use different layouts in Android?
  • How to use Linear Layout in Android?
  • How to use Relative Layout in Android?
  • How to use Absolute Layout in Android?
  • How to use Frame Layout in Android?
  • How to use Table Layout in Android?
  • How to use nested layouts in Android?
  • How to use a layout inside another layout in Android?

In Android, there are main five (5) layouts which are as follows;

  1. Linear Layout
  2. Relative Layout
  3. Absolute Layout
  4. Table Layout
  5. Frame Layout

Here I have used the same form controls/components in form to show how it looks in different layouts except Frame Layout.

Linear Layout in Android

Here I have used nested linear layout.

XML Code for Linear Layout :

[sourcecode language=”xml”]

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

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

<EditText
android:width="100px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>

<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<TextView
android:text="Password"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<EditText
android:width="100px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>

</LinearLayout>

[/sourcecode]

Here android:orientation=”vertical” will render controls in vertical manner and android:orientation=”horizontal” will render controls in horizontal manner.

Relative Layout in Android

Here in Relative layout we don’t need to nest layouts generally.

XML Code for Relative Layout :

[sourcecode language=”xml”]

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<TextView
android:id="@+id/userName"
android:text="First Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<EditText
android:id="@+id/editUserName"
android:width="100px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/userName"
android:layout_below="@id/userName"/>

<EditText
android:id="@+id/editpassword"
android:width="100px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editUserName"
android:layout_alignLeft="@id/editUserName"/>

<TextView
android:id="@+id/password"
android:text="Password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/editpassword"
android:layout_below="@id/editUserName" />

</RelativeLayout>

[/sourcecode]

Here we need to specify android:layout_toLeftOf=”@id/controlId”, android:layout_toRightOf=”@id/controlId”, android:layout_below=”@id/controlId”, android:layout_alignLeft=”@id/controlId”, etc. parameters so as to render the control at particular place.

Absolute Layout in Android

Here in Absolute Layout we need to specify exact pixel location or (x,y) coordinate where we need to render our controls.

XML Code for Absolute Layout :

[sourcecode language=”xml”]
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<TextView
android:layout_x="10px"
android:layout_y="110px"
android:text="User Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<EditText
android:layout_x="150px"
android:layout_y="100px"
android:width="100px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:layout_x="10px"
android:layout_y="160px"
android:text="Password"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<EditText
android:layout_x="150px"
android:layout_y="150px"
android:width="100px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</AbsoluteLayout>

[/sourcecode]

Table Layout in Android

Whenever you need to manage data in tabular manner, you can use Table Layout. Table Layout will help you to show data in rows and columns.
Here is the sample code showing how to use Table Layout :

[sourcecode language=”xml”]
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<TableRow>

<TextView
android:text="User Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1" />

<EditText
android:width="100px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</TableRow>

<TableRow>

<TextView
android:text="Password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1" />

<EditText
android:width="100px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</TableRow>

</TableLayout>
[/sourcecode]

Here in Table Layout, we use <TableRow> tag to add contents to a row same as that of HTML.

Frame Layout in Android

Frame Layout can be used to display various contents such as text, images, videos, image galleries, etc.
Sample code for Frame Layout is as follows :

[sourcecode language=”xml”]<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<TextView
android:text="http://aatul.me"
android:textSize="24sp"
android:textColor="#000000"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"/>

</FrameLayout>
[/sourcecode]

Adding TextView in Android

Objectives :

  • How to set TextView in Android?
  • How to add TextView in Android?
  • How to add TextView in Android via Java program?
  • How to set Layout parameters from Java code in Android?

TextView is a one of the Views available in Android. There are two (2) ways to add TextView in your Android application as follows :

  1. Using XML code
  2. Using Java code

XML Code example – Adding to a TextView in a layout xml file

[sourcecode language=”xml”]
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World" />

</LinearLayout>
[/sourcecode]

 

Java Code example – Creating and adding a new TextView to a Layout

[sourcecode language=”java”]
// Creates object of TextView
TextView textView = new TextView(this);
// Set text to display in TextView on Screen
textView.setText(“Hello World”);
// Add TextView to Linear Layout with layout parameters
((LinearLayout)findViewById(R.id.mainLayout))
.addView(textView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
[/sourcecode]

Above Java code can be written in onCreate() of your Activity.

Here LayoutParams.FILL_PARENT will set layout height to FILL_PARENT, LayoutParams.WRAP_CONTENT will set layout width to WRAP_CONTENT.

Posted from WordPress for Android

Writing HelloWorld in Android

Objective :

  • How to write HelloWorld Application in Android?
  • How to write HelloWorld App in Android?
  • How to write HelloWorld in Android?

To create HelloWorldApp follow the steps below :

  • Go to File Menu > Go to New > Click on Android Application Project.

  • In New Android Application Window, enter Application Name as “HelloWorldApp”. Do not change anything else and click Next.

  • While you are in Configure Project Window, uncheck the Create custom launcher icon. Do not change anything else. Click Next.

  • In Create Activity window, click Next.

  • In Blank Activity Window, change the Activity Name to “HelloWorldApp”. Do not change anything else. Click Finish. And your HelloWorldApp Android application will be created.

Here is snapshot of HelloWorldApp project structure :

HelloWorldApp Project Structure in Android

XML Code for UI

File : activity_hello_world_app.xml

[sourcecode language=”xml”]

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

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

</RelativeLayout>
[/sourcecode]

XML code which manages all the string items or labels.

File : strings.xml

[sourcecode language=”xml”]
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">HelloWorldApp</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
</resources>

[/sourcecode]

Java Code

File : HelloWorldApp.java

[sourcecode language=”java”]
package com.example.helloworldapp;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class HelloWorldApp extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello_world_app);
}

}
[/sourcecode]

After writing your code, to run this snippet you need an AVD (Android Virtual Device) or Emulator. Here is tutorial link which will help you to create AVD so as to run your Android application.

How to create AVD (Android Virtual Device) or Emulator?

Objectives :

  • How to create AVD?
  • How to create Android Virtual Device?
  • How to create Emulator for Android?
  • What is AVD (Android Virtual Device)?

AVD (Android Virtual Device) / Emulator

AVD (Android Virtual Device) is an Emulator which helps in running Android applications onto your computer/laptop and you don’t need physical device every time you write/change code. Using an AVD you can run and test nearly every feature of your application right from simple text, GPS, Camera, Database, etc.

Creating AVD :

  • To create go to Window menu > Click on “Android Virtual Device Manager” or “AVD Manager” option.

  • In Android Virtual Device Manager or AVD Manager window, click New button.

  • In Create new Android Virtual Device (AVD) window, set credentials for new AVD you wish to create, press OK.

  • Select AVD you have created from the list and click Start > click Launch.

  • Here is an AVD you have created, now you are ready to run your Android application.

You can have a look at this tutorial so that you can Quickstart Android Application Development. So start writing your Android applications now.

Android Emulator Keyboard Shortcuts

Objectives:

  • How to use Emulator for Android?
  • What are the Android Emulator Shortcuts?
  • What are the Android Emulator Keyboards Shortcuts?
  • What are the Keyboard Shortcuts for Android Emulator?
  • How to use Android Emulator from Keyboard?

Keyboard shortcuts for Android Emulator :

Emulator Key / Function

Keyboard Shortcut

Home Home
Menu F2 or Page-up
Star Shift + F2 or Page-down
Back Esc
Call / Dial F3
Hang-up / End Call / Lock Screen F4
Search F5
Power F7
Volume-up Keypad ‘+’ or Ctrl + 5
Volume-down Keypad ‘-’ or Ctrl + 6
Camera Ctrl + Keypad 5 or Ctrl + F3
Previous Layout Orientation Keypad 7 or Ctrl + F11
Next Layout Orientation Keypad 9 or Ctrl + F12
Toggle Cell Networking ON/OFF F8
Toggle Code Profiling F9 (only with -trace startup option)
Toggle Full screen Mode Alt + Enter or Alt + Return
Toggle Trackball Mode F6
Enter Trackball Mode Temporarily Delete
DPad Left Keypad 4
DPad Right Keypad 6
DPad Up Keypad 8
DPad Down Keypad 2
DPad Center Keypad 5
Increase Onion Alpha Keypad * (Multiply or Star)
Decrease Onion Alpha Keypad / (Divide)

Enjoy coding.

Source : Android

DVM vs. JVM

Objectives :

  • What is DVM?
  • What is Dalvik Virtual Machine?
  • What is JVM?
  • What is Java Virtual Machine?
  • What are the differences between DVM & JVM?
  • What are the differences between Dalvik Virtual Machine & Java Virtual Machine?

 

DVM (Dalvik Virtual Machine)

JVM (Java Virtual Machine)

It is Register based which is designed to run on low memory.

It is Stack based.

DVM uses its own byte code and runs “.Dex” file. From Android 2.2 SDK Dalvik has got a Just in Time compiler

JVM uses java byte code and runs “.class” file having JIT (Just In Time).

DVM has been designed so that a device can run multiple instances of the VM efficiently. Applications are given their own instance.

Single instance of JVM is shared with multiple applications.

DVM supports Android operating system only.

JVM supports multiple operating systems.

For DVM very few Re-tools are available.

For JVM many Re-tools are available.

There is constant pool for every application.

It has constant pool for every class.

Here the executable is APK.

Here the executable is JAR.

 

Android Components

Objectives :

  • What are the components of Android?
  • Explain android components.

This is a small tutorial which explains about Android Components in brief.

[slideshare id=17537156&doc=androidcomponents-130323021024-phpapp02]

Architecture of Android

Objectives :

  • What is architecture of Android?
  • Explain android architecture.

This small tutorial is about architecture of Android which is essential to know if you are learning Android Application Development.

[slideshare id=17264723&doc=architectureofandroid-130316133212-phpapp02]

Android Workshop at RGCER (NYSS)

[gigya src=”http://player.videofy.me/player.swf?videoId=970900″ width=”480″ height=”360″ quality=”high” wmode=”transparent” allowFullScreen=”true”]

Conducted 3 days Android Workshop at Rajiv Gandhi College of Engineering & Research (formerly known as NYSS College), Nagpur from 25 Feb 2013 to 27 Feb 2013. The workshop was organized in association with the ADCC Academy, Nagpur under the Entrepreneurial Development Program (EDP).

The students who participated in this workshop had a great fun & learned a lot as per their feedback after the workshop, now the college have organized the Android Application Development Contest on 6th March 2013 and I am going to be the Judge for the event, lets see how students can develop Android Applications and what innovations they can come up with?