0% found this document useful (0 votes)
203 views16 pages

Lexioni - 09.programim Mobile

This document is a lecture summary in Albanian on mobile application development. It discusses creating activities in Android, using intents to start activities, and passing data between activities using intents. Specifically, it describes creating an app with two activities - one to collect a user's name and last name, and another to display that data passed via an intent. The summary provides an introduction to basic concepts needed for mobile development on Android.

Uploaded by

SaiDj Dulevic
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
203 views16 pages

Lexioni - 09.programim Mobile

This document is a lecture summary in Albanian on mobile application development. It discusses creating activities in Android, using intents to start activities, and passing data between activities using intents. Specifically, it describes creating an app with two activities - one to collect a user's name and last name, and another to display that data passed via an intent. The summary provides an introduction to basic concepts needed for mobile development on Android.

Uploaded by

SaiDj Dulevic
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Lektor: KRISTEL BOZHIQI

Departamenti Shkenca Kompjuterike, Fakulteti Teknologji Informacioni


Universiteti “Aleksandër Moisiu”, Durrës 2018

Cikël leksionesh
në "Programim
Mobile"
Ky kurs mëson studentët se si për të ndërtuar Apps celular për Android, iOS dhe
Windows 8, trininë që është platforma operative celulare e sotme.Android eshte
nje software stack per mobile devices qe perfshihen ne nje sistem operativ,
middleware dhe aplikacionet celes.Android SDK siguron tools dhe API-te e
nevojshme per te filluar zhvillimin e aplikacionit ne platformen Android. Ky Leksioni IX
kurs ofron një hyrje në zhvillimin e softuerit celular për ata me përvojë ne
programimim te orjentuar ne objekte ne Java si dhe njohuri ne formatin XML.

Android Aplication Development For Dummies 2 nd Edition Michael Burton, Donn


Shënim: Për më shumë informacion :
Felker, LEARNING MOBILE APP DEVELOPMET A Hands-on Guide to Buiding Apps with iOS and Android
Jakob Iversen, Michael Eierman,

1 Cikël leksionesh në "Programim Mobile"


Lektor: KRISTEL BOZHIQI
Departamenti Shkenca Kompjuterike, Fakulteti Teknologji Informacioni
Universiteti “Aleksandër Moisiu”, Durrës 2018

Intent
In the previous lesson we have created an application that consists of two Activities. I will
remind you that to create an Activity you need to:
- create a class that extends android.app.Activity
- create an Activity-tag in the manifest-file and specify the class created for it in the Name
field.
 I hope the previous lesson wasn’t too complicated and you are now comfortable with Activity
creation procedure. Now we can pay attention to Activity invocation code.
1 Intent intent = new Intent(this, ActivityTwo.class);

2 startActivity(intent);
We’ve used an Intent object. You can read about it here, here and here. But the information is
quite complicated for understanding from scratch. I will try to explain in my own words.

What Intent is
In our case Intent is an object in which we specify, which Activity we need to invoke. After it
we pass this Intent-object to startActivity method, which finds the corresponding Activity and
shows it. When creating an Intent we have used Intent(Context packageContext, Class
cls) constructor with two parameters.
The first parameter is Context. If you remember, when we created View in one of our previous
lessons, we have also used a Context object. Activity is a subclass of Context, that’s why we
can use Activity as a Context object - this. To be short, Context is an object that grants access
to basic application functions such as: access to resources, file system, Activity invocation, etc.
I guess in future we will explore examples where we will see explicitly what Context is used for
and how it is used.
The second parameter is a class name. I will remind that when creating Activity, we specify its
class name in the manifest-file. Now if we specify the same class for Intent - the system will
look up the corresponding Activity in the manifest-file and show it.
 

Explicit invocation
Invoking Activity with such an Intent is an explicit invocation. That is, using the class
we explicitly specify which Activity we would like to see. It is usually used inside one
application. It can be illustrated in the following way:
We create an Intent here and pass it Class_B class as a parameter. After this we
invoke startActivity method with created Intent as a parameter. Method checks

2 Cikël leksionesh në "Programim Mobile"


Lektor: KRISTEL BOZHIQI
Departamenti Shkenca Kompjuterike, Fakulteti Teknologji Informacioni
Universiteti “Aleksandër Moisiu”, Durrës 2018
if Activity (with Class_B) is present in AndroidManifest, if yes it displays the Activity. It is all
in the bounds of single application.

Implicit invocation
There is also an implicit Activity invocation. The difference is that for creating an Intent we
use not a class object, but fill action, data, category parameters with specific values. The
combination of these values defines a goal which we are trying to accomplish. For example,
sending a letter, opening a link, editing some text, viewing an image, calling a specific number
and so on. By-turn we specify an Intent Filter for the Activity - it is a set of the same
parameters: action, data, category (but the values are its own, they depend on what Activity is
capable of doing). And if the parameters of our intent match the conditions of this filter, the
activity is invoked. But now the search goes through all the Activities in the system.
If several Activities are found, the system gives you a choice, which application exactly would
you like to use. It can be illustrated as following:

3 Cikël leksionesh në "Programim Mobile"


Lektor: KRISTEL BOZHIQI
Departamenti Shkenca Kompjuterike, Fakulteti Teknologji Informacioni
Universiteti “Aleksandër Moisiu”, Durrës 2018

Intent is created in Application_1, action, data, category parameters are filled. Let’s name this


set of parameters Param_C for convenience. Using startActivity, this Intent will be sent to
look for an appropriate Activity, which will be able to accomplish what we need (that is what is
defined by Param_C). There are different applications in the system, and each of them has
several Activities. For some of them an Intent Filter is defined (Param_A, Param_B sets and so
on), for some it is not. startActivity method matches the set of parameters from Intent and sets
of parameters of Intent Filter for each Activity. If the sets match (Param_C) for both, the
Activity is considered appropriate.
If, in the end, only one Activity is found - it is displayed. If there are several Activities found,
the user is given a list, from which he can choose which application to use.
For example, if there are several music players installed on the system and you open an mp3 file,
the system will show you the list of Activities, that are able to play music and will ask you to
choose which one of them to use. And those Activities that can edit text, show pictures, make
calls and so on, will be ignored. 
We will gradually find out nuances of this mechanism and values, using which you can fill
action, data and category in Intent and Intent Filter. For now, it is important to understand, that
in case of implicit invocation, one application sends an Intent and all other applications check
this Intents’ parameters with their own Activity -> Intent Filter. Intent is a basic concept of

4 Cikël leksionesh në "Programim Mobile"


Lektor: KRISTEL BOZHIQI
Departamenti Shkenca Kompjuterike, Fakulteti Teknologji Informacioni
Universiteti “Aleksandër Moisiu”, Durrës 2018
Extras –passing data using Intent
In the previous lessons we found out what an Intent is and how to use it. We have invoked one
Activity from another passing an action. Now we will learn how to transfer data. We will make
the simplest application. On the first screen we will enter our name and surname and the second
screen will display this data. We will transfer data inside Intent.
Open main.xml and draw a screen with fields and send button:
<?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">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:gravity="center_horizontal"
        android:text="Input your Name">
    </TextView>
    <TableLayout
        android:id="@+id/tableLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:stretchColumns="1">
        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
5 Cikël leksionesh në "Programim Mobile"
Lektor: KRISTEL BOZHIQI
Departamenti Shkenca Kompjuterike, Fakulteti Teknologji Informacioni
Universiteti “Aleksandër Moisiu”, Durrës 2018
            <TextView
                android:id="@+id/textView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="First Name">
            </TextView>
            <EditText
                android:id="@+id/etFName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp">
                <requestFocus>
                </requestFocus>
            </EditText>
        </TableRow>
        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/textView2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Last Name">
            </TextView>
            <EditText
                android:id="@+id/etLName"

6 Cikël leksionesh në "Programim Mobile"


Lektor: KRISTEL BOZHIQI
Departamenti Shkenca Kompjuterike, Fakulteti Teknologji Informacioni
Universiteti “Aleksandër Moisiu”, Durrës 2018
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp">
            </EditText>
        </TableRow>
    </TableLayout>
    <Button
        android:id="@+id/btnSubmit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Submit">
    </Button>
</LinearLayout>
We will enter name and surname into EditText and Submit button will invoke other screen and
pass it this data. 
Write code for MainAcitivty.java:

package ru.startandroid.develop.p0281intentextras; 
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
 
public class MainActivity extends Activity implements OnClickListener {

7 Cikël leksionesh në "Programim Mobile"


Lektor: KRISTEL BOZHIQI
Departamenti Shkenca Kompjuterike, Fakulteti Teknologji Informacioni
Universiteti “Aleksandër Moisiu”, Durrës 2018
   
  EditText etFName;
  EditText etLName;
   
  Button btnSubmit;  
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         
        etFName = (EditText) findViewById(R.id.etFName);
        etLName = (EditText) findViewById(R.id.etLName);
         
        btnSubmit = (Button) findViewById(R.id.btnSubmit);
        btnSubmit.setOnClickListener(this);
         
    }
 
 
  @Override
  public void onClick(View v) {
    Intent intent = new Intent(this, ViewActivity.class);
    intent.putExtra("fname", etFName.getText().toString());
    intent.putExtra("lname", etLName.getText().toString());
    startActivity(intent);

8 Cikël leksionesh në "Programim Mobile"


Lektor: KRISTEL BOZHIQI
Departamenti Shkenca Kompjuterike, Fakulteti Teknologji Informacioni
Universiteti “Aleksandër Moisiu”, Durrës 2018
  }
}
Define input fields and a button. We assign button a listener - Activity (this). Let’s
observe onClick method implementation. We create Intent using the class, not action. If you
remember we started to get acquainted with Intent this way. I will remind - it means that the
system will look through the manifest-file of our application and will display an Activity if it
finds one with the same class. ViewActivity is not created yet, that’s why the code will be
underlined in red. However, we can still save the file. We will create this Activity a little bit later
and the error will disappear.
So the Intent has been created, let’s observe the code further. putExtra method is used. It has lots
of varieties and is similar to put method for Map, that is, it adds a key-value pair to the object.
First parameter is key(name), the second - value.
We’ve put two objects with names fname and lname into Intent. fname contains the value of
etFName field, lname - value of the etLName field. The only thing left to do is to send the
equipped Intent using startActivity method.
 
Let’s create the second Activity now. Let’s name it ViewActivity.
Create layout-file view.xml for it:
<?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/tvView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp"
        android:text="TextView"
        android:textSize="20sp">

9 Cikël leksionesh në "Programim Mobile"


Lektor: KRISTEL BOZHIQI
Departamenti Shkenca Kompjuterike, Fakulteti Teknologji Informacioni
Universiteti “Aleksandër Moisiu”, Durrës 2018
    </TextView>
</LinearLayout>
It is just a TextView here which will display the incoming data.
 
Create ViewActivity class. Write this code:
package ru.startandroid.develop.p0281intentextras;
 
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
 
public class ViewActivity extends Activity {
   
  TextView tvView;
   
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.view);
     
    tvView = (TextView) findViewById(R.id.tvView);
     
    Intent intent = getIntent();
     
    String fName = intent.getStringExtra("fname");
    String lName = intent.getStringExtra("lname");
     
10 Cikël leksionesh në "Programim Mobile"
Lektor: KRISTEL BOZHIQI
Departamenti Shkenca Kompjuterike, Fakulteti Teknologji Informacioni
Universiteti “Aleksandër Moisiu”, Durrës 2018
    tvView.setText("Your name is: " + fName + " " + lName);
  }
}
Find TextView, then receive Intent and extract String objects with
names fname and lname from it. These are the same objects, which we put inside in
MainActivity.java. Form the output string into the TextView using the received data.
Don’t forget to register Activity inside manifest. This time we don’t need any intent filters as we
know the name of Activity class and use the explicit invocation.
Save everything and run the application.
You will see the following screen:

Fill in the fields with whatever you wish. I will write John in the First Name field and Smith in
the Last Name field. 
Press Submit:

11 Cikël leksionesh në "Programim Mobile"


Lektor: KRISTEL BOZHIQI
Departamenti Shkenca Kompjuterike, Fakulteti Teknologji Informacioni
Universiteti “Aleksandër Moisiu”, Durrës 2018

ViewActivity is displayed, it read data from Intent and displayed it on the screen.
You can put not only String data inside Intent. In the list of Intent methods you can see all the
types that putExtra method can receive as a parameter.

12 Cikël leksionesh në "Programim Mobile"


Lektor: KRISTEL BOZHIQI
Departamenti Shkenca Kompjuterike, Fakulteti Teknologji Informacioni
Universiteti “Aleksandër Moisiu”, Durrës 2018

Exercise: Intent & Bundle

I implemented one activity with two layout. In this exercise, I implement the same application,
with same function and presentation, using two activity, HelloAndroid.java and
HelloAndroid_2.java. The activity is switched using Intent, the data is passed using Bundle.

We can keep both layout, main.xml and main2.xml, no change.

We have to re-write HelloAndroid.java to two separated files, HelloAndroid.java and


HelloAndroid_2.java.

HelloAndroid.java

package com.example.helloandroid3;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.view.View;

public class HelloAndroid extends Activity {

private
Button okButton;
Button cancel1Button;
EditText textName;
EditText textPhonenumberIs;
EditText textEmailIs;
EditText textWebsiteIs;
EditText textAddressIs;

/** Called when the activity is first created. */


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

startLayout1();
}

private Button.OnClickListener okOnClickListener = new


Button.OnClickListener(){
@Override
public void onClick(View v) {
textName = (EditText) findViewById(R.id.whoareyou);
CharSequence textName_value = textName.getText();

13 Cikël leksionesh në "Programim Mobile"


Lektor: KRISTEL BOZHIQI
Departamenti Shkenca Kompjuterike, Fakulteti Teknologji Informacioni
Universiteti “Aleksandër Moisiu”, Durrës 2018
textPhonenumberIs = (EditText) findViewById(R.id.phonenumberIs);
CharSequence textPhonenumberIs_value =
textPhonenumberIs.getText();

textEmailIs = (EditText) findViewById(R.id.emailIs);


CharSequence textEmailIs_value = textEmailIs.getText();

textWebsiteIs = (EditText) findViewById(R.id.websiteIs);


CharSequence textWebsiteIs_value = textWebsiteIs.getText();

textAddressIs = (EditText) findViewById(R.id.addressIs);


CharSequence textAddressIs_value = textAddressIs.getText();

Intent intent = new Intent();


intent.setClass(HelloAndroid.this, HelloAndroid_2.class);

Bundle bundle = new Bundle();


bundle.putCharSequence("bName", textName_value);
bundle.putCharSequence("bPhonenumber", textPhonenumberIs_value);
bundle.putCharSequence("bEmail", textEmailIs_value);
bundle.putCharSequence("bWebsite", textWebsiteIs_value);
bundle.putCharSequence("bAddress", textAddressIs_value);

intent.putExtras(bundle);

startActivity(intent);
finish();
}
};

private Button.OnClickListener cancelOnClickListener = new


Button.OnClickListener(){
@Override
public void onClick(View v) {
finish();
}
};

private void startLayout1(){


setContentView(R.layout.main);
okButton = (Button) findViewById(R.id.ok);
okButton.setOnClickListener(okOnClickListener);
cancel1Button = (Button) findViewById(R.id.cancel_1);
cancel1Button.setOnClickListener(cancelOnClickListener);
};
}

HelloAndroid_2.java

package com.example.helloandroid3;

import android.app.Activity;

14 Cikël leksionesh në "Programim Mobile"


Lektor: KRISTEL BOZHIQI
Departamenti Shkenca Kompjuterike, Fakulteti Teknologji Informacioni
Universiteti “Aleksandër Moisiu”, Durrës 2018
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;

public class HelloAndroid_2 extends Activity {

private
Button backButton;
Button cancel2Button;
TextView nameField;
TextView phonenumberField;
TextView emailField;
TextView websiteField;
TextView addressField;

/** Called when the activity is first created. */


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

startLayout2();

Bundle bundle = this.getIntent().getExtras();


CharSequence textName_value = bundle.getCharSequence("bName");
nameField = (TextView) findViewById(R.id.name);
nameField.setText("Hello "+textName_value);

CharSequence textPhonenumberIs_value =
bundle.getCharSequence("bPhonenumber");
phonenumberField = (TextView) findViewById(R.id.phonenumber);
phonenumberField.setText("Phone Number: "+textPhonenumberIs_value);

CharSequence textEmailIs_value = bundle.getCharSequence("bEmail");


emailField = (TextView) findViewById(R.id.email);
emailField.setText("Email: "+textEmailIs_value);

CharSequence textWebsiteIs_value = bundle.getCharSequence("bWebsite");


websiteField = (TextView) findViewById(R.id.website);
websiteField.setText("Website: "+textWebsiteIs_value);

CharSequence textAddressIs_value = bundle.getCharSequence("bAddress");


addressField = (TextView) findViewById(R.id.address);
addressField.setText("Address: "+textAddressIs_value);
};

private Button.OnClickListener backOnClickListener = new


Button.OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(HelloAndroid_2.this, HelloAndroid.class);

startActivity(intent);
finish();
}

15 Cikël leksionesh në "Programim Mobile"


Lektor: KRISTEL BOZHIQI
Departamenti Shkenca Kompjuterike, Fakulteti Teknologji Informacioni
Universiteti “Aleksandër Moisiu”, Durrës 2018
};

private Button.OnClickListener cancelOnClickListener = new


Button.OnClickListener(){
@Override
public void onClick(View v) {
finish();
}
};

private void startLayout2(){


setContentView(R.layout.main2);
backButton = (Button) findViewById(R.id.back);
backButton.setOnClickListener(backOnClickListener);
cancel2Button = (Button) findViewById(R.id.cancel_2);
cancel2Button.setOnClickListener(cancelOnClickListener);
};
}

Because we have two activity, so we have to modify the file AndroidManifest.xml, to include
the additional activity.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloandroid3"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name=".HelloAndroid"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".HelloAndroid_2"></activity>
</application>
<uses-sdk android:minSdkVersion="2" />
</manifest>

16 Cikël leksionesh në "Programim Mobile"

You might also like