This is an old revision of the document!


Lab 04 - Advanced Android Interfaces

Activities

For Android applications, each window is called an activity. From a programming point of view, an activity is a class that extends the Activity class. Unlike other systems in which a program contains several windows displayed simultaneously, in Android, the screen is always occupied by a single window. Moreover, windows are connected by the simple fact that a window generates several windows. The window that appears on the screen when the application is started is considered the main activity that generates all the others. To create an activity, we need to create a class that extends the Activity class. At startup, the program will generate it, and we can interact with the window through events.

Some of the most important events are:

  • onCreate
  • onPause
public class MainActivity extends Activity {
 
   @Override
   public void onCreate(Bundle savedInstanceState)
   {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);        
   }
   @Override
   protected void onPause() 
   {
       // TODO Auto-generated method stub
       super.onPause();
   }
 
} 

For each event, do not forget to call the parent function, e.g: super.onCreate().

onCreate

This event is called when the activity is created. Here is where we initialize the window components. To make the elements on the window visible, it is necessary to call the setContentView function. There are cases in which when starting the application, it must return to a previous state (e.g. after a forced stop). Thus, it is recommended that we check if the window is a new one or not, e.g. if the Bundle variable of passed as parameter is null or not.

if (savedInstanceState! = null) {// window is not new and some remaining data must be loaded} 
else {// window is new} 

onPause

When this event is called, the window prepares to stop. That is why it is important to save all the changes we need. Here we will stop processes that are no longer needed when the application is not running, such as video or audio elements.

Building the interface

One of the most important elements of an application is the graphical interface. In order to be able to differentiate the graphical interface from the writing of the application code, the two are in different files. Also, the interface design is done in another language, a descriptive one. It resembles the HTML language, being made up of bookmarks.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical" >
    <TextView android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Hello, I am a TextView" />
    <Button android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello, I am a Button" />
</LinearLayout>

There is a bookmark for each item on the window. The name of the bookmark is the same as the class name on the code side. The elements have certain characteristics: positioning, dimensions, color, text. To set each property we write a parameter in the following way: android: property = “value”

dapm/laboratoare/02_2.1616924409.txt.gz · Last modified: 2021/03/28 12:40 by ioana_maria.culic
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0