Posts

Convert web page to pdf

Image
 To convert a web page on a WebView to pdf in Sketchware, follow the steps given below. 1. Create a more block extra in your Sketchware project and put following codes in it. } android.print.PrintJob printJob; @androidx.annotation.RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) private void PrintTheWebPage(WebView webView) { android.print.PrintManager printManager = (android.print.PrintManager) this.getSystemService(Context.PRINT_SERVICE); String jobName = "My_webpage" + webView.getUrl(); android.print.PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter(jobName); assert printManager != null; printJob = printManager.print(jobName, printAdapter, new android.print.PrintAttributes.Builder().build()); } { 2. In the button click event for saving the webpage as pdf put following codes. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { PrintTheWebPage( webview1 ); } else {  showMessage("Not available for device below Android LOLLIPOP"); } He...

Rectifying FileUriExposedException

If we use the code Uri imageuri = Uri.fromFile(new java.io.File(_path)); It will cause FileUriExposedException if the file:// Uri is exposed to another app. You cannot use uri.fromFile(File) in new versions of android because it causes file uri exposed exception. You have to use File provider. If you are using Sketchware : 1. First Add Camera Component. 2. Then use following code: Uri imageUri = FileProvider.getUriForFile(MainActivity.this, getApplicationContext().getPackageName() + ".provider", new java.io.File(_path)); If you are using android studio 1. Put following code in AndroidManifest.xml: <provider android:name="androidx.core.content.FileProvider" android:authorities="com.xxxx.xxxx.provider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths"/> </provider> Replace com.xxxx.xxxx in c...

Create Compass app in Sketchware

Image
 To create a Compass app in Sketchware, follow the steps given below. 1. In View area add an ImageView imageview1 . Set image of a Compass as it's image. Make sure that North is towards the top. 2. Create a Gyroscope component gyro and another Gyroscope component gyroMagnet . 3. Create a More block extra and put following code in it. } private float[] floatGravity = new float[3]; private float[] floatGeoMagnetic = new float[3]; private float[] floatOrientation = new float[3]; private float[] floatRotationMatrix = new float[9]; { 4. In onCreate event use blocks Gyroscope gyro sensor stop , and Gyroscope gyroMagnet sensor stop . After that put following code in an add source directly block: gyro.registerListener(_ gyro _sensor_listener, gyro.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_NORMAL); gyro.registerListener(_ gyroMagnet _sensor_listener, gyro.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),SensorManager.SENSOR_DELAY_NORMAL); 5. In onPause event ...

Fullscreen button on VideoView

Image
This example shows how to add a fullscreen button on VideoView in Sketchware android project. In the project there will be a button to pick video files and a LinearLayout for the VideoView. On picking the video file, it will play in VideoView and there will be a fullscreen button on the MediaController of the VideoView, clicking on which the video will become fullscreen. 1. In main.xml add a Button button1 , a TextView textview1 , and a LinearLayout linear2 . For linear2 set padding 0, height 300, and gravity CENTER_HORIZONTAL. 2. In View manager set orientation to Both landscape and portrait. 3. Create a FilePicker component picker with mime type video/* . 4. Create a List String list , a String path , and a boolean isFullscreen . 5. Create a More block declare . Put following code in it. } RelativeLayout rl; VideoView videoView; MediaController mc; FrameLayout fm; private int mOriginalSystemUiVisibility; { 6. In onCreate event use add source directly block and put following code i...

Display List Map in RecyclerView

Image
  This post describes a sample project which uses RecyclerView to display a List Map containing keys 'text' and 'image_url'. 1. In  main.xml  add a Linear  linear2  for displaying the RecyclerView. 2. Create a CustomView  custom_item.xml . In this add a Linear  linear1 , an ImageView  c_imageview1 , and a TextView  c_textview1 . 3. Switch On AppCompat and Design . 4. Create a More block  extra . In this block put codes to declare a RecyclerView recyclerView . } androidx.recyclerview.widget.RecyclerView recyclerView; { 5. In  onCreate  use following codes. // Define recyclerView . recyclerView = new androidx.recyclerview.widget.RecyclerView(this); // Add RecyclerView to  linear2 . linear2 .addView(recyclerView); 6. Suppose you have a List Map  maplist  containing key ' text ' for some text and key ' image_url ' for url of image. 7. Create another More block  adapter , and put following codes in it. // This co...

RecyclerView example in Sketchware

Image
 This post describes a sample project which uses RecyclerView to display a list of images and texts in Sketchware. 1. Create a new project in Sketchware. 2. In main.xml add a Linear linear2 for displaying the RecyclerView. 3. Create a CustomView custom_item.xml . In this add a Linear linear1 , an ImageView c_imageview1 , and a TextView c_textview1 . 4. Add images using image manager in Sketchware. 5. Switch On AppCompat and Design . 6. Create a More block extra . In this block put code given below. This code creates a class MyImage , which can be constructed with a String and an int variable. The String variable will be used for setting title and int variable for setting image resource id. } public class MyImage { private String mTitle; private int mImage; public MyImage(String title, int imageId) { mTitle = title; mImage = imageId; } public String getTitle(){ return mTitle; } public int getImage() { return mImage; } } { 7. Create another More block adapter , and put following co...

Read XML file using XmlPullParser in Sketchware

Image
To read an Xml String do following: 1. Create a more block  readXmlString [myxmlData] to [TextView: textview] . Put following codes in it. try { org.xmlpull.v1.XmlPullParserFactory factory = org.xmlpull.v1.XmlPullParserFactory.newInstance(); factory.setNamespaceAware(true); org.xmlpull.v1.XmlPullParser xpp = factory.newPullParser(); xpp.setInput(new java.io.StringReader(_xmlData)); int eventType = xpp.getEventType(); String text = ""; while (eventType != org.xmlpull.v1.XmlPullParser.END_DOCUMENT) { if (eventType == org.xmlpull.v1.XmlPullParser.START_DOCUMENT) { } else if (eventType == org.xmlpull.v1.XmlPullParser.START_TAG) { text = text + xpp.getName(); } else if (eventType == org.xmlpull.v1.XmlPullParser.END_TAG) { } else if (eventType == org.xmlpull.v1.XmlPullParser.TEXT) { text = text +" "+ xpp.getText(); } eventType = xpp.next(); } _textview.setText(text); } catch (org.xmlpull.v1.XmlPullParserException xppe){ showMessage(xppe.getMessage()); } catch (java.io.IO...

Sort a List Map in Sketchware

Image
To sort a List Map we can use a Comparator to compare the values of keys in the List Map and then sort according to it. 1. Suppose you have a ListMap maplist which is displayed in custom ListView listview1 . 2. Create a more block sort [List Map: mylist] accordingToKey [key] . Put following codes in it. Comparator list_sorter = new Comparator<HashMap<String, Object>>(){ @Override public int compare(HashMap<String, Object> map1, HashMap<String, Object> map2){ try { Double num1 = Double.parseDouble(map1.get(_key).toString()); Double num2 = Double.parseDouble(map2.get(_key).toString()); return Double.compare(num1, num2); } catch (NumberFormatException e){ return map1.get(_key).toString().compareTo(map2.get(_key).toString()); } } }; Collections.sort(_mylist, list_sorter); 3. Use the block where you want to sort the List Map. The block should be followed by ListView setListCustomViewData block for displaying the sorted List Map. The blocks in above image will sort m...

Android Code Snippets

Disable screenshot getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE); Hide suggestions from the soft keyboard on EditText view EditText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD); Hide soft keyboard on Button Click android.view.inputmethod.InputMethodManager imm = (android.view.inputmethod.InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); Hide WebView scrollbar webview1.setVerticalScrollBarEnabled(false); webview1.setHorizontalScrollBarEnabled(false); Get Screen Height Point sPoint = new Point(); WindowManager wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE); wm.getDefaultDisplay().getSize(sPoint); int screenHeight = sPoint.y; Get Screen Width Point sPoint = new Point(); WindowManager wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE); wm.getDefaultDisplay().getS...

Get battery charge percentage

Image
To get the percentage of battery charge in an android device, we can use the following code: BatteryManager bm=(BatteryManager)getSystemService(BATTERY_SERVICE); int battery_percent = bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY); The result battery_percent is an integer. If we have a TextView textview1 on our page, to display the battery percentage in textview1 we can use following code: textview1.setText(battery_percent + "%"); To check if battery status is full or not , following code can be used: IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); Intent batteryStatus = registerReceiver(null, ifilter); int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1); if(status == BatteryManager.BATTERY_STATUS_FULL) { Toast.makeText(getApplicationContext(),"Device is fully charged",Toast.LENGTH_LONG).show(); } else { Toast.makeText(getApplicationContext(),"Device is not fully charged",Toast.LENGTH_LONG).show(...