Search code examples
androidandroid-layoutfindviewbyid

changing the layout of current activity


FrameLayout content = (FrameLayout) findViewById(android.R.id.content); 

This gives me an error

error: cannot find symbol
    FrameLayout rootLayout = (FrameLayout)findViewById(android.R.id.content);
                                          ^
symbol:   method findViewById(int)

I have already imported the required R package


Solution

  • It seems like you are trying to access the layout of your current Activity from a different class. Instead of trying to find your FrameLayout in the different class, save the reference to the FrameLayout inside of your Activity, and pass the FrameLayout to your seperate class (the class where you are currently seeing this issue).

    E.g.

    Activity Class:

    ...
    OtherObject myOtherObject = new OtherObject();
    FrameLayout frameLayout = (FrameLayout) findViewById(R.id.my_frame_layout);
    myOtherObject.doStuffWithFrameLayout(frameLayout);
    ...
    

    OtherObject Class:

    ...
    public void doStuffWithFrameLayout(FrameLayout frameLayout) {
        //You can use the FrameLayout here and do stuff with it.
    
        //You will likely also want to pass in a Context object if you want to
        //create a LayoutInflater or do other Context-dependent stuff
    }
    ...