Posts

Share an image from an ImageView

To share an image from an ImageView in your Sketchware Android project, in VIEW area add a Button button1, which will act as a share button. In the event of on button1 click, use an add source directly block and put following code in it: Bitmap bm = ((android.graphics.drawable.BitmapDrawable) imageview1.getDrawable()).getBitmap(); try { java.io.File file = new java.io.File(getExternalCacheDir() + "/image.jpg"); java.io.OutputStream out = new java.io.FileOutputStream(file); bm.compress(Bitmap.CompressFormat.JPEG, 100, out); out.flush(); out.close(); } catch (Exception e) { showMessage(e.toString()); } Intent iten = new Intent(android.content.Intent.ACTION_SEND); iten.setType("*/*"); iten.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new java.io.File(getExternalCacheDir() + "/image.jpg"))); startActivity(Intent.createChooser(iten, "Send image")); Now save and run the project. You should be able to share the image from imageview1.

Using TextToSpeech in Sketchware

Image
To know how to use of TextToSpeech class in Sketchware, for reading the text from an EditText field, follow the instructions given below. 1. In your Sketchware Android project, and an EditText edittext1 , and a Button button1 . 2. In LOGIC area, in onCreate event, use add source directly block and in the block put the code given below. It should be the last block in onCreate event. t1=new android.speech.tts.TextToSpeech(getApplicationContext(), new android.speech.tts.TextToSpeech.OnInitListener() { @Override public void onInit(int status) { if(status != android.speech.tts.TextToSpeech.ERROR) { t1.setLanguage(Locale.UK); } } }); } android.speech.tts.TextToSpeech t1; private void nothing(){ This code initializes the TextToSpeech engine. It can speak only after initialization. If the user performs some other Activity after initialization, then it will not speak. It has to be initialized again for it to speak. So place this code again at appropriate place so that it is...

Convert image to base64 String and vice versa

Image to base64 String To convert image to base64 String use the code given below. 1. Define a new ByteArrayOutputStream. java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream(); 2. Convert image in ImageView to Bitmap. Bitmap bm = ((android.graphics.drawable.BitmapDrawable) imageview1.getDrawable()).getBitmap(); Or convert image in drawable folder to Bitmap. Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.image); 3. Convert bitmap image to byte array and encode it to String. bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); byte[] imageBytes = baos.toByteArray(); String imageString = android.util.Base64.encodeToString(imageBytes, android.util.Base64.DEFAULT); Base64 String to image 1. Decode String to Byte array byte[] imageBytes = android.util.Base64.decode(imageString, android.util.Base64.DEFAULT); 2. Convert Byte array to Bitmap Bitmap decodedImage = BitmapFactory.decodeByteArray(imageBytes, 0, ima...

Firebase auth in Sketchware for Login Activity

Image
To create a login activity using Firebase Auth in Sketchware, follow the steps given below. This method uses Firebase authentication service for creating login. 1. In your Firebase account, go to Firebase authentication. 2. In Firebase authentication web set-up, go to SIGN-IN method, and enable Email/password and Anonymous. 3. Go to Project settings in your Firebase project and copy the Web API Key, Project ID, and App ID. 4. Paste the Project ID, App ID, and Web API Key in your project in Sketchware, in the Firebase settings. 5. On the  MainActivity  page add a File Shared preferences component  user:user  and an Intent  i . Also add a Timer t and a Firebase Auth testlogin . 6. Create a new page login.xml with Activity called  LoginActivity . 7. In  onCreate  event of MainActivity use blocks as shown in image below. It identifies main page with  File user key page . It also checks if user is logged in to Fi...

A simple navigation drawer in Sketchware

Image
In the latest version of Sketchware (v3.0.0), appcompat-v7 and design have been added, and we can now add a navigation drawer to our project. This navigation drawer uses a CustomView drawer_main.xml . In order to create a Navigation drawer, follow the steps given below. 1. In your Sketchware android project, go to  Library manager  and switch on AppCompat and Design. 2. In  View manager , go to MainActivity.java and select  Navigation Drawer Activity . 3. Now go to the CustomView  drawer_main  and add a LinearV  linear1 . Set it's width as MATCH_PARENT. 4. Add items in the linear1 of drawer_main and customize it to change it's look. 5. Create a new Intent component  i . 6. Now in the EVENT area, under section drawer , add the events you want to execute (like textview2 onClick , textview3 onClick , etc.). 7. Add codes to be executed in each of these events. 8. Save and run the project.

Codes to improve firebase chatting app in sketchware

Image
Here are a few codes which can be used in the chat application created using Firebase in Sketchware. 1. Remove divider​ height of ListView To remove the divider height of ListView displaying messages, put following code in onCreate , in an add source directly block. listview1.setDivider(null); listview1.setDividerHeight(0); 2. Display end of ListView on refresh To make the ListView scroll to display the last element of list when updated, put following code in onCreate , in an add source directly block. listview1.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL); listview1.setStackFromBottom(true); 3. Set width of ListView items to 3/4 of screen width Firstly, in CustomView , insert a LinearH linear1, and inside that insert a LinearV linear2. Put all contents to be displayed in linear2. Now, in event onBindCustomView , add a number variable width. Set width to getDisplayWidthPixels * 0.75, and set it as width of linear2 using following code. LinearLayout.Layou...

How to DELETE data from FIREBASE database from Sketchware app?

Image
If you have an app which displays data from firebase database in a custom ListView, and you want to delete data from it, then follow this method. This method of deleting data is for apps which push data to firebase database on button click by using blocks as shown in image below, retrieves data from firebase database, on Firebase DB onChildAdded event, by using blocks as shown in image below, In order to delete data from this kind of app follow the steps given below. 1. Add a new String list str . 2. In the CustomView, add an ImageView imageview2 and set it's image to delete icon. On clicking this ImageView the item will get deleted. 3. In  Firebase DB onChildAdded  event, inside Firebase DB... get children to List Map... then block, add the block Add [childKey] to List String [str] , as shown in image below. This will create a list of keys or children at the DatabaseReference 'Chat'. 4. In the event onBindCustomView add th...