Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Mobile application Development

Lab # 2

Submitted by
RIFFAQAT HUSSAIN

Submitted to

Ms. Anum Asghar

Faculty of Engineering & Computer Sciences

Department of BSIT-Morning

6th Semester

National University of Modern Languages, Islamabad

Page 1 of 6
Tasks:

Xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal">

<Button
android:id="@+id/button_count"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Count"
android:layout_marginBottom="20dp" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="560dp"
android:orientation="horizontal"
android:gravity="center_horizontal">

<TextView
android:id="@+id/textView_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="48sp"
android:textStyle="bold"
android:layout_gravity="center"
android:layout_marginEnd="8dp" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">

<Button
android:id="@+id/button_toast"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show Toast"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp" />

</LinearLayout>

</LinearLayout>

Page 2 of 6
Java code:
package com.example.lab2;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private int count = 0;


private TextView textViewCount;

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

Button buttonToast = findViewById(R.id.button_toast);


Button buttonCount = findViewById(R.id.button_count);
textViewCount = findViewById(R.id.textView_count);

buttonToast.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showToast();
}
});

buttonCount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
count++;
displayCount();
}
});
}

private void showToast() {


Toast.makeText(this, "Hello Toast!", Toast.LENGTH_SHORT).show();
}

private void displayCount() {


textViewCount.setText(String.valueOf(count));
}
}

Page 3 of 6
screenshots:

After clicking buttons:

Page 4 of 6
Lab assignment

Change layout :

Xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="300dp"
android:orientation="vertical"
android:gravity="start">

<Button
android:id="@+id/button_toast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Toast"
android:layout_marginTop="20dp" />

<Button
android:id="@+id/button_count"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="160dp"

android:text="Count" />

</LinearLayout>

<TextView
android:id="@+id/textView_count"
android:layout_width="wrap_content"
android:layout_height="120dp"
android:text="0"
android:textSize="48sp"
android:textStyle="bold"
android:layout_gravity="center"
android:layout_marginStart="8dp" />

</LinearLayout>

Page 5 of 6
Screenshot

Page 6 of 6

You might also like