Posts

Car racing game in Sketchware

Image
1. In main.xml , add two TextViews text_score and text_hs . Also add a Button button1 . 2. Add a SharedPreferences component sp:sp , and an Intent component intent . 3. Add onStart event and put blocks as shown below. 4. Create a new page game.xml . 5. In the event button1 onClick , use Intent to move to GameActivity. 6. Add two images in drawable folder with names car_yellow and car_blue . In Resource manager, in drawable folder, create a new file car_yellow.xml and put following codes in it. <?xml version="1.0" encoding="utf-8"?> <vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="200dp" android:height="400dp" android:viewportWidth="80" android:viewportHeight="160"> <path android:fillColor="#FFEB3B" android:strokeColor="#222222" android:strokeWidth="1" android:strokeLineJoin="ro...

Pdf.js with open, print and save enabled

Image
This post shows how to use Pdf.js in Sketchware project with OPEN, PRINT AND SAVE enabled. 1. Create a new project in Sketchware pro (package name in my project is com.pdf.onweb). In  main.xml  add a WebView  pdfWebView . 2. Switch On AppCompat and design. 3. Download  pdf.js  from following link: https://github.com/mozilla/pdf.js/releases/download/v5.4.296/pdfjs-5.4.296-dist.zip or https://mozilla.github.io/pdf.js/getting_started/#download 4. Extract the contents of the downloaded zip file. 5. In Sketchware pro project, in  Asset Manager , add a sample pdf file and rename it as  sample.pdf . Also, create a new folder  pdfjs . 6. In  pdfjs folder  import all the contents extracted from the downloaded zip file. 7. In assets folder, for showing custom error page, add error.html . Below is a sample error.html page. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" conte...

Create list of all installed apps

Image
1. This post describes how to create an app which shows a list of all installed apps. 2. In main.xml add a RecyclerView. 3. Switch on AppCompat and design. 4. Create a custom view recycler_item.xml . In this put an ImageView imageview1 , and put three TextViews text_label , text_package , and text_category . 5. For RecyclerView select recycler_item.xml as Custom View. 6. Create a Map List maplist . 7. Add following imports . import android.content.pm.PackageManager; import android.content.pm.ApplicationInfo; 8. In onCreate , get all apps and add to maplist. PackageManager pm = this.getPackageManager(); List<ApplicationInfo> apps = pm.getInstalledApplications(PackageManager.GET_META_DATA); for (ApplicationInfo ai : apps) { int category = ApplicationInfo.CATEGORY_UNDEFINED; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { category = ai.category; } HashMap<String, Object> map = new HashMap<>(); ...

Convert json array to html table

Image
1. Suppose you have a json array, or data in a List Map which is displayed in a ListView. e.g. fruits.json file in assets folder To read it and set the json array to a string variable: a) Add a method to read assets file: public String _readJsonFromAsset(final String _filename) { String json = null; try { InputStream is = this.getAssets().open(_filename); int size = is.available(); byte[] buffer = new byte[size]; is.read(buffer); is.close(); json = new String(buffer, StandardCharsets.UTF_8); } catch (IOException ex) { ex.printStackTrace(); return null; } return json; } For above method, in Sketchware, you can create a more block readJsonFromAsset of type String and with a String variable filename . Then put codes below. String json = null; try { InputStream is = this.getAssets().open(_filename); int size = is.available(); byte[] buffer = new byte[size]; is.read(buffer); is.close();...

Simple ExoPlayer app in Sketchware

Image
1. To add ExoPlayer in Sketchware pro project, in Library manager --> Local Library, download following dependency: com.google.android.exoplayer:exoplayer:2.19.1 Do not skip sub-dependencies. 2. Select all downloaded dependencies except kotlin-stdlib-common-v1.6.21 and kotlin-stdlib-v1.6.21 3. Switch On AppCompat and design and Material library. 4. In permission manager, add INTERNET permissions . 5. In main.xml add three TextViews with links to videos. 6. Add an Intent component intent . 7. Create a new page, player.xml . 8. In TextView onClick events, use Intent to move to PlayerActivity . The video URL is saved with key "url". 9. In player.xml , add a LinearLayout, and convert it to com.google.android.exoplayer2.ui.PlayerView Set height to match_parent Change ID to playerView Add custom attributes: app:use_controller value true app:show_buffering value  when_playing 10. In PlayerActivity, add event import and add following imports: import com.google.android.exopl...

Save ListView as Pdf file

Image
1. On the page containing the ListView listview1 , Button savePdf , and an EditText edittext1 (This is for entering the file name). 2. Create a String variable filename . 3. In Activity, add Import event and add following imports: import java.nio.charset.StandardCharsets; import android.content.ContentValues; import android.content.Context; import android.net.Uri; import android.os.Build; import android.os.Environment; import android.provider.MediaStore; import android.widget.Toast; import android.graphics.pdf.PdfDocument; import java.io.IOException; import java.io.OutputStream; 4. Create a more block savePdfToDownloads [PdfDocument document][String filename] Here PdfDocument is a custom parameter. To add it, More Block -- Custom Parameter -- parameter: m.PdfDocument , variable: document . Put following codes in this more block: Uri pdfUri = null; OutputStream outputStream = null; try { ContentValues contentValues = new ContentValues(); contentVa...

Tween animation examples

1. View Animations in android dependent on  android.view.animation can be used to animate Views in android. 2. The animation can be applied to Views (ImageView, TextView, etc.). 3. XML files for the animation are placed in res/anim/ folder. 4. The root tags for the XML file include: <alpha> (fade in/out) <scale> (zoom) <translate> (move) <rotate> (rotate) <set> (combine multiple animations) 5. Below are examples of each of them: a) res/anim/fade_in.xml <?xml version="1.0" encoding="utf-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="1000" /> b) res/anim/fade_out.xml <?xml version="1.0" encoding="utf-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:fromAlpha="1.0" android:toAlpha="0.0...

Pdf Viewer for a Url using pdf.js

Image
This post shows how to download a pdf file in app cache and display it in WebView using Pdf.js in Sketchware project. 1. Create a new project in Sketchware pro. In  main.xml,  in a Linear Horizontal, add an EditText  edittext1  and a Button load . For edittext1 set lines to 1. Below that add a WebView  pdfWebView . 2. Switch On AppCompat and design. 3. Download  pdf.js  from following link: https://github.com/mozilla/pdf.js/releases/download/v5.4.296/pdfjs-5.4.296-dist.zip or https://mozilla.github.io/pdf.js/getting_started/#download 4. Extract the contents of the downloaded zip file. 5. In Sketchware pro project, in  Asset Manager , add a sample pdf file and rename it as  sample.pdf . Also, create a new folder  pdfjs . 6. In  pdfjs folder  import all the contents extracted from the downloaded zip file. 7. In  onCreate  event, get WebView settings and enable JavaScript, and file access. Load sample.pdf in the Web...