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

Name: Popup

Version: 2.0

Created On: 13-08-2021

Completed On: 14-08-2021

Created By: Faraz Firoz

Download: Popup.aix (7.5 KB)

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

Blocks:
Screenshot (175)

How to use
Screenshot (176)

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_set_get
component_method (1)
component_method

Download: Popup.aix (7.5 KB)

Useful Extension :heart: :heart:

Thank you @SK_TECH_CREATOR

Excellent work @Faraz_Firoz But I suggest you to make extensions that are diffrent as there is a component already available in niotron

Nice aix but, you can improve blocks part and add more options like colors, etc

That is available soon

Thank you @Salman_Dev
Also You have taught me how to create an extension

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 dismiss, gravity functions and events and listeners for dismiss
:upside_down_face:

Wow thak you @LGdeveloper I also think to add gravity, dismiss, text color, bg color
You have done gravity or dismiss now I am in searching of text and bg color
I think that is not possible for popup menu
If you know then dm me
And thanks for code

I will try and do research

Ok do as fast as you can also I do research

Nice and useful extension @Faraz_Firoz :grinning:

TIP

You can add a github link to the community of your code.

But I do not use github

Hmm…You can then post any other link containing your code

See you can get code in my post

Click on java code you get my code

good one, pleae what of if I want set as when item 1 click, open another screen. Please help me with blocks

Use

When popup1 OnClick
if get item = item1
open another screen

thank you for the information