Create PopupWindow in Sketchware

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

Follow the steps below to create a custom PopupWindow.

1. In VIEW area of your project add a new CustomView 'myview.xml'.

2. Design the CustomView the way you want your custom PopupWindow to look like.

In the image above, I have added four TextViews, of which two act as buttons (namely 'textview3' and 'textview4').

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

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

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

a) First create a View from the CustomView layout file (myview.xml) which will be used as PopupWindow.
View popupView = getLayoutInflater().inflate(R.layout.myview, null);

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

b) Next, use code to create a PopupWindow using the View created above (popupView), with width and height as WRAP_CONTENT.

final PopupWindow popup = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);

c) Define the TextViews used as buttons in the CustomView.

TextView t1 = popupView.findViewById(R.id.textview3);
TextView t2 = popupView.findViewById(R.id.textview4);

Note that 'textview3' and 'textview4' are id of the TextViews added in the CustomView, whereas 't1' and 't2' are names with which the TextViews have been defined.

d) Set OnClickListener for the two TextViews

t1.setOnClickListener(new OnClickListener() { public void onClick(View view) {
//YOUR CODE/BLOCKS HERE
popup.dismiss();
} });

t2.setOnClickListener(new OnClickListener() { public void onClick(View view) {
//YOUR CODES/BLOCKS HERE
popup.dismiss();
} });

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

e) Write code below to show the PopupWindow.

popup.showAtLocation(popupView, Gravity.CENTER, 0, 0);

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

View popupView = getLayoutInflater().inflate(R.layout.myview, null);
final PopupWindow popup = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);

TextView t1 = popupView.findViewById(R.id.textview3);
TextView t2 = popupView.findViewById(R.id.textview4);

t1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
//CODES TO BE EXECUTED
popup.dismiss();
}
});

t2.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick (View v){
//CODES TO BE EXECUTED
popup.dismiss();
}
});

popup.showAtLocation(popupView, Gravity.CENTER, 0, 0);

5. Save and run the project. The custom PopupWindow will show when the button is clicked.

Comments

  1. This is really tough to create a popup window in the sketchware ecen if your try. Check this site https://www.appscomparator.com/bookbub-review/ for book hub and review on different apps comparators.

    ReplyDelete
  2. To create a popup window we need to do so much working side and now the sketchware.click this link and get some big data provider as you might have need for you.

    ReplyDelete
  3. The sketchware could let the popup more perfect but yet the whole things are tough.house rental virginia will give you som rent for any kind of house in there.

    ReplyDelete
  4. What's with all these comment bots spamming you....

    Hi, I'm a real person and I'm new to Sketchware and programming but I'm enjoying it.

    http://sketchware.io/download.jsp?id=G3OU

    Is all I've got so far, app doesn't do much, Passcode in app is 41234, it's just to show you what I've managed to figure out for myself on there.

    Just wanted to say thanks for putting this site up and I'm wondering if you're still active? Get at me,

    Thanks, safe, peace,
    PHNTM

    ReplyDelete
    Replies
    1. Hi, im a beginner too! Would you like to team up and make a app as a developer team? Hope you still here i saw your comment but its from 2019 Please anwser me thank you. My instagram is silenceagaryt.im not a bot

      Delete
  5. Showing error on popup.dismiss();

    ReplyDelete
  6. Is it possible to open a pop-up from another pop-up? That doesn't work for me...

    ReplyDelete
  7. how put edit text in floating windows

    ReplyDelete
  8. Can images be used in pop-up windows?
    Sketchware Pro user

    ReplyDelete

Post a Comment

Popular posts from this blog

Simple car racing android game in Sketchware

Creating a Drawing View in Sketchware

Enable Fullscreen for Youtube videos in WebView

How to enable upload from webview in Sketchware?

List of Calendar Format symbols valid in Sketchware