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:
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
Download: Popup.aix (7.5 KB)
13 Likes
Excellent work @Faraz_Firoz But I suggest you to make extensions that are diffrent as there is a component already available in niotron
1 Like
Faraz_Firoz:
Nice aix but, you can improve blocks part and add more options like colors, etc
2 Likes
Thank you @Salman_Dev
Also You have taught me how to create an extension
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 dismiss, gravity functions and events and listeners for dismiss
1 Like
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
1 Like
Nice and useful extension @Faraz_Firoz
TIP
You can add a github link to the community of your code.
2 Likes
Hmm…You can then post any other link containing your code
1 Like
See you can get code in my post
1 Like
Click on java code you get my code
1 Like
good one, pleae what of if I want set as when item 1 click, open another screen. Please help me with blocks
2 Likes
Use
When popup1 OnClick
if get item = item1
open another screen
1 Like
thank you for the information
2 Likes