Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Mobile Application Programming - LAB

Experiment-5

Student Name: Kriti Jain


Semester: 5

UID: 20BCG1088 Branch: CSE - G&G

Section/Group: 20BCG/B

Date of Performance: 25-09-2022

Subject Name: Mobile Application Programming-LAB

Subject Code: 20CSG-331_20BCG-1_B


Problem:

Create an application that will change the color of


the screen, based on selected options from the
menu.

Algorithm:

Step 1: Design

1. Open the actual Project folder(app) in Android


Studio IDE / Eclipse IDE

2. Click res directory -> layout ->


activity_main.xml -> Design

3. Add Two EditText and one button to make it


center of the
screen.

4. Enter the corresponding ids for each


component.

5. Layout(UI)
PROGRAM CODE:

MainActivity.java Code

1. package com.example.experiment5_kriti;
2.
3. import android.os.Bundle;
4. import android.view.View;
5. import android.widget.Button;
6. import androidx.appcompat.app.AppCompatActivity;
7. import androidx.constraintlayout.widget.ConstraintLayout;
8. public class MainActivity extends AppCompatActivity {
9. @Override
10. protected void onCreate(Bundle savedInstanceState) {
11. super.onCreate(savedInstanceState);
12. setContentView(R.layout.activity_main);
13. Button button1, button2;
14. final ConstraintLayout constraintLayout;
15. button1 = findViewById(R.id.button1);
16. button2 = findViewById(R.id.button2);
17. constraintLayout = findViewById(R.id.bg);
18. button1.setOnClickListener(new View.OnClickListener()
{
19. @Override
20. public void onClick(View view) {
21.
constraintLayout.setBackgroundResource(R.color.blue);
22. }
23. });
24. button2.setOnClickListener(new View.OnClickListener()
{
25. @Override
26. public void onClick(View view) {
27.
constraintLayout.setBackgroundResource(R.color.red);
28. }
29. });
30. }
31. }
32.
XML CODE

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


2. <androidx.constraintlayout.widget.ConstraintLayout
3.
xmlns:android="http://schemas.android.com/apk/res/android"
4. xmlns:app="http://schemas.android.com/apk/res-auto"
5. xmlns:tools="http://schemas.android.com/tools"
6. android:layout_width="match_parent"
7. android:layout_height="match_parent"
8. android:id="@+id/bg"
9. tools:context=".MainActivity">
10. <Button
11. android:id="@+id/button1"
12. android:layout_width="wrap_content"
13. android:layout_height="wrap_content"
14. android:layout_marginTop="216dp"
15. android:text="Blue"
16. app:layout_constraintEnd_toEndOf="parent"
17. app:layout_constraintStart_toStartOf="parent"
18. app:layout_constraintTop_toTopOf="parent" />
19. <Button
20. android:id="@+id/button2"
21. android:layout_width="wrap_content"
22. android:layout_height="wrap_content"
23. android:layout_marginTop="196dp"
24. android:text="Red"
25. app:layout_constraintEnd_toEndOf="parent"
26. app:layout_constraintStart_toStartOf="parent"
27.
app:layout_constraintTop_toBottomOf="@+id/button1" />
28. </androidx.constraintlayout.widget.ConstraintLayout>
29.
OUTPUTS:

IDE Screen:
App Screenshots:

The background color of the screen changes when the


button is pressed.
Evaluation Grid (To be created as per the SOP and
Assessment guidelines by the faculty):

Sr. No. Parameters Marks Maximum


Obtained Marks
1.
2.
3.

Learning Outcomes:

● Understood the usage of OnClickListener in


Android Programming.

● Learned how to use the background as a constraint


layout entity.

You might also like