Posts

Select and delete multiple items in a list in Sketchware android project

Image
1. In image manager of project, add delete icon ic_delete_white . 2. In Resource, in menu folder, add an xml file named context_menu.xml . Put following code in this file: <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/delete" android:title="Delete" android:icon="@drawable/ic_delete_white" android:showAsAction="always"/> </menu> 3. Create a new custom variable of type SparseBooleanArray with name selectedItems and define it as new SparseBooleanArray(). 4. Put following codes in onCreate . Change names of variables as in your project. // Enable multiple selection mode with contextual action bar listview1.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL); // Handle selection events via MultiChoiceModeListener listview1.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() { @Override public void onItemCheckedStat...

A simple reminder app in Sketchware

Image
This post describes a simple reminder app using AlarmManager in Sketchware. 1. Create a new project in Sketchware. 2. In main.xml , add an EditText edittext1 , a Button button1 to set time for reminder, a TextView textview1 for displaying the date and time, a Button button2 for setting the reminder, and a ListView listview1 for displaying the reminders. 3. In Image manager, add two images ic_notifications_black and ic_delete_black . 4. Create a custom view items.xml . In this add two TextViews textview1 and textview2 , and an ImageView imageview1 . 5. For listview1, select items.xml as custom view. 6. Switch on AppCompat and design. 7. Add two Calendar components cal and cal2 , add a SharedPreferences component sp:sp and a Dialog component dialog . 8. Create three number variables reminder_code , current_time and code . Create a String variable message and a Map variable map . Also create a ListMap maplist . 9. Add a more block createNotificationChannel and add following cod...

Date and time picker in Sketchware

1. Create a new project in Sketchware. 2. Add a Button button1 for picking date and time. 3. Add a TextView textview1 , for displaying the selected date and time. 4. Add a Calendar component cal . 5. Create two more blocks, pickDate and pickTime . 6. In pickDate more block, use an add source directly block and put following code: DatePickerDialog datePicker = new DatePickerDialog( this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) { cal.set(Calendar.YEAR, year); cal.set(Calendar.MONTH, month); cal.set(Calendar.DAY_OF_MONTH, dayOfMonth); _pickTime(); } }, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH) ); datePicker.show(); 7. In pickTime more block, use an add source directly block and put following code: TimePickerDialog timePicker = new TimePickerDialog( this, new TimePickerDialog.OnTimeSetListener() { @Override ...

EncryptionHelper for encrypting json file

Add below code in a Java file named EncryptionHelper.java. import android.content.ContentResolver; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.net.Uri; import android.os.Environment; import android.os.ParcelFileDescriptor; import android.provider.MediaStore; import android.provider.OpenableColumns; import android.util.Base64; import android.webkit.MimeTypeMap; import android.widget.Toast; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.ByteArrayOutputStream; import java.nio.charset.StandardCharsets; import java.security.spec.KeySpec; import javax.crypto.Cipher; import javax.crypto.CipherInputStream; import javax.crypto.CipherOutputStream; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.GCMParameterSpec; import javax.crypto.spec.PBEKeySpec; import javax.crypto.spec.SecretKeySpec; publ...

Simple java.time.LocalDate examples

Add following imports: import java.time.LocalDate; import java.time.ZoneId; import java.time.format.DateTimeFormatter; Following are examples for displaying date: // Get current LocalDate LocalDate date = LocalDate.now(); // e.g., 28 April 2025 // Format LocalDate DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd MMMM yyyy"); String formattedDate = date.format(formatter); // "28 April 2025" // Display LocalDate textview2.setText(date.toString()); // Result: "2025-04-28" (ISO format) textview3.setText(formattedDate); // Result: "28 April 2025" // LocalDate of a specific time zone ZoneId zone1 = ZoneId.of("Asia/Kolkata"); // or ZoneId.of("+05:30") LocalDate date2 = LocalDate.now(zone1); textview4.setText(date2.format(formatter)); // Result: "28 April 2025" (same date if system time zone matches, otherwise adjusted) // LocalDate from year, month, and day of month LocalDate date3 = LocalDate.o...

Flash light app in Sketchware pro

Image
 Follow the steps given below to create a Flash light android app in Sketchware pro. 1. Create a new app in Sketchware pro. 2. Switch on AppCompat and Design. 3. In View area (main.xml), add a Linear Vertical with gravity set to center_horizontal and center_vertical. Inside this add an ImageView imageview1 . 4. In image manager, add two images ic_flash_on_black and ic_flash_off_black . 5. For imageview1, select ic_flash_off_black as image. 6. In MainActivity, add import event and add following imports in it. import android.hardware.camera2.CameraManager; import android.hardware.camera2.CameraAccessException; 7. Add Camera Component cam. 8. Create two boolean variables hasCameraFlash and flashLightStatus and add a String variable cameraId . 9. Add a custom variable cameraManager of type CameraManager. 10. In onCreate , define hasCameraFlash, cameraManager, and cameraId. Register a callback for cameraManager CameraManager.TorchCallback to listen for changes in the flashlight stat...

Add encryption in firebase group chat

Image
 Brief summary of this post: 1. The user enters a passphrase which is unique to the group chat and has to be shared to the user for granting him access. 2. The passphrase is saved in SharedPreferences. 3. A master key is generated using the passphrase. 4. While sending message, a secret key is generated using the master key and message ID. The message is encrypted using this secret key. 5. On retrieving message, it is decrypted using a secret key generated using master key and message ID. Steps: 1. In Button to enter the group chat page, add a dialog box (dialog2) with EditText (dialog_text1) where user can enter the passphrase. On entering the passphrase it is saved in SharedPreferences (sp:sp) and user moves to group chat activity using intent component. Code to add an EditText to Dialog component: final EditText dialog_text1 = new EditText(AdminpageActivity.this); dialog_text1.setLayoutParams(linear2.getLayoutParams()); dialog2.setView(dialog_text1); Code to get text from EditTe...

Using Ambilwarna Color Picker in settings to set text color and background color

Image
To add Ambilwarna Color Picker library in Sketchware. 1. In the Sketchware project, in options menu select Local library . Clicking on options menu will display the dexer dialog box as shown in image below. Select D8 or Dx in the dialog. 2. It will show dialog to write library dependency. Write the following dependency - com.github.yukuku:ambilwarna:2.0.1 and click on start. It will search and download the library in Sketchware. 3. Select the Ambilwarna library in Local library. To create the settings project, follow the steps given below: 1. In main.xml , add a Settings button and and EditText edittext1 . 2. In MainActivity , add a Shared preferences component s_pref:s_pref and an Intent component intent . 3. In onStart event, use following codes to retrieve text color and background color from shared preferences and set the text color and background color of edittext1. int textcolor = s_pref.getInt("textcolor", Color.BLACK); edittext1.setTextColor(textcolor); int bgco...

Retrieve contact list in Sketchware

Image
 To read contact list in Sketchware pro, follow the steps given below. 1. In the Sketchware project, in main.xml, add a ListView. Also add a custom view for ListView items. For the ListView, select the custom view. 2. Switch on AppCompat and design. 3. In permission manager, select android.permission.READ_CONTACTS . 4. In Activity, add Import event. Put following codes in the import event: import android.Manifest; import android.content.pm.PackageManager; import android.database.Cursor; import android.provider.ContactsContract; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; 5. Create a more block getAllContacts of type List Map. In this more block, put following codes: ArrayList<HashMap<String, Object>> contactList = new ArrayList<>(); ContentResolver contentResolver = MainActivity.this.getContentResolver(); Cursor cursor = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, ...

A simple sqlite example in Sketchware

Image
 In this example, the user can save details of pupils (name, age and place) in sqlite database in android app, and view the data in a ListView. Create a new project in Sketchware. In Options/Configuration, click on Java/Kotlin manager. Create a new Java class file with name DbHandler. Put following codes as contents of this DbHandler.java file. package com.my.newproject2; import android.content.ContentValues; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.database.Cursor; public class DbHandler extends SQLiteOpenHelper { private static final String DB_NAME = "studentdb"; private static final int DB_VERSION = 1; private static final String TABLE_NAME = "students"; private static final String ID_COL = "id"; private static final String NAME_COL = "name"; private static final String AGE_COL = "age"; private s...