Skip to main content

Custom dialog box in Sketchware using CustomView

The CustomView can be used to create a custom dialog box in Sketchware, by using simple codes.

Follow the steps below to create a custom dialog pop-up.

1. In VIEW area of your project add a new CustomView.
In the image above I have added a new CustomView 'cust.xml'.

2. Design the CustomView the way you want your custom dialog box to look like.
In the image above, I have added an ImageView, two TextViews, and two Buttons (namely 'button1' and 'button2').

3. Choose the event on which you want to show the dialog. It can be onBackPressed or onButtonClick, etc.

4. On the event when dialog is to be shown, use add source directly block to create and show the dialog box.


In the image above I have added the code to onBackPressed event. The code used is explained below.

a) First use the code to create a dialog.

final AlertDialog dialog2 = new AlertDialog.Builder(MainActivity.this).create();

Note that here 'MainActivity' is the name of the screen or java file in which the code is being used. Also note that 'dialog2' is name of the dialog box created.

b) Next, use code to set the View of dialog box to custom view.

View inflate = getLayoutInflater().inflate(R.layout.cust, null);
dialog2.setView(inflate);

Note that here 'cust' is name of the CustomView xml file.

c) Set title for the dialog box (optional).

dialog2.setTitle("Exit");

d) Define the buttons used in the CustomView.

Button but1 = (Button) inflate.findViewById(R.id.button1);
Button but2 = (Button) inflate.findViewById(R.id.button2);

Note that 'button1' and 'button2' are id of the buttons added in CustomView, whereas 'but1' and 'but2' is the name with which the buttons have been defined.

e) Write code to perform some action when button is clicked.

but1.setOnClickListener(new OnClickListener() { public void onClick(View view) { MainActivity.this.finish(); } });

but2.setOnClickListener(new OnClickListener() { public void onClick(View view) { dialog2.dismiss(); } });

The code above finish MainActivity (exits app in most cases) when button1 is clicked. And it dismiss the dialog box when button2 is clicked.

If you want to perform some other action when buttons are clicked, you can modify the code as per your requirement.

f) If you want that the dialog does not disappear when you click outside the dialog box, add the following code.

dialog2.setCancelable(false);

g) Write code to show the dialog.

dialog2.show();


So, the complete code for a custom dialog box may look like this:

final AlertDialog dialog2 = new AlertDialog.Builder(MainActivity.this).create();
View inflate = getLayoutInflater().inflate(R.layout.cust, null);
dialog2.setView(inflate);
dialog2.setTitle("Exit");
Button but1 = (Button) inflate.findViewById(R.id.button1);
Button but2 = (Button) inflate.findViewById(R.id.button2);
but1.setOnClickListener(new OnClickListener() { public void onClick(View view) {
MainActivity.this.finish(); } });
but2.setOnClickListener(new OnClickListener() { public void onClick(View view) { dialog2.dismiss(); } });
dialog2.show();



5. Save and run the project. The custom dialog box will show when back button is pressed.

Comments

Popular posts from this blog

Create Music Player app in Sketchware

To create a Music Player app in sketchware, follow the steps given below. 1. Create a new project in Sketchware. 2. In VIEW area on main.xml, add a ListView  listview1  with height wrap_content, and weight 1. Add a SeekBar  seekbar1 , an ImageView  imageview1 , and three TextViews  textview1 ,  textview2 , and  textview3 , as shown in the image below. 3. Create a CustomView  mycustom.xml  and add a TextView  textview1 , and an ImageView  imageview1  in it. For listview1 select mycustom.xml as it's customView. 4. Add a MediaPlayer component  mp , a Shared Preferences component  sp:sp  and a Timer component  timer . 5. Create six More Blocks  MPcreate(pos), MPstart , and  MPpause  for the MediaPlayer and  searchFolders ,  getFileList in [filePath]  and  extra . 6. Add three number variables  n, r  and  songPosition , and five String variables  currentfile ...

Admob rewarded video Ads in Sketchware

To integrate Admob Rewarded Video Ads to a Sketchware project, follow the steps given below. Prerequisites An Android project (Sketchware project) Account in Admob Always place the test ad unit ID before placing your ad unit ID. App ID and ad unit ID can be obtained by registering the app on Admob. But for using test ads no registration is required. Do not click on your own Ads. 1. Create an android project in Sketchware. 2. Add the app to your Admob account. For your app, generate an ad unit ID for Rewarded Video Ads. 3. In Sketchware project, navigate to  Library Manager  and open Admob settings. i. Click on Add manually and add the ad unit ID generated on Admob website, or use test ad unit ID ca-app-pub-3940256099942544/5224354917. ii. If you are not using any banner or interstitial ad units, select the same ad unit ID for both banner and interstitial ads. iii. Add Test Device automatically shown by Sketchware. iv. Save it. v. Switch ON Admob and AppCompat and design. ...

Firebase auth in Sketchware for Login Activity

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