Skip to content


Android Tutorial -01

Android seperates it’s function from layout, almost like css and html. So here we’ll show how to connect the two.

First create a new Android project with the following settings:

  • Project Name: Android Tutorial 01
  • Build Targer: 1.5
  • Application Name: same as project name
  • Package Name: com.ronguilmet.com.androidtutorial01
  • min sdk: 3
  • check: AndroidTutorial01Activity

Here we start off with modifying the stock app that is created. We have a java file and an xml file.

We edit existing TextView (for displaying text), and add an EditText (taking input text) and a command Button (for executing).

We will be able to enter text, and have it displayed in the existing TextView when we click on the command button.

We use the Linear layout here, which displays objects one below or above each other.

If you are looking for a more flexible layout, I use RealativeLayout.

In the next tutorial we’ll add more.


main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- command button -->
<Button
android:id="@+id/buttonShowText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Text" />
<!-- text field for input -->
<EditText
android:id="@+id/editTextMessage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Input Message" >
<requestFocus />
</EditText>
<!-- text to display -->
<TextView
android:id="@+id/textViewDisplayMessage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is the text to be displayed"
android:textSize="20dp" />
</LinearLayout>

AndroidTutorial01Activity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.ronguilmet.com.androidtutorial01;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class AndroidTutorial01Activity extends Activity {
    private Button bShowText;
    private EditText etMessage;
    private TextView tvDisplay;
    private String etMessageHolder;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bShowText = (Button) findViewById(R.id.buttonShowText);
etMessage = (EditText) findViewById(R.id.editTextMessage);
tvDisplay = (TextView) findViewById(R.id.textViewDisplayMessage);
bShowText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                etMessageHolder = etMessage.getText().toString();
       tvDisplay.setText(etMessageHolder);
            }
        });
}
}

Posted in Tutorial.