Posts

Showing posts from December, 2017

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 onText