This video shows the steps to create a SQLite database from scratch. Then it shows how one can write (Insert or update) the data in the database. Further, it shows the code to query the database and fetch the required data from the database. Finally, it shows how to use the fetched data to display it on a text view in the App’s layout and also to create a PDF file to write that text in the PDF.
We hope you like this video. For any query, suggestions or appreciations we will be glad to hear from you at: programmerworld1990@gmail.com
Source Code:
package com.example.mysqlitedbtopdffile;
import android.content.ContentValues;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Paint;
import android.graphics.pdf.PdfDocument;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;public class MainActivity extends AppCompatActivity {
private mySQLiteDBHandler sqlLiteDBHandler;
private EditText editTextSerialNumberInsert;
private EditText editTextSerialNumberFetch;
private EditText editTextInsert;
private TextView textViewDisplay;private SQLiteDatabase sqLiteDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActivityCompat.requestPermissions(this,new String[]{READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE}, PackageManager.PERMISSION_GRANTED);try {
sqlLiteDBHandler = new mySQLiteDBHandler(this,”PDFDatabase”, null,1);
sqLiteDatabase = sqlLiteDBHandler.getWritableDatabase();
sqLiteDatabase.execSQL(“CREATE TABLE PDFTable(SerialNumber TEXT, Text TEXT)”);
}
catch (Exception e){
e.printStackTrace();
}
editTextInsert = findViewById(R.id.editText2);
editTextSerialNumberInsert = findViewById(R.id.editText);
editTextSerialNumberFetch = findViewById(R.id.editText3);
textViewDisplay = findViewById(R.id.textView);
}public void InsertUpdatedButton(View view){
ContentValues contentValues = new ContentValues();
contentValues.put(“SerialNumber”, editTextSerialNumberInsert.getText().toString());
contentValues.put(“Text”, editTextInsert.getText().toString());
sqLiteDatabase.insert(“PDFTable”,null,contentValues);
sqLiteDatabase.update(“PDFTable”, contentValues, null,null);
}public void CreatePDF(View view){
String query = “Select Text from PDFTable where SerialNumber=” + editTextSerialNumberFetch.getText().toString();
Cursor cursor = sqLiteDatabase.rawQuery(query,null);
try {
cursor.moveToFirst();
textViewDisplay.setText(cursor.getString(0));
}
catch (Exception e){
e.printStackTrace();
textViewDisplay.setText(“”);
return;
}PdfDocument pdfDocument = new PdfDocument();
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(300, 600,1).create();
PdfDocument.Page page = pdfDocument.startPage(pageInfo);
page.getCanvas().drawText(cursor.getString(0),10, 25, new Paint());
pdfDocument.finishPage(page);
String filePath = Environment.getExternalStorageDirectory().getPath()+”/Download/”+editTextSerialNumberFetch.getText().toString()+”.pdf”;
File file = new File(filePath);
try {
pdfDocument.writeTo(new FileOutputStream(file));
} catch (IOException e) {
e.printStackTrace();
}
pdfDocument.close();
}
}————–
package com.example.mysqlitedbtopdffile;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;public class mySQLiteDBHandler extends SQLiteOpenHelper {
public mySQLiteDBHandler(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
}@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.example.mysqlitedbtopdffile”><uses-permission android:name=”android.permission.READ_EXTERNAL_STORAGE”/>
<uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE”/><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=”.MainActivity”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” /><category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
</application></manifest>
hi, i want to save multiple lines on a pdf document, how can i do it? with this code de application saves just 1 line
I think you can repeat below line for as many line of texts you want to write in your pdf file:
page.getCanvas().drawText(cursor.getString(0),10, 25, new Paint());
Alternatively, you also modify the first argument of the above function call to pass on the multiple lines. Something like below:
cursor.getString(0) + “\n” + cursor.getString(1) + “\n” + cursor.getString(2) + … so on.
Hope above helps.
Good Luck
Programmer World
–
“\n” doesn’t work for drawText.
I think we can use multiple times drawText API for every new line. Thus, just repeat the below line for each new line and it should work.
page.getCanvas().drawText(cursor.getString(0),10, 25, new Paint());
Cheers
Programmer World
–
Thank you