I sharing this example and hoping someone will benefit from it ;P
Pre-Requirement
- 1. Know the different between android layout
http://code.google.com/android/devel/ui/layout.html
2. Understand android GUI and how to implement it
http://www.droiddraw.org/widgetguide.html
The Idea
Based on the 2 forms … The first form will allow the user to enter value and pass it to the other form. The second form will only display the value that come from the first form

Step 1: Analysis the Prototype

Step 2: Implementing the GUI
- 1. Create 2 xml files in layout folder inside the res folder
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"
- android:background="#FFFEE9" android:padding="10px">
- <TextView id="@+id/nameLab"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Type your name"/>
- <EditText id="@+id/yourName"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_below="@id/nameLab"/>
- <Button id="@+id/okButt"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:layout_marginLeft="10px"
- android:text="OK"
- android:layout_below="@id/yourName"/>
- </RelativeLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <TextView id="@+id/myMsg"
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Welcome"
- android:textStyle="bold"/>
-- Each xml file will represent the GUI for one form
-- Avoid using capital letter in naming the xml file, cause it affect the R.java which may cause you a problem in future.
2. Use the following code as sample to start implementing the GUI
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent“
……complete the code……….
</RelativeLayout>
3. To test your GUI
Go to your java file under the src folder, and edit onCreate method by adding the following line on it:
--- super.onCreate(savedValues);
---setContentView(R.layout.nameOfYourXmlFile);
The implementation of the first form
Using xml Syntax Highlighting
Parsed in 0.003 seconds, using GeSHi 1.0.8.4

Screen shot for the emulator after implementing the first form
The implementation of the second form
Using xml Syntax Highlighting
Parsed in 0.001 seconds, using GeSHi 1.0.8.4
Notice: you can’t test the second one cause it dun have container [Layout]
Step 3: Make the link between the GUI and the process
All logical process should be done in java file
- 1. Open your java file
- class tutorial1 extends Activity {
- @Override
- protected void onCreate(Bundle savedValues) {
- super.onCreate(savedValues);
- setContentView(R.layout.mainwindow);
- Button okButt = (Button) findViewById(R.id.okButt);
- okButt.setOnClickListener(new Button.OnClickListener() {
- public void onClick(View v) {
- EditText yourName = (EditText) findViewById(R.id.yourName);
- setContentView(R.layout.secwindow);
- TextView myMsg = (TextView) findViewById(R.id.myMsg);
- // change the properties of msg
- myMsg.setTextSize(20);
- myMsg.setAlignment(Alignment.ALIGN_CENTER);
- myMsg.setText("Welcome "+yourName.getText().toString());} });
- }
- }
2. Load the GUI for the first form by using:
- setContentView(R.layout.nameOfYourXML);
3. Define the ok button in java file
- Button okButt = (Button) findViewById(R.id.theIDforOkButton);
4. Set on click listener to ok button
- okButt.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
//…write what you want to happen when u click..
}});
- EditText yourName = (EditText) findViewById(R.id.yourName);
setContentView(R.layout.secwindow);
TextView myMsg = (TextView) findViewById(R.id.myMsg);
myMsg.setTextSize(20);
myMsg.setAlignment(Alignment.ALIGN_CENTER);
myMsg.setText("Welcome "+yourName.getText().toString());
The final code for java code
Using java Syntax Highlighting
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
Notice: mainwindow refers to the first form xml file and secwindow refers to the second form
Problem that may appear
NullPointer Exception
- - Check the order when you define the component
- All used component should be declare before using the method of it
- After you use setContentView again, notice that all previous value will be removed only if you store it before reuse this method
Error belong to updating R.java file
- - You use import android.R; in one of your java files
- Your xml file name contain invalid char like capital letter
- Your project is not android project, convert it to android project
Error in xml file without knowing the source of it
- - You may use invalid attribute
- You don’t specify the xmlns:android
- Check the tags
Notice: I am new in this platform, but I will be happy to answer any question belong to this example. and If there is any wrong information i provide, just told me ^^, thx
Best Regards,
Ghalya






