Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
11 views50 pages

Android

The document provides a detailed guide on installing Android Studio, including system requirements, download instructions, installation steps for Windows, macOS, and Linux, and first-time setup. It also includes code examples for creating simple Android applications such as displaying a welcome message, changing layout color, adding two numbers, and building a calculator app. Each application example contains XML layout and Java code to demonstrate functionality.
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)
11 views50 pages

Android

The document provides a detailed guide on installing Android Studio, including system requirements, download instructions, installation steps for Windows, macOS, and Linux, and first-time setup. It also includes code examples for creating simple Android applications such as displaying a welcome message, changing layout color, adding two numbers, and building a calculator app. Each application example contains XML layout and Java code to demonstrate functionality.
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
You are on page 1/ 50

Name – Ujjwal

Baluni
Class – BCA (E2)
Roll No – 84
Que1- Shoe step by step procedure to install android studio

Ans – To install the android studio below are the following steps :

Step 1: System Requirements

Ensure your system meets the following requirements:

 Operating System:

o Windows: Windows 8/10/11 (64-bit)


o macOS: Mac OS X 10.14 or higher

o Linux: GNOME or KDE desktop


 RAM: At least 8 GB (16 GB recommended)

 Disk Space: Minimum of 8 GB

 JDK: Android Studio comes with an embedded JDK.

Step 2: Download Android Studio

1. Go to the official Android Studio


website: Download Android Studio

2. Click on Download Android Studio.

3. Accept the terms and conditions and start the download.

Step 3: Install Android

Studio For Windows:

1. Locate the downloaded .exe file (e.g., android-studio-ide-<version>-windows.exe) and double-


click to start the installation.

2. Follow the installer steps:

o Choose the components you want to install (Android SDK, Emulator, etc.)
o Select installation locations for Android Studio and Android SDK.
3. Click Install and wait for the installation to complete.

4. Once finished, click Finish to open Android Studio.

For macOS:

1. Open the downloaded .dmg file.

2. Drag and drop Android Studio into the Applications folder.

3. Open Android Studio from the Applications folder.

For Linux:

1. Extract the downloaded .zip file:


Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
unzip android-studio-ide-<version>-linux.zip

2. Navigate to the extracted android-studio/bin/

directory: cd android-studio/bin

3. Start Android Studio by running:

./studio.sh

Step 4: First-time Setup

1. When Android Studio first launches, it may ask if you want to import any previous settings. If
you don’t have any, select Do not import settings.

2. Android Studio will guide you through the Setup Wizard, which includes:

o Selecting UI themes (light/dark).


o Downloading and setting up the required SDK components.
Step 5: Install Required SDK Components

1. In the SDK Manager (opened by default after the first setup), ensure that the latest Android SDK is installed.

2. Select additional SDK tools if required, such as:

o Android Emulator
o Android SDK Build-Tools
Step 6: Create and Test a New Project

1. Click on Start a new Android Studio project.

2. Select the form factor (e.g., Phone/Tablet) and API Level.

3. Choose a project template (Empty Activity is a good start).

4. Name your project and choose the save location.

5. Click Finish to set up your project.

6. Once the project is set up, you can run the project on an emulator or a connected Android device.

Step 7: Emulator Setup (Optional)

1. Open the AVDManager (Android Virtual Device) from the toolbar.

2. Click Create Virtual Device and follow the instructions to set up an emulator.

3. Once the emulator is created, you can use it to run your Android projects.
Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
Ǫ2 - Create a GitHub repository and push your project into the

repository. Ans – Creating a new repository

Now write the above command in the terminal of the android studio –

1- Initialize a new Git repository: git init


2- Stage all project files for the initial commit: git add .
3- Commit the changes with a meaningful message: git commit -m "Initial commit for Android project"
4- Add your GitHub repository as the remote: git remote
add origin https://github.com/shriyanshurawat/for-
android-code-to-push.git
5- Rename the branch to main : git branch -M main
6- Push the code to your GitHub repository : git push -u origin main

Now –
Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
Ǫ3 . Develop an android application to display a message “welcome to graphic era

“ on button click. Ans – XML CODE :

<?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 to display the message -->


<TextView
android:id="@+id/textView"
android:layout_width="wrap_cont
ent"
android:layout_height="wrap_con
tent" android:text="Hello!"
android:textSize="20sp"
android:visibility="invisible"
app:layout_constraintBottom_toTopOf="@+id/
button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent
"
app:layout_constraintTop_toTopOf="parent" /
>

<!-- Button to trigger the display message -->


<Button
android:id="@+id/butt
on"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
app:layout_constraintBottom_toBottomOf="parent
" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textVie
w" />

</

androidx.constraintlayout.widget.ConstraintLay

out> JAVA CODE :

package com.example.welcomeapp;

import android.os.Bundle;
import android.view.View;
import
android.widget.Button;
import
android.widget.TextView;
import
Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends

AppCompatActivity {

private TextView textView;


Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
private Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Initialize the TextView and


Button textView =
findViewById(R.id.textView); button
= findViewById(R.id.button);

// Set an OnClickListener on the Button


button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Display the message in the
TextView textView.setText("Welcome
to Graphic Era");
textView.setVisibility(View.VISIBLE);
}
});
}
}

OUTPUT –
Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
Ǫ4. Develop an android application to change the layout color on

button click. Ans-Xml code –

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/mainLayout"

android:layout_width="match_par

ent"

android:layout_height="match_pa

rent"

android:orientation="vertical"

android:gravity="center"

android:padding="16dp">

<Button

android:id="@+id/

changeColorButton"

android:layout_width="wrap_cont

ent"

android:layout_height="wrap_con

tent" android:text="Change

Color" />

</LinearLayout>

Java Code –

package

com.example.changelayoutcolor;

import android.graphics.Color;

import

android.os.Bundle;

import

android.view.View;

import

android.widget.Button;

import android.widget.LinearLayout;

import

androidx.appcompat.app.AppCompatActivity;

import java.util.Random;
Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
public class MainActivity extends

AppCompatActivity { private LinearLayout

mainLayout;

private Button

changeColorButton; @Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
setContentView(R.layout.activity_main

); mainLayout =

findViewById(R.id.mainLayout);

changeColorButton = findViewById(R.id.changeColorButton);

// Set an OnClickListener on the button to change layout

color changeColorButton.setOnClickListener(new

View.OnClickListener() {

@Override

public void onClick(View v) {

// Generate a random color and set it as the

background Random random = new

Random();

int color = Color.rgb(random.nextInt(256), random.nextInt(256),

random.nextInt(256)); mainLayout.setBackgroundColor(color);

});

Output –
Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
Ǫ5. Create an android application to add two

numbers. Ans –

XML CODE :

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vert

ical"

android:padding="16dp"

android:gravity="center"

>

<!-- Input for First Number -->

<EditText

android:id="@+id/numb

er1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Enter first number"

android:inputType="number"

android:layout_marginBottom="16dp" /

>

<!-- Input for Second Number -->

<EditText

android:id="@+id/numb

er2"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Enter second number"

android:inputType="number"

android:layout_marginBottom="16dp" /

>
Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84

<!-- Button to Trigger Addition -->

<Button

android:id="@+id/addBut

ton"
Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
android:layout_width="wrap_cont

ent"

android:layout_height="wrap_con

tent" android:text="Add" />

<!-- TextView to Display Result -->

<TextView

android:id="@+id/resultText"

android:layout_width="wrap_cont

ent"

android:layout_height="wrap_con

tent" android:text="Result"

android:textSize="20sp"

android:layout_marginTop="16d

p" />

</LinearLayout>

JAVA CODE -

package com.example.additionapp;

import

androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import

android.widget.Button;

import

android.widget.EditText;

import

android.widget.TextView;

public class MainActivity extends AppCompatActivity {

private EditText number1,


Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
number2; private Button

addButton;

private TextView resultText;


Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

// Bind the XML elements to Java code

number1 =

findViewById(R.id.number1);

number2 =

findViewById(R.id.number2);

addButton =

findViewById(R.id.addButton);

resultText =

findViewById(R.id.resultText);

// Set onClickListener on the button

addButton.setOnClickListener(new View.OnClickListener()

@Override

public void onClick(View v) {

// Get values from EditText, convert to integers, and

calculate the sum String num1String =

number1.getText().toString();

String num2String = number2.getText().toString();

if (!num1String.isEmpty() && !

num2String.isEmpty()) { int num1 =

Integer.parseInt(num1String);

int num2 =

Integer.parseInt(num2String); int

sum = num1 + num2;

// Display the sum in the

TextView

resultText.setText("Result: " +
Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
sum);

} else {

// Handle cases where one or both fields

are empty resultText.setText("Please

enter both numbers");

}
Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
});

OUTPUT -

Ǫ6 . Create an android application of

calculator. 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">

<!-- Display for the result -->


<TextView
android:id="@+id/displ
ay"
android:layout_width="0
dp"
android:layout_height="wrap_content"
android:textSize="30sp"
android:gravity="right"
android:padding="20dp"
app:layout_constraintTop_toTopOf="par
ent"
app:layout_constraintStart_toStartOf="pa
rent"
app:layout_constraintEnd_toEndOf="parent
Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
" />
Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
<!-- Buttons for numbers and operations -->
<GridLayout
android:layout_width="0
dp"
android:layout_height="
0dp"
android:rowCount="5"
android:columnCount="
4"
app:layout_constraintTop_toBottomOf="@id/display"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">

<Button android:id="@+id/btn1" android:layout_width="0dp" android:layout_height="wrap_content" android:text="1"


android:layout_columnWeight="1"/>
<Button android:id="@+id/btn2" android:layout_width="0dp" android:layout_height="wrap_content" android:text="2"
android:layout_columnWeight="1"/>
<Button android:id="@+id/btn3" android:layout_width="0dp" android:layout_height="wrap_content" android:text="3"
android:layout_columnWeight="1"/>
<Button android:id="@+id/btnAdd" android:layout_width="0dp"
android:layout_height="wrap_content" android:text="+" android:layout_columnWeight="1"/>

<Button android:id="@+id/btn4" android:layout_width="0dp" android:layout_height="wrap_content" android:text="4"


android:layout_columnWeight="1"/>
<Button android:id="@+id/btn5" android:layout_width="0dp" android:layout_height="wrap_content" android:text="5"
android:layout_columnWeight="1"/>
<Button android:id="@+id/btn6" android:layout_width="0dp" android:layout_height="wrap_content" android:text="6"
android:layout_columnWeight="1"/>
<Button android:id="@+id/btnSubtract" android:layout_width="0dp"
android:layout_height="wrap_content" android:text="-" android:layout_columnWeight="1"/>

<Button android:id="@+id/btn7" android:layout_width="0dp" android:layout_height="wrap_content" android:text="7"


android:layout_columnWeight="1"/>
<Button android:id="@+id/btn8" android:layout_width="0dp" android:layout_height="wrap_content" android:text="8"
android:layout_columnWeight="1"/>
<Button android:id="@+id/btn9" android:layout_width="0dp" android:layout_height="wrap_content" android:text="9"
android:layout_columnWeight="1"/>
<Button android:id="@+id/btnMultiply" android:layout_width="0dp"
android:layout_height="wrap_content" android:text="*" android:layout_columnWeight="1"/>

<Button android:id="@+id/btnClear" android:layout_width="0dp"


android:layout_height="wrap_content" android:text="C" android:layout_columnWeight="1"/>
<Button android:id="@+id/btn0" android:layout_width="0dp" android:layout_height="wrap_content" android:text="0"
android:layout_columnWeight="1"/>
<Button android:id="@+id/btnEqual" android:layout_width="0dp"
android:layout_height="wrap_content" android:text="=" android:layout_columnWeight="1"/>
<Button android:id="@+id/btnDivide" android:layout_width="0dp"
android:layout_height="wrap_content" android:text="/" android:layout_columnWeight="1"/>
</GridLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
JAVA CODE -

package com.example.calculator;

import android.os.Bundle;
import android.view.View;
import
android.widget.Button;
import
android.widget.TextView;
import

androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends

AppCompatActivity {

private TextView display;


private double firstValue =
0; private double
secondValue = 0; private
String operator = "";
private boolean isOperatorPressed = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

display = findViewById(R.id.display);

View.OnClickListener numberClickListener = new


View.OnClickListener() { @Override
public void onClick(View
view) { Button button =
(Button) view; if
(isOperatorPressed) {
display.setText(button.getTex
t()); isOperatorPressed =
false;
} else {
display.setText(display.getText().toString() + button.getText());
}
}
};

View.OnClickListener operatorClickListener = new


View.OnClickListener() { @Override
public void onClick(View
view) { Button button =
(Button) view;
firstValue =
Double.parseDouble(display.getText().toString());
operator = button.getText().toString();
isOperatorPressed = true;
Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
}
};

// Set click listeners for numbers


Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
findViewById(R.id.btn0).setOnClickListener(numberClick
Listener);
findViewById(R.id.btn1).setOnClickListener(numberClick
Listener);
findViewById(R.id.btn2).setOnClickListener(numberClick
Listener);
findViewById(R.id.btn3).setOnClickListener(numberClick
Listener);
findViewById(R.id.btn4).setOnClickListener(numberClick
Listener);
findViewById(R.id.btn5).setOnClickListener(numberClick
Listener);
findViewById(R.id.btn6).setOnClickListener(numberClick
Listener);
findViewById(R.id.btn7).setOnClickListener(numberClick
Listener);
findViewById(R.id.btn8).setOnClickListener(numberClick
Listener);
findViewById(R.id.btn9).setOnClickListener(numberClickL
istener);

// Set click listeners for operators


findViewById(R.id.btnAdd).setOnClickListener(operatorClickList
ener);
findViewById(R.id.btnSubtract).setOnClickListener(operatorClickLi
stener);
findViewById(R.id.btnMultiply).setOnClickListener(operatorClick
Listener);
findViewById(R.id.btnDivide).setOnClickListener(operatorClickL
istener);

// Clear button
findViewById(R.id.btnClear).setOnClickListener(new
View.OnClickListener() { @Override
public void onClick(View
view) {
display.setText("");
firstValue = 0;
secondValue = 0;
operator = "";
}
});

// Equal button
findViewById(R.id.btnEqual).setOnClickListener(new
View.OnClickListener() { @Override
public void onClick(View view) {
secondValue =
Double.parseDouble(display.getText().toString()); double
result = 0;

switch (operator) {
case "+":
result = firstValue +
secondValue; break;
case "-":
Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
result = firstValue -
secondValue; break;
case "*":
result = firstValue *
secondValue; break;
case "/":
if (secondValue != 0) {
result = firstValue / secondValue;
} else {
display.setText("Error");
Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
return;
}
break;
}

display.setText(String.valueOf(result));
}
});
}
}

OUTPUT -
Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
Ǫ7. Create an application to set image on

image view. Ans – XML CODE :

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

<!-- ImageView to display the image -->


<ImageView
android:id="@+id/imageView"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginBottom="
20dp"
android:scaleType="centerCrop
"
android:layout_marginTop="20
dp"
app:layout_constraintTop_toTopOf="pa
rent"
app:layout_constraintStart_toStartOf="p
arent"
app:layout_constraintEnd_toEndOf="pare
nt"/>

<!-- Button to set the image on the ImageView -->


<Button
android:id="@+id/butt
on"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set Image"
app:layout_constraintTop_toBottomOf="@id/imag
eView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" /
>

</

androidx.constraintlayout.widget.ConstraintLay

out> JAVA CODE :

package com.example.imageviewapp;

import
android.os.Bundle;
import
android.view.View;
import
android.widget.Button;
Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
import android.widget.ImageView;
import

androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends

AppCompatActivity {

private ImageView
imageView; private
Button button;
Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Initialize the ImageView and Button


imageView =
findViewById(R.id.imageView); button
= findViewById(R.id.button);

// Set an OnClickListener on the Button to set the image in the ImageView


button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Set the image resource to the ImageView
imageView.setImageResource(R.drawable.my_image); // Replace "my_image" with your actual image file
name in drawable
}
});
}
}

OUTPUT –
Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
Ǫ8. Develop an android application to depict android’s

activity life cycle. Ans – XML CODE :

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:gravity="center"

android:orientation="vertic

al">

<TextView

android:id="@+id/statusText"

android:layout_width="wrap_cont

ent"

android:layout_height="wrap_con

tent" android:text="Lifecycle

Status"

android:textSize="20sp" />

</LinearLayout>

JAVA CODE :

package com.example.lifecycledemo;

import android.os.Bundle;

import

android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private TextView statusText;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
setContentView(R.layout.activity_main);
Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
statusText = findViewById(R.id.statusText);

statusText.setText("onCreate called");

@Override

protected void onStart() {

super.onStart();

statusText.setText("onStart

called");

@Override

protected void onResume() {

super.onResume();

statusText.setText("onResume

called");

@Override

protected void onPause() {

super.onPause();

statusText.setText("onPause

called");

@Override

protected void onStop() {

super.onStop();

statusText.setText("onStop

called");

@Override

protected void

onDestroy() {
Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
super.onDestroy();
Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
statusText.setText("onDestroy called");

OUTPUT –

Ǫ9 . Create a program to show implicit

intent . Ans –

XML CODE :

<?xml version="1.0" encoding="utf-8"?>


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

<LinearLayout
android:layout_width="match_par
ent"
android:layout_height="match_pa
rent"
android:orientation="vertical">

<TextView
android:id="@+id/textView"
android:layout_width="match_pa
rent"
android:layout_height="88dp"
android:text="ENTER URL:"
android:textColor="#180966"
android:textSize="30sp"
android:textStyle="bold"
android:textAlignment="center
"
Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
/>

<EditText
android:id="@+id/editT
ext"
android:layout_width="match_pa
rent"
android:layout_height="82dp"
android:ems="10"
android:hint="URL"
android:textAlignment="center
"
android:inputType="textPersonName" />

<Button
android:id="@+id/butt
on"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="OPEN URL"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
</

RelativeLayout

> JAVA CODE :

package com.example.implicit;

import

androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import
android.view.View; import
android.widget.Button;
import
android.widget.EditText;

public class MainActivity extends


AppCompatActivity { EditText e;
Button b;
String url="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

b=(Button)
findViewById(R.id.button);
e=(EditText)
findViewById(R.id.editText);
Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84

b.setOnClickListener(new
View.OnClickListener() { @Override
public void onClick(View view) {
url=e.getText().toString();
Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
Intent i=new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
}
}

OUTPUT -

Ǫ10. Create a program to show explicit

intent . Ans – XML CODE :

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_pa
rent" android:gravity="center"
android:padding="16dp">

<Button
android:id="@+id/btnOpenSecondAc
tivity"
android:layout_width="wrap_conte
nt"
android:layout_height="wrap_conte
nt" android:text="Go to Second
Activity" />

</LinearLayout>
Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
XML 2nd CODE :

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_par
ent"
android:layout_height="match_pa
rent" android:gravity="center"
android:padding="16dp">

<TextView
android:id="@+id/tvSecondActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to Second
Activity" android:textSize="24sp" />

</LinearLayout>

JAVA CODE :

package com.example.explicit;

import
android.content.Intent;
import
android.os.Bundle;
import
android.view.View;
import
android.widget.Button;
import

androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends

AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button btnOpenSecondActivity = findViewById(R.id.btnOpenSecondActivity);

btnOpenSecondActivity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Create an explicit intent to open SecondActivity
Intent intent = new Intent(MainActivity.this,
SecondActivity.class); startActivity(intent);
}
});
}
Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
}
Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
JAVA 2nd CODE :

package com.example.explicit;

import android.os.Bundle;
import

androidx.appcompat.app.AppCompatActivity;

public class SecondActivity extends

AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}

OUTPUT -
Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
Ǫ11. Develop an android application to demonstrate Event Listener and

Event Handlers. Ans – XML Code :

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vert

ical"

android:gravity="center"

android:padding="16dp"

>

<!-- TextView to display output when events are triggered -->

<TextView

android:id="@+id/outputText"

android:layout_width="wrap_cont

ent"

android:layout_height="wrap_con

tent" android:text="Event

Listener Demo"

android:textSize="20sp"

android:layout_marginBottom="20

dp" />

<!-- Button for Click Event -->

<Button

android:id="@+id/clickButton"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Click Me"

android:layout_marginBottom="20dp" /

>

<!-- Button for Long Press Event -->

<Button
Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
android:id="@+id/longPressButt

on"

android:layout_width="wrap_cont

ent"

android:layout_height="wrap_con

tent"
Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
android:text="Long Press Me" />

</LinearLayout>

JAVA CODE :

package com.example.eventlistenerdemo;

import android.os.Bundle;

import android.view.View;

import

android.widget.Button;

import

android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

// Define the UI

components private

TextView outputText;

private Button

clickButton; private Button

longPressButton;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

// Bind UI components to their corresponding XML

elements outputText = findViewById(R.id.outputText);

clickButton = findViewById(R.id.clickButton);

longPressButton =

findViewById(R.id.longPressButton);
Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
// Set an OnClickListener for the Click Me button

clickButton.setOnClickListener(new View.OnClickListener()

@Override

public void onClick(View v) {

// Update the TextView when the button is clicked

outputText.setText("Button Clicked!");

});

// Set an OnLongClickListener for the Long Press Me button

longPressButton.setOnLongClickListener(new View.OnLongClickListener() {

@Override

public boolean onLongClick(View v) {

// Update the TextView when the button is long-pressed

outputText.setText("Button Long Pressed!");

return true; // Return true to indicate that the event has been handled

} });

OUTPUT –
Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
Ǫ12. Develop an android application to create two buttons and switch between images

on button click. Ans – XML CODE :

<!-- res/layout/activity_main.xml -->

<RelativeLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:padding="16dp">

<!-- Left Arrow Button -->

<Button

android:id="@+id/button_left"

android:layout_width="wrap_cont

ent"

android:layout_height="wrap_con

tent"

android:layout_alignParentLeft="

true"

android:text="←"
android:textSize="24sp
" />

<!-- Right Arrow Button -->

<Button

android:id="@+id/button_right"

android:layout_width="wrap_cont

ent"

android:layout_height="wrap_con

tent"

android:layout_alignParentRight="

true"

android:text="→"
android:textSize="24sp
" />

<!-- Image View in the center -->


Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
<ImageView

android:id="@+id/imageView"

android:layout_width="wrap_conte

nt"

android:layout_height="wrap_cont

ent"
Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
android:layout_centerInParent="

true"

android:src="@drawable/image

1" />

</RelativeLayout>

JAVA CODE :

package com.example.imagechanger;

import

androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import

android.view.View;

import

android.widget.Button;

import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

// Array of images to cycle through

int[] images = {R.drawable.image1, R.drawable.image2, R.drawable.image3};

// Track the current image

index int

currentImageIndex = 0;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

// Find the buttons and ImageView

Button leftButton =
Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
findViewById(R.id.button_left); Button rightButton

= findViewById(R.id.button_right); ImageView

imageView = findViewById(R.id.imageView);
Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84

// Set click listener for the left arrow button

leftButton.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

// Decrease the image

index

currentImageIndex--;

if (currentImageIndex < 0) {

currentImageIndex = images.length - 1; // Wrap around if index is out of bounds

// Update the image displayed

imageView.setImageResource(images[currentImageIndex

} ]);

});

// Set click listener for the right arrow button

rightButton.setOnClickListener(new

View.OnClickListener() {

@Override

public void onClick(View v) {

// Increase the image index

currentImageIndex++;

if (currentImageIndex >= images.length) {

currentImageIndex = 0; // Wrap around if index is out of bounds

// Update the image displayed

imageView.setImageResource(images[currentImageIndex

} ]);

});

OUTPUT –
Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
Ǫ13. Develop an android application to display toast message on button click.

Ans – XML CODE –

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">

<!-- Button to display toast message -->


<Button
android:id="@+id/toastBut
ton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Toast"
android:layout_centerInParent="true"/>

</RelativeLayout>

JAVA CODE –

package com.example.toastapp;

import

androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import
android.view.View;
import
android.widget.Button;
import
android.widget.Toast;

public class MainActivity extends AppCompatActivity {

private Button toastButton;

@SuppressLint("MissingInflate
dId") @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Initialize the button


toastButton = findViewById(R.id.toastButton);

// Set the button click listener


toastButton.setOnClickListener(new
View.OnClickListener() {
@Override
Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
public void onClick(View v) {
// Show toast message
Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
Toast.makeText(MainActivity.this, "Button clicked!", Toast.LENGTH_SHORT).show();
}
});
}
}

OUTPUT -

You might also like