Select and delete multiple items in a list in Sketchware android project

 1. In image manager of project, add delete icon ic_delete_white.

2. In Resource, in menu folder, add an xml file named context_menu.xml. Put following code in this file:



      
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/delete"
android:title="Delete"
android:icon="@drawable/ic_delete_white"
android:showAsAction="always" />
</menu>

3. Create a new custom variable of type SparseBooleanArray with name selectedItems and define it as new SparseBooleanArray().


4. Put following codes in onCreate. Change names of variables as in your project.

      
// Enable multiple selection mode with contextual action bar
listview1.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);

// Handle selection events via MultiChoiceModeListener
listview1.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {

    @Override
    public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
        // Update the selection map based on user interaction
        if (checked) {
            selectedItems.put(position, true); // Mark item as selected
        } else {
            selectedItems.delete(position);    // Unmark item
        }

        // Refresh the adapter to visually highlight selected items
        ((BaseAdapter)listview1.getAdapter()).notifyDataSetChanged();

        // Update the ActionMode title with the number of selected items
        mode.setTitle(selectedItems.size() + " selected");
    }

    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        // Inflate the contextual menu (e.g., delete option)
        getMenuInflater().inflate(R.menu.context_menu, menu);
        return true;
    }

    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        // Handle clicks on contextual menu items
        if (item.getItemId() == R.id.delete) {
            // Loop backwards to safely remove selected items
            for (int i = listview1.getCount() - 1; i >= 0; i--) {
                if (selectedItems.get(i)) {
                    maplist.remove(i); // Remove item from data list
                }
            }

            // Clear selection and update UI
            selectedItems.clear();
            ((BaseAdapter)listview1.getAdapter()).notifyDataSetChanged();

            // Save updated list to SharedPreferences as JSON
            sp.edit().putString("data", new Gson().toJson(maplist)).commit();

            // Close action mode
            mode.finish();
            return true;
        }
        return false;
    }

    @Override
    public void onDestroyActionMode(ActionMode mode) {
        // Clear selection and refresh UI when action mode ends
        selectedItems.clear();
        ((BaseAdapter)listview1.getAdapter()).notifyDataSetChanged();
    }

    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        // Not used here, return false
        return false;
    }
});

5. In onBindCustomView, if selectedItems.get(_position, false), change background colour of outermost linear in custom view.


6. Save and run the project.

                         


How to enable upload from webview in Sketchware?

 Now that Sketchware has a block to add java code directly in the project, it is possible to enable file upload in webview.


To enable file upload in webview in sketchware app, follow the steps given below.

1. Insert a webview in VIEW area in the sketchware project. Note the ID of webview, usually it is webview1.

2. In LOGIC area of project, in onCreate event, add the block add source directly and copy the following code in it:
      
webview1.setWebChromeClient(new WebChromeClient() {
// For 3.0+ Devices
protected void openFileChooser(ValueCallback uploadMsg, String acceptType) { mUploadMessage = uploadMsg; Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*"); startActivityForResult(Intent.createChooser(i, "File Browser"), FILECHOOSER_RESULTCODE);
}

// For Lollipop 5.0+ Devices
public boolean onShowFileChooser(WebView mWebView, ValueCallback filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {
  if (uploadMessage != null) {
  uploadMessage.onReceiveValue(null);
  uploadMessage = null; } uploadMessage = filePathCallback; Intent intent = fileChooserParams.createIntent(); try {
  startActivityForResult(intent, REQUEST_SELECT_FILE);
  } catch (ActivityNotFoundException e) {
  uploadMessage = null; Toast.makeText(getApplicationContext(), "Cannot Open File Chooser", Toast.LENGTH_LONG).show(); return false; }
  return true; }
  
  //For Android 4.1 only
  protected void openFileChooser(ValueCallback uploadMsg, String acceptType, String capture) {
    mUploadMessage = uploadMsg; Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("image/*"); startActivityForResult(Intent.createChooser(intent, "File Browser"), FILECHOOSER_RESULTCODE);
    }
    
    protected void openFileChooser(ValueCallback uploadMsg) {
      mUploadMessage = uploadMsg; Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE);
      i.setType("image/*"); startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
}


});
 
Note that if the ID of webview is not webview1, change it accordingly in the code above. This code should be at the beginning of onCreate before the WebView loadUrl block.

เคเค• เคŸिเคช्เคชเคฃी เคญेเคœें

0 เคŸिเคช्เคชเคฃिเคฏाँ