Nicozan-Blog

Knowledge Seeking

Digging deeper into Android December 15, 2010

Filed under: Java — nicozan @ 3:22 pm

Continuing with the previous post , we`ll be seeing some files and objects used to set the layouts and actitvities of our application.

The AndroidManifest.xml placed in the root of our project is the configuration file of the project, it allows us to set the main Activity to run and some other settable parameters for our application. For example, if we want our Android applicacion to be able to use INTERNET, we`ll have to set a permission attribute like the next code snippet:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.wordpress.nicozan"
      android:versionCode="1"
      android:versionName="1.0">
    
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name="com.wordpress.nicozan.MainMenu"
                  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>

<uses-feature android:name="android.hardware.wifi" android:required="false" />
<uses-permission android:name="android.permission.INTERNET" />          
</manifest> 

The tag uses-permission is the one that give us the chance to set the INTERNET permission.

Now it`s time to talk about layouts (Graphical User Interfaces). Inside the folder res/layout/, we`ll find the xml files (The default xml file is the main.xml) that describe the elements of a particular layout. The elements can be Buttons, TextViews, EditableText and some others.
There are two kinds of layouts, Relative Layouts and Linear Layouts.In the first one, the positions of the children (elements) can be described in relation to each other or to the parent. The second one arranges its children in a single column or a single row.
Let`s see the next 2 examples:

main.xml

<?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">
    <TextView
        android:id="@+id/label1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Your Name:"/>
    <EditText
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:drawable/editbox_background"
        android:layout_below="@id/label1"/>
    <TextView
        android:id="@+id/label2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/name"
        android:text="Your Age:"/>
    <EditText
        android:id="@+id/age"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:drawable/editbox_background"
        android:layout_below="@id/label2"/>       
    <TextView
        android:id="@+id/label3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/age"
        android:text="E-MAIL:"/>
    <EditText
        android:id="@+id/email"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:drawable/editbox_background"
        android:layout_below="@id/label3"/>        
    <Button
        android:id="@+id/profile"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"   
        android:layout_below="@+id/email"     
        android:text="Show Profile" />
</RelativeLayout>

profile.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">   
    <LinearLayout
    	android:orientation="horizontal"
    	android:layout_width="fill_parent"
    	android:layout_height="fill_parent"
    	android:layout_weight="1">
    	<TextView
        	android:id="@+id/l1"
        	android:layout_width="wrap_content"
        	android:layout_height="fill_parent"
        	android:layout_weight="0"
        	android:text="Welcome:"/>    
    	<TextView
        	android:id="@+id/profName"
        	android:layout_width="wrap_content"
        	android:layout_height="fill_parent"
        	android:layout_weight="0"
        	android:text="USERNAME"/>        
     </LinearLayout>   	
     <LinearLayout
    	android:orientation="horizontal"
    	android:layout_width="fill_parent"
    	android:layout_height="fill_parent"
    	android:layout_weight="1">

    	<TextView
        	android:id="@+id/l2"
        	android:layout_width="wrap_content"
        	android:layout_height="fill_parent"
        	android:layout_weight="0"
        	android:text="Your Age is:"/>
        <TextView
        	android:id="@+id/profAge"
        	android:layout_width="wrap_content"
        	android:layout_height="fill_parent"
        	android:layout_weight="0"
        	android:text="USERAGE"/>            
    	        
     </LinearLayout>
     <LinearLayout
    	android:orientation="horizontal"
    	android:layout_width="fill_parent"
    	android:layout_height="fill_parent"
    	android:layout_weight="1">

    	<TextView
        	android:id="@+id/l3"
        	android:layout_width="wrap_content"
        	android:layout_height="fill_parent"
        	android:layout_weight="0"
        	android:text="Your Email is:"/>
        <TextView
        	android:id="@+id/profEmail"
        	android:layout_width="wrap_content"
        	android:layout_height="fill_parent"
        	android:layout_weight="0"
        	android:text="USEREMAIL"/>               	
	   </LinearLayout>   	   	
</LinearLayout>

After adding these two files to our project,you will see that in the R.class there will be new class attributes referencing the elements contained in these files.
So now let`s see how we can change layouts while we are running an activity:

mainMenu.java

package com.wordpress.nicozan;

import com.wordpress.nicozan.extras.User;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.BufferType;
import android.widget.Toast;


public class MainMenu extends Activity {
	/** Called when the activity is first created. */	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final Button button = (Button) findViewById(R.id.profile);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	User usr = new User();
            	//Here we get all the field Values...
            	final EditText name = (EditText) findViewById(R.id.name);
                final EditText age = (EditText) findViewById(R.id.age);
                final EditText email = (EditText) findViewById(R.id.email);
                
                //User Info
                usr.setUserName(name.getText().toString());
                usr.setAge(name.getText().toString());
                usr.setMail(email.getText().toString());
                
                setContentView(R.layout.profile);
                
                // Value Setting of the New Layout
                final TextView profName = (TextView) findViewById(R.id.profName);
                final TextView profAge = (TextView) findViewById(R.id.profAge);
                final TextView profEmail = (TextView) findViewById(R.id.profEmail);                
                
                profName.setText(name.getText(), BufferType.EDITABLE);
                profAge.setText(age.getText(),BufferType.EDITABLE);
                profEmail.setText(email.getText(),BufferType.EDITABLE);
               
            }
        });

    }
}

The code talks by it self, but the important thing to remark is the order in which the methods are called inside the onClick method. Because you can`t call the findViewById method, with a parameter referencing an element of a layout that hasn`t been set with the setContentView method.

Advertisements
 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s