Mobile App Development
Lab # 03
Submitted By
Aleena Qadir (RC-205)
Ansharah Aamir (RC-225)
Nagina Gul (RC-281)
Submitted To
Ms. Maryam Sajjad
Lab Activity:
Source Code:
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/heading"
android:layout_width="0dp"
android:layout_height="50dp"
android:background="#673AB7"
android:backgroundTint="#673AB7"
android:gravity="center_vertical"
android:text="Activity One"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<EditText
android:id="@+id/textMessage"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="120dp"
android:ems="10"
android:hint="@string/enter_your_message_here"
android:inputType="text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/heading"
app:layout_constraintVertical_bias="0.951" />
<Button
android:id="@+id/sendBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="28dp"
android:background="#A6A1A1"
android:backgroundTint="#A6A1A1"
android:text="@string/send"
android:textSize="20sp"
app:cornerRadius="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.448"
app:layout_constraintStart_toEndOf="@+id/textMessage" />
<TextView
android:id="@+id/receivedReply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.056"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/heading" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.myapplication;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
String ReplyMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
if(intent != null && intent.hasExtra("Reply")){
ReplyMessage = intent.getStringExtra("Reply");
}
EditText textMessage = findViewById(R.id.textMessage);
TextView receivedReply = findViewById(R.id.receivedReply);
receivedReply.setText(ReplyMessage);
Button sendMessage = findViewById(R.id.sendBtn);
sendMessage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String message = textMessage.getText().toString();
Intent intent = new Intent(MainActivity.this,
SecondActivity.class);
intent.putExtra("Message", message);
startActivity(intent);
}
});
}
}
SecondActivity.java
package com.example.myapplication;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class SecondActivity extends AppCompatActivity {
String ReceivedMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent = getIntent();
if(intent != null && intent.hasExtra("Message")){
ReceivedMessage = intent.getStringExtra("Message");
Log.d("SecondActivity", ReceivedMessage);
} else {
Toast.makeText(this, "Message Could Not Be Sent",
Toast.LENGTH_SHORT).show();
}
EditText textMessage = findViewById(R.id.textMessage);
TextView showMessage = (TextView)
findViewById(R.id.receivedMessage);
showMessage.setText(ReceivedMessage);
Button reply = findViewById(R.id.replyBtn);
reply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String message = textMessage.getText().toString();
Intent intent = new Intent(SecondActivity.this,
MainActivity.class);
intent.putExtra("Reply", message);
startActivity(intent);
}
});
}
}
ActivitySecond2.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SecondActivity">
<TextView
android:id="@+id/heading"
android:layout_width="0dp"
android:layout_height="50dp"
android:background="#673AB7"
android:backgroundTint="#673AB7"
android:gravity="center_vertical"
android:text="Activity Two"
android:textColor="@color/white"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<EditText
android:id="@+id/textMessage"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="120dp"
android:ems="10"
android:hint="@string/enter_your_message_here"
android:inputType="text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/heading"
app:layout_constraintVertical_bias="0.951" />
<Button
android:id="@+id/replyBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="28dp"
android:background="#A6A1A1"
android:backgroundTint="#A6A1A1"
android:text="@string/send"
android:textSize="20sp"
app:cornerRadius="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.448"
app:layout_constraintStart_toEndOf="@+id/textMessage" />
<TextView
android:id="@+id/receivedMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:textSize="20sp"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.056"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/heading" />
</androidx.constraintlayout.widget.ConstraintLayout>
Lab Task:
Source Code:
MainActivity.java
package com.example.lab3task2;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Fetching Buttons
Button textOne = (Button) findViewById(R.id.textOneBtn);
Button textTwo = (Button) findViewById(R.id.textTwoBtn);
Button textThree = (Button) findViewById(R.id.textThreeBtn);
//Text One Button Action Listener
textOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,
PassagesActivity.class);
intent.putExtra("Activity", "PassageOne");
startActivity(intent);
}
});
//Text Two Button Action Listener
textTwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,
PassagesActivity.class);
intent.putExtra("Activity", "PassageTwo");
startActivity(intent);
}
});
//Text Three Button Action Listener
textThree.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,
PassagesActivity.class);
intent.putExtra("Activity", "PassageThree");
startActivity(intent);
}
});
}
}
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lab Assignment"
android:textSize="34sp"
android:textStyle="bold|italic"
app:layout_constraintBottom_toTopOf="@+id/textOneBtn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/textOneBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text_one"
android:textSize="24sp"
app:cornerRadius="0dp"
android:paddingStart="35dp"
android:paddingEnd="35dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:backgroundTint="#088008"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.401" />
<Button
android:id="@+id/textTwoBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text_two"
android:textSize="24sp"
app:cornerRadius="0dp"
android:paddingStart="35dp"
android:paddingEnd="35dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:backgroundTint="#076B87"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textOneBtn"
app:layout_constraintVertical_bias="0.052" />
<Button
android:id="@+id/textThreeBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text_three"
android:textSize="24sp"
app:cornerRadius="0dp"
android:paddingStart="27dp"
android:paddingEnd="27dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textTwoBtn"
app:layout_constraintVertical_bias="0.078" />
</androidx.constraintlayout.widget.ConstraintLayout>
SecondActivity PassageActivity.java:
package com.example.lab3task2;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class PassagesActivity extends AppCompatActivity {
String textNo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_passages);
Intent intent = getIntent();
if(intent != null && intent.hasExtra("Activity")){
textNo = intent.getStringExtra("Activity");
} else {
Toast.makeText(this, "Message Could Not Be Sent",
Toast.LENGTH_SHORT).show();
}
TextView heading = (TextView) findViewById(R.id.heading);
heading.setText(textNo);
TextView textPassage = (TextView) findViewById(R.id.textView2);
if(textNo.equals("PassageOne")){
textPassage.setText(R.string.PassageOne);
} else if (textNo.equals("PassageTwo")) {
textPassage.setText(R.string.PassageTwo);
} else if (textNo.equals("PassageThree")){
textPassage.setText(R.string.PassageThree);
}
}
}
SecondActivity PakageActivity.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".PassagesActivity">
<TextView
android:id="@+id/heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="28sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ScrollView
android:id="@+id/scrollView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="40dp"
tools:layout_editor_absoluteY="40dp">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>