You are on page 1of 11

Software Applications for

Mobile Devices
Lab Journal 4

Name: Mahnoor Abdul Razzaq


Enrollment No: 01-131182-041
Class: BSE-6A
Instructor: Sir Waleed

DEPARTMENT OF SOFTWARE ENGINEERING

BAHRIA UNIVERSITY
ISLAMABAD CAMPUS
Title:

Passing extra items with Intents


Tools Used:

• Android Studio
Lab Task 1:
Create an application that will launch a new activity which will display the text “Second
Activity” when a user clicks a button on the app landing activity.
Solution:
Main activity:
package com.example.lab4;
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 {
private Button btnSecondActive;
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSecondActive = (Button) findViewById(R.id.btnSecondActive);
btnSecondActive.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intent = new Intent(MainActivity.this, MainActivity2.class);
startActivity(intent);
}
});
}
}

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">
<RelativeLayout
android:layout_width="match_parent"

android:layout_height="match_parent">
<Button
android:id="@+id/btnSecondActive"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
android:text="Second Activity" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Second Activity:
package com.example.lab4;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
}

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=".MainActivity2">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_centerHorizontal="true"
android:text="Second Activity" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Lab Task 2:
Create a view for entering two values along with a button to send the values.
• Keep overall layout of the view as ‘RelativeLayout’.
• At the top of the view, insert a ‘TextView’ that has width equal the width of the screen.
• Below this text view insert another ‘TextView’ and an ‘EditView’ both at the same height
and each covering 50% of the width of the screen.
• Repeat the above step once more.
• Add the button below the rest of the controls and set its width equal to the width of the
screen.
Solution:

<?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">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/lt2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Please Required Information"
android:textColor="#000000"
android:textSize="18sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:text="First Value :"
android:textColor="#000000"
android:textSize="16sp" />
<EditText
android:id="@+id/editTextTextPersonName"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView4"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="16sp"
android:layout_marginTop="5dp"
android:text="Second Value:" />
<EditText
android:id="@+id/editTextTextPersonName2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"

android:ems="10"
android:inputType="textPersonName" />
</LinearLayout>
</LinearLayout>
<Button
android:id="@+id/btnSecondActive"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/lt2"
android:layout_centerInParent="true"
android:layout_marginTop="10dp"
android:text="OK" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Lab Task 3:
Create a new activity.
• Pass the values given by user in the first activity to the second activity.
• Now in the second activity print the table of the numbers input by the user
Main Activity:
package com.example.lab4;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button btnSecondActive;
private EditText et1, et2;
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1 = (EditText) findViewById(R.id.edit1);
et2 = (EditText) findViewById(R.id.edit2);
btnSecondActive = (Button) findViewById(R.id.btnSecondActive);
btnSecondActive.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Integer temp1, temp2;
temp1 = Integer.parseInt(et1.getText().toString());
temp2 = Integer.parseInt(et2.getText().toString());
Intent i = new Intent(MainActivity.this, MainActivity2.class);
i.putExtra("first", temp1);
i.putExtra("second", temp2);
startActivity(i);
}
});
}
}

XML:
<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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/lt2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Please Enter 2 numbers"
android:textColor="#000000"
android:textSize="18sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:text="First Value :"
android:textColor="#000000"
android:textSize="16sp" />
<EditText
android:id="@+id/edit1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView4"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="16sp"
android:layout_marginTop="5dp"
android:text="Second Value:" />
<EditText
android:id="@+id/edit2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content”

android:ems="10"
android:inputType="textPersonName" />
</LinearLayout>
</LinearLayout>
<Button
android:id="@+id/btnSecondActive"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/lt2"
android:layout_centerInParent="true"
android:layout_marginTop="10dp"
android:text="OK" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Second Activity:
package com.example.lab4;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.widget.TextView;
public class MainActivity2 extends AppCompatActivity {
private TextView txtResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
txtResult = (TextView) findViewById(R.id.Result);
Intent i = getIntent();
int temp1 = i.getIntExtra("first",0);
int temp2 = i.getIntExtra("second",17);
for (int j =1; j<=temp2; j++){
int temp3 = temp1 * j;
txtResult.append(Integer.toString(temp1)+"*"+ Integer.toString(j)+"= "+
Integer.toString(temp3)+'\n');
}
}
}
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=".MainActivity2">
<RelativeLayout
android:layout_width="match_parent"

android:layout_height="match_parent">
<TextView
android:id="@+id/Result"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingLeft="10pt"
android:paddingTop="10pt"
android:textColor="#000000"
android:textSize="20sp" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Output:

Conclusion:
In this lab we have performed the assigned tasks.

You might also like