Posts

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...

ViewPager2 with Fragments in Sketchware

Image
1. Create a new project in Sketchware. 2. In  main.xml  add a TabLayout tablayout1 and a ViewPager viewpager1 . 3. Convert ViewPager to  androidx.viewpager2.widget.ViewPager2 . 4. Switch On AppCompat and design and Material3 Manager. 5. Add three new views of type Fragment: animals_fragment , fishes_fragment , and insects_fragment . Their corresponding java files will be AnimalsFragmentActivity.java , FishesFragmentActivity.java , and InsectsFragmentActivity.java  (See images below). 6. Configure your fragments as you like. 7. Add a new Java file  ViewPagerAdapter.java . Add following codes in it. package com.my.newproject18; import androidx.annotation.NonNull; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentActivity; import androidx.viewpager2.adapter.FragmentStateAdapter; import java.util.ArrayList; import java.util.List; public class ViewPagerAdapter extends FragmentStateAdapter { private final List<Fragment> fragme...

Loading pdfs from Google Drive

Image
This post shows how to download pdf files from Google Drive to app data directory and then copy it to cache and load it in WebView. 1. Create a new project in Sketchware pro (package name in my project is com.my.newbooks). 2. Add all pdfs which you want in your app to google drive and make their access public. 3. In  main.xml  add a GridView gridview1 , and a FloatingActionButton _fab . 4. Create a Custom View grid_item.xml with an ImageView imageview1 with fixed height 200dp and scale_type fit_center. Add a TextView text_filename and ImageView image_delete . 5. Select grid_item.xml as custom view of gridview1. 6. Add two images download icon and delete icon. 7. Create a new page download.xml , and in this add a SeekBar seekbar1 and a ListView listview1 . 8. Create a Custom View downloadlist_item.xml with a TextView textview1 and an ImageView imageview1 with download icon. 9. Select downloadlist_item.xml as custom view of listview1. 10. In MainActivity.java , add an Int...