5COSC005W - Tutorial 8 Exercises

As part of the tutorial for this week, you should complete ALL the tasks described in the following links and specifications: (your tutor will demonstrate you how to implement these tasks but make sure that you ask questions to your tutor for anything that you do not understand):

1 ListViews

Implement an Android application which displays as a list the names of your friends. You should use the ListView Android class. When the user clicks on any name of your friends then his/her name should be displayed in a Toast (covered in a previous week) or log the name by using the Log class. This is similar with the implementation of Spinner but instead you will be using the ListView class.

2 Custom ListViews

Implement an Android application which creates a custom ListView which displays a list of optional modules that a student can choose and a CheckBox next to them.

The student will be clicking on the checkboxes to select for which of the optional modules he/she would to register. The choices of the student should be saved in SharedPreferences so that they will be available the next time that the application starts (the chosen modules still appear as ticked in the checkboxes).

You should be using the following array to populate your ListView:

String[] modules = {"Mobile Application Development", "Machine Learning", "Client Server",
                    "iOS Programming", "Java Programming", "Python Programming"};

3 Creating Dynamic Layouts (Programmatically)

The following is another example of how to create layouts programmatically and add components during the execution of a program.

Make sure that you run and understand the full code.

The layout file: activity_main.html:

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linearlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:layout_gravity="center"
        android:text="Here is a list of options!"
         />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="center">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Add view dynamically"
            android:onClick="onClick"/>
    </LinearLayout>
</LinearLayout>

The activity code:

package uk.ac.westminster.dynamiclayoutexample;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    LinearLayout linearlayout;
    int count = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        linearlayout = findViewById(R.id.linearlayout);

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                                                                         LinearLayout.LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.CENTER;

        CheckBox chk1 = new CheckBox(this);
        chk1.setText("Option 1");
        chk1.setLayoutParams(params);

        CheckBox chk2 = new CheckBox(this);
        chk2.setText("Option 2");
        chk2.setLayoutParams(params);

        CheckBox chk3 = new CheckBox(this);
        chk3.setText("Option 3");
        chk3.setLayoutParams(params);

        linearlayout.addView(chk1);
        linearlayout.addView(chk2);
        linearlayout.addView(chk3);
    }

    public void onClick(View view) {
        LinearLayout lin1 = new LinearLayout(this);
        lin1.setOrientation(LinearLayout.HORIZONTAL);
        CheckBox chk = new CheckBox(this);

        TextView tv = new TextView(this);
        tv.setText("Option " + count);
        ++count;

        lin1.addView(chk);
        lin1.addView(tv);
        linearlayout.addView(lin1);
    }
}