0% found this document useful (0 votes)
117 views6 pages

Android Layouts and Toast Messages Guide

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
117 views6 pages

Android Layouts and Toast Messages Guide

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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="[Link] app:layout_constraintLeft_toLeftOf="parent"
/android"
xmlns:app="[Link] app:layout_constraintRight_toRightOf="parent"
auto" app:layout_constraintTop_toTopOf="parent"
xmlns:tools="[Link] 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 [Link]; protected void onCreate(Bundle
savedInstanceState) {
import [Link]; [Link](savedInstanceState);
setContentView([Link].activity_main);
import [Link]; btn = (Button) findViewById([Link]);
import [Link]; [Link](new
import [Link]; [Link]() {
import [Link]; @Override
public void onClick(View v) {
public class MainActivity extends AppCompatActivity [Link]([Link], "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 [Link]; Toast", Toast.LENGTH_SHORT);
View view = toast_1.getView();
import [Link];
[Link]().setColorFilter([Link]
import [Link]; or("#210070"), [Link].SRC_IN);
import [Link]; TextView text =
import [Link]; [Link]([Link]);
import [Link];
import [Link]; [Link]([Link]("#fffeee"));
import [Link]; toast_1.show();
import [Link];
Toast toast_2 =
public class MainActivity extends [Link]([Link], "This is a second
AppCompatActivity { Toast", Toast.LENGTH_LONG);
view = toast_2.getView();
Button btn;
@Override
[Link]().setColorFilter([Link]
protected void onCreate(Bundle
or("#210070"), [Link].SRC_IN);
savedInstanceState) {
text =
[Link](savedInstanceState);
[Link]([Link]);
setContentView([Link].activity_main);
btn = (Button) findViewById([Link]);
[Link](new [Link]([Link]("#fffeee"));
[Link]() { toast_2.show();
@Override }
public void onClick(View v) { });
Toast toast_1 = }
[Link]([Link], "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