Android SQLite Database Tutorial

Android SQLite Tutorial

Android SQLite Tutorial

Android SQLite Database Tutorial is part of local persistence tutorials. Local Persistence means the storage of data to the device for later use, even after the program that created it has been closed.

Options: 1.Using a SQLite database and Room Database. This is (obviously) the most powerful option, especially for querying data. 2.Using shared preferences. This is great for simple data, it’s very easy to use. Probably not the best way to store complicated objects or large amounts of data. 3.Using local files. Not good for querying data. Good for complicated objects and large amounts of data. You have to write the code that reads the data from the files, as well as the code that writes the data to the files. You have full control over the file format. Easy to copy this data to another device as a file.

Required Dependencies

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation "androidx.recyclerview:recyclerview:1.2.0-alpha01"
implementation 'com.rengwuxian.materialedittext:library:2.1.4'
implementation "androidx.cardview:cardview:1.0.0"
implementation 'com.google.code.gson:gson:2.8.6'
def sqlite_version = "2.1.0"

// Java language implementation
implementation "androidx.sqlite:sqlite:$sqlite_version"

}

Manifest File

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="com.androidhands.todolistsqlite.NewToDoItem" />
<activity android:name="com.androidhands.todolistsqlite.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

Styles XML File

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="menuStyle"
parent="Theme.AppCompat.Light">
<item name="android:background">@android:color/white</item>

</style>

</resources>

Main Activity XML File

sqlite database tutorial
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<include
android:id="@+id/toolBar"
layout="@layout/bar_layout"/>

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


</LinearLayout>

New TODO Item Activity XML File

<?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=".NewToDoItem">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
app:titleTextColor="@android:color/white"
style="@style/Base.ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/menuStyle"
android:id="@+id/toolBar"/>

<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:layout_margin="10dp">
<com.rengwuxian.materialedittext.MaterialEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="TO Do Item"
android:id="@+id/itemName"
android:textColor="@color/colorAccent"
android:textColorHint="@color/colorAccent"
android:padding="8dp"/>

</FrameLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="To Do Date"
android:textColor="@color/colorAccent"
android:textSize="20dp"
android:textStyle="bold"
android:id="@+id/toDoDate"
android:layout_weight="4"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Select Date"
android:id="@+id/toDoDateBtn"
android:textAllCaps="false"
android:textColor="@android:color/white"
android:background="@color/colorAccent"/>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="To Do Time"
android:textColor="@color/colorAccent"
android:textSize="20dp"
android:id="@+id/toDoTime"
android:textStyle="bold"
android:layout_weight="4"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Select Time"
android:textAllCaps="false"
android:id="@+id/toDoTimeBtn"
android:textColor="@android:color/white"
android:background="@color/colorAccent"/>
</LinearLayout>

<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:layout_margin="20dp">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/selectableItemBackground"
android:text="Add New Item"
android:textAllCaps="false"
android:id="@+id/saveNewItemBtn"
android:textColor="@android:color/white"
android:textStyle="bold"/>
</FrameLayout>

</LinearLayout>

ToDO List Item Row Layout XML File

<?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="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_margin="3dp">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="10dp"
app:cardCornerRadius="5dp"
app:cardBackgroundColor="@color/colorAccent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/toDoItem"
android:textColor="@android:color/white"
android:textStyle="bold"
android:textSize="20dp"
android:text="To Do Item"
android:padding="8dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/toDoDate"
android:textColor="@android:color/white"
android:textSize="14dp"
android:text="To Do Date"
android:padding="8dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/toDoTime"
android:textColor="@android:color/white"
android:textSize="12dp"
android:text="To Do Time"
android:padding="8dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@android:color/white"
android:layout_weight="1">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Edit"
android:id="@+id/edit"
android:textAllCaps="false"
android:textColor="@color/colorAccent"
android:background="?attr/selectableItemBackground"
/>
</FrameLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@android:color/white"
android:layout_weight="1">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Delete"
android:textAllCaps="false"
android:background="?attr/selectableItemBackground"
android:textColor="@color/colorAccent"
android:id="@+id/delete"
/>
</FrameLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@android:color/white"
android:textColor="@color/colorAccent"
android:layout_weight="1">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/selectableItemBackground"
android:textColor="@color/colorAccent"
android:text="Complete"
android:textAllCaps="false"
android:id="@+id/complete" />
</FrameLayout>



</LinearLayout>

</LinearLayout>

</androidx.cardview.widget.CardView>

</LinearLayout>

App Bar Layout XML File

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar 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="wrap_content"
app:titleTextColor="@android:color/white"
android:background="@color/colorAccent"
style="@style/Base.ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/menuStyle"
android:id="@+id/toolBar"

>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:orientation="vertical">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_action_add"
android:layout_margin="5dp"
tools:ignore="UselessParent">


<Button
android:layout_width="60dp"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:padding="5dp"
android:textAllCaps="false"
android:id="@+id/addNewItemActivityBtn"
android:textColor="@color/colorAccent"
tools:ignore="HardcodedText" />
</FrameLayout>
</LinearLayout>


</androidx.appcompat.widget.Toolbar>

Main Activity Java File

public class MainActivity extends AppCompatActivity {
Button addNewItem;
RecyclerView recyclerView;
public RecyclerView.Adapter adapter;
public List<ToDoModel> toDoModelList = new ArrayList<>();

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolBar);
setSupportActionBar(toolbar);
Objects.requireNonNull(getSupportActionBar()).setTitle("To Do List");
addNewItem = findViewById(R.id.addNewItemActivityBtn);
addNewItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,NewToDoItem.class));
finish();
}
});

recyclerView = findViewById(R.id.toDoRecyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);

String json = getIntent().getStringExtra("list");
if (json != null){
Gson gson = new Gson();
TypeToken<List<ToDoModel>> typeToken = new TypeToken<List<ToDoModel>>() {
};
List<ToDoModel> newItems = gson.fromJson(json, typeToken.getType());
Log.d("TAG", "onActivityResult: "+gson.toJson(newItems));
toDoModelList.addAll(newItems);




}else{
toDoModelList.add(new ToDoModel("Attend morning meeting","2020-03-07","11:00 AM",false));
}
adapter = new ToDoRecyclerAdapter(toDoModelList);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}

Android SQLite Database (create open helper class)

Lets start Android SQLite Database Tutorial. Create new Java file and name it ( itemsOpenHelperClass ) and this class will extend SQliteOpenHelper.

Related posts

Leave a Comment