Posts

Showing posts from August, 2020

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 code defines a class CategorylistAdapter which can be used as an adapter for RecyclerView.}pu…

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 codes in it.// This code defines a clas…

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.IOException ioe){showMessa…

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>>(){@Overridepublic 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 maplist according to…

Android Code Snippets

Disable screenshotgetWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
Hide suggestions from the soft keyboard on EditText viewEditText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
Hide soft keyboard on Button Clickandroid.view.inputmethod.InputMethodManager imm = (android.view.inputmethod.InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
Hide WebView scrollbarwebview1.setVerticalScrollBarEnabled(false);webview1.setHorizontalScrollBarEnabled(false);
Get Screen HeightPoint sPoint = new Point();WindowManager wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);wm.getDefaultDisplay().getSize(sPoint);int screenHeight = sPoint.y;
Get Screen WidthPoint sPoint = new Point();WindowManager wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);wm.getDefaultDisplay().getSize(sPoint);int s…

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(); }Apart from B…