How To Add A Component To Extension


#1

As We Do in android studio do xml code to add a item,how to do it in a extension in java?


#2

In Java:
You need to get the view of a ViewGroup(Arrangement)
Then programmatically create a view(Visible Component)
And then add the view(Visible Component) to the ViewGroup(Arrangement)

.getView();
.addView(component, index);


#3

But problem is that I am creating menu component (anyone requested me to make)but when it comes to add it’s XML there is problem so how to do it I can’t understand


#4

I don’t need to set it in arrangement
Actually menu component have its own code
I want to add that


#5

I tried full day adding different types of code


#6

You’ll still need to addview.


#7

You need to convert XML to Java.


#8

Can I get a example :sweat_smile:


#9

XML to java😅now I am getting confused
Can you just gi e a simple hello world example with label in xml


#10

And is it possible that I do this
PopupMenu popupMenu = new PopupMenu(MainActivity.this, button);

And get button by BaseButton


#11

As far as I know we can’t inflate xml(I could be wrong…)
So you need to manually create each view in java and add them to the ViewGroup.


#12
layout = new LinearLayout(form);
    
    LinearLayout.LayoutParams layoutparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, Gravity.START);
    
    activity.addContentView(layout, layoutparams);

#13

I will try to come up with a simple example for you in the next few days.


#14

Thank You For Upper Example Code
And Waiting
Thanks That is Also going to help me


#15

Actually this should work:

@SimpleFunction(description="createWebView")
 public void createWebView(AndroidViewComponent arrangement, String html){
                    ViewGroup myViewGroup = (ViewGroup)arrangement.getView();
            
            WebView myWebView = new WebView(activity);
            myWebView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
            
            myViewGroup.addView(myWebView);// add webview to arrangement 
        
        myWebView.getSettings().setJavaScriptEnabled(true);
        //myWebView.loadUrl(url);
        //String unencodedHtml = "<html><body>Hello World!</body></html>";
        String encodedHtml = Base64.encodeToString(html.getBytes(), Base64.NO_PADDING);
        myWebView.loadData(encodedHtml, "text/html", "base64");
       }

#16

But that’s web view,how do I compare it with menu popup :sweat_smile:
Anyway Thanks :grin::grin::grin::kissing_heart::kissing_heart:


#17

Just for checking block that your code will output I make a project and enter the code there then I got this error

Buildfile: /projects/goldv2/appinventor-sources/appinventor/build.xml

extensions:

clean:

init:

common_CommonUtils:

init:

CommonUtils:

common_CommonVersion:

init:

CommonVersion:
[exec] Result: 128
[exec] Result: 128

CopyToRunLibDir:

components_AndroidRuntime:

init:

CommonConstants:
[javac] Compiling 6 source files to /projects/goldv2/appinventor-sources/appinventor/components/build/classes/CommonConstants
[javac] warning: [options] bootstrap class path not set in conjunction with -source 1.7
[javac] 1 warning

HtmlEntities:

common_CommonVersion:

init:

CommonVersion:
[exec] Result: 128
[exec] Result: 128

AndroidRuntime:
[javac] Compiling 331 source files to /projects/goldv2/appinventor-sources/appinventor/components/build/classes/AndroidRuntime
[javac] warning: [options] bootstrap class path not set in conjunction with -source 1.7
[javac] /projects/goldv2/appinventor-sources/appinventor/components/src/com/appybuilder/krishjha07/WebView/WebView.java:36: error: cannot find symbol
[javac] ViewGroup myViewGroup = (ViewGroup)arrangement.getView();
[javac] ^
[javac] symbol: class ViewGroup
[javac] location: class WebView
[javac] /projects/goldv2/appinventor-sources/appinventor/components/src/com/appybuilder/krishjha07/WebView/WebView.java:36: error: cannot find symbol
[javac] ViewGroup myViewGroup = (ViewGroup)arrangement.getView();
[javac] ^
[javac] symbol: class ViewGroup
[javac] location: class WebView
[javac] /projects/goldv2/appinventor-sources/appinventor/components/src/com/appybuilder/krishjha07/WebView/WebView.java:38: error: cannot find symbol
[javac] WebView myWebView = new WebView(activity);
[javac] ^
[javac] symbol: variable activity
[javac] location: class WebView
[javac] /projects/goldv2/appinventor-sources/appinventor/components/src/com/appybuilder/krishjha07/WebView/WebView.java:39: error: cannot find symbol
[javac] myWebView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
[javac] ^
[javac] symbol: class LayoutParams
[javac] location: class WebView
[javac] /projects/goldv2/appinventor-sources/appinventor/components/src/com/appybuilder/krishjha07/WebView/WebView.java:39: error: cannot find symbol
[javac] myWebView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
[javac] ^
[javac] symbol: variable LayoutParams
[javac] location: class WebView
[javac] /projects/goldv2/appinventor-sources/appinventor/components/src/com/appybuilder/krishjha07/WebView/WebView.java:39: error: cannot find symbol
[javac] myWebView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
[javac] ^
[javac] symbol: variable LayoutParams
[javac] location: class WebView
[javac] /projects/goldv2/appinventor-sources/appinventor/components/src/com/appybuilder/krishjha07/WebView/WebView.java:43: error: cannot find symbol
[javac] myWebView.getSettings().setJavaScriptEnabled(true);
[javac] ^
[javac] symbol: method getSettings()
[javac] location: variable myWebView of type WebView
[javac] /projects/goldv2/appinventor-sources/appinventor/components/src/com/appybuilder/krishjha07/WebView/WebView.java:46: error: cannot find symbol
[javac] String encodedHtml = Base64.encodeToString(html.getBytes(), Base64.NO_PADDING);
[javac] ^
[javac] symbol: variable Base64
[javac] location: class WebView
[javac] /projects/goldv2/appinventor-sources/appinventor/components/src/com/appybuilder/krishjha07/WebView/WebView.java:46: error: cannot find symbol
[javac] String encodedHtml = Base64.encodeToString(html.getBytes(), Base64.NO_PADDING);
[javac] ^
[javac] symbol: variable Base64
[javac] location: class WebView
[javac] /projects/goldv2/appinventor-sources/appinventor/components/src/com/appybuilder/krishjha07/WebView/WebView.java:47: error: cannot find symbol
[javac] myWebView.loadData(encodedHtml, “text/html”, “base64”);
[javac] ^
[javac] symbol: method loadData(String,String,String)
[javac] location: variable myWebView of type WebView
[javac] Note: Some input files use or override a deprecated API.
[javac] Note: Recompile with -Xlint:deprecation for details.
[javac] Note: Some input files use unchecked or unsafe operations.
[javac] Note: Recompile with -Xlint:unchecked for details.
[javac] 10 errors
[javac] 1 warning

BUILD FAILED
/projects/goldv2/appinventor-sources/appinventor/build.xml:35: The following error occurred while executing this line:
/projects/goldv2/appinventor-sources/appinventor/build-common.xml:372: The following error occurred while executing this line:
/projects/goldv2/appinventor-sources/appinventor/components/build.xml:141: The following error occurred while executing this line:
/projects/goldv2/appinventor-sources/appinventor/build-common.xml:118: Compile failed; see the compiler error output for details.

Total time: 5 seconds
s/src/com/appybuilder/krishjha07/WebView/WebView.java:46: error: cannot find symbol
[javac] String encodedHtml = Base64.encodeToString(html.getBytes(), Base64.NO_PADDING);
[javac] ^
[javac] symbol: variable Base64
[javac] location: class WebView
[javac] /projects/goldv2/appinventor-sources/appinventor/components/src/com/appybuilder/krishjha07/WebView/WebView.java:46: error: cannot find symbol
[javac] String encodedHtml = Base64.encodeToString(html.getBytes(), Base64.NO_PADDING);
[javac] ^
[javac] symbol: variable Base64
[javac] location: class WebView
[javac] /projects/goldv2/appinventor-sources/appinventor/components/src/com/appybuilder/krishjha07/WebView/WebView.java:47: error: cannot find symbol
[javac] myWebView.loadData(encodedHtml, “text/html”, “base64”);
[javac] ^
[javac] symbol: method loadData(String,String,String)
[javac] location: variable myWebView of type WebView
[javac] Note: Some input files use or override a deprecated API.
[javac] Note: Recompile with -Xlint:deprecation for details.
[javac] Note: Some input files use unchecked or unsafe operations.
[javac] Note: Recompile with -Xlint:unchecked for details.
[javac] 10 errors
[javac] 1 warning

BUILD FAILED
/projects/goldv2/appinventor-sources/appinventor/build.xml:35: The following error occurred while executing this line:
/projects/goldv2/appinventor-sources/appinventor/build-common.xml:372: The following error occurred while executing this line:
/projects/goldv2/appinventor-sources/appinventor/components/build.xml:141: The following error occurred while executing this line:
/projects/goldv2/appinventor-sources/appinventor/build-common.xml:118: Compile failed; see the compiler error output for details.

Total time: 6 seconds


#18

=

import android.view.ViewGroup;// https://developer.android.com/reference/android/view/ViewGroup


#19

Still error

Buildfile: /projects/goldv2/appinventor-sources/appinventor/build.xml

extensions:

clean:

init:

common_CommonUtils:

init:

CommonUtils:

common_CommonVersion:

init:

CommonVersion:
[exec] Result: 128
[exec] Result: 128

CopyToRunLibDir:

components_AndroidRuntime:

init:

CommonConstants:
[javac] Compiling 6 source files to /projects/goldv2/appinventor-sources/appinventor/components/build/classes/CommonConstants
[javac] warning: [options] bootstrap class path not set in conjunction with -source 1.7
[javac] 1 warning
[javac] Creating empty /projects/goldv2/appinventor-sources/appinventor/components/build/classes/CommonConstants/com/google/appinventor/components/common/package-info.class
[jar] Building jar: /projects/goldv2/appinventor-sources/appinventor/build/components/CommonConstants.jar
[jar] Building jar: /projects/goldv2/appinventor-sources/appinventor/build/components/CommonConstants-gwt.jar

HtmlEntities:
[mkdir] Created dir: /projects/goldv2/appinventor-sources/appinventor/components/build/classes/HtmlEntities
[javac] Compiling 1 source file to /projects/goldv2/appinventor-sources/appinventor/components/build/classes/HtmlEntities
[javac] warning: [options] bootstrap class path not set in conjunction with -source 1.7
[javac] 1 warning
[jar] Building jar: /projects/goldv2/appinventor-sources/appinventor/components/build/HtmlEntities.jar

common_CommonVersion:

init:

CommonVersion:
[exec] Result: 128
[exec] Result: 128

AndroidRuntime:
[javac] Compiling 331 source files to /projects/goldv2/appinventor-sources/appinventor/components/build/classes/AndroidRuntime
[javac] warning: [options] bootstrap class path not set in conjunction with -source 1.7
[javac] /projects/goldv2/appinventor-sources/appinventor/components/src/com/appybuilder/krishjha07/WebView/WebView.java:39: error: cannot find symbol
[javac] WebView myWebView = new WebView(activity);
[javac] ^
[javac] symbol: variable activity
[javac] location: class WebView
[javac] /projects/goldv2/appinventor-sources/appinventor/components/src/com/appybuilder/krishjha07/WebView/WebView.java:40: error: cannot find symbol
[javac] myWebView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
[javac] ^
[javac] symbol: class LayoutParams
[javac] location: class WebView
[javac] /projects/goldv2/appinventor-sources/appinventor/components/src/com/appybuilder/krishjha07/WebView/WebView.java:40: error: cannot find symbol
[javac] myWebView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
[javac] ^
[javac] symbol: variable LayoutParams
[javac] location: class WebView
[javac] /projects/goldv2/appinventor-sources/appinventor/components/src/com/appybuilder/krishjha07/WebView/WebView.java:40: error: cannot find symbol
[javac] myWebView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
[javac] ^
[javac] symbol: variable LayoutParams
[javac] location: class WebView
[javac] /projects/goldv2/appinventor-sources/appinventor/components/src/com/appybuilder/krishjha07/WebView/WebView.java:42: error: incompatible types: WebView cannot be converted to View
[javac] myViewGroup.addView(myWebView);// add webview to arrangement
[javac] ^
[javac] /projects/goldv2/appinventor-sources/appinventor/components/src/com/appybuilder/krishjha07/WebView/WebView.java:44: error: cannot find symbol
[javac] myWebView.getSettings().setJavaScriptEnabled(true);
[javac] ^
[javac] symbol: method getSettings()
[javac] location: variable myWebView of type WebView
[javac] /projects/goldv2/appinventor-sources/appinventor/components/src/com/appybuilder/krishjha07/WebView/WebView.java:47: error: cannot find symbol
[javac] String encodedHtml = Base64.encodeToString(html.getBytes(), Base64.NO_PADDING);
[javac] ^
[javac] symbol: variable Base64
[javac] location: class WebView
[javac] /projects/goldv2/appinventor-sources/appinventor/components/src/com/appybuilder/krishjha07/WebView/WebView.java:47: error: cannot find symbol
[javac] String encodedHtml = Base64.encodeToString(html.getBytes(), Base64.NO_PADDING);
[javac] ^
[javac] symbol: variable Base64
[javac] location: class WebView
[javac] /projects/goldv2/appinventor-sources/appinventor/components/src/com/appybuilder/krishjha07/WebView/WebView.java:48: error: cannot find symbol
[javac] myWebView.loadData(encodedHtml, “text/html”, “base64”);
[javac] ^
[javac] symbol: method loadData(String,String,String)
[javac] location: variable myWebView of type WebView
[javac] Note: Some input files use or override a deprecated API.
[javac] Note: Recompile with -Xlint:deprecation for details.
[javac] Note: Some input files use unchecked or unsafe operations.
[javac] Note: Recompile with -Xlint:unchecked for details.
[javac] Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
[javac] 9 errors
[javac] 1 warning

BUILD FAILED
/projects/goldv2/appinventor-sources/appinventor/build.xml:35: The following error occurred while executing this line:
/projects/goldv2/appinventor-sources/appinventor/build-common.xml:372: The following error occurred while executing this line:
/projects/goldv2/appinventor-sources/appinventor/components/build.xml:141: The following error occurred while executing this line:
/projects/goldv2/appinventor-sources/appinventor/build-common.xml:118: Compile failed; see the compiler error output for details.

Total time: 6 seconds
e following error occurred while executing this line:
/projects/goldv2/appinventor-sources/appinventor/build-common.xml:372: The following error occurred while executing this line:
/projects/goldv2/appinventor-sources/appinventor/components/build.xml:141: The following error occurred while executing this line:
/projects/goldv2/appinventor-sources/appinventor/build-common.xml:118: Compile failed; see the compiler error output for details.

Total time: 6 seconds


#20

=

import android.view.ViewGroup.LayoutParams;

You need to do import for all…