0% found this document useful (0 votes)
24 views33 pages

Installing Android Studio Guide

Uploaded by

Ashish Hooda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views33 pages

Installing Android Studio Guide

Uploaded by

Ashish Hooda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Name: - Aashish Hooda Roll No.

: - 01
Course: -BCA(5 )
th
Sec.:- G1
Subject:- Android Programming Lab (PBC501)

QUES1: - Show step by step procedure to install android studio.


EXPLANATION: - Steps for the downloading of the ANDRIOD STUDIO for the very first time.
Steps to Install Android Studio on Windows Step

1: Go to [Link] website.

Step 2: Click on the Download Android Studio Button.

Step 3: After the downloading has finished, open the file from downloads and run it. It will
prompt the following dialog box.

1
Name: - Aashish Hooda Roll No.: - 01
Course: -BCA(5 )
th
Sec.:- G1
Subject:- Android Programming Lab (PBC501)

Step 4: It will start the installation, and once it is completed, it will be like the image shown
below.

STEP 5:- DOWNLOAD ALL THE COMPONENTS OF THE ANDRIOD.

STEP 6: - CHOOSE THE JAVA LANGUAGE FOR THE FIRST EMPTY VIEWS ACTIVITY

2
Name: - Aashish Hooda Roll No.: - 01
Course: -BCA(5 )
th
Sec.:- G1
Subject:- Android Programming Lab (PBC501)

Click on Start a new Android Studio project to build a new app

3
Name: - Aashish Hooda Roll No.: - 01
Course: -BCA(5 )
th
Sec.:- G1
Subject:- Android Programming Lab (PBC501)

QUES2: -Create a GitHub repository to push your project into the repository.

4
Name: - Aashish Hooda Roll No.: - 01
Course: -BCA(5 )
th
Sec.:- G1
Subject:- Android Programming Lab (PBC501)

5
Name: - Aashish Hooda Roll No.: - 01
Course: -BCA(5 )
th
Sec.:- G1
Subject:- Android Programming Lab (PBC501)

QUES 3: -Develop an android application to display a message WELCOME TO


GRAPHIC ERA on a button click.
EXPLANATION: - This simple application demonstrates how to interact with UI
components in Android. When the user clicks the button, the app displays a specified text,
showing how to manage visibility and update the UI dynamically.

#XML CODE: -
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
id" xmlns:app="[Link]
xmlns:tools="[Link]
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/e1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="124dp"
android:ems="10"
android:inputType="text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="328dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</[Link]>

# JAVA CODE:-
package [Link];

6
Name: - Aashish Hooda Roll No.: - 01
Course: -BCA(5 )
th
Sec.:- G1
Subject:- Android Programming Lab (PBC501)

import [Link];
import [Link];
import [Link];
import
[Link];
import
[Link];

import [Link]; import


[Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends


AppCompatActivity { private TextView t1; private
Button b1; @Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
[Link](this);
setContentView([Link].activity_main);
t1=(TextView) findViewById([Link].e1); b1=(Button)
findViewById([Link].b1);
[Link](new [Link]() {
@Override
public void onClick(View view) {
[Link]("welcome to GRAPHIC ERA");

}
});

[Link](findViewById([Link]), (v, insets) -


>{
Insets systemBars = [Link]([Link]());
[Link]([Link], [Link], [Link], [Link]);
return insets;
});
}

7
Name: - Aashish Hooda Roll No.: - 01
Course: -BCA(5 )
th
Sec.:- G1
Subject:- Android Programming Lab (PBC501)

OUTPUT :-

8
Name: - Aashish Hooda Roll No.: - 01
Course: -BCA(5 )
th
Sec.:- G1
Subject:- Android Programming Lab (PBC501)

QUES 4: - Create an android application to change background colour on


button click.
EXPLANATION: -
This application demonstrates basic user interaction in Android by changing the button's
background color upon clicking.

▪ The changeBackgroundColor() method generates a random color using the Random


class.
▪ [Link](int red, int green, int blue) creates a color based on random RGB values
(each between 0 and 255).
▪ We then set this color as the button's background using
setBackgroundColor(color).

XML CODE: -
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link] android:id="@+id/main"
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context=".MainActivity">

<Button android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="36dp"
android:layout_marginBottom="268dp"
android:text="BLUE"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<Button android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="84dp"
android:text="GREEN"
android:textSize="20sp"
app:layout_constraintBaseline_toBaselineOf="@+id/b1"
app:layout_constraintEnd_toEndOf="parent" />

9
Name: - Aashish Hooda Roll No.: - 01
Course: -BCA(5 )
th
Sec.:- G1
Subject:- Android Programming Lab (PBC501)

<Button android:id="@+id/b3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_marginTop="52dp"
android:singleLine="true"
android:text="RED"
android:textSize="20sp"
app:layout_constraintStart_toEndOf="@+id/b1"
app:layout_constraintTop_toBottomOf="@+id/b1" />
</[Link]>

JAVA CODE: -

package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

import [Link]; import


[Link]; import
[Link];
import [Link]; import
[Link];
import [Link];

public class MainActivity extends AppCompatActivity {


private Button bb1;
private Button bb2; private
Button bb3;
ConstraintLayout cl;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState); [Link](this);
setContentView([Link].activity_main);
bb1=(Button)findViewById([Link].b1);

10
Name: - Aashish Hooda Roll No.: - 01
Course: -BCA(5 )
th
Sec.:- G1
Subject:- Android Programming Lab (PBC501)

bb2=(Button)findViewById([Link].b2);
bb3=(Button)findViewById([Link].b3);
cl=(ConstraintLayout)findViewById([Link]);
[Link](new [Link]() {

@Override
public void onClick(View view) {
[Link]([Link]);
}
});
[Link](new [Link]() {
@Override public
void onClick(View view) {
[Link]([Link]);
}
});

OUTPUT:-

11
Name: - Aashish Hooda Roll No.: - 01
Course: -BCA(5 )
th
Sec.:- G1
Subject:- Android Programming Lab (PBC501)

QUES 5: - Create an android application to add two numbers.


Step 1: Set Up Your Project
1. Open Android Studio and create a new project.
2. Choose the Empty Activity template and name your project (e.g.,
"AddTwoNumbers").
Step 2: Design the Layout
Open activity_main.xml in the res/layout directory and define the layout using two
EditText fields for number input, a Button to perform the addition, and a TextView to
display the result.
<RelativeLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">

<EditText
android:id="@+id/editTextNumber1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter first number"
android:inputType="numberDecimal"/>

<EditText
android:id="@+id/editTextNumber2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter second number"
android:inputType="numberDecimal"
android:layout_below="@id/editTextNumber1"

12
Name: - Aashish Hooda Roll No.: - 01
Course: -BCA(5 )
th
Sec.:- G1
Subject:- Android Programming Lab (PBC501)

android:layout_marginTop="16dp"/>

<Button
android:id="@+id/buttonAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:layout_below="@id/editTextNumber2"
android:layout_marginTop="16dp"/>

<TextView
android:id="@+id/textViewResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonAdd"
android:layout_marginTop="16dp"
android:textSize="18sp"/>
</RelativeLayout>
Step 3: Implement Logic in MainActivity
Open [Link] and implement the logic to perform the addition.
package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

13
Name: - Aashish Hooda Roll No.: - 01
Course: -BCA(5 )
th
Sec.:- G1
Subject:- Android Programming Lab (PBC501)

public class MainActivity extends AppCompatActivity {

private EditText editTextNumber1;


private EditText editTextNumber2;
private TextView textViewResult;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

editTextNumber1 = findViewById([Link].editTextNumber1);
editTextNumber2 = findViewById([Link].editTextNumber2);
textViewResult = findViewById([Link]);
Button buttonAdd = findViewById([Link]);

[Link](new [Link]() {
@Override
public void onClick(View v) {
String number1 = [Link]().toString();
String number2 = [Link]().toString();

if (![Link]() && ![Link]()) {


double sum = [Link](number1) +
[Link](number2);
[Link]("Result: " + sum);
} else {
[Link]("Please enter both numbers.");
}

14
Name: - Aashish Hooda Roll No.: - 01
Course: -BCA(5 )
th
Sec.:- G1
Subject:- Android Programming Lab (PBC501)

}
});
}
}
Running the App
1. Connect your device or use an emulator.
2. Run the application.
3. Input two numbers and tap the "Add" button to see the result.
This app serves as a simple introduction to Android development, covering UI design,
user input handling, and basic arithmetic operations.

OUTPUT: -

15
Name: - Aashish Hooda Roll No.: - 01
Course: -BCA(5 )
th
Sec.:- G1
Subject:- Android Programming Lab (PBC501)

QUES 6: - Create an Android application of calculator


EXPLANATION: -
▪ The method retrieves the values from the EditText fields.
▪ It checks if both inputs are provided and performs the requested arithmetic
operation.
▪ The result is displayed in the TextView. If division by zero is attempted, an
appropriate message is shown.

XML CODE: -
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
id" xmlns:app="[Link]
xmlns:tools="[Link]
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<EditText
android:id="@+id/n1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="73dp" android:layout_marginTop="110dp"
android:ems="10"
android:inputType="number"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText android:id="@+id/n2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="72dp"
android:layout_marginTop="64dp"
android:ems="10"
android:inputType="number"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/n1" />

<Button
android:id="@+id/b1"
16
Name: - Aashish Hooda Roll No.: - 01
Course: -BCA(5 )
th
Sec.:- G1
Subject:- Android Programming Lab (PBC501)

android:layout_width="94dp"
android:layout_height="67dp"
android:layout_marginStart="16dp"
android:text="+"
android:textSize="34sp"
app:layout_constraintBaseline_toBaselineOf="@+id/b2"
app:layout_constraintStart_toStartOf="parent" />

<Button android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginBottom="20dp"
android:text="-"
android:textSize="34sp"
app:layout_constraintBottom_toTopOf="@+id/b4"
app:layout_constraintStart_toEndOf="@+id/b1" />

<Button android:id="@+id/b3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="52dp"
android:text="*"
android:textSize="34sp"
app:layout_constraintBaseline_toBaselineOf="@+id/b2"
app:layout_constraintEnd_toEndOf="parent" />

<Button android:id="@+id/b4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginBottom="32dp"
android:text="%"
android:textSize="34sp"
app:layout_constraintBottom_toTopOf="@+id/t1"
app:layout_constraintStart_toStartOf="parent" /> <TextView
android:id="@+id/t1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_marginStart="28dp"
android:layout_marginBottom="164dp"
android:text="result"
android:textSize="20sp"

17
Name: - Aashish Hooda Roll No.: - 01
Course: -BCA(5 )
th
Sec.:- G1
Subject:- Android Programming Lab (PBC501)

app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</[Link]
>

JAVA CODE: -
package [Link];

import [Link]; import


[Link];
import [Link]; import
[Link]; import
[Link];
import [Link];

import [Link]; import


[Link];
import [Link]; import
[Link]; import
[Link];

public class MainActivity extends AppCompatActivity {


private Button bb1;
private Button bb2; private
Button bb3; private Button
bb4; private EditText nn1;
private EditText nn2;
private TextView tt1; @Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState); [Link](this);
setContentView([Link].activity_main);
bb1=(Button)findViewById([Link].b1);
bb2=(Button)findViewById([Link].b2);
bb3=(Button)findViewById([Link].b3);
bb4=(Button)findViewById([Link].b4);
nn1=(EditText)findViewById([Link].n1);
nn2=(EditText)findViewById([Link].n2);
tt1=(TextView)findViewById([Link].t1);
[Link](new [Link]() {
@Override
public void onClick(View view) {
int

18
Name: - Aashish Hooda Roll No.: - 01
Course: -BCA(5 )
th
Sec.:- G1
Subject:- Android Programming Lab (PBC501)

num1=[Link]([Link]().toString());
int num2=[Link]([Link]().toString());
int sum=num1+num2;
[Link]("answer:"+[Link](sum));

}
});

[Link](new [Link]() {
@Override
public void onClick(View view) {
int
num1=[Link]([Link]().toString());
int num2=[Link]([Link]().toString());
int sum=num1-num2;
[Link]("answer:"+[Link](sum));
}
});
[Link](new [Link]() {
@Override
public void onClick(View view) {
int
num1=[Link]([Link]().toString());
int num2=[Link]([Link]().toString());
int sum=num1*num2;
[Link]("answer:"+[Link](sum));
}

});
[Link](new [Link]() { @Override
public void onClick(View view) {
int
num1=[Link]([Link]().toString());
int num2=[Link]([Link]().toString());
int sum=num1%num2;
[Link]("answer:"+[Link](sum));
}

});
[Link](findViewById([Link]), (v, insets) -> {
Insets systemBars = [Link]([Link]());

19
Name: - Aashish Hooda Roll No.: - 01
Course: -BCA(5 )
th
Sec.:- G1
Subject:- Android Programming Lab (PBC501)

[Link]([Link], [Link], [Link], [Link]);


return insets;
});
}
}

OUTPUT: -

20
Name: - Aashish Hooda Roll No.: - 01
Course: -BCA(5 )
th
Sec.:- G1
Subject:- Android Programming Lab (PBC501)

QUES 7: - Create an android application to set the image-on-image view.


EXPLANATION: -
Step 1: Set Up Your Project
1. Open Android Studio and create a new project with an Empty Activity.
2. Name your project and choose your preferred settings.
Step 2: Update Layout
Open activity_main.xml and define the layout with an ImageView and a Button.
<RelativeLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/sample_image" /> <!-- Initial image -->

<Button
android:id="@+id/buttonSetImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set Image"
android:layout_below="@id/imageView"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"/>
</RelativeLayout>
Step 3: Add Image Resource
Place your new image (e.g., new_image.png) in the res/drawable folder.
Step 4: Update MainActivity

21
Name: - Aashish Hooda Roll No.: - 01
Course: -BCA(5 )
th
Sec.:- G1
Subject:- Android Programming Lab (PBC501)

In [Link] (or [Link]), add the logic to change the image when the button is
clicked.

▪ The state is toggled each time the button is clicked. JAVA CODE: -
package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {

private ImageView imageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

imageView = findViewById([Link]);
Button buttonSetImage = findViewById([Link]);

[Link](v ->
[Link]([Link].new_image)); // Change image
}
}
Step 5: Run Your App
1. Connect your device or use an emulator.
2. Click the "Run" button in Android Studio.
3. Click the button in the app to change the image in the ImageView.
This basic app demonstrates setting an image programmatically in an Android application. You
can further enhance it with image selection from the gallery or network loading libraries like
Glide or Picasso.
OUTPUT: -

22
Name: - Aashish Hooda Roll No.: - 01
Course: -BCA(5 )
th
Sec.:- G1
Subject:- Android Programming Lab (PBC501)

Ques 8: - Create a program to depict android activity lifecycle.


EXPLANATION: -
This application illustrates the Android Activity lifecycle by updating the UI to reflect the
current state of the activity. As you navigate through different states (e.g., launching the
app, pressing the home button, returning to the app), you can observe how the lifecycle
methods are triggered and how the UI responds.
Each lifecycle method (onCreate), (onStart), (onResume), (onPause), (onStop), (onDestroy),
and (onRestart) is overridden to update the TextView with the corresponding method name
whenever the activity transitions to that state.

XML CODE: -
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
id" xmlns:app="[Link]
xmlns:tools="[Link]
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</[Link]>

23
Name: - Aashish Hooda Roll No.: - 01
Course: -BCA(5 )
th
Sec.:- G1
Subject:- Android Programming Lab (PBC501)

#JAVA CODE:-

package [Link]; import


[Link]; import
[Link]; import
[Link]; import [Link];
import [Link]; import
[Link]; import
[Link]; import
[Link];
import [Link]; import
[Link]; import
[Link]; public
class MainActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle
savedInstanceState) {
[Link](savedInstanceState);
[Link](this);
setContentView([Link].activity_main);
Log.d("TAG","oncreate:I AM RUNNING");
[Link]([Link],"onCreate",Toast.LENGTH_LONG);
[Link](findViewById([Link]), (v, insets) -> {
Insets systemBars = [Link]([Link]());
[Link]([Link], [Link], [Link], [Link]);
return insets;
});

}
@Override
protected void onStart() {
[Link]();
Log.d("TAG","onStart:I AM RUNNING");
[Link]([Link],"onStart",Toast.LENGTH_LONG);

24
Name: - Aashish Hooda Roll No.: - 01
Course: -BCA(5 )
th
Sec.:- G1
Subject:- Android Programming Lab (PBC501)

@Override
protected void onResume() {
[Link]();
Log.d("TAG","onresume:I AM RUNNING");
[Link]([Link],"onResume",Toast.LENGTH_LONG)
@Override
protected void onPause() {
[Link]();
Log.d("tag","onPause:i am running");
[Link]([Link],"onPause",Toast.LENGTH_LONG);
}
@Override
protected void onStop()
{ [Link]();
Log.d("TAG","onstop: I AM RUNNING");
[Link]([Link],"OnStop",Toast.LENGTH_LONG);
}

@Override
protected void onRestart() {
[Link]();
Log.d("TAG","onRESTART: I AM RUNNING");
[Link]([Link],"OnRestart",Toast.LENGTH_LONG);
}
@Override
protected void onDestroy() {
[Link]();
Log.d("TAG","onDESTORY: I AM RUNNING");
[Link]([Link],"ondestory",Toast.LENGTH_LONG);

OUTPUT:-

25
Name: - Aashish Hooda Roll No.: - 01
Course: -BCA(5 )
th
Sec.:- G1
Subject:- Android Programming Lab (PBC501)

QUES 9-- Create a program to show implicit intent.


Step 1: Set Up Your Project
1. Open Android Studio and create a new project.
2. Choose the Empty Activity template and name your project (e.g.,
"ImplicitIntentExample").
Step 2: Create the Layout for MainActivity
Open activity_main.xml and define the layout with a Button that, when clicked, will open
a web page using an implicit intent.
<RelativeLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">

<Button
android:id="@+id/buttonOpenWeb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open Web Page"
android:layout_centerInParent="true"/>
</RelativeLayout>
Step 3: Implement Logic in MainActivity
In [Link], implement the implicit intent to open a web browser.
package [Link];
26
Name: - Aashish Hooda Roll No.: - 01
Course: -BCA(5 )
th
Sec.:- G1
Subject:- Android Programming Lab (PBC501)

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

Button buttonOpenWeb = findViewById([Link]);


[Link](new [Link]() {
@Override
public void onClick(View v) {
// Create an implicit intent to open a web page
Intent intent = new Intent(Intent.ACTION_VIEW);
[Link]([Link]("[Link] // URL to open
startActivity(intent); // Start the intent
}
});
}
}
Explanation
● Implicit Intent: The intent does not specify a specific component; instead, it indicates an
action (ACTION_VIEW) and provides data (the URL).
● Handling Actions: When the button is clicked, the intent triggers the default web browser
to open the specified URL.
● Flexibility: This approach allows the user to choose any suitable application installed on
their device that can handle the action, such as a web browser or an app.
Running the App
1. Connect your device or use an emulator.
2. Run the application.
3. Tap the button to open the specified web page in your default web browser.
This example illustrates how to use implicit intents to interact with other applications and
services on an Android device.

27
Name: - Aashish Hooda Roll No.: - 01
Course: -BCA(5 )
th
Sec.:- G1
Subject:- Android Programming Lab (PBC501)

OUTPUT: -

QUES 10-- Create a program to show explicit intent.


Step 1: Set Up Your Project
1. Open Android Studio and create a new project.
2. Choose the Empty Activity template and name your project (e.g.,
"ExplicitIntentExample").
Step 2: Create the Layout for MainActivity
Open activity_main.xml and define the layout with a Button that, when clicked, will
launch a second activity.
<RelativeLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">

<Button
android:id="@+id/buttonLaunchSecondActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Launch Second Activity"
android:layout_centerInParent="true"/>
</RelativeLayout>
Step 3: Create Second Activity
Create a new Activity named [Link]. Right-click on your package, select
New > Activity > Empty Activity, and name it SecondActivity.
activity_second.xml (Layout for Second Activity):

28
Name: - Aashish Hooda Roll No.: - 01
Course: -BCA(5 )
th
Sec.:- G1
Subject:- Android Programming Lab (PBC501)

<RelativeLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">

<TextView
android:id="@+id/textViewMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to the Second Activity!"
android:textSize="24sp"
android:layout_centerInParent="true"/>
</RelativeLayout>
Step 4: Implement Logic in MainActivity
In [Link], implement the explicit intent to launch SecondActivity.
package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

Button buttonLaunch = findViewById([Link]);


[Link](new [Link]() {
@Override
public void onClick(View v) {
Intent intent = new Intent([Link], [Link]);
startActivity(intent); // Launch SecondActivity
}
});
}
}
Step 5: Implement Second Activity
In [Link], you can simply set the content view.

29
Name: - Aashish Hooda Roll No.: - 01
Course: -BCA(5 )
th
Sec.:- G1
Subject:- Android Programming Lab (PBC501)

package [Link];

import [Link];
import [Link];

public class SecondActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_second);
}
}
Explanation
● Explicit Intent: The intent explicitly specifies the target activity (SecondActivity) to
launch.
● Button Click: When the button in MainActivity is clicked, it triggers the intent, launching
the SecondActivity.
● Activity Lifecycle: Each activity can manage its lifecycle and UI independently.
Running the App
1. Connect your device or use an emulator.
2. Run the application.
3. Tap the button to navigate to the second activity and see the welcome message.
This example illustrates how to use explicit intents to navigate between activities in an Android
application.

OUTPUT: -

30
Name: - Aashish Hooda Roll No.: - 01
Course: -BCA(5 )
th
Sec.:- G1
Subject:- Android Programming Lab (PBC501)

QUES 13: - Develop an android application to display the toast message on button
click.
EXPLANATION:-
The [Link](...) method is used to create a Toast message. It takes three
parameters:
• The context (in this case, [Link]).
• The message to display ("Hello, this is a Toast message!").
• The duration for which the Toast should be displayed (Toast.LENGTH_SHORT for a
brief display).
The show () method is called to display the Toast.

XML CODE: -
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
id" xmlns:app="[Link]
xmlns:tools="[Link]
android:id="@+id/main"

31
Name: - Aashish Hooda Roll No.: - 01
Course: -BCA(5 )
th
Sec.:- G1
Subject:- Android Programming Lab (PBC501)

android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</[Link]>

JAVA CODE:-
package [Link];

import [Link];
import [Link];
import [Link];
import [Link];

import [Link]; import


[Link];
import [Link]; import
[Link]; import
[Link]; public
class MainActivity extends AppCompatActivity
{ private Button t1; @Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
[Link](this);
setContentView([Link].activity_main);
t1=(Button)findViewById([Link].b1);
[Link](new [Link]() {
@Override
public void onClick(View view) {
[Link]([Link],"first toast
message",Toast.LENGTH_LONG).show();
32
Name: - Aashish Hooda Roll No.: - 01
Course: -BCA(5 )
th
Sec.:- G1
Subject:- Android Programming Lab (PBC501)

}
});
[Link](findViewById([Link]), (v, insets) -> {
Insets systemBars = [Link]([Link]());
[Link]([Link], [Link], [Link], [Link]);
return insets;
});

OUTPUT:-

33

You might also like