Java Question Load a view in xml file in res\layout\ads.xml to my B4A layout

tuhatinhvn

Active Member
Licensed User
Longtime User
I am writing a B4A library for Startapp In App Ads Plugin but i can not know how to make this (StartAPp SDK) - LoAD A VIEW TO MY ACTIVITY (This view is have xml code as below)

To add the Automatic Banner, add the following view inside your Activity layout XML:
<com.startapp.android.publish.banner.Banner
android:id="@+id/startAppBanner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

I know that layout of B4A is file BAL have diffirent structure than xml layout

I think i will create a xml and copy to folder res/layout/startapp.xml(or ads.xml,etc....) mark it read-only
then write code java in library to load this view to my B4A activity

But i dont know much about java code to load it in a library B4A. And content of this xml??
I dont know anything about linerlayout or relativelayout,....

Anyone can help?
 

tuhatinhvn

Active Member
Licensed User
Longtime User
HOw to add this view to my activity??

B4X:
<com.startapp.android.publish.banner.Banner
android:id="@+id/startAppBanner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Hard to say without seeing their API. However this is a View so you need to call its constructor and then call from Basic4android:
B4X:
Dim Banner1 As StartAppBanner
Banner1.Initialize(...)
Activity.AddView(Banner1, ...)

See AdMob code for example:
B4X:
@Version(1.40f)
@ShortName("AdView")
@Events(values={"ReceiveAd", "FailedToReceiveAd (ErrorCode As String)",
     "AdScreenDismissed", "PresentScreen"})
@ActivityObject
@DontInheritEvents
@Permissions(values={"android.permission.INTERNET", "android.permission.ACCESS_NETWORK_STATE"})
@DependsOn(values={"GoogleAdMobAdsSdk"})
public class AdViewWrapper extends ViewWrapper<AdView> {
   /**
    * 320dip x 50dip (default size)
    */
   public static Object SIZE_BANNER = AdSize.BANNER;
   /**
    * 468dip x 60dip - tablet only
    */
   public static Object SIZE_IAB_BANNER = AdSize.IAB_BANNER;
   /**
    * 728dip x 90dip - tablet only
    */
   public static Object SIZE_IAB_LEADERBOARD = AdSize.IAB_LEADERBOARD;
   /**
    * 300dip x 250dip - tablet only
    */
   public static Object SIZE_IAB_MRECT = AdSize.IAB_MRECT;
   /**
    * Ad will use the full available width automatically.
    *You can use this code to add such an ad to the bottom of the screen:
    *<code>
    *Adview1.Initialize2("Ad", "xxxxxxxx", AdView1.SIZE_SMART_BANNER)
    *Dim height As Int
    *If GetDeviceLayoutValues.ApproximateScreenSize < 6 Then
    *  'phones
    *  If 100%x > 100%y Then height = 32dip Else height = 50dip
    *Else
    *  'tablets
    *  height = 90dip
    *End If
    *Activity.AddView(AdView1, 0dip, 100%y - height, 100%x, height)</code>
    */
   public static Object SIZE_SMART_BANNER = AdSize.SMART_BANNER;
   /**
    * Initializes the AdView using the default 320dip x 50dip size.
    *EventName - Name of Subs that will handle the events.
    *PublisherId - The publisher id you received from AdMob.
    */
   public void Initialize(final BA ba, String EventName, String PublisherId) {
     Initialize2(ba, EventName, PublisherId, AdSize.BANNER);
   }
   /**
    * Initializes the AdView.
    *EventName - Name of Subs that will handle the events.
    *PublisherId - The publisher id you received from AdMob.
    *Size - One of the SIZE constants.
    */
   public void Initialize2(final BA ba, String EventName, String PublisherId, Object Size) {
     setObject(new AdView(ba.activity, (AdSize)Size, PublisherId));
     super.Initialize(ba, EventName);
     final String eventName = EventName.toLowerCase(BA.cul);
     getObject().setAdListener(new AdListener() {

       @Override
       public void onFailedToReceiveAd(Ad ad, AdRequest.ErrorCode e){
         ba.raiseEvent(getObject(), eventName + "_failedtoreceivead", e.toString());
       }
       @Override
       public void onReceiveAd(Ad ad) {
         ba.raiseEvent(getObject(), eventName + "_receivead");
       }
       @Override
       public void onDismissScreen(Ad arg0) {
         ba.raiseEventFromDifferentThread(getObject(), null, 0, eventName + "_adscreendismissed", false, null);
       }
       @Override
       public void onLeaveApplication(Ad arg0) {
         //
       }
       @Override
       public void onPresentScreen(Ad arg0) {
         ba.raiseEventFromDifferentThread(getObject(), null, 0, eventName + "_presentscreen", false, null);

       }
     });
   }

   /**
    * Sends a request to AdMob, requesting an ad.
    */
   public void LoadAd() {
     AdRequest req = new AdRequest();
     
     getObject().loadAd(req);
   }

}
 

vpires

Member
Licensed User
Longtime User
B4X:
public void Initialize(final BA ba, String EventName,Boolean show) {
           
            setObject(new Banner(ba.activity));
            super.Initialize(ba, EventName);
            this.myba = ba;
            this.eventName = EventName.toLowerCase(BA.cul);
            this.startappsearch = new StartAppSearch();
            startappsearch.init(myba.activity);
            if (show)
                {
                    startappsearch.showSearchBox(myba.activity);
                }
           
            startAppAd = new StartAppAd(myba.activity);
        }

I've done it like this
 
Top