Posts

Display a list in a Dialog Box in Sketchware

Image
To display a list of items in a Dialog Box in Sketchware follow the steps given below. 1. In your Sketchware android project, add a Button button1 and a TextView textview1 . The Button is for showing Dialog Box and TextView is for displaying the selected item. 2. Now go to LOGIC area, and add a List String list . 3. In onCreate event, add items to the list. 4.  Create a new Dialog component dialog . 5. In Button onClick event use the Block Dialog setTitle , to set the title of the Dialog Box. 6. After that use an add source directly block and write following code in it. dialog.setAdapter( new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, list), new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface _di, int _pos){ textview1.setText(list.get(_pos)); }} ); 6. After the above code in Button onClick event, add Dialog show block. 7. Save and run the project. On clicking the button you will see a...

OptionsMenu in Sketchware

Image
In order to create OptionsMenu in Sketchware, follow the steps given below. 1. Create a more Block extra in your project. 2. To define this more block extra  use an add source directly block and put following code in it. } @Override public boolean onCreateOptionsMenu (Menu menu){ menu.add(0, 0, 0, "Item 1"); menu.add(0, 1, 1, "Item 2"); menu.add(0, 2, 2, "Item 3"); return true; } Note that here the first } closes more block. Rest of the code is outside the more block extra . This is to code for onCreateOptionsMenu where three items are added to the menu. Also note that 0, 1, and 2 are Id of the items added to the menu, and Item 1, Item 2, Item 3 are the title of the items added. Change the titles to whatever you want. To display the OptionsMenu items as icons on the ActionBar, add the id of the images to the menu items. Suppose ic_settings_white is the icon for item 1. Then the code above will change as below. } @Override pub...

Check for latest app version using Firebase in Sketchware

Image
You can store the latest version of your app in Firebase realtime database, and then check and compare it with the actual version of installed app. Follow the instructions below to know how to do this. 1. In your Firebase app in sketchware, in library manager, make sure you have entered correct App ID and project ID, and that Firebase switch is on. 2. Make sure the rules in your Firebase database are read and write true. 3. On the MainActivity or the Activity in which you want to check for the latest app version, add a new FirebaseDB component called Ver:version . 4. Create three new String variables, package_name ,  your_version and latest_version , and a new Map variable map . 5. In  onCreate  event, set the string  package_name  to package name of your app. 6. Next in onCreate, use an add source directly block and put codes for getting the version of presently installed app. try { android.content.pm.PackageInfo pinfo = getPackageManage...

DatePickerDialog in Sketchware

Image
To create a DatePickerDialog in Sketchware android project follow the steps given below. 1. In VIEW area of your sketchware android project, insert a LinearH and inside it insert a TextView textview1 , an EditText edittext1 and an ImageView imageview1 . For textview1 write text as ' Date: '. For edittext1 write hint as dd/mm/yyyy  and deselect 'enabled' in it's properties. 2. Choose image of a Calendar using image manager and set it as image of imageview1 . 3. Switch On AppCompat and Design. 4. Create a More Block showDatePicker . In this block, use an add source directly block and put following code. androidx.appcompat.app.AppCompatDialogFragment newFragment = new DatePickerFragment(); newFragment.show(getSupportFragmentManager(), "Date Picker"); 5. Create another More Block  extra.  In this put codes to define a DialogFragment. } public static class DatePickerFragment extends androidx.appcompat.app.AppCompatDialogFragment impl...

Changing text size and color of spinner in Sketchware

Image
To change the text size and color of spinner dropdown list, follow the instructions given below. 1. In VIEW area of your Sketchware Android project, insert a Spinner spinner1 . 2. In LOGIC area, add a new String list list1 . 3. In onCreate event add items to the list. 4. After adding items to the String list, insert an add source directly block and put following code in it: spinner1.setAdapter(new ArrayAdapter (this, android.R.layout.simple_list_item_1, android.R.id.text1, list1) { @Override public View getView(int position, View convertView, ViewGroup parent) { TextView textView1 = (TextView) super.getView(position, convertView, parent); textView1.setTextColor(Color.RED); textView1.setTextSize(24); return textView1; } @Override public View getDropDownView(int position, View convertView, ViewGroup parent) { TextView textView1 = (TextView) super.getDropDownView(position, convertView, parent); textView1.setTextColor(Color.RED); textView1.setTextSize(24); retu...

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...