Courses / Mobile Application Development (CDE-315) – BS CS 5th Semester

Week 3 Lab 3 – Student Information App: Change Image and Name from Resource and String Folders

Week 3 – Android OS: Architecture, Framework and Application Development · 120 min read · 13 MCQs to check yourself

Philipp Lackner – Resources and qualifiers (Android Basics)

Objective

Show a student card (photo, name, roll number, programme) whose image comes from the drawable folder and whose text comes from strings.xml, and switch to a second student from Java code. CLOs: CLO-2 (develop an app), CLO-1 (understand the Android resource system).

What you will build in CampusMate

The welcome screen from Lab 1 becomes a Student Card. A button "Next student" changes the photo and all texts using resources only – no text is hard-coded in Java.

Before (Lab 1) After (Lab 3)
"Welcome to CampusMate" + Say Hello button Photo + name + roll + programme + "Next student" button

Workflow

Step What you do File
1 Add two student photos res/drawable/
2 Put all texts in string resources res/values/strings.xml
3 Add a colour and a dimension colors.xml, dimens.xml
4 Design the card layout activity_main.xml
5 Switch student from Java MainActivity.java
6 Add an Urdu translation (bonus) values-ur/strings.xml
7 Run and test Emulator

Step 1 – Add images to drawable

  1. Find two small square photos (or avatar images) – PNG/JPG, under 300 KB.
  2. Rename them using only lowercase letters, numbers and underscores: student_ali.png, student_sara.png.
  3. Copy them and paste into res → drawable (choose the plain drawable folder).

A name like Ali Photo.PNG gives a build error – resource names must be lowercase with no spaces.

Step 2 – String resources (res/values/strings.xml)

<resources>
    <string name="app_name">CampusMate</string>

    <string name="student1_name">Ali Raza</string>
    <string name="student1_roll">Roll No: 23-BSCS-015</string>
    <string name="student1_program">BS Computer Science – 5th Semester</string>

    <string name="student2_name">Sara Khan</string>
    <string name="student2_roll">Roll No: 23-BSCS-027</string>
    <string name="student2_program">BS Computer Science – 5th Semester</string>

    <string name="next_student">Next student</string>
    <string name="card_title">Student Card</string>
</resources>

Why resources? One place to change text, automatic translation support, and Android Studio warns you about hard-coded strings.

Step 3 – Colour and dimension resources

res/values/colors.xml – add inside <resources>:

<color name="campus_green">#1B5E20</color>

Create res/values/dimens.xml (right-click values → New → Values Resource File → dimens):

<resources>
    <dimen name="photo_size">140dp</dimen>
</resources>

Step 4 – Card layout (activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:padding="24dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/card_title"
        android:textColor="@color/campus_green"
        android:textSize="22sp"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/imgStudent"
        android:layout_width="@dimen/photo_size"
        android:layout_height="@dimen/photo_size"
        android:layout_marginTop="16dp"
        android:contentDescription="Student photo"
        android:scaleType="centerCrop"
        android:src="@drawable/student_ali" />

    <TextView
        android:id="@+id/tvName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="12dp"
        android:text="@string/student1_name"
        android:textSize="22sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tvRoll"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/student1_roll" />

    <TextView
        android:id="@+id/tvProgram"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/student1_program" />

    <Button
        android:id="@+id/btnNext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="@string/next_student" />
</LinearLayout>

Notice: every value starts with @ – @string/…, @drawable/…, @color/…, @dimen/… – the layout only refers to resources.

Step 5 – Switch student from Java (MainActivity.java)

package com.example.campusmate;

import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private ImageView imgStudent;
    private TextView tvName, tvRoll, tvProgram;
    private Button btnNext;
    private boolean showingFirst = true;     // which student is on screen

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

        imgStudent = findViewById(R.id.imgStudent);
        tvName = findViewById(R.id.tvName);
        tvRoll = findViewById(R.id.tvRoll);
        tvProgram = findViewById(R.id.tvProgram);
        btnNext = findViewById(R.id.btnNext);

        btnNext.setOnClickListener(v -> switchStudent());
    }

    private void switchStudent() {
        if (showingFirst) {
            imgStudent.setImageResource(R.drawable.student_sara);   // image from drawable
            tvName.setText(R.string.student2_name);                 // text from strings.xml
            tvRoll.setText(R.string.student2_roll);
            tvProgram.setText(R.string.student2_program);
        } else {
            imgStudent.setImageResource(R.drawable.student_ali);
            tvName.setText(R.string.student1_name);
            tvRoll.setText(R.string.student1_roll);
            tvProgram.setText(R.string.student1_program);
        }
        showingFirst = !showingFirst;      // flip for the next click
    }
}

Key idea: in Java a resource is just an int ID in the generated R class – R.drawable.student_sara, R.string.student2_name. setText() and setImageResource() accept these IDs directly. To get the text itself as a String use getString(R.string.student2_name).

Step 6 (bonus) – Urdu translation

  1. Right-click res → New → Android Resource File → File name strings, Resource type Values → choose Locale → ur (Urdu) → OK. This creates values-ur/strings.xml.
  2. Add Urdu versions, e.g. <string name="next_student">اگلا طالب علم</string>.
  3. Change the emulator language to Urdu (Settings → System → Languages) – the app switches text automatically, no Java change needed.

Run and test

Test Expected result
Open app Ali's photo and details
Tap Next student Sara's photo and details
Tap again Back to Ali
Change phone language to Urdu (bonus) Button and title in Urdu

Common errors and fixes

Problem Fix
'A' is not a valid file-based resource name Rename the image: lowercase, no spaces, no dashes
Image looks stretched Use android:scaleType="centerCrop" and a square image
Resources$NotFoundException You passed a resource ID where text was expected, e.g. setText("" + R.string.x) – use setText(R.string.x)
Warning "Hardcoded string" Move the text to strings.xml and use @string/name

Short and long questions

1. (Viva) How does Android choose values-ur/strings.xml instead of values/strings.xml?

Model answer

At run time the resource system matches the device configuration (here the locale set to Urdu) with folder qualifiers and picks the best match; missing strings fall back to the default values folder.

2. (Viva) What are the rules for naming resource files?

Model answer

Only lowercase letters a–z, digits 0–9 and underscores; the name must start with a letter and has no spaces or capitals (e.g. student_ali.jpg). Otherwise the build fails with an invalid resource name error.

3. (Viva) Differentiate between dp and sp.

Model answer

dp (density-independent pixel) keeps view sizes consistent across screen densities; sp (scale-independent pixel) is like dp but also scales with the user's font-size setting, so it is used for text sizes.

4. (Viva) Write code to change a TextView's text and an ImageView's image from resources when a button is clicked.

Model answer

Inside the button's setOnClickListener: tvName.setText(R.string.name_sara) and img.setImageResource(R.drawable.student_sara) (Java uses the same calls with semicolons).

This lesson ends with a quiz of 13 MCQs. Open the lesson on PrepOasis to take it and save your progress.

Previous: Week 3 Theory – Android OS – Architecture, Application Framework and Application Development · Next: Week 4 Theory – iOS – Architecture, Frameworks, App Lifecycle and Development Tools

All lessons in Mobile Application Development (CDE-315) – BS CS 5th Semester