0% found this document useful (0 votes)
51 views

Lab03: Working With Layouts and Toast Messages: Question No. 1

The document describes code for a mobile application lab assignment involving layouts and toast messages. It includes 4 questions: 1) Adding a text view and button to an activity, 2) Changing to a relative layout, 3) Displaying a toast message on button click, and 4) Adding another activity to display two toast messages with customized colors on button click. Code snippets are provided for each question to demonstrate the implementation.

Uploaded by

Johnny
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Lab03: Working With Layouts and Toast Messages: Question No. 1

The document describes code for a mobile application lab assignment involving layouts and toast messages. It includes 4 questions: 1) Adding a text view and button to an activity, 2) Changing to a relative layout, 3) Displaying a toast message on button click, and 4) Adding another activity to display two toast messages with customized colors on button click. Code snippets are provided for each question to demonstrate the implementation.

Uploaded by

Johnny
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Saad Baig 02-134181-147

Lab03: Working with Layouts and Toast Messages


Question No. 1 :
Start a new project and add a textView and Button on the main activity.

Question No. 2 :
Change the default relative layout to relative layout from xml view of the main activity.

Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout app:layout_constraintBottom_toBottomOf="parent"
xmlns:android="http://schemas.android.com/apk/res app:layout_constraintLeft_toLeftOf="parent"
/android"
xmlns:app="http://schemas.android.com/apk/res- app:layout_constraintRight_toRightOf="parent"
auto" app:layout_constraintTop_toTopOf="parent"
xmlns:tools="http://schemas.android.com/tools" app:layout_constraintVertical_bias="0.499" />
android:layout_width="match_parent"
android:layout_height="match_parent" <Button
tools:context=".MainActivity"> android:id="@+id/button"
android:layout_width="110dp"
<TextView android:layout_height="64dp"
android:id="@+id/textView" android:layout_alignParentEnd="true"
android:layout_width="181dp" android:layout_alignParentBottom="true"
android:layout_height="70dp" android:layout_marginEnd="159dp"
android:layout_above="@+id/button" android:layout_marginBottom="191dp"
android:layout_alignParentEnd="true" android:text="Button"
android:layout_marginEnd="126dp"
android:layout_marginBottom="127dp"
app:layout_constraintBottom_toBottomOf="parent"
android:text="Hello World!"
app:layout_constraintEnd_toEndOf="parent"
android:textSize="30dp"
app:layout_constraintHorizontal_bias="0.458"
app:layout_constraintStart_toStartOf="parent"

Miss Ambreen Akram Mobile Application Development Lab No 3


Saad Baig 02-134181-147

app:layout_constraintTop_toBottomOf="@+id/textV
iew" </RelativeLayout>
app:layout_constraintVertical_bias="0.187" />

Output :

Question No 3.
Display a “Toast message” in the button click event.
Code :
package com.saadbaig.layoutandtoast; protected void onCreate(Bundle
savedInstanceState) {
import androidx.appcompat.app.AppCompatActivity; super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
import android.os.Bundle; btn = (Button) findViewById(R.id.button);
import android.view.View; btn.setOnClickListener(new
import android.widget.Button; View.OnClickListener() {
import android.widget.Toast; @Override
public void onClick(View v) {
public class MainActivity extends AppCompatActivity Toast.makeText(MainActivity.this, "Talha
{ Munawar", Toast.LENGTH_SHORT).show();
}
Button btn; });
@Override }
}

Miss Ambreen Akram Mobile Application Development Lab No 3


Saad Baig 02-134181-147

Miss Ambreen Akram Mobile Application Development Lab No 3


Saad Baig 02-134181-147

Question No. 4
Add another activity and display at least two “Toast Messages” of your own choice on Button click and
changing the background colour.
Code :
package com.saadbaig.layoutandtoast; Toast", Toast.LENGTH_SHORT);
View view = toast_1.getView();
import androidx.appcompat.app.AppCompatActivity;
view.getBackground().setColorFilter(Color.parseCol
import android.graphics.Color; or("#210070"), PorterDuff.Mode.SRC_IN);
import android.graphics.PorterDuff; TextView text =
import android.os.Bundle; view.findViewById(android.R.id.message);
import android.view.View;
import android.widget.Button; text.setTextColor(Color.parseColor("#fffeee"));
import android.widget.TextView; toast_1.show();
import android.widget.Toast;
Toast toast_2 =
public class MainActivity extends Toast.makeText(MainActivity.this, "This is a second
AppCompatActivity { Toast", Toast.LENGTH_LONG);
view = toast_2.getView();
Button btn;
@Override
view.getBackground().setColorFilter(Color.parseCol
protected void onCreate(Bundle
or("#210070"), PorterDuff.Mode.SRC_IN);
savedInstanceState) {
text =
super.onCreate(savedInstanceState);
view.findViewById(android.R.id.message);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new text.setTextColor(Color.parseColor("#fffeee"));
View.OnClickListener() { toast_2.show();
@Override }
public void onClick(View v) { });
Toast toast_1 = }
Toast.makeText(MainActivity.this, "This is first }

Miss Ambreen Akram Mobile Application Development Lab No 3


Saad Baig 02-134181-147

Miss Ambreen Akram Mobile Application Development Lab No 3


Saad Baig 02-134181-147

Miss Ambreen Akram Mobile Application Development Lab No 3

You might also like