Posts

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

Convert Multiple images to a PDF file

Image
This post shows a simple project which converts multiple images into a PDF document in Sketchware. 1. Create a new project in Sketchware. 2. In View area of main.xml , add Button button_open Button button_save EditText edittext1 ListView listview1 3. Create a Custom View itemview.xml . In this view add ImageView imageview1 4. For listview1 select itemview as CustomView. 5. Add a FilePicker fp for picking images ("image/*"). 6. Add a number variable n , and a String variable save_path , a String variable path1 , a List String  list , and List Map  imagelist . 7. In button_open onClick event use FilePicker pick files block. 8. In FilePicker onFilesPicked event, add selected paths to imagelist  using key 'pic', and display  imagelist in listview1 using blocks shown in image below. 9. In ListView onBindCustomView event, set image of imageview1 to file path from imagelist . 10. In button_save onClick event, create new PDF document, add images from imagelist...

Firebase Query to search data with value of a key

Image
The example in this post uses Firebase Query to search for a specific username, entered by user in an EditText, in a list of all users. 1. The data to be searched is shown in image below. We will search the value of key "username". 2. In View area of your project, add an EditText edittext1 , a Button button1 , and a ListView listview1 . 3. Create a Custom View user.xml . In this page, add an ImageView imageview1 and a TextView textview1 to display the details retrieved. The ImageView here has width and height 40 and scale type CENTER_CROP. The linear containing them has Gravity center_vertical. 4. For listview1 select user.xml as CustomView. 5. Create a FirebaseDb component ( user ) specifying the data location ( users ) to be searched. 6. Add a String variable query , and a List Map result_users . 7. In onCreate event use FirebaseDb stop listening. 8. In button1 onClick , set String query to text in  edittext1 . Then ...

Replacing a color in image

Image
To select a color in an image and replace it with a new color, follow the steps given below. 1. In main.xml, add two Buttons button_pick and button_replace , add an ImageView imageview1 , add two LinearLayout linear_getcolor and linear_setcolor , add an EditText edittext1 , and add a ProgressBar progressbar1 . 2. Create a FilePicker imagepicker : image/* for picking image. 3. Create a String variable path and five number variables width , height , w , h , and n . 4. In onCreate , use blocks to make ProgressBar Gone, and setOnTouchListener for imageview1 . The code used is imageview1.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { int eid = event.getAction(); switch (eid) { case MotionEvent.ACTION_MOVE: int x = (int)event.getX(); int y = (int)event.getY(); if (bitmap != null){ if (x<width && x>0 && y<height && y>0){ colorint = bitmap.getPixel(x, y); linear_getc...

Firebase Login/Register with email verification

Image
To verify the email address of a user registered in your firebase app in Sketchware, follow the steps given below. 1. In main.xml , add three EditText fields, edit_email , edit_username , and edit_password , and add two Buttons register and login . 2. Add a FirebaseAuth component fauth , a FirebaseDb component user:users , and an Intent component i . 3. Add a Map Variable map , a String variable username , and a Boolean variable emailVerified . 4. Create a new page verify_email.xml . 5. In MainActivity.java , in onCreate event, if user is logged in and email is verified move to ChatActivity or the home page of your app and Finish Activity. And if user is logged in and email in not verified move to VerifyEmailActivity . See image below. The code use in add source directly block: emailVerified = fauth.getCurrentUser().isEmailVerified(); 6. In the event register button onClick , if length of text in all EditText fields is more than 0, set String username to edit_...