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.
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.
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.
How would you go about adding a text field and making one button enter that text into a field?
ReplyDelete.
DeleteI didn't understand your question. If you are asking how to add Edittext field in Dialog Box, watch this: https://youtu.be/RT9PXi8aXfA
ReplyDeleteAppreciate the quick response. Sorry about that question it was poorly worded.
DeleteI actually figured that part out. The only thing left that I need regarding this is the ability to add a checkbox to the dialog window.
This comment has been removed by the author.
ReplyDeleteYou have to use Intent:
Deletehttp://www.sketchwarehelp.com/2017/07/how-to-create-new-screen-and-link-it-in.html?m=1
This comment has been removed by the author.
DeleteYou did not understand me, what I wanted to say is that how do I touch the button of the custom dialog box to send me to another page.
ReplyDeleteChange button1 Click code above to what is given below.
Deletebut1.setOnClickListener(new OnClickListener() { public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), YourNewActivity.class);
startActivity(intent);
} });
Or you can divide the code and use blocks in between.
DeleteCan you post the divided snippets I tried this and nothing I tried worked
DeleteWould you modify the snippets so that a set image block can be used?
DeleteI dont want to offend u but your questions sound to me like "would you program my whole project pls"
DeleteHi i want do a locker for app (in main activity with a customwiew name vip) that when i press button 1 is checks if the value of text is equal to value given of me.
ReplyDeleteI tried to do the code but i just failed :(
There it is
final AlertDialog vip = new AlertDialog.Builder (MainActivity.this).create ();
View
inflate = getLayoutInflater ().inflate (R.layout.cust, null);
vip.setView (inflate);
Button
but1 = (Button) inflate.findViewById (R.id.button1);
but1.setOnClickListener (new OnClickListener ()
{
public void onClick (View view)
{
(vip.this.edittext1.getText ().toString ().
contains ("Value"))} dialog2.dismiss ()};
}});
dialog2.show ();
I want to know how i can use this code with a edittext, and make the value from the edittext go to another string
ReplyDeleteYou can create the EditText programmatically and set it as View of Dialog, or add it to any View in CustomView. Check this: https://youtu.be/ZuwQRGlgbC8
Deletehow to add a function that when a button click once a function done and on second click another function done
ReplyDeleteUse a number variable increase by one on click and if "variable" equals 1 do for a if "variable" equals 2 do for b
Deletehow to add a backspace button
ReplyDeleteOn click substring to length minus 1
DeleteHow about a listview in dialog?
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteI stand corrected. I made a mistake in the layout
DeleteIm having a very strange issue. I got the custom dialog to work, then I modified it to have a checkbox instead of the second button, that also worked. Then I set the behavior for the checkbox when checked. That also worked. But when I copied the code and pasted it to the app that I want to use it for, it keeps giving me an error while its compiling, saying cannot resolve checkbox1. I checked three times and the id of the checkbox is checkbox1. I put an exact copy of the code from the working app, and even saved the layout to my collection and used it inbthe second app so it should be completely identical. Please help me out, I feel like there is some supernatural thumb holding me back once again just as I have a little success
ReplyDeletefinal AlertDialog dialog2 = new AlertDialog.Builder(MainActivity.this).create();
DeleteView inflate = getLayoutInflater().inflate(R.layout.cust, null);
dialog2.setView(inflate);
dialog2.setTitle("Exit");
Button but1 = (Button) inflate.findViewById(R.id.button1);
checkbox1 = (CheckBox) findViewById(R.id.checkbox1);
but1.setOnClickListener(new OnClickListener() { public void onClick(View view) {
MainActivity.this.finish(); } });
checkbox1.setOnClickListener(new OnClickListener() { public void onClick(View view) {
preferences.edit().putString("new", "false").commit();
} });
dialog2.show();
You didn't Create "checkbox1" in the second app.
DeleteWrite it as
ReplyDeleteCheckBox checkbox1 = (CheckBox).....
Hey! How to do this with linear1?
DeleteLinearLayout linear1 = inflate.findViewById(R.id.linear1);
DeleteIt shows COMPILE LOG in NEW SKETCHWARE;
ReplyDeletePress enter before the two { public void onclick(Viewview)
DeleteIs it possible to set image view in response to previous events I'm trying to display custom dialog on image click and I want the dialog to display the clicked image
ReplyDeletePut an ImageView in CustomView. Use findViewById to define an ImageView, and setImage of that ImageView from the path or location of image.
DeleteThe images are all app resources whats the easiest way to set the image
DeleteThis comment has been removed by the author.
ReplyDeleteHow can I add edittext in this custom view along with buttons. And get the text of edit text in a string?
ReplyDeleteIs it possible to make the corners round?
ReplyDeleteDid you ever find out?
DeleteYes it is
DeleteHow to change other page from custom dialog.
ReplyDeleteHow to set ImageView image programmatically in Custom dialog box in Sketchware using CustomView ?
ReplyDeletePlease give me 2 lines of that code only.
It's urgent needed Sanjeev Sir. 🙏🙏🙏
I had added a webview "web" in custom dialog and i want it to go back when a button is pressed but it gives error "Cannot refer to the non-final local variable web defined in an enclosing scope" please solve this
ReplyDeleteHow can i add a gridview on the customlist ?
ReplyDeleteHow can i open link on button click in dialog? Thank you
ReplyDeleteHow can i open link on button click in dialog? Thank you
ReplyDeleteWhy its show error everytime i'm using dialog.dismiss();
ReplyDeleteComo adiciono uma fonte no dialog custumizado?
ReplyDeleteHow to Close (dismiss) the Dialog on an another Method?
ReplyDeleteFor example: Show Dialog in on create and dismiss it on Network Response (componement requests Network on Response) ? It says Dialog can not be resolved to a variable...
Declare the AlertDialog in a More Block by placing } and { before and after declaring it.
DeleteIn more block:-
}
AlertDialog dialog2;
{
In onCreate:-
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();
I have a problem, when running the app, I get an error that says that the findViewById code is undefined for the view type
ReplyDeleteHullas dialog chiqarish oson ekan men urgandim
ReplyDeleteFaqat dialogni orqa fonini olib bulmayabdi
nice
ReplyDeleteI want to add a spinner to dialog box
ReplyDeleteThank you sir! Once again you've provided solid code that worked flawlessly.
ReplyDeletei have this error: Cannot refer to the non-final local variable inflate defined in an enclosing scope
ReplyDeleteI don't know why
My sketchware doesn't want to connect to the internet, and it's affecting me from accessing firebase and other functions that require internet connection in the app, what do I do?
ReplyDeleteHow to make it not cancelable?
ReplyDeleteHow can I make it so that when I load a website in my webview I can touch a button and autocomplete the previously saved data?
ReplyDeleteHow can I debug this errors please help me out,
ReplyDelete[/rage/emulated/0/.sketchware/mysc/604/app/src/main/res/layout/customlist.xml:8: error: No resource identifier found for attribute 'cardElevation' in package 'com.my.storysaver', , /storage/emulated/0/.sketchware/mysc/604/app/src/main/res/layout/customlist.xml:8: error: No resource identifier found for attribute 'cardCornerRadius' in package 'com.my.storysaver', , /storage/emulated/0/.sketchware/mysc/604/app/src/main/res/layout/main.xml:24: error: No resource identifier found for attribute 'tabGravity' in package 'com.my.storysaver', , /storage/emulated/0/.sketchware/mysc/604/app/src/main/res/layout/main.xml:24: error: No resource identifier found for attribute 'tabMode' in package 'com.my.storysaver', , /storage/emulated/0/.sketchware/mysc/604/app/src/main/res/layout/main.xml:24: error: No resource identifier found for attribute 'tabIndicatorHeight' in package 'com.my.storysaver', , /storage/emulated/0/.sketchware/mysc/604/app/src/main/res/layout/main.xml:24: error: No resource identifier found for attribute 'tabIndicatorColor' in package 'com.my.storysaver', , /storage/emulated/0/.sketchware/mysc/604/app/src/main/res/layout/main.xml:24: error: No resource identifier found for attribute 'tabSelectedTextColor' in package 'com.my.storysaver', , /storage/emulated/0/.sketchware/mysc/604/app/src/main/res/layout/main.xml:24: error: No resource identifier found for attribute 'tabTextColor' in package 'com.my.storysaver', , /storage/emulated/0/.sketchware/mysc/604/app/src/main/res/layout/main.xml:24: error: No resource identifier found for attribute 'tabTextAppearance' in package 'com.my.storysaver', ]