Include XML layout for Android?

166 views
Skip to first unread message

Gareth Murfin

unread,
Nov 7, 2016, 11:41:42 AM11/7/16
to CodenameOne Discussions
If I want to include a XML layout for my Android build, can I and where do I put it ?

Gareth Murfin

unread,
Nov 7, 2016, 12:04:18 PM11/7/16
to CodenameOne Discussions
I have put it in native\android which I assume is the logical thing to do. But the sample I am working from references R.id.native_ad_body in the xml and I get package R does not exist.

Gareth Murfin

unread,
Nov 7, 2016, 12:07:07 PM11/7/16
to CodenameOne Discussions
Ie, in my native android side can I do tings like this ?

  TextView nativeAdTitle = (TextView) adView.findViewById(R.id.native_ad_title);

in order to get at this in the xml ? 

<TextView
android:id="@+id/native_ad_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:lines="1"
android:textColor="@android:color/black"
android:textSize="18sp" />

Gareth Murfin

unread,
Nov 7, 2016, 12:39:51 PM11/7/16
to CodenameOne Discussions
Sorry for all the questions, if it is possible, how do I actually display some native android widgets? Im basically just trying to do "native facebook adverts" ( https://developers.facebook.com/docs/audience-network/android/native-api ) they appear in a section of your screen and contain an advert (they dont take over full screen) so I want to embed a LinearLayout inside a CN1 container basically, if this is possible?

Shai Almog

unread,
Nov 8, 2016, 12:45:11 AM11/8/16
to CodenameOne Discussions
You can use a native widget if you wrap it in a PeerComponent. Your native interface can return a PeerComponent and when you generate a native interface the Android implementation will be a View. You can then return the view.

The Android XML won't work, you will need to handcode that in Java. Notice that every Android XML syntax can be written in Java so this should be possible. The alternative is to wrap this entire thing in AAR which might be more painful.

Gareth Murfin

unread,
Nov 8, 2016, 8:59:16 AM11/8/16
to CodenameOne Discussions
Thanks Shai. Im almost at the point of using PeerComponents, but I have one weird error  that appears to happen when I run   MediaView nativeAdMedia = new MediaView(context);

It says the class is not found, it seems to find the others such as com.facebook.ads.NativeAd; but not com.facebook.ads.MediaView; any ideas why that is?

ejecting re-init on previously-failed class java.lang.Class<com.facebook.ads.internal.i.c.e>
I/art     ( 1046): Rejecting re-init on previously-failed class java.lang.Class<com.facebook.ads.internal.i.c.e>
I/art     ( 1046): Rejecting re-init on previously-failed class java.lang.Class<com.facebook.ads.internal.i.c.c>
I/art     ( 1046): Rejecting re-init on previously-failed class java.lang.Class<com.facebook.ads.internal.i.c.c>
I/art     ( 1046): Rejecting re-init on previously-failed class java.lang.Class<com.facebook.ads.internal.i.c.c>
I/art     ( 1046): Rejecting re-init on previously-failed class java.lang.Class<com.facebook.ads.internal.i.c.c>
I/art     ( 1046): Rejecting re-init on previously-failed class java.lang.Class<com.facebook.ads.internal.i.c.c>
I/art     ( 1046): Rejecting re-init on previously-failed class java.lang.Class<com.facebook.ads.internal.i.e.c.b>
I/art     ( 1046): Rejecting re-init on previously-failed class java.lang.Class<com.facebook.ads.internal.i.e.c.b>
I/MotoNetwCtrlr( 1193): onReceive: Received intent=Intent { act=android.net.wifi.RSSI_CHANGED flg=0x4000010 (has extras) } Action=android.net.wifi.RSSI_CHANGED
I/MotoNetwCtrlr.MotoWifiSignalCtrlr( 1193): handleBroadcast: Entered: Intent=Intent { act=android.net.wifi.RSSI_CHANGED flg=0x4000010 (has extras) } Action=android.net.wifi.RSSI_CHANGED
I/MotoNetwCtrlr.MotorolaWifiSignalController( 1193): isDirty: returns false
I/MotoNetwCtrlr.MotoWifiSignalCtrlr( 1193): handleBroadcast: Completed
I/MotoNetwCtrlr( 1193): onReceive: Completed intent=Intent { act=android.net.wifi.RSSI_CHANGED flg=0x4000010 (has extras) } Action=android.net.wifi.RSSI_CHANGED
I/art     ( 1046): Rejecting re-init on previously-failed class java.lang.Class<com.facebook.ads.internal.i.c.c>
D/AndroidRuntime( 1046): Shutting down VM
E/AndroidRuntime( 1046): FATAL EXCEPTION: main
E/AndroidRuntime( 1046): Process: com.raffler, PID: 1046
E/AndroidRuntime( 1046): java.lang.NoClassDefFoundError: com.facebook.ads.internal.i.c.c
E/AndroidRuntime( 1046):        at com.facebook.ads.MediaView.<init>(Unknown Source)
E/AndroidRuntime( 1046):        at com.facebook.ads.MediaView.<init>(Unknown Source)
E/AndroidRuntime( 1046):        at userclasses.nativeinterfaces.NativeFBAdvertImpl.inflateAd(NativeFBAdvertImpl.java:225)
E/AndroidRuntime( 1046):        at userclasses.nativeinterfaces.NativeFBAdvertImpl.onAdLoaded(NativeFBAdvertImpl.java:130)
E/AndroidRuntime( 1046):        at com.facebook.ads.NativeAd$1$1.a(Unknown Source)
E/AndroidRuntime( 1046):        at com.facebook.ads.internal.c.b$1$1.run(Unknown Source)
E/AndroidRuntime( 1046):        at android.os.Handler.handleCallback(Handler.java:739)
E/AndroidRuntime( 1046):        at android.os.Handler.dispatchMessage(Handler.java:95)
E/AndroidRuntime( 1046):        at android.os.Looper.loop(Looper.java:135)
E/AndroidRuntime( 1046):        at android.app.ActivityThread.main(ActivityThread.java:5343)
E/AndroidRuntime( 1046):        at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime( 1046):        at java.lang.reflect.Method.invoke(Method.java:372)
E/AndroidRuntime( 1046):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
E/AndroidRuntime( 1046):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
W/ActivityManager(  893):   Force finishing activity 1 com.raffler/.MyApplicationStub
I/OpenGLRenderer(  893): Initialized EGL, version 1.4
W/ActivityManager(  893): Activity pause timeout for ActivityRecord{18b90b08 u0 com.raffler/.MyApplicationStub t5554 f}

Gareth Murfin

unread,
Nov 8, 2016, 9:55:37 AM11/8/16
to CodenameOne Discussions
I stuck recyclerview-v7-21.0.3.aar in native/android and definied the build hint for supportV4, seems to get past the MediaView now :-D

Gareth Murfin

unread,
Nov 8, 2016, 10:45:17 AM11/8/16
to CodenameOne Discussions
So Im now trying to return the PeerComponent, my "view" on the android side is a linearlayout, returning this as a PeerComponent fails to compile, I also tried returning adView.getRootView(); but again this failed, and also casting my linear to a view before returning also fails to compile (View cannot be converted to PeerComponent). What must I do to my linear layout to get it returned as a view?

Gareth Murfin

unread,
Nov 8, 2016, 11:21:09 AM11/8/16
to CodenameOne Discussions
Aha, this works.. not got anything to actually *show* yet :)

public android.view.View getView()
    {
      
        return nativeAdMedia;

Gareth Murfin

unread,
Nov 8, 2016, 1:31:20 PM11/8/16
to CodenameOne Discussions
Well I cant actually get a linearlayout to appear, but if I use the facebooks own "MediaView" it does appear in the centre of a borderlayout. But this is just the background image for the advert (its supposed to be a linearlayout containing textviews about the advert too), what I really need is to return a linear layout containing all of this, however when I do this I just see a black square added to my form. Is it possible to return a linearlayout as a peercomponent and have it display? or do I have to return it in some other format? Really quite hard doing trial and error on this as upload and download times slow everything down.

Shai Almog

unread,
Nov 9, 2016, 12:54:20 AM11/9/16
to CodenameOne Discussions
It should work fine not sure why you are seeing the black screen. If you try to interact with the black screen are the components physically there?
Do you see anything suspicious in DDMS?

Gareth Murfin

unread,
Nov 9, 2016, 9:05:40 AM11/9/16
to CodenameOne Discussions
Thanks mate im gonna recreate it on the Android side, when that works ill bring it back into cn1, probably just some dumb error of mine. I dont see much in ddms and tapping them does nothing. 

Gareth Murfin

unread,
Nov 16, 2016, 5:05:19 AM11/16/16
to CodenameOne Discussions
So I got it all working apart from that when I add it to my form, there is a black gap around it, I have tried stylising everything on the android side to transparent, but for some reason there is still black around my peer component.

Shai Almog

unread,
Nov 16, 2016, 10:25:57 PM11/16/16
to CodenameOne Discussions
Transparent might not be the right strategy, try removing the padding/margin.

Gareth Murfin

unread,
Nov 17, 2016, 11:10:32 PM11/17/16
to CodenameOne Discussions
Looks like I finally got it to work by not leaving the other areas of the borderlayout empty, ie put a blank label in north, east, south, west, I suppose that was kind of obvious in hindsight!.  

Gareth Murfin

unread,
Nov 24, 2016, 7:12:18 AM11/24/16
to CodenameOne Discussions
As a side note on this, I think you really really should be able to design stuff for UI in XML on the android side and inflate that to work in CN1. Coding all the ui by hand on android side and bringing it through is quite hellish (not to mention the same for ios!). I have never been a one to code guis, I have always preferred graphical editors (still using Matisse :)), in Android studio you can make so much cool ui so easily in the xml/ui editor, but you cant use it in cn1. I guess the solution right now is to somehow convert that xml into android java code to include in the native implementation for cn1, but there is no automated tool for this yet as far as I know :-/

Shai Almog

unread,
Nov 25, 2016, 12:35:20 AM11/25/16
to CodenameOne Discussions
You can do that you just need to package it in a lib for iOS and an an aar for Android.

Gareth Murfin

unread,
Nov 25, 2016, 7:32:37 PM11/25/16
to CodenameOne Discussions
Oh, do you have a tutorial, sounds infinitely LESS painful :)

On 25 November 2016 at 13:35, Shai Almog <shai....@gmail.com> wrote:
You can do that you just need to package it in a lib for iOS and an an aar for Android.

--
You received this message because you are subscribed to a topic in the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/codenameone-discussions/2mYpHTODURI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to codenameone-discussions+unsub...@googlegroups.com.
Visit this group at https://groups.google.com/group/codenameone-discussions.
To view this discussion on the web visit https://groups.google.com/d/msgid/codenameone-discussions/a537d63a-abff-4fae-970b-64e841ef577e%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Gareth Murfin
(Android Freelancer - www.garethmurfin.co.uk)

Shai Almog

unread,
Nov 26, 2016, 12:57:12 AM11/26/16
to CodenameOne Discussions
No. aar is a bit new so our 3rd party library integration video/guide predated that.

Gareth Murfin

unread,
Nov 26, 2016, 4:02:45 AM11/26/16
to CodenameOne Discussions
All good man hope to see it in 2017 :) 
Reply all
Reply to author
Forward
0 new messages