Java Question Access B4A Views from Java and vice versa

bloxa69

Active Member
Licensed User
Longtime User
Hi everybody,
I am trying to access B4A Views from my library and/or my library's views from B4A. It includes adding / modifying the views.
I've searched the forum but couldn't find a sample if any. I would guess it can be done with Reflection library, but I'm getting lost in it.
 

thedesolatesoul

Expert
Licensed User
Longtime User
You can just pass the views to the library where they will be unwrapped and in a slightly different form.

Warning: pseudojava
B4X:
void modifyB4AView(anywheresoftware.b4a.button btnb4a){
  android.widget.Button javabtn;
  javabtn = btnb4a.getObject();
  btnb4a.setObject(javabtn);
}

Its actually simpler than this, but to give you an idea.
 

bloxa69

Active Member
Licensed User
Longtime User
hey, thanks man, looks like a way to go, will try ASAP.

how about B4A Panel, what kind of view is that in Java? ViewGroup?
 

bloxa69

Active Member
Licensed User
Longtime User
Yes, that is a ViewGroup if I remember correctly.

I've tried it and it works great, here is the working sample code for those who's struggling with the same thing:

B4X:
        public void modifyB4Panel(BA pBA, PanelWrapper panelB4A, int left, int right, int width, int height){
            ViewGroup javaPanel;
            javaPanel = panelB4A.getObject();
            ImageView imageView = new ImageView(pBA.activity);
            imageView.setBackgroundDrawable(GetApplicationDrawable("nehe_android"));
            imageView.setBackgroundColor(Colors.Red);
            RelativeLayout.LayoutParams imgParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            imgParams.width = width;
            imgParams.height = height;
            imgParams.leftMargin = left;     
            imgParams.rightMargin = right;     
            imageView.setLayoutParams(imgParams);
            imageView.offsetLeftAndRight(200);
               
            javaPanel.addView(imageView);
            panelB4A.setObject(javaPanel);
        }

Now:
I'm trying to modify B4A view (Panel) from withing Java code by using the panel name and not passing the Panel object from B4A;
as a first step I am trying to find my Panel, and it can find the panel ID but then I guess it cannot access that B4A Panel, it shows me the Panel ID in the log, but when I tried to check the Panel's size, it gives null pointer error.

B4X:
int id = pBA.activity.getResources().getIdentifier(PanelName, "id", pBA.context.getPackageName());
      Common.Log("id found: "+id);
//this line gives null pointer error
       ViewGroup container = (ViewGroup)pBA.activity.findViewById(id);
       Common.Log("container found - width: "+container.getWidth());

I guess I should use anywheresoftware.b4a.objects.PanelWrapper instead of ViewGroup but it looks like it doesn't have findViewById method.
When I use PanelWrapper instead of ViewGroup it says "cannot cast from View to PanelWrapper".
 
Last edited:
Top