How to show your first Views on the screen

15 02 2010

In this Post we will learn how to show our first simple Views on the screen. Each Activity has the method setContentView(). For now we need to know two ways to show some view by calling this Activity’s method.

#1 – Using a View declared on main.xml file.

Well, when you create a project from Eclipse this task is automatically done! Your Activity has a code like this:

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

public class WingsActivity extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 }
}

Isn’t it? Open the file ./res/layout/main.xml. There is something like that:


<?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:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="@string/hello"
 />
</LinearLayout>

You don’t need to write anything on a xml to show a View through this way. There are two Views declared on xml: a LinearLayout and a TextView. When you call  setContentView(R.layout.main), you are telling to your Activity that it must to show everything declared on main.xml. To show a button below the TextView, just insert the tag below:

<?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:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="@string/hello"
 />
<Button
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="Click Me!"
 />
</LinearLayout>

Now your screen must show something like that:

#2 – Instantiating your Views manually

You don’t depends on the file main.xml (or any other layout file) to show views on the screen. If you want to add Views through “JAVA MODE” it’s ok, but you need to know a bit more about layouts configuration and other little things. But don’t worry about that for now. In this Post we will only to show a View instantiating manually, nothing more. =)

public class WingsActivity extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);

 Button button = new Button(this);
 button.setText("MANUAL BUTTON");
 setContentView(button);

 }
}

Result…

“WHAT? My button is so big…”

Yeah! As I sad before, you must to learn a bit more about Layout configuration if you want to create a layout without xml. Use this example to have some experience with other types of views. =)

RECOMMENDED:

It’s a good idea you read a bit more about Android Views. Please, visit:  http://developer.android.com/guide/tutorials/views/index.html

See ya!