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:

    Code:
    mPlayMedia = findViewById(R.id.play);
    mPauseMedia = findViewById(R.id.pause);
    mMediaSeekBar = (
    SeekBar) findViewById(R.id.media_seekbar);
    mRunTime = (
    TextView) findViewById(R.id.run_time);
    mTotalTime = (
    TextView) findViewById(R.id.total_time);
    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:

    Code:
    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);
            BA.Log(
    "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);
            BA.Log(
    "resourceId: " + resourceId);
            mPlayerContainer.findViewById(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.

    Cheers,
    Walter
     
  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!
    Cheers,
    Walter
     
  4. bparent

    bparent Member Licensed User

    I look forward to your posting on how you figured this out.
     
Loading...