0% found this document useful (0 votes)
3 views

android programming

The document provides a step-by-step guide for installing Android Studio, creating GitHub repositories, and developing various Android applications. It includes detailed instructions for setting up the environment, coding examples for applications that display messages, change background colors, add numbers, and create a calculator. Each application is accompanied by XML layout and Java code snippets to facilitate understanding and implementation.

Uploaded by

Pawan Singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

android programming

The document provides a step-by-step guide for installing Android Studio, creating GitHub repositories, and developing various Android applications. It includes detailed instructions for setting up the environment, coding examples for applications that display messages, change background colors, add numbers, and create a calculator. Each application is accompanied by XML layout and Java code snippets to facilitate understanding and implementation.

Uploaded by

Pawan Singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 41

Name – Shivang Badoni

Roll No – 59
Section – G1
University Roll no – 2221971
Problem statement 1 – Show 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.
Name – Shivang Badoni
Roll No – 59
Section – G1
University Roll no – 2221971
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:
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.
Name – Shivang Badoni
Roll No – 59
Section – G1
University Roll no – 2221971
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 AVD Manager (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 – Shivang Badoni
Roll No – 59
Section – G1
University Roll no – 2221971
Problem Statement 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
Name – Shivang Badoni
Roll No – 59
Section – G1
University Roll no – 2221971
Now –
Name – Shivang Badoni
Roll No – 59
Section – G1
University Roll no – 2221971
4.

Problem Statement 3 - Develop an android application to display a message “welcome to


graphic era “ on button click.
XML Code –

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


<LinearLayout
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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="12"
android:id="@+id/text"
android:textSize="30sp"
android:textAlignment="center"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:text="Click Me!!"
android:textSize="20sp"/>
</LinearLayout>

Java Code –

package com.codewithshivang.project3;

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

import androidx.appcompat.app.AppCompatActivity;
Name – Shivang Badoni
Roll No – 59
Section – G1
University Roll no – 2221971

public class MainActivity extends AppCompatActivity {

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

TextView text;
Button btn;

text = findViewById(R.id.text);
btn = findViewById(R.id.btn);

btn.setOnClickListener(new View.OnClickListener(){

@Override
public void onClick(View v) {
text.setText("welcome to graphic era ");
}
});
}
}
Name – Shivang Badoni
Roll No – 59
Section – G1
University Roll no – 2221971
Output –
Name – Shivang Badoni
Roll No – 59
Section – G1
University Roll no – 2221971
Problem Statement 4 – Create an android application to change background
color on button click.
XML Code –
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:id="@+id/liMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b1"
android:text="Click Me!!"
android:textSize="20sp"/>

</LinearLayout>

Java Code –

package com.codewithshivang.project4;

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;

public class MainActivity extends AppCompatActivity {

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

Button btn;
LinearLayout liMain;
Name – Shivang Badoni
Roll No – 59
Section – G1
University Roll no – 2221971

btn = findViewById(R.id.b1);
liMain = findViewById(R.id.liMain);

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

liMain.setBackgroundColor(Color.parseColor("#FFBB86FC"));

}
});

}
}

Output –
Name – Shivang Badoni
Roll No – 59
Section – G1
University Roll no – 2221971
Problem Statement 5 – Create an android application to add two numbers.
Xml code –
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="12"
android:id="@+id/txt1"
android:inputType="number"
android:textSize="25sp"
android:hint="Enter First Number"/>

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="12"
android:id="@+id/txt2"
android:inputType="number"
android:textSize="25sp"
android:hint="Enter Second Number"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:textSize="30sp"
android:id="@+id/btn"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtview"
android:ems="12"
android:textSize="25sp"/>
</LinearLayout>
Name – Shivang Badoni
Roll No – 59
Section – G1
University Roll no – 2221971
Java Code –
package com.codewithshivang.project5;

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

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

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

EditText txt1,txt2;
Button btn;
TextView txtview;

txt1 = findViewById(R.id.txt1);
txt2 = findViewById(R.id.txt2);
btn = findViewById(R.id.btn);
txtview = findViewById(R.id.txtview);

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double a = Double.parseDouble(txt1.getText().toString());
double b = Double.parseDouble(txt2.getText().toString());
double sum = a+b;
txtview.setText("Result : "+ sum);
}
});
}
}
Name – Shivang Badoni
Roll No – 59
Section – G1
University Roll no – 2221971
Output –
Name – Shivang Badoni
Roll No – 59
Section – G1
University Roll no – 2221971
Problem Statement 6 – Create an android application of calculator.
XML Code –
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">

<TextView
android:id="@+id/display"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:padding="16dp"
android:text="0"
android:textSize="32sp"
android:textColor="#000000"
android:background="#e0e0e0"
android:layout_marginBottom="16dp"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="8dp">

<Button
android:id="@+id/btn_clear"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="C" />

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

<Button
android:id="@+id/btn_multiply"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
Name – Shivang Badoni
Roll No – 59
Section – G1
University Roll no – 2221971
android:text="*" />

<Button
android:id="@+id/btn_ "
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="DEL" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="8dp">

<Button
android:id="@+id/btn_7"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="7" />

<Button
android:id="@+id/btn_8"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="8" />

<Button
android:id="@+id/btn_9"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="9" />

<Button
android:id="@+id/btn_subtract"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="-" />
</LinearLayout>

<LinearLayout
Name – Shivang Badoni
Roll No – 59
Section – G1
University Roll no – 2221971
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="8dp">

<Button
android:id="@+id/btn_4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="4" />

<Button
android:id="@+id/btn_5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="5" />

<Button
android:id="@+id/btn_6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="6" />

<Button
android:id="@+id/btn_add"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="+" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="8dp">

<Button
android:id="@+id/btn_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1" />
Name – Shivang Badoni
Roll No – 59
Section – G1
University Roll no – 2221971

<Button
android:id="@+id/btn_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2" />

<Button
android:id="@+id/btn_3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3" />

<Button
android:id="@+id/btn_equals"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="=" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<Button
android:id="@+id/btn_0"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="0" />

<Button
android:id="@+id/btn_dot"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="." />

</LinearLayout>
</LinearLayout>
Name – Shivang Badoni
Roll No – 59
Section – G1
University Roll no – 2221971
Java Code –
package com.codewithshivang.calculator;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

TextView display;
String currentOperator = "";
double firstValue = 0;
double secondValue = 0;
boolean isNewOperator = false;

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

display = findViewById(R.id.display);

setNumericButtonListeners();
setOperatorButtonListeners();
}

private void setNumericButtonListeners() {


View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Button button = (Button) v;
String value = button.getText().toString();

if (isNewOperator) {
display.setText(value);
isNewOperator = false;
} else {
display.setText(display.getText().toString() + value);
}
}
};

findViewById(R.id.btn_0).setOnClickListener(listener);
findViewById(R.id.btn_1).setOnClickListener(listener);
Name – Shivang Badoni
Roll No – 59
Section – G1
University Roll no – 2221971
findViewById(R.id.btn_2).setOnClickListener(listener);
findViewById(R.id.btn_3).setOnClickListener(listener);
findViewById(R.id.btn_4).setOnClickListener(listener);
findViewById(R.id.btn_5).setOnClickListener(listener);
findViewById(R.id.btn_6).setOnClickListener(listener);
findViewById(R.id.btn_7).setOnClickListener(listener);
findViewById(R.id.btn_8).setOnClickListener(listener);
findViewById(R.id.btn_9).setOnClickListener(listener);
findViewById(R.id.btn_dot).setOnClickListener(listener);
}

private void setOperatorButtonListeners() {


View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Button button = (Button) v;
String operator = button.getText().toString();

if (!operator.equals("=")) {
if (!currentOperator.isEmpty()) {
calculate();
}

currentOperator = operator;
firstValue = Double.parseDouble(display.getText().toString());
isNewOperator = true;
} else {
calculate();
currentOperator = "";
}
}
};

findViewById(R.id.btn_add).setOnClickListener(listener);
findViewById(R.id.btn_subtract).setOnClickListener(listener);
findViewById(R.id.btn_multiply).setOnClickListener(listener);
findViewById(R.id.btn_divide).setOnClickListener(listener);
findViewById(R.id.btn_equals).setOnClickListener(listener);
findViewById(R.id.btn_clear).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
display.setText("0");
firstValue = 0;
secondValue = 0;
currentOperator = "";
}
Name – Shivang Badoni
Roll No – 59
Section – G1
University Roll no – 2221971
});
}

private void calculate() {


secondValue = Double.parseDouble(display.getText().toString());

double result = 0;
switch (currentOperator) {
case "+":
result = firstValue + secondValue;
break;
case "-":
result = firstValue - secondValue;
break;
case "*":
result = firstValue * secondValue;
break;
case "/":
if (secondValue != 0) {
result = firstValue / secondValue;
}
break;
}

display.setText(String.valueOf(result));
isNewOperator = true;
}
}
Name – Shivang Badoni
Roll No – 59
Section – G1
University Roll no – 2221971
Output –
Name – Shivang Badoni
Roll No – 59
Section – G1
University Roll no – 2221971
Problem statement 7 – Create a application to set image on image view .
XML code-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">

<ImageView
android:layout_width="200sp"
android:layout_height="360sp"
app:srcCompat="@drawable/image"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is image view"
android:textSize="30sp"/>
</LinearLayout>

Java Code –

package com.codewithshivang.setimageview;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Name – Shivang Badoni
Roll No – 59
Section – G1
University Roll no – 2221971
Output –
Name – Shivang Badoni
Roll No – 59
Section – G1
University Roll no – 2221971
Problem Statement 8 – Create a program to depict android activity life cycle.
XML Code –
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:gravity="center"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ANDROID LIFECYCLE"
android:textSize="30sp" />
</LinearLayout>

Java Code –
package com.codewithshivang.activitylifecycle;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("Lifecycycle","onCreate Invoke");
}

public void onStart(){


super.onStart();
Log.d("LifeCycle","onStart Invoke");

public void onResume(){


super.onResume();
Log.d("LifeCycle","onResume Invoke");
Name – Shivang Badoni
Roll No – 59
Section – G1
University Roll no – 2221971
}

public void onPause(){


super.onPause();
Log.d("LifeCycle","onPause Invoke");

public void onStop(){


super.onStop();
Log.d("LifeCycle","onStop Invoke");

public void onDestroy(){


super.onDestroy();
Log.d("LifeCycle","onDestroy Invoke");

}
}

Output –
Name – Shivang Badoni
Roll No – 59
Section – G1
University Roll no – 2221971
Screenshot –
Name – Shivang Badoni
Roll No – 59
Section – G1
University Roll no – 2221971
Problem Statement 9 – Create a program to show implicit intent .
XML Code –
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="88dp"
android:text="ENTER URL:"
android:textColor="#180966"
android:textSize="30sp"
android:textStyle="bold"
android:textAlignment="center"
/>

<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="82dp"
android:ems="10"
android:hint="URL"
android:textAlignment="center"
android:inputType="textPersonName" />

<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="OPEN URL"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
Name – Shivang Badoni
Roll No – 59
Section – G1
University Roll no – 2221971
Java Code –
package com.codewithshivang.implicitintent;

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);

b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
url=e.getText().toString();
Intent i=new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
}
}
Name – Shivang Badoni
Roll No – 59
Section – G1
University Roll no – 2221971
Output –
Name – Shivang Badoni
Roll No – 59
Section – G1
University Roll no – 2221971
Problem statement 10 – Create a program to show explicit intent .

Activity 1
XML Code –
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This Is First Activity."
android:textSize="30sp"/>

<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NEXT"
android:textSize="20sp"/>
</LinearLayout>

Java Code –
package com.codewithshivang.explicitintent;

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);
Name – Shivang Badoni
Roll No – 59
Section – G1
University Roll no – 2221971

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


Intent iNext = new Intent(MainActivity.this,MainActivity2.class);

btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

startActivity(iNext);

}
});
}
}

Activity 2

XML Code –

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


<LinearLayout
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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity2">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This Is Second Activity."
android:textSize="30sp"/>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" PREVIOUS"
android:textSize="20sp"/>

</LinearLayout>
Name – Shivang Badoni
Roll No – 59
Section – G1
University Roll no – 2221971
Java Code –

package com.codewithshivang.explicitintent;

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

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity2 extends AppCompatActivity {

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

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


Intent iPrevious = new Intent(MainActivity2.this,MainActivity.class);

btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

startActivity(iPrevious);
}
});

}
}
Name – Shivang Badoni
Roll No – 59
Section – G1
University Roll no – 2221971
Output –
Name – Shivang Badoni
Roll No – 59
Section – G1
University Roll no – 2221971
Problem Statement 11 – Develop an android application to demonstrate Event Listener and
Event Handlers.
XML Code –
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp">

<TextView
android:id="@+id/outputText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Event Listener Demo"
android:textSize="20sp"
android:layout_marginBottom="20dp" />

<Button
android:id="@+id/clickButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:layout_marginBottom="20dp" />

<Button
android:id="@+id/longPressButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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 {

private TextView outputText;


Name – Shivang Badoni
Roll No – 59
Section – G1
University Roll no – 2221971
private Button clickButton;
private Button longPressButton;

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

outputText = findViewById(R.id.outputText);
clickButton = findViewById(R.id.clickButton);
longPressButton = findViewById(R.id.longPressButton);

clickButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
outputText.setText("Button Clicked!");
}
});

longPressButton.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
outputText.setText("Button Long Pressed!");
return true;
} });
}
}
Name – Shivang Badoni
Roll No – 59
Section – G1
University Roll no – 2221971
Output –
Name – Shivang Badoni
Roll No – 59
Section – G1
University Roll no – 2221971
Problem Statement 12 – Develop an android application to create two buttons and switch
between images on button click.
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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
tools:context=".MainActivity">

<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="391dp"
android:src="@drawable/img1" />
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:text="Image 1"/>

<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:text="Image 2" />

</RelativeLayout>

Java code –
package com.codewithshivang.imageswitch;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
Name – Shivang Badoni
Roll No – 59
Section – G1
University Roll no – 2221971
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 btn1,btn2;
ImageView imageView;

btn1 = findViewById(R.id.btn1);
btn2 = findViewById(R.id.btn2);
imageView = findViewById(R.id.imageView);

btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imageView.setImageResource(R.drawable.img1);
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imageView.setImageResource(R.drawable.img2);
}
});

}
}
Name – Shivang Badoni
Roll No – 59
Section – G1
University Roll no – 2221971
Output –
Name – Shivang Badoni
Roll No – 59
Section – G1
University Roll no – 2221971
Problem Statement 13 – Develop an android application to display toast message on button
click.
XML Code –
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Example of Toast"
android:textSize="30sp"/>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me!!"
android:textSize="20sp"/>

</LinearLayout>

Java Code –

package com.codewithshivang.toast;

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

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Name – Shivang Badoni
Roll No – 59
Section – G1
University Roll no – 2221971

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


btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "This is a Toast.", Toast.LENGTH_SHORT).show();
}
});

}
}

Output –

You might also like