Regi -explicit
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#FAFAFA"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:gravity="center_horizontal"
android:padding="24dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imgProfileMain"
android:layout_width="120dp"
android:layout_height="120dp"
android:src="@android:drawable/ic_menu_camera"
android:contentDescription="Profile Image"
android:layout_marginBottom="24dp"
android:background="@drawable/circle_bg"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/tvTitle"
android:text="User Registration"
android:textSize="28sp"
android:textStyle="bold"
android:textColor="#3F51B5"
android:layout_marginBottom="24dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/etName"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@drawable/edittext_bg"
android:padding="12dp"
android:hint="Enter your full name"
android:inputType="textPersonName"
android:textColor="#212121"
android:textSize="16sp" />
<EditText
android:id="@+id/etEmail"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@drawable/edittext_bg"
android:padding="12dp"
android:hint="Enter your email address"
android:inputType="textEmailAddress"
android:textColor="#212121"
android:textSize="16sp"
android:layout_marginTop="16dp" />
<RadioGroup
android:id="@+id/rgGender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp">
<RadioButton
android:id="@+id/rbMale"
android:text="Male"
android:textColor="#3F51B5"
android:textSize="16sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/rbFemale"
android:text="Female"
android:textColor="#3F51B5"
android:textSize="16sp"
android:layout_marginStart="24dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
<Spinner
android:id="@+id/spCountry"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginTop="20dp"
android:background="@drawable/edittext_bg"
android:padding="8dp" />
<Button
android:id="@+id/btnDob"
android:layout_width="match_parent"
android:layout_height="48dp"
android:text="Select Date of Birth"
android:textColor="#FFFFFF"
android:backgroundTint="#3F51B5"
android:textSize="16sp"
android:layout_marginTop="20dp" />
<TextView
android:id="@+id/tvDob"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#757575"
android:textSize="16sp"
android:gravity="center"
android:layout_marginTop="8dp" />
<ProgressBar
android:id="@+id/pbLoading"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginTop="24dp"
android:visibility="gone"
android:indeterminateTint="#3F51B5" />
<Button
android:id="@+id/btnSubmit"
android:layout_width="match_parent"
android:layout_height="48dp"
android:text="Submit"
android:textColor="#FFFFFF"
android:backgroundTint="#00796B"
android:textSize="16sp"
android:layout_marginTop="24dp" />
</LinearLayout>
</ScrollView>
Activity_next.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center"
android:padding="24dp"
android:background="#FAFAFA"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imgProfileNext"
android:layout_width="120dp"
android:layout_height="120dp"
android:src="@android:drawable/ic_menu_camera"
android:contentDescription="Profile Image"
android:layout_marginBottom="24dp"
android:background="@drawable/circle_bg"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/tvResult"
android:textColor="#222222"
android:textSize="20sp"
android:lineSpacingExtra="6dp"
android:textAlignment="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ProgressBar
android:id="@+id/pbLoadingNext"
android:layout_width="48dp"
android:layout_height="48dp"
android:visibility="gone"
android:layout_marginTop="20dp"
android:indeterminateTint="#3F51B5" />
</LinearLayout>
MainAcitivity.java:
package com.example.registration_form;
import androidx.appcompat.app.AppCompatActivity;
import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity implements DatePickerDialog.OnDateSetListener {
private EditText etName, etEmail;
private RadioGroup rgGender;
private Spinner spCountry;
private Button btnDob, btnSubmit;
private TextView tvDob;
private ProgressBar pbLoading;
private String selectedGender = "";
private String selectedDate = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etName = findViewById(R.id.etName);
etEmail = findViewById(R.id.etEmail);
rgGender = findViewById(R.id.rgGender);
spCountry = findViewById(R.id.spCountry);
btnDob = findViewById(R.id.btnDob);
btnSubmit = findViewById(R.id.btnSubmit);
tvDob = findViewById(R.id.tvDob);
pbLoading = findViewById(R.id.pbLoading);
String[] countries = {
"Select Country",
"United States",
"Canada",
"United Kingdom",
"Australia",
"India",
"Germany",
"France",
"Japan"
};
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_spinner_item, countries);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spCountry.setAdapter(adapter);
rgGender.setOnCheckedChangeListener((group, checkedId) -> {
RadioButton rb = findViewById(checkedId);
if (rb != null) selectedGender = rb.getText().toString();
});
btnDob.setOnClickListener(v -> {
Calendar calendar = Calendar.getInstance();
DatePickerDialog dpd = new DatePickerDialog(
MainActivity.this,
MainActivity.this,
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
dpd.show();
});
btnSubmit.setOnClickListener(v -> {
pbLoading.setVisibility(View.VISIBLE);
String name = etName.getText().toString().trim();
String email = etEmail.getText().toString().trim();
String country = spCountry.getSelectedItem().toString();
Intent intent = new Intent(MainActivity.this, NextActivity.class);
intent.putExtra("name", name);
intent.putExtra("email", email);
intent.putExtra("gender", selectedGender);
intent.putExtra("country", country);
intent.putExtra("dob", selectedDate);
startActivity(intent);
pbLoading.setVisibility(View.GONE);
});
}
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
selectedDate = String.format("%d-%02d-%02d", year, month + 1, dayOfMonth);
tvDob.setText("Selected Date: " + selectedDate);
}
}
NextActivity.java:
package com.example.registration_form;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
public class NextActivity extends AppCompatActivity {
private TextView tvResult;
private ProgressBar pbLoadingNext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_next);
tvResult = findViewById(R.id.tvResult);
pbLoadingNext = findViewById(R.id.pbLoadingNext);
pbLoadingNext.setVisibility(View.VISIBLE);
tvResult.setVisibility(View.GONE);
new Handler().postDelayed(() -> {
String name = getIntent().getStringExtra("name");
String email = getIntent().getStringExtra("email");
String gender = getIntent().getStringExtra("gender");
String country = getIntent().getStringExtra("country");
String dob = getIntent().getStringExtra("dob");
String displayText = "User Details\n\n" +
"Name: " + (name.isEmpty() ? "N/A" : name) + "\n" +
"Email: " + (email.isEmpty() ? "N/A" : email) + "\n" +
"Gender: " + (gender.isEmpty() ? "N/A" : gender) + "\n" +
"Country: " + (country.equals("Select Country") ? "N/A" : country) + "\n" +
"Date of Birth: " + (dob.isEmpty() ? "N/A" : dob);
tvResult.setText(displayText);
pbLoadingNext.setVisibility(View.GONE);
tvResult.setVisibility(View.VISIBLE);
}, 1500);
}
}
Implicit
MainActivity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button browseButton, dialButton, smsButton, shareButton, emailButton, shareDataButton, buttonOpenSecond;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
browseButton = findViewById(R.id.browseButton);
dialButton = findViewById(R.id.dialButton);
smsButton = findViewById(R.id.smsButton);
shareButton = findViewById(R.id.shareButton);
emailButton = findViewById(R.id.emailButton);
shareDataButton = findViewById(R.id.shareDataButton);
buttonOpenSecond = findViewById(R.id.buttonOpenSecond);
browseButton.setOnClickListener(v -> {
String url = "https://www.google.com";
Intent browseIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(browseIntent);
});
dialButton.setOnClickListener(v -> {
Intent dialIntent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:+913979484100"));
startActivity(dialIntent);
});
smsButton.setOnClickListener(v -> {
Intent smsIntent = new Intent(Intent.ACTION_SENDTO);
smsIntent.setData(Uri.parse("smsto:" + Uri.encode("+913979484100")));
smsIntent.putExtra("sms_body", "Lalalalalala");
startActivity(smsIntent);
});
shareButton.setOnClickListener(v -> {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "hjejkhekjh");
startActivity(Intent.createChooser(shareIntent, "Share via"));
});
emailButton.setOnClickListener(v -> {
Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
emailIntent.setData(Uri.parse("mailto:hehaho@gmail.com"));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "give placement");
emailIntent.putExtra(Intent.EXTRA_TEXT, "10lpa minimum needed!!!!!!!");
startActivity(emailIntent);
});
shareDataButton.setOnClickListener(v -> {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "This is the text data to share with other apps!");
startActivity(Intent.createChooser(intent, "Share text via"));
});
buttonOpenSecond.setOnClickListener(v -> {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
});
}
}
SecondActivity.java:
package com.example.receiverapp;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView txtreceive;
txtreceive=findViewById(R.id.txtreceive);
Intent getintent;
getintent=getIntent();
String val=getintent.getStringExtra(Intent.EXTRA_TEXT);
txtreceive.setText(val);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.ReceiverApp"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
</intent-filter>
</activity>
</application>
</manifest>
File Storage(ext & int)
MainActivity.java:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class MainActivity extends AppCompatActivity {
private String storedText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText editText = findViewById(R.id.editText);
TextView textView = findViewById(R.id.textView);
Button internalStoreBtn = findViewById(R.id.internalStoreBtn);
Button internalLoadBtn = findViewById(R.id.internalLoadBtn);
Button externalStoreBtn = findViewById(R.id.externalStoreBtn);
Button externalLoadBtn = findViewById(R.id.externalLoadBtn);
String extStorageState = Environment.getExternalStorageState();
internalStoreBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String currentText = editText.getText().toString();
try (FileOutputStream fos = openFileOutput("messagefile", MODE_PRIVATE);
OutputStreamWriter writer = new OutputStreamWriter(fos)) {
writer.write(currentText);
writer.flush();
Toast.makeText(getApplicationContext(), "Message saved", Toast.LENGTH_LONG).show();
editText.setText("");
} catch (IOException e) {
e.printStackTrace();
}
}
});
internalLoadBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try (FileInputStream fis = openFileInput("messagefile");
InputStreamReader reader = new InputStreamReader(fis)) {
char[] buffer = new char[250];
int charRead;
StringBuilder stringBuilder = new StringBuilder();
while ((charRead = reader.read(buffer)) > 0) {
String data = String.copyValueOf(buffer, 0, charRead);
stringBuilder.append(data);
}
Toast.makeText(getApplicationContext(), "Message read", Toast.LENGTH_LONG).show();
textView.setText(stringBuilder.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
externalStoreBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Environment.MEDIA_MOUNTED.equals(extStorageState)) {
File myFile = new File(getExternalFilesDir("mydir"), "sample.txt");
String currentText = editText.getText().toString();
try (FileOutputStream fos = new FileOutputStream(myFile);
OutputStreamWriter writer = new OutputStreamWriter(fos)) {
writer.write(currentText);
writer.flush();
Toast.makeText(getApplicationContext(), "Message saved to external storage", Toast.LENGTH_LONG).show();
editText.setText("");
System.out.println(getExternalFilesDir("mydir"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
externalLoadBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Environment.MEDIA_MOUNTED.equals(extStorageState) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState)) {
File myFile = new File(getExternalFilesDir("mydir"), "sample.txt");
try (FileInputStream fis = new FileInputStream(myFile);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader reader = new BufferedReader(isr)) {
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line).append("\n");
}
Toast.makeText(getApplicationContext(), "Message read from external storage", Toast.LENGTH_LONG).show();
textView.setText(stringBuilder.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
}
}
Animation
MainAcitivity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity2 extends AppCompatActivity {
Button btnRotate, btnTranslate, btnScaling,
btnFadeIn, btnFadeOut, btnBounce, btnGrey,
btnInvert;
ImageView imageView;
boolean isGrey = false;
boolean isInverted = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
imageView = findViewById(R.id.imageView);
btnRotate = findViewById(R.id.btnRotate);
btnTranslate = findViewById(R.id.btnTranslate);
btnScaling = findViewById(R.id.btnScaling);
btnFadeIn = findViewById(R.id.btnFadeIn);
btnFadeOut = findViewById(R.id.btnFadeOut);
btnBounce = findViewById(R.id.btnBounce);
btnGrey = findViewById(R.id.btnGrey);
btnInvert = findViewById(R.id.btnInvert);
btnRotate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Animation rotate = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.rotate);
imageView.startAnimation(rotate);
}
});
btnTranslate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Animation translate = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.translate);
imageView.startAnimation(translate);
}
});
btnScaling.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Animation scale = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.scale);
imageView.startAnimation(scale);
}
});
btnFadeIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Animation fadeIn = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.fade_in);
imageView.startAnimation(fadeIn);
}
});
btnFadeOut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Animation fadeOut = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.fade_out);
imageView.startAnimation(fadeOut);
}
});
btnBounce.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Animation bounce = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.bounce);
imageView.startAnimation(bounce);
}
});
btnGrey.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bitmap originalBitmap = BitmapFactory.decodeResource(
getResources(),R.drawable.killua);
Bitmap grayscaleBitmap = Bitmap.createBitmap(originalBitmap.getWidth(),
originalBitmap.getHeight(),
originalBitmap.getConfig());
if(isGrey){
imageView.setImageBitmap(originalBitmap);
isGrey = false;
}
else{
for(int x = 0; x < originalBitmap.getWidth();x++){
for(int y = 0; y < originalBitmap.getHeight();y++){
int pixel = originalBitmap.getPixel(x,y);
int alpha = Color.alpha(pixel);
int gray = (Color.red(pixel) + Color.blue(pixel) + Color.blue(pixel)) / 3;
grayscaleBitmap.setPixel(x,y,Color.argb(alpha,gray,gray,gray));
}
}
imageView.setImageBitmap(grayscaleBitmap);
isGrey = true;
}
}
});
btnInvert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bitmap originalBitmap = BitmapFactory.decodeResource(
getResources(),R.drawable.killua);
Bitmap grayscaleBitmap = Bitmap.createBitmap(originalBitmap.getWidth(),
originalBitmap.getHeight(),
originalBitmap.getConfig());
if(isGrey){
imageView.setImageBitmap(originalBitmap);
isGrey = false;
}
else{
for(int x = 0; x < originalBitmap.getWidth();x++){
for(int y = 0; y < originalBitmap.getHeight();y++){
int pixel = originalBitmap.getPixel(x,y);
int alpha = Color.alpha(pixel);
int red = 255-Color.red(pixel);
int blue = 255 - Color.blue(pixel);
int green = 255 - Color.green(pixel);
grayscaleBitmap.setPixel(x,y,Color.argb(alpha,red,green,blue));
}
}
imageView.setImageBitmap(grayscaleBitmap);
isGrey = true;
}
}
});
}
}
audio video
MainActivity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;
public class MainActivity extends AppCompatActivity {
VideoView vv;
Button play,pause,stop,vplay;
MediaPlayer mp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
play=findViewById(R.id.play);
pause=findViewById(R.id.pause);
stop=findViewById(R.id.stop);
vplay=findViewById(R.id.vplay);
vv=findViewById(R.id.vv);
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mp == null) {
mp=MediaPlayer.create(getApplicationContext(),R.raw.sample);
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
mp.start();
Toast.makeText(getApplicationContext(),"Playing",Toast.LENGTH_LONG).show();
}
}
});
pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mp!=null){
mp.pause();
Toast.makeText(getApplicationContext(),"Audio paused",Toast.LENGTH_LONG).show();
}
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mp!=null){
mp.release();
mp=null;
Toast.makeText(getApplicationContext(),"Video stopped",Toast.LENGTH_LONG).show();
}
}
});
vplay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String path="android.resource://"+getPackageName()+"/"+R.raw.video;
Uri uri=Uri.parse(path);
vv.setVideoURI(uri);
MediaController mc= new MediaController(MainActivity.this);
mc.setAnchorView(vv);
vv.setMediaController(mc);
vv.start();
}
});
}
}
GPS
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Gps"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java:
package com.example.gps;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
public class MainActivity extends AppCompatActivity implements LocationListener {
private LocationManager lm;
Button btnlocation;
TextView txtlocation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtlocation = findViewById(R.id.txtlocation);
btnlocation = findViewById(R.id.btnlocation);
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 100);
}
btnlocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getLocation();
}
});
}
private void getLocation() {
lm = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 100, MainActivity.this);
}
@Override
public void onLocationChanged(@NonNull Location location) {
Toast.makeText(getApplicationContext(),location.getLongitude()
+ " " +location.getLatitude(),Toast.LENGTH_LONG).show();
Geocoder geocoder = new Geocoder(MainActivity.this, Locale.getDefault());
try {
List <Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
String address = addresses.get(0).getAddressLine(0);
txtlocation.setText(address);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
volleyball
build.grade.kts:
implementation("com.android.volley:volley:1.2.1")
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Volley"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java:
package com.example.volley;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity {
Button btnString, btnJson;
TextView txtdata;
String links = "https://www.w3schools.com/xml/note.xml";
String Jsonlinks = "https://mocki.io/v1/7d6d5cdb-04bf-4df6-8e0c-5e0a70069eb4";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtdata = findViewById(R.id.textView);
btnString = findViewById(R.id.btnString);
btnJson = findViewById(R.id.btnJson);
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
btnString.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringRequest stringRequest = new StringRequest(Request.Method.GET, links, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
txtdata.setText(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
queue.add(stringRequest);
}
});
btnJson.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET,
Jsonlinks, null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
buffer.append("Roll no.: " + obj.getInt("roll") + "\n" +
"Name: " + obj.getString("name") + "\n" +
"Email: " + obj.getString("email") + "\n\n");
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
txtdata.setText(buffer.toString());
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
}
);
queue.add(jsonArrayRequest);
}
});
}
}
okhttp
build.gradle.kts:
implementation("com.squareup.okhttp3:okhttp:4.12.0")
AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET"/>
MainActivity.java:
package com.example.okhttp;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttp;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity {
Button btnString, btnJson;
TextView txtdata;
String links = "https://www.w3schools.com/xml/note.xml";
String Jsonlinks = "https://mocki.io/v1/7d6d5cdb-04bf-4df6-8e0c-5e0a70069eb4";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtdata = findViewById(R.id.textView);
btnString = findViewById(R.id.btnString);
btnJson = findViewById(R.id.btnJson);
OkHttpClient client = new OkHttpClient();
btnString.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Request req = new Request.Builder().url(links).build();
client.newCall(req).enqueue(new Callback() {
@Override
public void onFailure(@NonNull Call call, @NonNull IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
txtdata.setText(response.body().string());
}
});
}
});
btnJson.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Request jsonReq = new Request.Builder().url(Jsonlinks).build();
client.newCall(jsonReq).enqueue(new Callback() {
@Override
public void onFailure(@NonNull Call call, @NonNull IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
try {
String jsonString = response.body().string();
JSONArray jsonArray = new JSONArray(jsonString);
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject studentObj = jsonArray.getJSONObject(i);
buffer.append("Roll no.: " + studentObj.getInt("roll") + "\n" +
"Name: " + studentObj.getString("name") + "\n" +
"Email: " + studentObj.getString("email") + "\n\n");
}
MainActivity.this.runOnUiThread(() -> {
txtdata.setText(buffer.toString());
});
} catch (JSONException e) {
e.printStackTrace();
MainActivity.this.runOnUiThread(() -> {
txtdata.setText(e.getMessage());
});
}
}
});
}
});
}
}
-> res → layout → activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btnString"
android:text="Fetch XML"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnJson"
android:text="Fetch JSON"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp" />
<TextView
android:id="@+id/textView"
android:text=""
android:textSize="16sp"
android:padding="16dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
retrofit
MainActivity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView txtview;
Button btnfetch;
String url= "https://jsonplaceholder.typicode.com/";
txtview=findViewById(R.id.txtview);
btnfetch=findViewById(R.id.btnfetch);
btnfetch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Retrofit retrofit=new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.build();
myapi api=retrofit.create(myapi.class);
Call<List<model>> call=api.getmodels();
call.enqueue(new Callback<List<model>>() {
@Override
public void onResponse(Call<List<model>> call, Response<List<model>> response) {
List<model> data=response.body();
for(int i=0;i<data.size();i++){
txtview.append("id:"+ data.get(i).getId()+ "\n");
txtview.append("user_id: "+data.get(i).getUserId()+"\n");
txtview.append("Title: "+data.get(i).getTitle()+"\n");
txtview.append("Body: "+data.get(i).getBody()+"\n\n");
}
}
@Override
public void onFailure(Call<List<model>> call, Throwable t) {
}
});
}
});
}
}
myapi.java
package com.example.myapplication;
import java.util.List;
import retrofit2.Call;
import retrofit2.http.GET;
public interface myapi {
@GET("posts")
Call <List<model>> getmodels ();
}
model.java
package com.example.myapplication;
public class model {
int userId,id;
String title,body;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btnfetch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Fetch Posts" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="12dp">
<TextView
android:id="@+id/txtview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:padding="8dp" />
</ScrollView>
</LinearLayout>
crud sqlite
MainActivity.java
package com.example.myapplication;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.app.DatePickerDialog;
import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
private EditText edtname, edtrno, edtDob, edtstream, edtAddress;
private Button btninsert, btnread, btndelete, btnsearch, btnupdate;
private TextView txtdata;
private dbhandler con;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeViews();
con = new dbhandler(this);
con.getWritableDatabase();
setupButtonListeners();
}
private void initializeViews() {
btninsert = findViewById(R.id.btninsert);
btnread = findViewById(R.id.btnread);
btndelete = findViewById(R.id.btndelete);
btnsearch = findViewById(R.id.btnsearch);
btnupdate = findViewById(R.id.btnupdate);
txtdata = findViewById(R.id.txtdata);
edtname = findViewById(R.id.edtname);
edtrno = findViewById(R.id.edtrno);
edtDob = findViewById(R.id.edtDob);
edtstream = findViewById(R.id.edtstream);
edtAddress = findViewById(R.id.edtAddress);
}
private void setupButtonListeners() {
btninsert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = edtname.getText().toString();
int rno = Integer.parseInt(edtrno.getText().toString());
String dob = edtDob.getText().toString();
String stream = edtstream.getText().toString();
String address = edtAddress.getText().toString();
boolean status = con.savedata(name, rno, dob, stream, address);
Toast.makeText(
getApplicationContext(),
status ? "Inserted successfully" : "Insertion failed",
Toast.LENGTH_LONG
).show();
}
});
btnread.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Cursor c = con.showdata();
StringBuffer buffer = new StringBuffer();
if (c.getCount() == 0) {
Toast.makeText(
getApplicationContext(),
"No record Found",
Toast.LENGTH_LONG
).show();
return;
}
while (c.moveToNext()) {
buffer.append("ID:" + c.getString(0) + "\n");
buffer.append("ROLL Number " + c.getInt(1) + "\n");
buffer.append("Student name: " + c.getString(2) + "\n");
String dobStr = c.getString(3);
buffer.append("Date of Birth: " + dobStr + "\n");
try {
int age = calculateAge(dobStr);
buffer.append("Age: " + age + "\n");
} catch (Exception e) {
buffer.append("Age: N/A\n");
}
buffer.append("Stream: " + c.getString(4) + "\n");
buffer.append("Address: " + c.getString(5) + "\n\n");
}
showmessage("Records", buffer.toString());
}
});
btndelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = edtname.getText().toString();
int status = con.deleterecord(name);
Toast.makeText(
getApplicationContext(),
status > 0 ? "Deleted successfully" : "Deletion failed",
Toast.LENGTH_LONG
).show();
}
});
btnupdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = edtname.getText().toString();
int rno = Integer.parseInt(edtrno.getText().toString());
int status = con.updaterecord(name, rno);
Toast.makeText(
getApplicationContext(),
status == -1 ? "Updated unsuccessfully" : "Updated successfully",
Toast.LENGTH_LONG
).show();
}
});
btnsearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Integer rno = Integer.parseInt(edtrno.getText().toString());
StringBuffer buffer = new StringBuffer();
Cursor c = con.searchdata(rno);
if (c.getCount() == 0) {
Toast.makeText(
getApplicationContext(),
"No record Found",
Toast.LENGTH_LONG
).show();
showmessage("records", "no record found");
return;
}
while (c.moveToNext()) {
buffer.append("ID: " + c.getString(0) + "\n");
buffer.append("ROLL Number: " + c.getInt(1) + "\n");
buffer.append("Student name: " + c.getString(2) + "\n");
String dobStr = c.getString(3);
buffer.append("Date of Birth: " + dobStr + "\n");
try {
int age = calculateAge(dobStr);
buffer.append("Age: " + age + "\n");
} catch (Exception e) {
buffer.append("Age: N/A\n");
}
buffer.append("Stream: " + c.getString(4) + "\n");
buffer.append("Address: " + c.getString(5) + "\n\n");
}
txtdata.setText(buffer.toString());
}
});
edtDob.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(
MainActivity.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
edtDob.setText(dayOfMonth + "/" + (monthOfYear + 1) + "/" + year);
}
},
year, month, day
);
datePickerDialog.show();
}
});
}
private void showmessage(String title, String Message) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(Message);
builder.show();
}
private int calculateAge(String dobStr) {
try {
@SuppressLint("SimpleDateFormat")
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("dd/MM/yyyy");
java.util.Date dobDate = sdf.parse(dobStr);
Calendar dob = Calendar.getInstance();
dob.setTime(dobDate);
Calendar today = Calendar.getInstance();
int age = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR);
if (today.get(Calendar.MONTH) < dob.get(Calendar.MONTH) ||
(today.get(Calendar.MONTH) == dob.get(Calendar.MONTH) &&
today.get(Calendar.DAY_OF_MONTH) < dob.get(Calendar.DAY_OF_MONTH))) {
age--;
}
return age;
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
}
DBHandler.java:
package com.example.myapplication;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHandler extends SQLiteOpenHelper {
private static final String DB_NAME = "student.db";
private static final String TABLE_NAME = "student";
private static final int DB_VERSION = 7;
public DatabaseHandler(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE " + TABLE_NAME +
" (" +
"ID INTEGER PRIMARY KEY AUTOINCREMENT," +
"RNO INTEGER," +
"NAME TEXT," +
"DOB TEXT," +
"STREAM TEXT," +
"ADDRESS TEXT" +
")";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion < 4) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
public boolean saveData(String name, int rno, String dob, String stream, String address) {
try {
SQLiteDatabase db = getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("RNO", rno);
contentValues.put("NAME", name);
contentValues.put("DOB", dob);
contentValues.put("STREAM", stream);
contentValues.put("ADDRESS", address);
long status = db.insert(TABLE_NAME, null, contentValues);
return status != -1;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public Cursor showData() {
String query = "SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getReadableDatabase();
return db.rawQuery(query, null);
}
public int deleteRecord(String name) {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(TABLE_NAME, "NAME=?", new String[] { name });
}
public int updateRecord(String name, int rno) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("RNO", rno);
contentValues.put("NAME", name);
return db.update(TABLE_NAME, contentValues, "RNO=" + rno, null);
}
public Cursor searchData(int rno) {
String query = "SELECT * FROM " + TABLE_NAME + " WHERE RNO=?";
SQLiteDatabase db = this.getReadableDatabase();
return db.rawQuery(query, new String[] { String.valueOf(rno) });
}
}
crud firebase
Select Hamburger > Tools > Firebase > Realtime Database Java > Follow the instructions by connecting and creating things
Student.java
package com.example.firebase;
public class Student {
String id;
Integer rollno;
String name;
Double gradePointAverage;
String department;
public Student(String id, Integer rollno, String name, Double gradePointAverage, String department) {
this.id = id;
this.rollno = rollno;
this.name = name;
this.gradePointAverage = gradePointAverage;
this.department = department;
}
public Student() {}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Integer getRollno() {
return rollno;
}
public void setRollno(Integer rollno) {
this.rollno = rollno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getGradePointAverage() {
return gradePointAverage;
}
public void setGradePointAverage(Double gradePointAverage) {
this.gradePointAverage = gradePointAverage;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
}
Activity_xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:padding="16dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<EditText
android:id="@+id/edname"
android:hint="Name"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/edrollno"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Roll No"
android:inputType="numberDecimal" />
<EditText
android:id="@+id/edgpa"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="GPA"
android:inputType="numberDecimal" />
<EditText
android:id="@+id/eddept"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Department" />
<Button
android:id="@+id/btnadd"
android:text="Add User"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btnupdate"
android:text="Update User"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btndel"
android:text="Delete User"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btnview"
android:text="View All Users"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.firebase;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
EditText edname, edrollno, edgpa, eddept;
Button btnadd, btnview, btndel, btnupdate;
ListView lv;
String selectedId = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edname = findViewById(R.id.edname);
edrollno = findViewById(R.id.edrollno);
edgpa = findViewById(R.id.edgpa);
eddept = findViewById(R.id.eddept);
btnadd = findViewById(R.id.btnadd);
btnview = findViewById(R.id.btnview);
btnupdate = findViewById(R.id.btnupdate);
btndel = findViewById(R.id.btndel);
lv = findViewById(R.id.lv);
List<Student> studentList = new ArrayList<>();
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("student");
btnadd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = edname.getText().toString();
Integer roll = Integer.parseInt(edrollno.getText().toString());
Double gpa = Double.parseDouble(edgpa.getText().toString());
String dept = eddept.getText().toString();
selectedId = myRef.push().getKey();
myRef.child(selectedId).setValue(new Student(selectedId, roll, name, gpa, dept));
Toast.makeText(getApplicationContext(),"Data inserted successfully!!!",Toast.LENGTH_LONG).show();
}
});
btndel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!selectedId.isEmpty()){
myRef.child(selectedId).removeValue();
Toast.makeText(getApplicationContext(),"Data deleted successfully!!!",Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(getApplicationContext(),"Error in deletion!!!",Toast.LENGTH_LONG).show();
}
}
});
btnupdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!selectedId.isEmpty()) {
String name = edname.getText().toString();
int rollno = Integer.parseInt(edrollno.getText().toString());
Double gpa = Double.parseDouble(edgpa.getText().toString());
String dept = eddept.getText().toString();
myRef.child(selectedId).setValue(new Student(selectedId, rollno, name, gpa, dept));
Toast.makeText(MainActivity.this, "Data updated successfully!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Select a student to update!", Toast.LENGTH_SHORT).show();
}
}
});
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Student s = studentList.get(position);
edname.setText(s.getName());
edrollno.setText(String.valueOf(s.getRollno()));
edgpa.setText(String.valueOf(s.getGradePointAverage()));
eddept.setText(s.getDepartment());
selectedId = s.getId();
}
});
btnview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
studentList.clear();
for(DataSnapshot snp : snapshot.getChildren()) {
Student std = snp.getValue(Student.class);
studentList.add(std);
}
List<String> display = new ArrayList<>();
for(Student s: studentList) {
display.add("ID: " + s.getId() +
"\nRoll No: " + s.getRollno() +
"\nName: " + s.getName() +
"\nGPA: " + s.getGradePointAverage() +
"\nDepartment: " + s.getDepartment());
}
ArrayAdapter<String> adapter = new ArrayAdapter<>(MainActivity.this,
android.R.layout.simple_list_item_1, display);
lv.setAdapter(adapter);
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
});
}
}
Shared Pref
SplashScreen.java
package com.example.myapplication;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import androidx.appcompat.app.AppCompatActivity;
public class SplashActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
SharedPreferences prefs = getSharedPreferences("MyPrefs", MODE_PRIVATE);
final boolean loggedIn = prefs.getBoolean("isLoggedIn", false);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (loggedIn) {
startActivity(new Intent(SplashActivity.this, HomeActivity.class));
} else {
startActivity(new Intent(SplashActivity.this, LoginActivity.class));
}
finish();
}
}, 1500);
}
}
LoginActivity.java
package com.example.myapplication;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class LoginActivity extends AppCompatActivity {
EditText etUser, etPass;
CheckBox chkRemember;
Button btnLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
etUser = findViewById(R.id.etUser);
etPass = findViewById(R.id.etPass);
chkRemember = findViewById(R.id.chkRemember);
btnLogin = findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(v -> {
String username = etUser.getText().toString().trim();
String password = etPass.getText().toString().trim();
if (username.equals("admin") && password.equals("1234")) {
if (chkRemember.isChecked()) {
SharedPreferences prefs = getSharedPreferences("MyPrefs", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("isLoggedIn", true);
editor.apply();
}
startActivity(new Intent(LoginActivity.this, HomeActivity.class));
finish();
} else {
Toast.makeText(LoginActivity.this, "Invalid Username or Password", Toast.LENGTH_SHORT).show();
}
});
}
}
HomeActivity.java
package com.example.myapplication;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class HomeActivity extends AppCompatActivity {
Button btnLogout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
btnLogout = findViewById(R.id.btnLogout);
btnLogout.setOnClickListener(v -> {
SharedPreferences prefs = getSharedPreferences("MyPrefs", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.clear();
editor.apply();
startActivity(new Intent(HomeActivity.this, LoginActivity.class));
finish();
});
}
}
Alert-SP
MainActivity.java
package com.example.myapplication;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
EditText etUser, etPass;
Button btnLogin;
SharedPreferences prefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etUser = findViewById(R.id.etUser);
etPass = findViewById(R.id.etPass);
btnLogin = findViewById(R.id.btnLogin);
prefs = getSharedPreferences("MyPrefs", MODE_PRIVATE);
etUser.setText(prefs.getString("username", ""));
etPass.setText(prefs.getString("password", ""));
btnLogin.setOnClickListener(v -> showAlert());
}
private void showAlert() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Save Login?");
builder.setMessage("Do you want to save your credentials?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
SharedPreferences.Editor editor = prefs.edit();
editor.putString("username", etUser.getText().toString());
editor.putString("password", etPass.getText().toString());
editor.apply();
Toast.makeText(MainActivity.this, "Saved!", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "Not Saved", Toast.LENGTH_SHORT).show();
}
});
builder.show();
}
}
notification
MainActivity.java
package com.example.myapplication;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationCompat;
import androidx.core.content.ContextCompat;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.btnNotify);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
"myChannel",
"MyChannel",
NotificationManager.IMPORTANCE_DEFAULT
);
NotificationManager manager = getSystemService(NotificationManager.class);
if (manager != null) {
manager.createNotificationChannel(channel);
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
if (ContextCompat.checkSelfPermission(this,
android.Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{android.Manifest.permission.POST_NOTIFICATIONS}, 100);
}
}
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pi = PendingIntent.getActivity(
MainActivity.this,
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
);
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this, "myChannel")
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setContentTitle("Simple Notification")
.setContentText("Tap to open SecondActivity")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pi)
.setAutoCancel(true);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (manager != null) {
manager.notify(1, builder.build());
}
}
});
}
}
SecondActivity.java
package com.example.myapplication;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication">
<activity
android:name=".SecondActivity"
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>