Posts

Create pop-up menu in Sketchware

Image
In order to display a pop-up menu on button click or on ListView item click in your Sketchware project, use the codes provided below. PopupMenu on button click 1. To display a PopupMenu when a Button is clicked, in onButtonClick event, use add source directly block, and put following code in it: PopupMenu popup = new PopupMenu(MainActivity.this, button1); Menu menu = popup.getMenu(); menu.add("Delete"); menu.add("Show"); This code creates a PopupMenu for button1, with two options Delete and Show. The same code can be used for creating PopupMenu for ImageView, TextView or or any other widget by using their id in place of button1. 2. After this add another​ add source directly block and put following code in it: popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener(){ @Override public boolean onMenuItemClick(MenuItem item){ switch (item.getTitle().toString()){ case "Delete": This code sets OnMenuItemClickListener f...

Custom Toast message using CustomView in Sketchware

Image
To create a custom Toast message in Sketchware, follow the steps given below. 1. In your Sketchware android project, in VIEW area, create a new CustomView with name custom1. In this CustomView add a LinearH linear1 and a TextView textview1. Customize the textview1 and linear1 as per your requirement. 2. In LOGIC area, add a new More Block with name customToast(String _text) as shown in image below. 3. Next define this customToast Block. In event customToast MoreBlock use add source directly block, and in the block, write codes as described below. First define a new LayoutInflater and use it to create a View from the CustomView. LayoutInflater inflater = getLayoutInflater(); View toastLayout = inflater.inflate(R.layout.custom1, null); Initialize textview1 and linear1 used in CustomView. TextView textview1 = (TextView) toastLayout.findViewById(R.id.textview1); LinearLayout linear1 = (LinearLayout) toastLayout.findViewById(R.id.linear1); Set the text of tex...

Code of a simple android app which can browse and open a text file

Image
Here I provide the codes of a simple android app which can be used to browse and open a text file from device. Codes in main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Button" android:id="@+id/select"/> <EditText android:layout_height="wrap_content" android:layout_width="match_parent" android:ems="10" android:id="@+id/edittext1"/> <EditText android:layout_height="match_parent" android:layout_width="match_parent" android:ems="10" android:id="@+i...

How to share an image from Drawable folder?

To share an image in drawable folder of your sketchware android project, first save the image in app cache and then share the saved image using it's Uri. The code to be used is provided below. 1. First add the image (my_image.jpg) to be shared in your Sketchware android project using image manager. 2. Add a Button button1 which will act as share button. 3. In event button1 onClick, use add source directly block and add following code: Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.drawable.my_image); String path = getExternalCacheDir()+"/shareimage.jpg"; java.io.OutputStream out = null; java.io.File file=new java.io.File(path); try { out = new java.io.FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } path=file.getPath(); Uri bmpUri = Uri.parse("file://"+path); Intent shareIntent = new Intent(); shareIntent = new Intent(android.conte...

Using GradientDrawable as background in Sketchware Android Project

Image
The public class GradientDrawable can be used to create a Drawable image with gradient of several colors which can be set as the background of any View. The codes provided below shows how to use GradientDrawable in Sketchware Android Project. The codes provided below should be added in onCreate event, in add source directly block. 1. To set a single color as background of any View, use following code: android.graphics.drawable.GradientDrawable gd = new android.graphics.drawable.GradientDrawable(); gd.setColor(Color.parseColor("#ff000000")); linear1.setBackground(gd); Or android.graphics.drawable.GradientDrawable gd = new android.graphics.drawable.GradientDrawable(); gd.setColor(Color.WHITE); linear1.setBackground(gd); Here gd is a GradientDrawable. Here hex color code can be use to set background color. And here gd is set as background of linear1. It can also be set as background of any other View (like button1, etc.). 2. To create a GradientDrawabl...

How to check internet connection in Sketchware?

Image
To check internet connection in your Sketchware android project, follow the steps given below. 1. In your Sketchware project, add a new RequestNetwork component network .  2. In onCreate event or in the event when you want to check internet connection, use the block RequestNetwork start network request to method GET to url http://google.com with tag A , as shown in image below.  3. Add event RequestNetwork onResponse, and here Toast the message "Connected to Internet" or do other things when user is connected.  4. Add event RequestNetwork onErrorResponse, and here Toast the message "Not Connected to Internet" or do other things when user is not connected. 5. Save and run the project.

How to find and​ highlight a word in a text field in Sketchware?

Image
We can highlight a particular searched character sequence in a TextView or EditText field or any other text field by using Spannable. To use Spannable class to highlight a searched text in Sketchware Android Project, follow the steps given below. 1. In VIEW area of your project, add an EditText field ( edittext1 ) which will act as input field for search term. Add a TextView ( textview1 ) which will display the number of results found. And add another TextView ( textview2 ) or EditText (edittext2) field which will contain the text in which the word is to be searched. 2. In LOGIC area, in onCreate event set the text to be searched in TextView field. 3. If you want the app to search from any text added by user, use EditText instead textview2 in step no.1 and do not set the text in onCreate. 4. Add a new String variable text . Add three number variables total, len  and y . 4. Add a new event edittext1 onTextChanged . 5. In the event edittext1 on...

Code for drawing in any View with finger

Image
To draw using finger in any LinearLayout , set the padding of the LinearLayout​ to 0 and add following code in onCreate event using add source directly block. dv = new DrawingView(this); linear2 .addView(dv); mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setDither(true); mPaint.setColor(Color.GREEN); mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeJoin(Paint.Join.ROUND); mPaint.setStrokeCap(Paint.Cap.ROUND); mPaint.setStrokeWidth(12); } DrawingView dv; private Paint mPaint; private Canvas mCanvas; public class DrawingView extends View { public int width; public int height; private Bitmap mBitmap; private Path mPath; private Paint mBitmapPaint; Context context; private Paint circlePaint; private Path circlePath; public DrawingView(Context c) { super(c); context=c; mPath = new Path(); mBitmapPaint = new Paint(Paint.DITHER_FLAG); circlePaint = new Paint(); circlePath = new Path(); circlePaint.setAntiAlias(true); circlePaint.setColor(Color.BLUE); circlePa...

Get list of app icons and app names of all installed apps in Sketchware

Image
We can create a list of app names, app icons, app versions and app package names from information received using PackageInfo class. Follow the steps below to create a list of all installed apps, with app name, app icons, package name, version name and version code. 1. In VIEW area of your sketchware project, in main.xml add a ListView. 2. Add a new CustomView custm.xml and in this add an ImageView and four TextViews. 3. For the ImageView​ set width and height as 60dp and scale_type as FIT_XY. 4. In main.xml, for ListView select custm as customView. 5. In LOGIC area, add four List String (list11, list12, list13, and list14), add a List Map (list1), and an Intent component i. These four will act as list of app name, package name, version name, and version code. 6. In  onCreate  event, add an add source directly block. Inside the block add following code. List<android.content.pm.PackageInfo> listn = getApplicationContext().getPackageMa...

Check if an app is installed, and get it's version in Sketchware

Image
We can check if an app is installed or not by using it's package name in simple codes in Sketchware. Check if an app is installed or not We can get information about any installed app from it's package name, by using PackageManager and PackageInfo methods. PackageInfo provides Overall information about the contents of a package. This corresponds to all the information collected from AndroidManifest.xml. In onCreate event in LOGIC of your app, use an add source directly block and add following code: boolean isAppInstalled = appInstalledOrNot(" com.besome.sketch "); if(isAppInstalled) { showMessage(" Sketchware app is installed "); } else { showMessage(" Sketchware is not installed ");} Create a More Block  extra . In the more block  extra , use an  add source directly  block and put following codes: } private boolean appInstalledOrNot(String uri) { android.content.pm.PackageManager pm = getPackageManager(); try { pm.getPackageI...