You are on page 1of 11

PROBLEM STATEMENT 5: Write an application that draws graphical primitives such as circle,

rectangle and square. Fill all these primitives with different colours.
OBJECTIVE: To learn the concept of relative layout and imageview

XML SOURCECODE:-
<?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">

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView" />
</RelativeLayout>

JAVA SOURCECODE:-
package com.example.myapplication;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.widget.ImageView;

import com.example.myapplication.R;

public class MainActivity extends Activity


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

//Creating a Bitmap
Bitmap bg = Bitmap.createBitmap(720, 1280, Bitmap.Config.ARGB_8888);

//Setting the Bitmap as background for the ImageView


ImageView i = (ImageView) findViewById(R.id.imageView);
i.setBackgroundDrawable(new BitmapDrawable(bg));

//Creating the Canvas Object


Canvas canvas = new Canvas(bg);

//Creating the Paint Object and set its color & TextSize
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setTextSize(50);

//To draw a Rectangle


canvas.drawText("Rectangle", 420, 150, paint);
canvas.drawRect(400, 200, 650, 700, paint);
//To draw a Circle
canvas.drawText("Circle", 120, 150, paint);
canvas.drawCircle(200, 350, 150, paint);

//To draw a Square


canvas.drawText("Square", 120, 800, paint);
canvas.drawRect(50, 850, 350, 1150, paint);

//To draw a Line


canvas.drawText("Line", 480, 800, paint);
canvas.drawLine(520, 850, 520, 1150, paint);
}
}

OUTPUT:-
PROBLEM STATEMENT 4: Create an application which displays three buttons named Red,
Yellow, and Green. The background colour of activity should be changed to appropriate
colour when user click on any of these buttons.
OBJECTIVE:- To learn the concept of button and setbackground method

XML SOURCECODE:-
<?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:id="@+id/lay"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RED"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.146" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GREEN"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/button"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/button"
app:layout_constraintTop_toBottomOf="@+id/button"
app:layout_constraintVertical_bias="0.252" />

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="YELLOW"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/button2"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="@+id/button2"
app:layout_constraintTop_toBottomOf="@+id/button2"
app:layout_constraintVertical_bias="0.335" />

</androidx.constraintlayout.widget.ConstraintLayout>
JAVA SOURCECODE:-
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

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

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1,b2,b3;
TextView t1;
ConstraintLayout lay;
b1=findViewById(R.id.button);
b2=findViewById(R.id.button2);
lay=findViewById(R.id.lay);
b3=findViewById(R.id.button3);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
lay.setBackgroundColor(Color.RED);
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
lay.setBackgroundColor(Color.GREEN);
}
});
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
lay.setBackgroundColor(Color.YELLOW);
}
});
}
}
OUTPUT:-
PROBLEM STATEMENT 3: Write an Android code to show the working of Activity Lifecycle

OBJECTIVE:- To learn the concept of dalvik logcat and activity lifecycle


XML SOURCECODE:-
<?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
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" />

</androidx.constraintlayout.widget.ConstraintLayout>

JAVA SOURCECODE:-
package com.example.myapplication;

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("activity_lifecycle","Oncreate_invoked");

}
protected void onRestart(){
super.onRestart();
Log.d("activity_lifecycle","OnRestart_invoked");
}
protected void onStart(){
super.onStart();
Log.d("activity_lifecycle","OnStart_invoked");
}
protected void onResume(){
super.onResume();
Log.d("activity_lifecycle","OnResume_invoked");
}
protected void onPause(){
super.onPause();
Log.d("activity_lifecycle","OnPause_invoked");
}
protected void onStop(){
super.onStop();
Log.d("activity_lifecycle","OnStop_invoked");
}
protected void onDestroy(){
super.onDestroy();
Log.d("activity_lifecycle","OnDestroy_invoked");
}
}

OUTPUT:-
PROBLEM STATEMENT 6: Develop an application that validates the login page and
registration page.
OBJECTIVE:- To learn the objective of toast and if statement

XML SOURCECODE:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<EditText
android:id="@+id/usernameEditText"
android:hint="Username"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<EditText
android:id="@+id/passwordEditText"
android:hint="Password"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<Button
android:text="Login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="login" />

</LinearLayout>

JAVA SOURCECODE:-
package com.example.myapplication;

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

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

EditText usernameEditText, passwordEditText;

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

usernameEditText = findViewById(R.id.usernameEditText);
passwordEditText = findViewById(R.id.passwordEditText);
}

public void login(View view) {


String username = usernameEditText.getText().toString();
String password = passwordEditText.getText().toString();

if (username.equals("admin") && password.equals("password")) {


Toast.makeText(this, "Login successful",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Invalid username or password",
Toast.LENGTH_SHORT).show();
}
}
}

OUTPUT:-
PROBLEM STATEMENT 7: Write an Android code to design a password with the combination of
letter, digit and special character.
OBJECTIVE:- To learn the concept of String and android programing

XML SOURCECODE:-
<?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_parent"
android:gravity="center">

<TextView
android:id="@+id/passwordTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:textStyle="bold" />

<Button
android:id="@+id/generatePasswordButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Generate Password"
android:onClick="generatePasswordButtonOnClick"
android:layout_marginTop="24dp" />

</LinearLayout>

JAVA SOURCECODE:-
package com.example.passwordgenrate;

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

import com.example.passwordgenrate.R;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

private static final String CHARACTERS =


"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+
";

TextView passwordTextView;

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

passwordTextView = findViewById(R.id.passwordTextView);

String generatedPassword = generatePassword(12);


passwordTextView.setText(generatedPassword);
}

public void generatePasswordButtonOnClick(View view) {


String generatedPassword = generatePassword(12);
passwordTextView.setText(generatedPassword);
}

private String generatePassword(int length) {


Random random = new Random();
StringBuilder sb = new StringBuilder(length);
for (int i = 0; i < length; i++) {
int randomIndex = random.nextInt(CHARACTERS.length());
char randomChar = CHARACTERS.charAt(randomIndex);
sb.append(randomChar);
}
return sb.toString();
}
}

OUTPUT:

You might also like