Pages Navigation Menu

Coding is much easier than you think

Creating an Editable Text box Android Application using Eclipse:

Creating an Editable Text box Android Application using Eclipse:

In this tutorial, you will be Learning, how to display text box (editable) in an activity?

Here you go!!!!

 

Before creating an android application using Eclipse, let us see some of the key terms involved while developing this application.

Key terms involved in this project:

Activity: An activity provides a user interface for a single screen in your application. The window typically fills the screen, but may be smaller than the screen and float on top of other windows. An application usually consists of multiple activities that are loosely bound to each other.
 
** UPDATE: Android Complete tutorial now available here.
 
Intent: Android uses a special class called Intent to move from screen to screen. Intent describe what an application wants done. The two most important parts of the intent data structure are the action and the data to act upon. Typical values for action are MAIN (the front door of the application), VIEW, PICK, EDIT, etc. The data is expressed as a Uniform Resource Indicator (URI). For example, to view a website in the browser, you would create Intent with the VIEW action and the data set to a Website-URI.

View: It’s a data structure whose properties store the layout and content for a specific rectangular area of the screen. A View object handles measuring, its layout, drawing, focus changes, scrolling, and key/gestures for the screen area it represents. The View class serves as a base class for all widgets. The list of widgets available includes i.e. TextView, EditText, Button, RadioButton, Checkbox, ScrollView, etc.,

Textfield: A text field allows the user to type text into your app. It can be either single line or multi-line. Touching a text field places the cursor and automatically displays the keyboard. In addition to typing, text fields allow for a variety of other activities, such as text selection (cut, copy, paste) and data look-up via auto-completion. You can add a text field to you layout with the EditText object. You should usually do so in your XML layout with a <EditText> element.

Text fields can have different input types, such as number, date, password, or email address. The type determines what kind of characters are allowed inside the field, and may prompt the virtual keyboard to optimize its layout for frequently used characters.

The tutorial is about how to display textbox that allows users to edit text in the UI of the android application.

This project is developed in Eclipse 4.2 (Juno) and tested with Android 2.2

If you are new to new to android application project, to create a new project in Eclipse refer Creation of Android Project.

Coding:

Now let’s go to the coding section. This project requires following files.

Source Code:

  1. Textbox.java Activity (main screen)

Activity Files:

  1. activity_textbox.xml – main screen/ layout

res – Values:

  1. strings.xml -strings available in layout/activity

Manifest file:

  1. AndroidManifest.xml

Here is the coding for the above files.

Textbox.java:

package com.example.textbox; // textbox package
package com.example.textbox; // textbox package
import android.os.Bundle; // A mapping from String values to various Parcelable types.
import android.app.Activity; // Required to create an activity.
import android.view.Menu; // Interface for managing the items in a menu.
public class Textbox extends Activity { // all classes extends activity
	@Override
	protected void onCreate(Bundle savedInstanceState) { // Create an activity/ screen
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_textbox); // display activity textbox  when app starts (Main)
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_textbox, menu);
		return true;
	}
}

Activity_textbox.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/info"
>
<!-- Displays text Enter text on the screen -->

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

<!--Text Editor-->

<EditText
android:id="@+id/entertext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

Menu : activity_textbox.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/info"
>
<!-- Displays text Enter text on the screen -->

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

<!--Text Editor-->

<EditText
android:id="@+id/entertext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

style=”text-decoration: underline;”>AndroidManifest.xml:

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.textbox.Textbox"
            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>

strings.xml:

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

<! -- Application Name -->

    <string name="app_name">Textbox</string>

<! -- Text message -->

    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>

</resources>

Styles. xml:

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

<! -- Application Name -->

    <string name="app_name">Textbox</string>

<! -- Text message -->

    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>

</resources>

Run the android application:

Android applications can be run on the android devices. You can either connect a hardware to the system to test the application or you can use the android virtual devices (AVD) Manager to create/manage the virtual devices running on emulator to run the application.

If you are new to create a Android Virtual Device (AVD), refer Creating Android Virtual Device.

To run the application/ project, Build your project using Project -> Build Project.

Build project

This will show errors in Console window at the bottom of the working area in case your project contains.

If you are new to create a Android Virtual Device (AVD), refer Creating Android Virtual Device.

If your build is successful, Run your application using Run -> Run or Press Ctrl +F11.

Run Project

Upon running the application, the emulator will be launched with the selected/ available AVD created on your screen.

Start Emulator

To test your application, unlock the screen and double click on your app.

You can see your app with the image set during the android project creation in AVD.

AVD Home screen_3

Now the activity created for textbox is shown as in the figure below.

Text Box

Click on the text box area to see the keypad on the screen.

TextBox_Click

Now type any text using the keypad available on the screen.

Textbox_type

Thus the android application project is executed successfully.

 


Click Here To Share This..!!
Share

Advertisements
%d bloggers like this: