Posts

Gemini API in WebView

1. Create a new Sketchware project. 2. In main.xml , add a WebView webview1 . 3. In permission manager, add INTERNET permission. 4. In assets folder, add a file index.html and put following codes in it. Important: In code apiKey: "YOUR_API_KEY_HERE" Replace YOUR_API_KEY_HERE with your own API key <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Gemini AI Demo</title> <!-- Load Highlight.js theme --> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github-dark.min.css"> <link rel="stylesheet" href="style.css"> <style> /* Additional styling for syntax highlighting */ .hljs { background: #2d2d2d; color: #f8f8f2; ...

GenAI example in Sketchware

Image
1. Visit https://aistudio.google.com/ -- Login or Register -- Click on Get API Key. 2. Click on create API Key and create a new API key. 3. Copy the API Key. 4. In main.xml in Sketchware, add EditText edittext1 , Button button1 , TextView textview1 . 5. Create a String variable API_KEY . 6. In onCreate , set value of API_KEY to the API key copied from aistudio.google.com. 7. Add imports event and import following. import okhttp3.Call; import okhttp3.Callback; import okhttp3.MediaType; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; import okhttp3.ResponseBody; import java.util.concurrent.TimeUnit; 8. Create a more block of type String (returns String) with name  buildJsonText [String query_text]  and put following codes in it. try { JSONObject content = new JSONObject(); content.put("text", _query_text); JSONArray parts = new JSONArray(); parts.put(content); JSONObject contentObj = new JSONObject(); con...

Download url to app files directory example

Image
1. This post is about downloading a file from its url to getFilesDir() of the app. Similar code can be used to download to getCacheDir() with little modification. 2. In permission manager add INTERNET permission. 3. Create a java class file LinkHelper.java and put following codes in it. (Change package name to package name of your app). package com.my.newproject5; import android.content.Context; import android.net.Uri; import android.os.Handler; import android.os.ParcelFileDescriptor; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.HashMap; public class LinkHelper { // INTERFACES public interface DownloadCallback { void onProgress(int percent); // 0 → 100 void onSuccess(File file); void onError(String message); } // ASYNC FILE DOWNLOAD WITH PROGRESS LISTENER public static void downloadToAppDataAsyn...

Creating a View Class with ball moving animation

Image
1. Add a Sound file bounce_sound.mp3 . 2. Add a new java class file BouncingBallView.java . Put following codes in it. package com.my.animation; import android.view.View; import android.content.Context; import android.media.SoundPool; import android.media.AudioAttributes; import android.graphics.Paint; import android.graphics.Paint.Style; import android.graphics.Color; import android.graphics.Canvas; public class BouncingBallView extends View { private Paint mypaint; private SoundPool soundPool; private int soundId; private boolean isLoaded = false; int directionX = 1; // 1 = moving right, -1 = moving left int directionY = 1; // 1 = moving down, -1 = moving up int speed = 4; int radius = 30; int distanceX = radius; int distanceY = radius; int timeX = 0; int timeY = 0; int viewWidth = 0; int viewHeight = 0; public BouncingBallView(Context context) { super(context); init(context); } ...

Crop image example in Sketchware

Image
1. Create a new project in Sketchware. 2. In  main.xml * Add a Button btn_pick * Below this, add an ImageView imageview1 . * Below this add a Linear Horizontal linear_buttons . Inside linear_buttons, add three Buttons: btn_rotate , btn_crop , btn_download . 3. Switch ON AppCompat and design. 4. In Java/Kotlin Manager add a new java file BitmapCacheUtils.java and put following codes in it. package com.my.newproject10; import android.content.ContentValues; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Build; import android.os.Environment; import android.provider.MediaStore; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; public class BitmapCacheUtils { private static fina...