Thursday, June 2, 2011

Beginning Android Tutorial #1 (Part 2 of 3) - Addition of an EditText field

 
This is a continuation from Part 1 of the program in which I will be adding an EditText widget so that we can now input some text. Using the entered text will take place in part 3.

     I am going to jump right into the main.xml file and add the actual EditText widget with some basic attributes. I am going to assign an id as well as the basic width and height settings. Below is what the main.xml looks like after I have made those additions.

main.xml

<?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:id="@+id/display_text"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"/>
    
<EditText
    android:id="@+id/entered_text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inputType="textMultiLine|textAutoCorrect"/>
</LinearLayout>

     The portion android:inputType allows the EditText widget to start a new line when the text has reached the end of the screen.  The auto correct portion will do the usual reformatting of i to I, dont to don't and so on.  Both of these options allow for a more user friendly experience although these attributes can be removed and changed as needed based on the type of text that will be going in the EditText widget.

Get the source code here:  BasicHelloWorld2.zip

      That is the short and sweet of Part 2 of 3.  The next portion of this program will have a substantial bit more of "meat" to it.  That way we can use any text that has been entered into the EditText widget.  As usual...more to come

No comments: