AP All Practical
AP All Practical
AP All Practical
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;
@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;
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;
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();
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);
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"-->
activity_main2.xml
</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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
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();
}
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;
// 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 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();
});
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;
@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;
@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;
@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;
@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;
// 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">
Side_slide.xml
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android">
<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">
</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">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="48dp"
android:textSize="25sp"
android:textAlignment="center" />
<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
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);
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";
return context.createConfigurationContext(configuration);
}
@SuppressWarnings("deprecation")
private static Context updateResourcesLegacy(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(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>
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;
@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);
// 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;
MainActivity.java
package com.example.practical17;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import androidx.appcompat.app.AppCompatActivity;
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;
@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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit.setVisibility(View.VISIBLE);
submit.setVisibility(View.GONE);
}
}
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();
}
}
});
}
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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
dBmain = new DBmain(this);
findid();
displaydata();
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);
}
}
@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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(new CustomAdapter(generateData()));
recyclerView.addItemDecoration(new DividerItemDecoration(this,
DividerItemDecoration.VERTICAL));
}
}
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;
@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();
}
@Override
public void onClick(View view) {
Toast.makeText(view.getContext(), "position : " + getLayoutPosition() + " text : " +
this.textView.getText(), Toast.LENGTH_SHORT).show();
}
}
}
OUTPUT: