Java Question Loading a Layout and Inflating it in Java

Discussion in 'Libraries developers questions' started by walterf25, Jul 24, 2015.

  1. walterf25

    walterf25 Well-Known Member Licensed User

    Hello Java experts, i'm trying to wrap a simple AudioPlayer found here.
    The problem i'm having is that i don't understand how to load a xml layout that is found in the res folder, in one of the examples they are loading the buttons, seekbar, etc... layout like this:

    mPlayMedia = findViewById(;
    mPauseMedia = findViewById(;
    mMediaSeekBar = (
    SeekBar) findViewById(;
    mRunTime = (
    TextView) findViewById(;
    mTotalTime = (
    TextView) findViewById(;
    My Question is if I use the #AdditionalResources in B4A how can i find and load this layouts?

    This is the code i have so far in Java:

    public class GenAudioPlayer extends AbsObjectWrapper<AudioWife>{
    private static AudioWife audiowife;
    private ViewGroup mPlayerContainer;
    public void Initialize(BA ba, String uri) throws InstantiationException, IllegalAccessException{
    Uri ll = Uri.parse(uri);
    "Uri parsed: " + ll);
            LayoutInflater inflater = (LayoutInflater) ba.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    String resourceName="aw_player";
            int resourceId = BA.applicationContext.getResources().getIdentifier(resourceName, 
    "layout", BA.packageName);
    "resourceId: " + resourceId);
            AudioWife.getInstance().init(ba.context.getApplicationContext(), ll).useDefaultUi(mPlayerContainer, inflater);

    When I compile the library and run it in B4A it compiles just fine but it crashes with this error:
    I'm assuming it can not find the resources, any ideas on how to fix this?

    Thanks all in advanced.

  2. warwound

    warwound Expert Licensed User

    The best solution i've found for libraries that need to access resources is to make the Eclipse project an 'android library' project instead of a 'standard java' library project.
    Within the wrapper code in Eclipse you can now reference resources using the standard R.? syntax.

    Now when you use the #AdditionalResources attribute in b4a you need to use the package name of the Eclipse android project.

    Search google for info on using Eclipse to create an 'android library' project.
  3. walterf25

    walterf25 Well-Known Member Licensed User

    Hey warwound thanks for your reply, i apologize i forgot to update the thread, but i figured it out, I tried converting the project into a library but for some reason it didn't work, I will post soon the code so that others who encounter the same issue can see how i figured it out.

    Thanks a lot!
  4. bparent

    bparent Member Licensed User

    I look forward to your posting on how you figured this out.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice