[Free] Popup Extension - An extension to create popup menu

Name: Popup

Version: 2.0

Created On: 13-08-2021

Created By: Faraz Firoz

Download: Popup.aix (7.5 KB)

Thanking: Thanks to @Aarush_Kumar , @Salman_Dev for his help

Blocks:
Screenshot (180)

How to use

Result

If you all like this extension then click the like button

Java code
package com.faraz.popup;

import android.app.Activity;
import android.content.Context;
import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.annotations.SimpleFunction;
import com.google.appinventor.components.annotations.SimpleProperty;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.runtime.EventDispatcher;
import com.google.appinventor.components.runtime.AndroidViewComponent;
import android.widget.PopupMenu;
import android.view.View;
import android.widget.AdapterView;
import android.view.MenuItem;
import android.view.Menu;
import android.text.SpannableString;
import android.graphics.Color;
import android.text.style.ForegroundColorSpan;
import android.graphics.drawable.Drawable;
import android.widget.PopupMenu.OnDismissListener;

@DesignerComponent(
version = 1,
description = "",
category = ComponentCategory.EXTENSION,
nonVisible = true,
iconName = "")

@SimpleObject(external = true)

public class Popup extends AndroidNonvisibleComponent {

private Context context;
private Activity activity;
private PopupMenu popup;

public Popup(ComponentContainer container){
super(container.$form());
this.activity = container.$context();
this.context = container.$context();
}

@SimpleFunction(description = "Creates popup menu")
public void Create(AndroidViewComponent component) {
View view = component.getView(); 
popup = new PopupMenu(context, view);

popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
OnClick(item.getTitle().toString());
return true;
}
});
}

@SimpleFunction(description = "Add item in popup menu")
public void Add(String name) {
popup.getMenu().add(name);
}

@SimpleFunction(description = "Show popup menu")
public void Show() {
popup.show();
}

@SimpleFunction(description = "Dismiss popup menu")
public void Dismiss() {
popup.dismiss();
}

@SimpleEvent(description = "Detect click of popup menu")
public void OnClick(String name) {
EventDispatcher.dispatchEvent(this, "OnClick", name);
}

@SimpleFunction(description = "Set text color of particular item in popup menu")
public void SetTextColor(String hexColor, int index) {
Menu menu = popup.getMenu();
MenuItem item = menu.getItem(index);
SpannableString s = new SpannableString(item.getTitle().toString());
s.setSpan(new ForegroundColorSpan(Color.parseColor(hexColor)), 0, s.length(), 0);
item.setTitle(s);
}

@SimpleProperty(description = "Set text color of all item in popup menu")
public void TextColor(String hexColor) {
Menu menu = popup.getMenu();
int menuSize = menu.size() - 1;
for (int index = 0; index <= menuSize; index++) {
MenuItem item = menu.getItem(index);
SpannableString s = new SpannableString(item.getTitle().toString());
s.setSpan(new ForegroundColorSpan(Color.parseColor(hexColor)), 0, s.length(), 0);
item.setTitle(s);
}
}

}

Version: 2.0
New blocks available
I am working on gravity and add icon
New blocks
component_method
This block dismiss popup menu
component_method (1)
This block set text color of a particular item
component_set_get
This block set text color of whole item
Download: Popup.aix (7.5 KB)

29 Likes

Good, You should not use property blocks to add an element, use a function.

3 Likes

When I use function it give me error

1 Like

umm thats not possible try again @Faraz_Firoz

I have tried but I got error
not in compile I got error in appp inventor

1 Like

can you show the error we may help

2 Likes

nice extension! its great to use in developing apps

i did try one

popup_extension_testing.aia (21.8 KB)

i hope you could explain or simply elaborate regarding the block available .Thanks

4 Likes
package com.faraz.popup;

import android.app.Activity;

import android.content.Context;

import com.google.appinventor.components.annotations.*;

import com.google.appinventor.components.annotations.SimpleFunction;

import com.google.appinventor.components.annotations.SimpleProperty;

import com.google.appinventor.components.common.ComponentCategory;

import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;

import com.google.appinventor.components.runtime.ComponentContainer;

import com.google.appinventor.components.runtime.EventDispatcher;

import com.google.appinventor.components.runtime.AndroidViewComponent;

import android.widget.PopupMenu;

import android.view.View;

import android.widget.AdapterView;

import android.view.MenuItem;

import java.util.List;

@DesignerComponent(

        version = 1,

        description = "",

        category = ComponentCategory.EXTENSION,

        nonVisible = true,

        iconName = "")

@SimpleObject(external = true)

public class Popup extends AndroidNonvisibleComponent {

    private Context context;

    private Activity activity;

    private PopupMenu popup;

    public Popup(ComponentContainer container){

        super(container.$form());

        this.activity = container.$context();

        this.context = container.$context();

    }

    @SimpleFunction(description = "Creates popup menu")

    public void Create(AndroidViewComponent component) {

        View view = component.getView(); 

        popup = new PopupMenu(context, view);

        popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

                    public boolean onMenuItemClick(MenuItem item) {

                        OnClick (item.getTitle().toString());

                        return true;

                    }

                });
popup.setOnDismissListener(new PopupMenu.OnDismissListener(){
@Override
    public void onDismiss() {
      Dismissed();
    }
});

    }

    @SimpleProperty(description = "Add item in popup menu")

    public void Add(String name) {

        popup.getMenu().add(name);

    }

    @SimpleFunction(description = "Show popup menu")

    public void Show() {

        popup.show();

    }
@SimpleFunction(description = "Show popup menu")

    public void Dismiss() {

        popup.dismiss();

    }

@SimpleProperty
public void Gravity(int bru){
popup.setGravity(bru);
}

    @SimpleEvent(description = "")

    public void OnClick (String name) {

        EventDispatcher.dispatchEvent(this, "OnClick", name);

    }

    @SimpleEvent(description = "")

    public void Dismissed() {

        EventDispatcher.dispatchEvent(this, "Dismissed");

    }
@SimpleProperty
public int gravity()
return popup.getGravity();
}

Added some functions

5 Likes

Nice extension @Faraz_Firoz
:smiley:

2 Likes

@Know_About_IT thank you for code

2 Likes

Wow, you're Incredible @Faraz_Firoz :star_struck: :+1:

1 Like

Thank you @Salman_Dev

2 Likes

Really nice work @Faraz_Firoz :+1:
Maybe in future you can add icons support in popup menu

2 Likes

Yes after some days I am searching on this

3 Likes

Nice work!
slazzer-edit-image

3 Likes

Thank you @Pradevel

3 Likes

Thanks for the contribution to the community. :metal:

2 Likes

Version: 2.0
New blocks available
I am working on gravity and add icon
New blocks
component_method
This block dismiss popup menu
component_method (1)
This block set text color of a particular item
component_set_get
This block set text color of whole item
Download: Popup.aix (7.5 KB)

3 Likes

What do the purple SetTextColor block and the green TextColor block do? How is the action of these two blocks different?
Another thing, a better idea is to use integer colors. The small colored blocks from the color drawer can then be used directly.

1 Like

Purple set text color set color of a particular item and green text color set color of whole item