Android
Android
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 :
Operating System:
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.
For macOS:
For Linux:
directory: cd android-studio/bin
./studio.sh
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:
1. In the SDK Manager (opened by default after the first setup), ensure that the latest Android SDK is installed.
o Android Emulator
o Android SDK Build-Tools
Step 6: Create and Test a New Project
6. Once the project is set up, you can run the project on an emulator or a connected Android device.
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
Now write the above command in the terminal of the android studio –
Now –
Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
Ǫ3 . Develop an android application to display a message “welcome to graphic era
</
androidx.constraintlayout.widget.ConstraintLay
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;
AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OUTPUT –
Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
Ǫ4. Develop an android application to change the layout color on
<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
mainLayout;
private Button
changeColorButton; @Override
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);
color changeColorButton.setOnClickListener(new
View.OnClickListener() {
@Override
Random();
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"
>
<EditText
android:id="@+id/numb
er1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:layout_marginBottom="16dp" /
>
<EditText
android:id="@+id/numb
er2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:layout_marginBottom="16dp" /
>
Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
<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
<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;
addButton;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
number1 =
findViewById(R.id.number1);
number2 =
findViewById(R.id.number2);
addButton =
findViewById(R.id.addButton);
resultText =
findViewById(R.id.resultText);
addButton.setOnClickListener(new View.OnClickListener()
@Override
number1.getText().toString();
if (!num1String.isEmpty() && !
Integer.parseInt(num1String);
int num2 =
Integer.parseInt(num2String); int
TextView
resultText.setText("Result: " +
Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
sum);
} else {
}
Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
});
OUTPUT -
calculator. XML –
</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;
AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
display = findViewById(R.id.display);
// 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
</
androidx.constraintlayout.widget.ConstraintLay
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;
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);
OUTPUT –
Name – Ujjwal
Baluni
Class – BCA (E2)
Roll No – 84
Ǫ8. Develop an android application to depict android’s
<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;
@Override
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
super.onStart();
statusText.setText("onStart
called");
@Override
super.onResume();
statusText.setText("onResume
called");
@Override
super.onPause();
statusText.setText("onPause
called");
@Override
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 –
intent . Ans –
XML CODE :
<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
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;
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 -
<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 :
<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;
AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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;
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
<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
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
android:id="@+id/clickButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:layout_marginBottom="20dp" /
>
<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;
// Define the UI
components private
TextView outputText;
private Button
longPressButton;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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
outputText.setText("Button Clicked!");
});
longPressButton.setOnLongClickListener(new View.OnLongClickListener() {
@Override
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
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<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
" />
<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
" />
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;
index int
currentImageIndex = 0;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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
leftButton.setOnClickListener(new View.OnClickListener() {
@Override
index
currentImageIndex--;
if (currentImageIndex < 0) {
imageView.setImageResource(images[currentImageIndex
} ]);
});
rightButton.setOnClickListener(new
View.OnClickListener() {
@Override
currentImageIndex++;
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.
</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;
@SuppressLint("MissingInflate
dId") @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OUTPUT -