Android Question Can setLayoutAnimated be called inside a Class ?

max123

Well-Known Member
Licensed User
Longtime User
Hi all,

as for thread title, can setLayoutAnimated be called inside a Class ?

I use this code:
B4X:
'Show a console with layout animation.
'Duration:  The animation duration in milliseconds. Pass 0 for no animation.
Sub ShowConsole (Duration As Int)
    mDuration = Duration ' Store for future uses
    mTextConsole.SetLayoutAnimated(mDuration, 0, 100%y-mHeight, 100%x, mHeight)
    mLabelConsole.SetLayoutAnimated(mDuration, 0, 64%y, 100%x, 20dip)
    mLogActive = True
End Sub

'Hide a console with layout animation.
'Duration:  The animation duration in milliseconds. Pass 0 for no animation.
Sub HideConsole(Duration As Int)
    mDuration = Duration ' Store for future uses
    mTextConsole.SetLayoutAnimated(mDuration, 0, 100%y, 100%x, mHeight)
    mLabelConsole.SetLayoutAnimated(mDuration, 0, 100%y, 100%x, 20dip)
    mLogActive = False
End Sub
The TextConsole and TextLabel controls are added by code in the Class Initialize method this way:
B4X:
'Inizialize the JSConsole object.
Public Sub Initialize(Parent As Panel, Module As Object, EventName As String, WebView As WebView, Height As Float)
    mParent = Parent
    mModule = Module
    mEventName = EventName.ToLowerCase
    mHeight = Height
 
    mWebView1 = WebView
    mWebView1.Initialize("mWV1")

    mTextConsole.Initialize("TextConsole")
    mTextConsole.TextSize = 10
    mTextConsole.Typeface = Typeface.SANS_SERIF
    mTextConsole.Color = Colors.DarkGray
    mTextConsole.TextColor = Colors.Green
    mTextConsole.Gravity = Gravity.LEFT + Gravity.TOP
    mTextConsole.Padding = Array As Int (3dip, 23dip, 3dip, 3dip)
    mTextConsole.Text = "Ready" & CRLF & CRLF
    mParent.AddView(mTextConsole, 0, 100%y-Height, 100%x, Height)
 
    mLabelConsole.Initialize("LabelConsole")
    mLabelConsole.TextSize = 12
    mLabelConsole.Typeface = Typeface.DEFAULT_BOLD
    mLabelConsole.Color = Colors.Blue
    mLabelConsole.TextColor = Colors.LightGray
    mLabelConsole.Gravity = Gravity.CENTER_HORIZONTAL + Gravity.CENTER_VERTICAL
    mLabelConsole.Padding = Array As Int (1dip, 1dip, 1dip, 1dip)
    mLabelConsole.Text = "JS CONSOLE"
    mParent.AddView(mLabelConsole, 0, 100%y-Height, 100%x, 20dip)

When I compile this is the error message.
Many thanks

** Activity (main) Resume **
jsconsole_hideconsole (java line: 98)
java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to anywheresoftware.b4a.BALayout$LayoutParams
at anywheresoftware.b4a.objects.ViewWrapper.SetLayoutAnimated(ViewWrapper.java:337)
at b4a.console.jsconsole._hideconsole(jsconsole.java:98)
at b4a.console.jsconsole._labelconsole_click(jsconsole.java:183)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:5225)
at android.view.View$PerformClick.run(View.java:21195)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5451)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
I use this code
Try to reproduce it in a small project and upload it. You are giving not enough details for us to help you.
 
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
Ok, many thanks all for very fast replies. I Appreciated ;) B4X The Rock Community ...

The project is small, I had the necessity to integrate a sort of Javascript console to one of my apps.
This worked well many thanks to @drgottjr here that posted me an example on how to add to a WebCromeClient to WebView using inline java code.
https://www.b4x.com/android/forum/threads/webviewextras.12453/page-16#post-884695

I've tried then to write it inside a class to be easly ported in projects, but I had this problem.

I will post a full code here ...

NOTE: I changed the package name part from 'main' to 'jsconsole' that is a class name, not sure if this is correct.
On the Activity works. Please correct me if it is wrong.

Many thanks
 

Attachments

  • JSConsole.zip
    10.5 KB · Views: 152
Last edited:
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
Sorry, now I have another problem not relative to this my thread, you posted, but I never received notifications.
If I go on Alerts I only see just your two latest, nor olders. This is strange....
I have private conversations open, now it only show 'no conversations'.

I tried to logout and relogin two times but nothing changed.

EDIT: For private conversations if I do ShowAll I can see my conversations, but for Alerts if I do ShowAll I just see your two last, all others do not appears.
 
Last edited:
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
Now I refactored it.

I found the mistake because setLayoutAnimated do not worked, I passed to a Inizialize sub the 'Activity.Parent', passing just 'Activity' it now compile.

All compile now, but when inline java is called the app crashes without log errors in the line if (ba. subExists). ba is Null here, how initialize it ?
Using a try/catch to force a debug error the error message is this:
Something went wrong: Attempt to invoke virtual method 'boolean anywheresoftware.b4a.BA.subExists(java.lang.String)' on a null object reference
I've tried to add 'BA ba' as first argument but I do not know where exactly put it.

Here my inline Java code. Any suggestion please ?
Java:
#if Java
   //import anywheresoftware.b4a.*;
   import anywheresoftware.b4a.BA;   
   import anywheresoftware.b4a.keywords.Common;
    
   import android.content.Context;   
   import android.webkit.WebView;
   import android.webkit.WebChromeClient;
   import android.webkit.WebResourceRequest;
   import android.webkit.WebResourceResponse;   
   import android.webkit.ConsoleMessage;

    public static class MyChromeClient extends WebChromeClient {
        Context context;
        WebView webview;
        String fullEventName;

        BA ba;  // HERE ba is Null HOW TO INITIALIZE it ?

        public MyChromeClient(Context c, WebView w, String event) {  // aggiunto BA ba
            this.context = c;
            this.webview = w;
            this.fullEventName = event.toLowerCase(BA.cul) + "_console";
            
            w.setWebChromeClient(new WebChromeClient() {
            @Override
            public boolean onConsoleMessage (ConsoleMessage consoleMessage) {
               String logMessage = "JS LOG: " + consoleMessage.message() +
                    "    -    (" + consoleMessage.sourceId() + " @ Line: " + consoleMessage.lineNumber() + ")";
                    
                    BA.Log("Event: " + fullEventName);
                    BA.Log(logMessage);  // <<<<<<< Redirect JS Log to B4A Log (You can do it here or in B4A Sub Event)                 
          
                  try {
                        //ba = (BA) this.getClass().getField("processBA").get(null);
                  
                        // CRASH HERE EVEN WITH ALL COMMENTED, IF REMOVE THIS PART DO NOT CRASH BUT NO CALLBACK ARE CALLED
                        if (ba.subExists(fullEventName)) { 
                            //BA.Log("SUB " + fullEventName + " EXISTS");
                            // ba.raiseEventFromUI(this, fullEventName, new Object[] {logMessage});   
                        } else {
                            //BA.Log("SUB " + fullEventName + " DO NOT EXISTS");
                        } 
                } catch(Exception e) {
                      BA.Log("Something went wrong: " + e.getMessage());
                  }                             
                    return true;
                }
         });
          
        }   
    }
#End if
 
Last edited:
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
Please help, I blocked. I cannot code....

you told me to upload the project and I uploaded it, my last modification was just to replace Activity.Parent with Activity in the main when Initialize the class.

Many Thanks
 
Upvote 0
Top