02 - Android SDK

  • Description: Development environment on Android, overview of the Android SDK
  • Practical part: Android SDK development





Task 0 - Setting up the environment

To install Android Studio on your own machine follow the step here: Installing Android Studio. Android studio is already installed on the workstations in the lab.

Task 1 - Create and run an application

Create a new Android Project in Android Studio (Start a new Android Studio Project). Choose a name and leave other options as they are. On the next screen choose Phone and Tablet and choose API level 15 or higher. Choose Empty Activity on the next screen. Continue to the next screen and click finish. On the sidebar on the left choose project and open java > student.example.com > MainActivity. This activity is very simple right now.

Run the project by clicking the green arrow on the toolbar above the editing area. Android Studio will prompt you to choose a device. Follow the steps in the next tasks to create a new virtual device.

Task 2 - Creating a virtual device

Create a virtual device:

  1. Click next to the Android Virtual Device list under the Launch emulator option
  2. Click Create Virtual Device.
  3. Choose Nexus 4 or Galaxy Nexus screen resolution. Click Next.
  4. Choose Marshmallow x86 image. Download if needed. Click next.
  5. Give the device 512-1024 MBs of RAM and 64 MB VM Heap (Under Advance Options)
  6. Give the device 200 MB SD Card.
  7. Select Use Host GPU.

On Windows, if HAXM service is started, Android Virtual Devices will run virtualized. On Linux KVM is required for this.

On Linux you have to specify emulator launch parameters to use virtualization:

  1. Click Run > Edit Configurations in the top menu
  2. Select the Emulator tab and in the Additional command line options field, enter: -qemu -m 1024 -enable-kvm

You can also add this to the general setup by following the previous steps, but selecting: Defaults > Android Application between the two steps.

You can also launch the emulator from the terminal, assuming default location:

./Android/Sdk/tools/emulator -avd Nexus_4_API_23 -gpu on -qemu -m 1024 -enable-kvm

Close the AVD Manager window, select the newly created device from the list. Wait for the emulator to boot and check that your empty application starts.

Task 3 - Add a button

The MainActivity.java file should be displayed. If not, open it using the Package Explorer pane. You will find it in the src folder.

The Activity should be very simple. It has an onCreate method which gets called when the Activity gets started.

In the onCreate method the Activity calls


This sets the layout for the Activity. R is a class generated by the SDK and it maps every resource to a number. It contains subclasses for every folder found in the res folder, in this case layout.

Open the res/layout/activity_main.xml file. You will see the layout editor. In the Palette pane, find the button and drag it to the activity. You will see the Outline pane to the right of the screen change to reflect this. Select the button and check its ID field in the Properties panel, under the list of UI elements. Change the text of the button to “Press me!”. Also check the ID field of the TextView element.

Go back to MainActivity.java.

Add the following code to the onCreate method. Make sure the ids match the ones in your layout.

        final Button b1 = (Button) findViewById(R.id.button); /* make sure the button ID matches */
        final TextView tv1 = (TextView) findViewById(R.id.textView); /* make sure the textView ID matches */
        b1.setOnClickListener(new OnClickListener() {
        	int n = 1;
		public void onClick(View v) {
			tv1.setText("Hello again! " + Integer.toString(n++));

The findViewById calls are used to get the objects corresponding to the Button and the TextView that were already declared. These objects are declared as final variables in order for them to be visible inside the OnClickListener call (anonymous class). The OnClickListener object contains an onClick method which gets executed when the button is pressed. In this case, it updates the TextView to reflect the number of clicks on the button.

Task 4 - Use logcat for debugging

Start the application installed in Task 2. Make sure the Android Monitor tab is expanded at the panel at the bottom and that logcat tab is selected inside this tab. Type any text in the box and then click one of the buttons. A new line containing your text should appear in the Logcat. Depending on what button you press, the line color will vary. It is also possible to filter these log entries: above the logs themselves there is a text entry input which allows textual filtering. It allows you to limit scope by prefixing searches with pid:, tag:, app: or text: and it accepts Java-style regular expressions. To the left of this text field there is a drop-down which allows you to select verbosity. Choosing a level will only allow messages of that level or higher to be displayed.

Import the LogLevel project into Android Studio (File > New > Import Project and navigate to the project in the archive. Look over the code. Each button has a method attached which calls a static method from the Log class. These methods generate log entries on different levels of importance: d=debug, i=info, w=warning, e=error.

Add another two buttons to the application: one that generates an assert level log entry and one that repeats the previously generated log entry, but with a higher level of importance (e.g. previously a debug entry was created, now the button click should generate an info entry). For the second button if the previous entry was an assert entry, then the next entry will also be an assert entry.

Task 5 - View weight

Use the LogLevel project as a start. Notice that the EditText box only takes up one line of the screen and there should be some screen space left at the bottom of the screen. Make the EditText box take up all available space. To do this, you should set the weight parameter of the EditText view to 1 (or any integer).

The weight parameter is used to distribute empty screen space to the elements which need it. First, the layout builder will place all the elements for which the size is easy to determine. Then, the empty space will be distributed to the elements with weight, based on their weight: if the total weight of all elements is 4, an element with a weight of 1 will get 1/4 of the empty screen space, an element with 2 weight will get 2/4, and so on.

Task 6 - Use ADB to transfer files and install apps

Connect to it using ADB. ADB can be used from a terminal on all operating systems. To get a list of connected devices use can use:

adb devices

To get a shell on the connected device use:

adb shell

Android devices normally come with a more limited set of commands, so a text editor might not be available.

Create a text file on your machine. Copy it to the connected device using:

adb push <local> <remote>

The file should be copied to the /sdcard/ partition on the phone. Connect to the device using adb shell, and use cat to check the contents of the file. To copy from the device use:

adb pull <remote> [<local>]

Both push and pull can be used with both directories and files.

To install an application through adb use:

adb install <file>

Install LogLevel.apk (in the laboratory archive).

To check all available options of the ADB use:

adb --help
osp/lectures/lecture-sdk.txt · Last modified: 2016/11/06 18:44 by laura.gheorghe
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