- Get link
- X
- Other Apps
๐ฑ Building an Android Login & Review App with SQLite and RecyclerView
In this post, we'll walk through how to build an Android app with two core features:
A login system using SQLite.
A review list display using RecyclerView and CardView.
This is a beginner-friendly project and a great way to learn about Android components like
SQLiteOpenHelper
,RecyclerView
, and Activity navigation.
๐ง Project Structure
-
DBHelper.java
– Handles login credentials. -
MainActivity3.java
– Displays a list of reviews. -
MyAdapter.java
– Custom RecyclerView Adapter. -
userentry.xml
– Layout for each review card. -
activity_main3.xml
– Layout forMainActivity3
. -
Other activities like
MainActivity4
(add reviews) andMainActivity5
(details view) are referenced but not covered in this post.
๐ DBHelper.java – Login DB Helper
package com.example.exmprac;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context) {
super(context, "loginner.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table logindeet (name TEXT primary key, pswd TEXT )");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS logindeet");
}
public void insert(String name, String pswd) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", name);
values.put("pswd", pswd);
db.insert("logindeet", null, values);
}
public boolean veri(String name, String pswd) {
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM logindeet WHERE name = ? AND pswd=?", new String[]{name, pswd});
boolean b = cursor.moveToPosition(0);
cursor.close();
return b;
}
}
๐ MainActivity3.java – Display Reviews
package com.example.exmprac;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import java.util.ArrayList;
public class MainActivity3 extends AppCompatActivity {
public ListView list;
public FloatingActionButton fab;
public DBHelper2 dbHelper; // Assumes DBHelper2 is defined elsewhere
private RecyclerView rcv;
MyAdapter adapter;
ArrayList<String> name, review;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main3);
Toast.makeText(this, "MainActivity3 started", Toast.LENGTH_SHORT).show();
dbHelper = new DBHelper2(this);
name = new ArrayList<>();
review = new ArrayList<>();
adapter = new MyAdapter(this, name, review);
fab = findViewById(R.id.fab2);
rcv = findViewById(R.id.rc);
rcv.setAdapter(adapter);
rcv.setLayoutManager(new LinearLayoutManager(this));
populateview();
fab.setOnClickListener(view -> {
Intent switchActivityIntent = new Intent(MainActivity3.this, MainActivity4.class);
startActivity(switchActivityIntent);
});
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
}
private void populateview() {
Cursor cursor = dbHelper.fetch();
if (cursor.getCount() == 0)
Toast.makeText(this, "No data exists", Toast.LENGTH_SHORT).show();
while (cursor.moveToNext()) {
name.add(cursor.getString(0));
review.add(cursor.getString(1));
}
adapter.notifyDataSetChanged();
}
}
๐งพ userentry.xml – Review Card Layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp">
<LinearLayout
android:id="@+id/lvv"
android:layout_width="412dp"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Name: " />
</LinearLayout>
<TextView
android:id="@+id/textView5"
android:layout_width="236dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Review: " />
<Button
android:id="@+id/button4"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Print" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" />
</LinearLayout>
</androidx.cardview.widget.CardView>
๐งฉ MyAdapter.java – Custom Adapter for RecyclerView
package com.example.exmprac;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private Context context;
private ArrayList name, review;
public MyAdapter(Context context, ArrayList name, ArrayList review) {
this.context = context;
this.name = name;
this.review = review;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.userentry, parent, false);
return new MyViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.name.setText(String.valueOf(name.get(position)));
holder.review.setText(String.valueOf(review.get(position)));
holder.bt.setOnClickListener(view -> {
Intent intz = new Intent(context, MainActivity5.class);
intz.putExtra("name", String.valueOf(name.get(position)));
intz.putExtra("review", String.valueOf(review.get(position)));
context.startActivity(intz);
});
}
@Override
public int getItemCount() {
return name.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView name, review;
Button bt;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
name = itemView.findViewById(R.id.textView4);
review = itemView.findViewById(R.id.textView5);
bt = itemView.findViewById(R.id.button4);
}
}
}
- Get link
- X
- Other Apps
Comments
Post a Comment