Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

AP All Practical

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

Practical : 1

Aim: To print “hello world” using string.xml file in android application.

CODE:
activity_main.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"
android:background="#70C9F1"
tools:context=".MainActivity">

<TextView
android:layout_width="150dp"
android:layout_height="60dp"
android:background="#02415E"
android:paddingTop="17dp"
android:text="Hello World!"
android:textAlignment="center"
android:textColor="#03A9F4"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.practical1;

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

public class MainActivity extends AppCompatActivity {

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

OUTPUT:
Practical : 2
Aim: Create an Android App to implement Activity life cycle.

CODE:
activity_main.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">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java
package com.example.practical2;

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

public class MainActivity extends AppCompatActivity {


Toast toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(getApplicationContext(), "onCreate Called",
Toast.LENGTH_LONG).show();
}
protected void onStart() {
super.onStart();
Toast.makeText(getApplicationContext(), "onStart Called",
Toast.LENGTH_LONG).show();
}
@Override
protected void onRestart() {
super.onRestart();
Toast.makeText(getApplicationContext(), "onRestart Called",
Toast.LENGTH_LONG).show();
}
protected void onPause() {
super.onPause();
Toast.makeText(getApplicationContext(), "onPause Called",
Toast.LENGTH_LONG).show();
}
protected void onResume() {
super.onResume();
Toast.makeText(getApplicationContext(), "onResume Called",
Toast.LENGTH_LONG).show();
}
protected void onStop() {
super.onStop();
Toast.makeText(getApplicationContext(), "onStop Called",
Toast.LENGTH_LONG).show();
}
protected void onDestroy() {
super.onDestroy();
Toast.makeText(getApplicationContext(), "onDestroy Called",
Toast.LENGTH_LONG).show();
}
}

OUTPUT:
Practical : 3
Aim: To perform button click event in android application. There are 3
different methods used to handle button click event. (Mention All 3
methods)

CODE:
activity_main.xml
<?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:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:gravity="center|center_horizontal"
android:orientation="vertical"
tools:context=".MainActivity">

<Button
android:id="@+id/btnToast1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1st way"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="147dp"
tools:layout_editor_absoluteY="75dp" />

<Button
android:id="@+id/btnToast2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="2nd way"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="147dp"
tools:layout_editor_absoluteY="147dp" />

<Button
android:id="@+id/btnToast3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:onClick="thway"
android:text="3rd way"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="147dp"
tools:layout_editor_absoluteY="220dp" />

</LinearLayout>

MainActivity.java
package com.example.practical3;

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;

// ( OnClickListener event 3 ways)


public class MainActivity extends AppCompatActivity implements View.OnClickListener {

Button b1,b2,b3;

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

b1=findViewById(R.id.btnToast1);
b2=findViewById(R.id.btnToast2);
b3=findViewById(R.id.btnToast3);

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "1st way ", Toast.LENGTH_SHORT).show();
}
});

b2.setOnClickListener(this);
}

@Override
public void onClick(View view) {
Toast.makeText(this, "2nd way", Toast.LENGTH_SHORT).show();

public void thway(View view) {


Toast.makeText(this, "3rd way", Toast.LENGTH_SHORT).show();
}
}
Practical : 4
Aim: Create an Android App to design simple calculator.

CODE:
activity_main.xml
<?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:background="@android:color/holo_blue_light"
android:orientation="vertical"
android:padding="20dp"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:text="CALCULATOR"
android:textColor="@android:color/black"
android:textSize="25sp" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:orientation="horizontal">
<EditText
android:id="@+id/first_no"
android:layout_width="102dp"
android:layout_height="59dp"
android:layout_marginHorizontal="50dp"
android:ems="10"
android:hint="Enter"
android:textColorHint="@color/black"/>
<EditText
android:id="@+id/second_no"
android:layout_width="102dp"
android:layout_height="59dp"
android:ems="10"
android:hint="Enter"
android:textColorHint="@color/black"/>

</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:orientation="horizontal">
<TextView
android:id="@+id/answer"
android:layout_width="102dp"
android:layout_height="59dp"
android:layout_marginHorizontal="50dp"
android:hint="ans"
android:textSize="35sp"
android:textColorHint="@color/black"/>
</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="250dp"
android:layout_marginBottom="30dp"
android:orientation="vertical">
<Button
android:id="@+id/sub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:text="-"
android:textSize="25sp" />
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:text="+"
android:textSize="25sp"
tools:ignore="OnClick" />
<Button
android:id="@+id/div"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:text="/"
android:textSize="25sp" />
<Button
android:id="@+id/mul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:text="X"
android:textSize="25sp" />
<Button
android:id="@+id/equals"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:text="="
android:textSize="35sp" />
</LinearLayout>

</LinearLayout>

MainActivity.java
package com.example.practical4;

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;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText no1 , no2;
Button add ,mul ,div , sub,equal;
TextView answer;
double ans = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// for text views
no1 = findViewById(R.id.first_no);
no2 = findViewById(R.id.second_no);

// for button with operations


add = findViewById(R.id.add);
mul = findViewById(R.id.mul);
div = findViewById(R.id.div);
sub = findViewById(R.id.sub);

// for equal to button


equal = findViewById(R.id.equals);

// for answer field


answer = findViewById(R.id.answer);

add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String num1 = no1.getText().toString();
String num2 = no2.getText().toString();
if (num1.isEmpty() || num2.isEmpty()) {
Toast.makeText(getApplicationContext(),"Enter
Numbers",Toast.LENGTH_SHORT).show();
}
else {
double a = Double.parseDouble(no1.getText().toString());
double b = Double.parseDouble(no2.getText().toString());
ans = a + b;
}
}
});
sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String num1 = no1.getText().toString();
String num2 = no2.getText().toString();
if (num1.isEmpty() || num2.isEmpty()) {
Toast.makeText(getApplicationContext(),"Enter
Numbers",Toast.LENGTH_SHORT).show();
}
else {
double a = Double.parseDouble(no1.getText().toString());
double b = Double.parseDouble(no2.getText().toString());
ans = a - b;
}
}
});
mul.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String num1 = no1.getText().toString();
String num2 = no2.getText().toString();
if (num1.isEmpty() || num2.isEmpty()) {
Toast.makeText(getApplicationContext(),"Enter
Numbers",Toast.LENGTH_SHORT).show();
}
else {
double a = Double.parseDouble(no1.getText().toString());
double b = Double.parseDouble(no2.getText().toString());
ans = a * b;
}
}
});
div.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String num1 = no1.getText().toString();
String num2 = no2.getText().toString();

if (num1.isEmpty() || num2.isEmpty()) {
Toast.makeText(getApplicationContext(), "Enter Numbers",
Toast.LENGTH_SHORT).show();
} else {
double a = Double.parseDouble(no1.getText().toString());
double b = Double.parseDouble(no2.getText().toString());
if (b != 0)
ans = a / b;
else
Toast.makeText(getApplicationContext(), "Enter Valid Numbers",
Toast.LENGTH_SHORT).show();
}
}
});
equal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String ans1 = String.valueOf(ans);
answer.setText(ans1);
ans= 0;
}
});

}
}

OUTPUT:
Practical : 5
Aim: To implement Splash screen(Welcome Screen) without animation in
android.

CODE:
activity_main.xml
?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/idRLContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"

android:orientation="vertical"
tools:context=".MainActivity">
<!-- android:background="@color/material_dynamic_primary80"-->

<!--on below line we are adding an image view-->


<ImageView
android:id="@+id/idIVLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginStart="25dp"
android:layout_marginTop="25dp"
android:layout_marginEnd="25dp"
android:layout_marginBottom="25dp"
android:src="@drawable/splash_icon" />

<!--on below line we are creating progress bar-->


<ProgressBar
android:id="@+id/idPBLoading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/idIVLogo"
android:layout_centerInParent="true"
android:indeterminateTint="@color/white" />
</RelativeLayout>

activity_main2.xml

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


<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity2">

<!--on below line we are creating


a text for our app-->
<TextView
android:id="@+id/idTVHeading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="20dp"
android:gravity="center"
android:padding="10dp"
android:text="Welcome"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold" />

</RelativeLayout>

MainActivity.java

package com.example.practical5;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.WindowManager;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// on below line we are configuring our window to full screen


getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

setContentView(R.layout.activity_main);

// on below line we are calling handler to run a task


// for specific time interval
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// on below line we are
// creating a new intent
Intent i = new Intent(MainActivity.this, MainActivity2.class);

// on below line we are


// starting a new activity.
startActivity(i);

// on the below line we are finishing


// our current activity.
finish();
}
}, 2000);

}
}

MainActivity2.java

package com.example.practical5;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity2 extends AppCompatActivity {

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

Output :
Practical : 6
Aim: Create an Android App to perform Toast.
CODE:
activity_main.xml
<?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:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:gravity="center|center_horizontal"
android:orientation="vertical"
tools:context=".MainActivity">

<Button
android:id="@+id/btnToast1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1st way"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="147dp"
tools:layout_editor_absoluteY="75dp" />

<Button
android:id="@+id/btnToast2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="2nd way"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="147dp"
tools:layout_editor_absoluteY="147dp" />

<Button
android:id="@+id/btnToast3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:onClick="thway"
android:text="3rd way"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="147dp"
tools:layout_editor_absoluteY="220dp" />

</LinearLayout>

MainActivity.java
package com.example.practical3;

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;
// ( OnClickListener event 3 ways)
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

Button b1,b2,b3;

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

b1=findViewById(R.id.btnToast1);
b2=findViewById(R.id.btnToast2);
b3=findViewById(R.id.btnToast3);

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "1st way ", Toast.LENGTH_SHORT).show();
}
});

b2.setOnClickListener(this);
}

@Override
public void onClick(View view) {
Toast.makeText(this, "2nd way", Toast.LENGTH_SHORT).show();
}

public void thway(View view) {


Toast.makeText(this, "3rd way", Toast.LENGTH_SHORT).show();
}
}

OUTPUT:
Practical : 7
Aim: Create an Android App to display Dialog Box in android.
CODE:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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:layout_marginTop="180dp"
android:gravity="center_horizontal"
android:text="Press The Back Button of Your Phone."
android:textSize="30dp"
android:textStyle="bold" />
</RelativeLayout>

MainActivity.java
package com.example.practical7;

import android.content.DialogInterface;
import android.os.Bundle;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


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

// Declare the onBackPressed method when the back button is pressed this method will call
@Override
public void onBackPressed() {
// Create the object of AlertDialog Builder class
super.onBackPressed();
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

// Set the message show for the Alert time


builder.setMessage("Do you want to exit ?");

// Set Alert Title


builder.setTitle("Alert !");

// Set Cancelable false for when the user clicks on the outside the Dialog Box then it will
remain show
builder.setCancelable(false);

// Set the positive button with yes name Lambda OnClickListener method is use of
DialogInterface interface.
builder.setPositiveButton("Yes", (DialogInterface.OnClickListener) (dialog, which) -> {
// When the user click yes button then app will close
finish();
});
// Set the Negative button with No name Lambda OnClickListener method is use of
DialogInterface interface.
builder.setNegativeButton("No", (DialogInterface.OnClickListener) (dialog, which) -> {
// If user click no then dialog box is canceled.
dialog.cancel();
});

// Create the Alert dialog


AlertDialog alertDialog = builder.create();
// Show the Alert Dialog box
alertDialog.show();
}
}

Output :
Practical : 8
Aim: To perform Explicit Intent in android.

CODE:
activity_main.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"
android:background="#F47DC5"
tools:context=".MainActivity">

<TextView
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Welcome to Home Screen"
android:textAlignment="center"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#D6336B"
android:onClick="newsScreen"
android:text="Go to New Screen"
android:textColor="#000000"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java
package com.example.practical9;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void newsScreen(View view) {
Intent i = new Intent(getApplicationContext(), MainActivity2.class);
startActivity(i);
}
}

activity_main2.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"
android:background="#F47DC5"
tools:context=".MainActivity2">

<TextView
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Welcome to New Screen"
android:textAlignment="center"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#D6336B"
android:onClick="homeScreen"
android:text="Go to Home Screen"
android:textColor="@color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity2.java
package com.example.practical9;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity2 extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
public void homeScreen(View view) {
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
}
}
Output :
Practical : 9
Aim: Create an Android App to perform Implicit Intent - Messages wiring
components together. The source and destination for the content transfer
are not known. Only the task and the action to be performed are known.

CODE:
activity_main.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"
android:background="#E5A3F1"
tools:context=".MainActivity">

<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Search here"
android:textColorHint="@color/black"
android:minHeight="48dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="search"
android:text="Search"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java
package com.example.practical8;
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 {

@Override
protected void onCreate(Bundle savedInstanceState) {
EditText editText;
Button button;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.btn);
editText = (EditText) findViewById(R.id.editText);

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String url=editText.getText().toString();
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
}
});
}
}

Output :
Practical : 10
Aim: To Design UI of LDRP logo and title screen using Constraint Layout.
(Use Vertical and Horizontal Guidelines)

CODE:
activity_main.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"
android:background="#C881D5"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginTop="60dp"
android:src="@drawable/ldrp_805"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="LDRP-ITR"
android:textSize="28sp"
app:layout_constraintEnd_toStartOf="@+id/guideline8"
app:layout_constraintStart_toStartOf="@+id/guideline7"
app:layout_constraintTop_toBottomOf="@+id/imageView" />

<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="Log in"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="Sign up"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="20dp" />

<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_end="20dp" />

<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_begin="20dp" />

<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_end="20dp" />

<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_begin="20dp"
app:layout_constraintGuide_percent="0.75" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java
package com.example.practical10;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

Output:
Practical : 11
Aim: Create an Android App to implement checkbox and radio button.

CODE:
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/tvRg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="Select The Subject"
android:textAppearance="?android:attr/textAppearanceMedium" />

<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tvRg"
android:layout_centerHorizontal="true"
android:orientation="horizontal"
android:showDividers="beginning|middle|end"
android:layout_marginTop="10dp"
android:id="@+id/radioGroup" >
<RadioButton
android:id="@+id/rb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Maths"
android:checked="false" />
<RadioButton
android:id="@+id/rb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chemistry"
android:checked="true" />
<RadioButton
android:id="@+id/rb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Physics"
android:checked="false" />
</RadioGroup>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SUBMIT"
android:id="@+id/btnSubmit"
android:layout_below="@+id/radioGroup"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/tvCb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/radioGroup"
android:gravity="center"
android:text="Give Your Ratings Here."
android:layout_marginTop="65dp"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Excellent"
android:id="@+id/cb1"
android:layout_below="@+id/tvCb"
android:layout_centerHorizontal="true"
android:checked="false"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Good"
android:id="@+id/cb2"
android:layout_below="@+id/cb1"
android:layout_centerHorizontal="true"
android:checked="false"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Okay"
android:id="@+id/cb3"
android:layout_below="@+id/cb2"
android:layout_centerHorizontal="true"
android:checked="false"/>
</RelativeLayout>

MainActivity.java
package com.example.practical11;
import android.os.Bundle;
//import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.CheckBox;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


// These are the global variables
RadioGroup radioGroup;
RadioButton selectedRadioButton;
Button buttonSubmit;
CheckBox cb1, cb2, cb3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// layout instances
buttonSubmit = (Button) findViewById(R.id.btnSubmit);
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
cb1 = (CheckBox) findViewById(R.id.cb1);
cb2 = (CheckBox) findViewById(R.id.cb2);
cb3 = (CheckBox) findViewById(R.id.cb3);
buttonSubmit.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// get the selected RadioButton of the group
selectedRadioButton =
(RadioButton)findViewById(radioGroup.getCheckedRadioButtonId());
//get RadioButton text
String yourVote = selectedRadioButton.getText().toString();
// display it as Toast to the user
Toast.makeText(MainActivity.this, "Selected Radio Button is:" + yourVote ,
Toast.LENGTH_LONG).show();
}
});
buttonSubmit.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
//Get the selected RadioButton
selectedRadioButton = (RadioButton)
findViewById(radioGroup.getCheckedRadioButtonId());
// get RadioButton text
String yourVote = selectedRadioButton.getText().toString();
String checkBoxChoices = "";
if (cb1.isChecked()) {
checkBoxChoices += cb1.getText().toString() + "\tYES";
}
else{
checkBoxChoices += cb1.getText().toString() + "\tNO";
}
if (cb2.isChecked()) {
checkBoxChoices += cb2.getText().toString() + "\tYES";
}
else{
checkBoxChoices += cb2.getText().toString() + "\tNO";
}
if (cb3.isChecked()) {
checkBoxChoices += cb3.getText().toString() + "\tYES";
}
else{
checkBoxChoices += cb3.getText().toString() + "\tNO";
}
Toast.makeText(MainActivity.this, "Selected Radio Button is:" + yourVote + "\n
CheckBox Choices: \n "+ checkBoxChoices , Toast.LENGTH_LONG).show();
}
});
}
}

OUTPUT:
Practical : 12
Aim: Create an Android App to implement SeekBar and menu.

CODE:
activity_main.xml
<?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">

<SeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:max="100" />

<TextView
android:id="@+id/seekBarValueTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/seekBar"
android:layout_centerHorizontal="true"
android:paddingTop="16dp"
android:text="SeekBar Value: 0"
android:textSize="18sp" />
</RelativeLayout>

menu_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_item1"
android:title="Menu Item 1" />
<item
android:id="@+id/menu_item2"
android:title="Menu Item 2" />
<item
android:id="@+id/menu_item3"
android:title="Menu Item 3" />
</menu>

MainActivity.java
package com.example.practical12;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SeekBar;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private TextView seekBarValueTextView;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize views
seekBarValueTextView = findViewById(R.id.seekBarValueTextView);
SeekBar seekBar = findViewById(R.id.seekBar);

// SeekBar listener
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// Update TextView with SeekBar value
seekBarValueTextView.setText("SeekBar Value: " + progress);
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.menu_item1:
// Handle action for menu item 1
return true;
case R.id.menu_item2:
// Handle action for menu item 2
return true;
case R.id.menu_item3:
// Handle action for menu item 3
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}

OUTPUT:
Practical : 13
Aim: To implement Splash screen(Welcome Screen) with animation in
android.

CODE:
activity_main.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"
android:background="#fff"
tools:context=".MainActivity">

<!-- THIS IS SIMPLE TEXTVIEW-->


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome To Home"
android:textColor="#EA0909"
android:textSize="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Side_slide.xml
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android">

<!--THIS CODE IS FOR SIDE ANIMATION-->


<translate
android:duration="1500"
android:fromXDelta="-50%"
android:fromYDelta="0%" />

<alpha
android:duration="1500"
android:fromAlpha="0.1"
android:toAlpha="1.0" />
</set>

activity_splash_screen.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"
android:background="#fff"
tools:context=".SplashScreen">

<!--THIS IS IMAGEVIEW FOR OUR IMAGE IN SPLASH SCREEN-->


<!--YOU CAN ADD YOUR IMAGE TO DRAWABLES.-->
<ImageView
android:id="@+id/SplashScreenImage"
android:layout_width="300dp"
android:layout_height="200dp"
android:src="@drawable/icon"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

SplashScreen.java
package com.example.practical13;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.WindowManager;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;

@SuppressWarnings("deprecation")
public class SplashScreen extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
// This is used to hide the status bar and make
// the splash screen as a full screen activity.
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN
);
// HERE WE ARE TAKING THE REFERENCE OF OUR IMAGE
// SO THAT WE CAN PERFORM ANIMATION USING THAT IMAGE
ImageView backgroundImage = findViewById(R.id.SplashScreenImage);
android.view.animation.Animation slideAnimation =
AnimationUtils.loadAnimation(this, R.anim.side_slide);
backgroundImage.startAnimation(slideAnimation);
// we used the postDelayed(Runnable, time) method
// to send a message with a delayed time.
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(SplashScreen.this, MainActivity.class);
startActivity(intent);
finish();
}
}, 3000); // 3000 is the delayed time in milliseconds.
}
}
OUTPUT:
Practical : 14
Aim: Create an Android App to implement Change language (Eng to Hindi)

CODE:
activity_main.xml
<?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">

<!--text view for the message to display-->

<TextView

android:id="@+id/textView"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_margin="48dp"

android:text="Change The Language"

android:textSize="25sp"

android:textAlignment="center" />

<!--button view for hindi language-->

<Button

android:id="@+id/btnHindi"

android:layout_width="wrap_content"

android:layout_height="wrap_content"
android:layout_margin="16dp"

android:background="@color/colorPrimary"

android:text="Hindi"

android:textSize="25sp"

android:textColor="#212121" />

<!--button view for english language-->

<Button

android:id="@+id/btnEnglish"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_margin="16dp"

android:background="@color/colorPrimary"

android:text="English"

android:textSize="25sp"
android:textColor="#212121" />

</LinearLayout>

MainActivity.java
package com.example.practical14;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView messageView;
Button btnHindi, btnEnglish;
Context context;
Resources resources;

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

// referencing the text and button views


messageView = (TextView) findViewById(R.id.textView);
btnHindi = findViewById(R.id.btnHindi);
btnEnglish = findViewById(R.id.btnEnglish);

// setting up on click listener event over the button


// in order to change the language with the help of
// LocaleHelper class
btnEnglish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
context = LocaleHelper.setLocale(MainActivity.this, "en");
resources = context.getResources();
messageView.setText(resources.getString(R.string.language));
}
});

btnHindi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
context = LocaleHelper.setLocale(MainActivity.this, "hi");
resources = context.getResources();
messageView.setText(resources.getString(R.string.language));
}
});

}
}

LocalHelper.java
package com.example.practical14;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.preference.PreferenceManager;

import java.util.Locale;

class LocaleHelper {
private static final String SELECTED_LANGUAGE =
"Locale.Helper.Selected.Language";

// the method is used to set the language at runtime


public static Context setLocale(Context context, String language) {
persist(context, language);

// updating the language for devices above android nougat


if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return updateResources(context, language);
}
// for devices having lower version of android os
return updateResourcesLegacy(context, language);
}

private static void persist(Context context, String language) {


SharedPreferences preferences =
PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(SELECTED_LANGUAGE, language);
editor.apply();
}

// the method is used update the language of application by creating


// object of inbuilt Locale class and passing language argument to it
@TargetApi(Build.VERSION_CODES.N)
private static Context updateResources(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);

Configuration configuration = context.getResources().getConfiguration();


configuration.setLocale(locale);
configuration.setLayoutDirection(locale);

return context.createConfigurationContext(configuration);
}

@SuppressWarnings("deprecation")
private static Context updateResourcesLegacy(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);

Resources resources = context.getResources();

Configuration configuration = resources.getConfiguration();


configuration.locale = locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
configuration.setLayoutDirection(locale);
}

resources.updateConfiguration(configuration, resources.getDisplayMetrics());

return context;
}
}

values\strings.xml
<resources>
<string name="app_name">Practical 14</string>
<string name="selected_language">English</string>
<string name="language">Hello</string>
</resources>

Hi-Rin\strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">GFG | Change App Language</string>

<string name="selected_language">हि न्दी</string>

<string name="language">नमस्ते </string>


</resources>

OUTPUT :
Practical : 15
Aim: Create an Android App to implement Shared Preference.

CODE:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:ignore="HardcodedText">

<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:text="Shared Preferences Demo"
android:textColor="@android:color/black"
android:textSize="24sp" />

<!--EditText to take the data from the user and save the data in SharedPreferences-->
<EditText
android:id="@+id/edit1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textview"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="Enter your Name"
android:minHeight="48dp"
android:padding="10dp" />

<!--EditText to take the data from the user and save the data in SharedPreferences-->
<EditText
android:id="@+id/edit2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/edit1"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="Enter your Age"
android:inputType="number"
android:minHeight="48dp"
android:padding="10dp" />
</RelativeLayout>

MainActivity.java
package com.example.practical15;

import androidx.appcompat.app.AppCompatActivity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {


private EditText name, age;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = findViewById(R.id.edit1);
age = findViewById(R.id.edit2);
}

// Fetch the stored data in onResume() Because this is what will be called when the app
opens again
@Override
protected void onResume() {
super.onResume();
// Fetching the stored data from the SharedPreference
SharedPreferences sh = getSharedPreferences("MySharedPref", MODE_PRIVATE);
String s1 = sh.getString("name", "");
int a = sh.getInt("age", 0);

// Setting the fetched data in the EditTexts


name.setText(s1);
age.setText(String.valueOf(a));
}

// Store the data in the SharedPreference in the onPause() method


// When the user closes the application onPause() will be called and data will be stored
@Override
protected void onPause() {
super.onPause();
// Creating a shared pref object with a file name "MySharedPref" in private mode
SharedPreferences sharedPreferences = getSharedPreferences("MySharedPref",
MODE_PRIVATE);
SharedPreferences.Editor myEdit = sharedPreferences.edit();

// write all the data entered by the user in SharedPreference and apply
myEdit.putString("name", name.getText().toString());
myEdit.putInt("age", Integer.parseInt(age.getText().toString()));
myEdit.apply();
}
}

OUTPUT :
Practical : 16
Aim: Create an Android App to display student details in List View (using
Adapter class).

CODE:
activity_main.xml
<?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">

<ListView
android:id="@+id/simpleListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="@color/material_blue_grey_800"
android:dividerHeight="1dp" />
</LinearLayout>

Activity_listview.xml
<?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">

<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"

android:paddingBottom="20dp"
android:textColor="@color/black" />
</LinearLayout>

MainActivity.java
package com.example.practical16;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;import android.widget.ListView;

public class MainActivity extends Activity


{
// Array of strings...
ListView simpleList;
String countryList[] = {"India", "China", "australia", "Portugle", "America",
"NewZealand"};

@Override protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
simpleList = (ListView)findViewById(R.id.simpleListView);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
R.layout.activity_listview, R.id.textView, countryList);
simpleList.setAdapter(arrayAdapter);
}
}
OUTPUT:
Practical : 17
Aim: To implement Web View in android application.
CODE:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!-- unique ID of WebView -->
<WebView
android:id="@+id/web"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp" />
</RelativeLayout>

MainActivity.java
package com.example.practical17;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find the WebView by its unique ID
WebView webView = findViewById(R.id.web);
// loading https://www.geeksforgeeks.org url in the WebView.
webView.loadUrl("https://www.ldrp.ac.in");
// this will enable the javascript.
webView.getSettings().setJavaScriptEnabled(true);
// WebViewClient allows you to handle
// onPageFinished and override Url loading.
webView.setWebViewClient(new WebViewClient());
}
}

AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
OUTPUT :
Practical : 18
Aim: Create an Android App to implement CRUD operation using SqLite
Database.
CODE:
activity_main.xml
<?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">

<EditText
android:id="@+id/fname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="First Name"
android:inputType="text" />

<EditText
android:id="@+id/lname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Last Name"
android:inputType="text" />

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

<Button
android:id="@+id/submit_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="SUBMIT" />

<Button
android:id="@+id/edit_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EDIT"
android:visibility="gone" />

<Button
android:id="@+id/display_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="DISPLAY" />
</LinearLayout>
</LinearLayout>
DBmain.java
package com.example.practical18;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

public class DBmain extends SQLiteOpenHelper {


private static final String DBNAME = "student";
private static final String TABLE = "subject";
private static final int VER = 1;

public DBmain(@Nullable Context context) {


super(context, DBNAME, null, VER);
}

@Override
public void onCreate(SQLiteDatabase db) {
String query = "create table " + TABLE + "(id integer primary key, fname text, lname
text)";
db.execSQL(query);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String query = "drop table if exists " + TABLE + "";
db.execSQL(query);
onCreate(db);
}
}
MainActivity.java
package com.example.practical18;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


DBmain dBmain;
SQLiteDatabase sqLiteDatabase;
EditText lname, fname;
Button submit, display, edit;
int id = 0;

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

dBmain = new DBmain(this);


//create object
findid();
insertData();
cleardata();
editdata();
}
private void editdata() {
if (getIntent().getBundleExtra("studata") != null) {
Bundle bundle = getIntent().getBundleExtra("studata");
id = bundle.getInt("id");
fname.setText(bundle.getString("fname"));
lname.setText(bundle.getString("lname"));

edit.setVisibility(View.VISIBLE);
submit.setVisibility(View.GONE);
}
}

private void cleardata() {


fname.setText("");
lname.setText("");
}

private void insertData() {


submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ContentValues contentValues = new ContentValues();
contentValues.put("fname", fname.getText().toString().trim());
contentValues.put("lname", lname.getText().toString().trim());

sqLiteDatabase = dBmain.getWritableDatabase();
Long recid = sqLiteDatabase.insert("subject", null, contentValues);
if (recid != null) {
Toast.makeText(MainActivity.this, "successfully insert",
Toast.LENGTH_SHORT).show();
cleardata();
} else {
Toast.makeText(MainActivity.this, "something wrong try again",
Toast.LENGTH_SHORT).show();
}
}
});
display.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
startActivity(intent);
}
});
edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ContentValues contentValues = new ContentValues();
contentValues.put("fname", fname.getText().toString().trim());
contentValues.put("lname", lname.getText().toString().trim());

sqLiteDatabase = dBmain.getWritableDatabase();
long recid = sqLiteDatabase.update("subject", contentValues, "id=" + id, null);
if (recid != -1) {
Toast.makeText(MainActivity.this, "Update successfully",
Toast.LENGTH_SHORT).show();
submit.setVisibility(View.VISIBLE);
edit.setVisibility(View.GONE);
cleardata();
} else {
Toast.makeText(MainActivity.this, "something wrong try again",
Toast.LENGTH_SHORT).show();
}
}
});
}

private void findid() {


fname = (EditText) findViewById(R.id.fname);
lname = (EditText) findViewById(R.id.lname);
submit = (Button) findViewById(R.id.submit_btn);
display = (Button) findViewById(R.id.display_btn);
edit = (Button) findViewById(R.id.edit_btn);
}
}
Activity_main2.xml
<?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"
tools:context=".MainActivity2">
<ListView
android:divider="#E6DCCE"
android:dividerHeight="1dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lv"/>
</LinearLayout>
MainActivity2.java
package com.example.practical18;

import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Random;

public class MainActivity2 extends AppCompatActivity {


DBmain dBmain;
SQLiteDatabase sqLiteDatabase;
String[] fname, lname;
int[] id;
ListView lv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
dBmain = new DBmain(this);
findid();
displaydata();

private void displaydata() {


sqLiteDatabase = dBmain.getReadableDatabase();
Cursor cursor = sqLiteDatabase.rawQuery("select *from subject", null);
if (cursor.getCount() > 0) {
id = new int[cursor.getCount()];
fname = new String[cursor.getCount()];
lname = new String[cursor.getCount()];
int i = 0;

while (cursor.moveToNext()) {
id[i] = cursor.getInt(0);
fname[i] = cursor.getString(1);
lname[i] = cursor.getString(2);
i++;
}
CustAdapter custAdapter = new CustAdapter();
lv.setAdapter(custAdapter);
}
}

private void findid() {


lv = (ListView) findViewById(R.id.lv);
}

private class CustAdapter extends BaseAdapter {


@Override
public int getCount() {
return fname.length;
}

@Override
public Object getItem(int position) {
return null;
}

@Override
public long getItemId(int position) {
return 0;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
TextView txtfname, txtlname;
ImageButton txt_edit, txt_delete;
CardView cardview;
convertView = LayoutInflater.from(MainActivity2.this).inflate(R.layout.singledata,
parent, false);
txtfname = convertView.findViewById(R.id.txt_fname);
txtlname = convertView.findViewById(R.id.txt_lname);
txt_edit = convertView.findViewById(R.id.txt_edti);
txt_delete = convertView.findViewById(R.id.txt_delete);
cardview = convertView.findViewById(R.id.cardview);
cardview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//background random color
Random random = new Random();
cardview.setCardBackgroundColor(Color.argb(255, random.nextInt(256),
random.nextInt(256), random.nextInt(256)));
txt_delete.setVisibility(View.VISIBLE);
txt_edit.setVisibility(View.VISIBLE);
txtfname.setVisibility(View.GONE);
txtlname.setVisibility(View.GONE);
}
});
txtfname.setText(fname[position]);
txtlname.setText(lname[position]);
txt_edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle bundle = new Bundle();
bundle.putInt("id", id[position]);
bundle.putString("fname", fname[position]);
bundle.putString("lname", lname[position]);
Intent intent = new Intent(MainActivity2.this, MainActivity.class);
intent.putExtra("studata", bundle);
startActivity(intent);
}
});
txt_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sqLiteDatabase = dBmain.getReadableDatabase();
long recremove = sqLiteDatabase.delete("subject", "id=" + id[position], null);
if (recremove != -1) {
Toast.makeText(MainActivity2.this, "successfully delete",
Toast.LENGTH_SHORT).show();
startActivity(new Intent(MainActivity2.this, MainActivity.class));
displaydata();
}
}
});
return convertView;
}
}
}
Singledata.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="12dp"
app:cardElevation="8dp"
android:id="@+id/cardview"
app:cardMaxElevation="8dp"
app:cardPreventCornerOverlap="true"
app:cardUseCompatPadding="true">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp">
<TextView
android:visibility="visible"
android:id="@+id/txt_fname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="textfname" />

<TextView
android:visibility="visible"
android:id="@+id/txt_lname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:gravity="center"
android:text="textlname" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/txt_lname"
android:orientation="horizontal">

<ImageButton
android:background="@null"
android:layout_weight="1"
android:visibility="gone"
android:id="@+id/txt_edti"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_baseline_edit_24"/>

<ImageButton
android:visibility="gone"
android:background="@null"
android:src="@drawable/ic_baseline_delete_24"
android:id="@+id/txt_delete"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>
</RelativeLayout>
</androidx.cardview.widget.CardView>
OUTPUT :
Practical : 19
Aim: Create an Android App to implement Recycler View.
CODE:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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">

<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recycler_view"/>

</FrameLayout>
MainActivity.java
package com.example.practical19;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

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

RecyclerView recyclerView = findViewById(R.id.recycler_view);

recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(new CustomAdapter(generateData()));
recyclerView.addItemDecoration(new DividerItemDecoration(this,
DividerItemDecoration.VERTICAL));
}

private List<String> generateData() {


List<String> data = new ArrayList<>();
for (int i = 0; i < 10; i++) {
data.add(String.valueOf(i) + "th Element");
}
return data;
}

}
list_item_view.xml
<?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="46dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textview"
android:text="TextView"
android:textSize="16dp" />

</LinearLayout>
CustomAdapter.java
package com.example.practical19;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {


private List<String> data;
public CustomAdapter (List<String> data){
this.data = data;
}

@Override
public CustomAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int
viewType) {
View rowItem =
LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_view, parent, false);
return new ViewHolder(rowItem);
}

@Override
public void onBindViewHolder(CustomAdapter.ViewHolder holder, int position) {
holder.textView.setText(this.data.get(position));
}

@Override
public int getItemCount() {
return this.data.size();
}

public static class ViewHolder extends RecyclerView.ViewHolder implements


View.OnClickListener {
private TextView textView;

public ViewHolder(View view) {


super(view);
view.setOnClickListener(this);
this.textView = view.findViewById(R.id.textview);
}

@Override
public void onClick(View view) {
Toast.makeText(view.getContext(), "position : " + getLayoutPosition() + " text : " +
this.textView.getText(), Toast.LENGTH_SHORT).show();
}
}
}
OUTPUT:

You might also like