12

I know that I can set the content of the view in an Android app by saying setContentView(int). Is there a function I can use to know what the current content view is? I don't know if that makes any sense, but what I'm looking for is a function called, say, getContentView that returns an int.

Ideally, it would look like this:

setContentView(R.layout.main); // sets the content view to main.xml
int contentView = getContentView(); // does this function exist?

How would I do that?

Lincoln Bergeson
  • 2,841
  • 4
  • 29
  • 51

4 Answers4

9

Citing Any easy, generic way in Android to get the root View of a layout?

This answer and comments give one method: [Get root view from current activity

findViewById(android.R.id.content)

Given any view in your hierarchy you can also call:

view.getRootView()

to obtain the root view of that hierarchy.

The "decor view" can also be obtained via getWindow().getDecorView(). This is the root of the view hierarchy and the point where it attaches to the window, but I'm not sure you want to be messing with it directly.

Community
  • 1
  • 1
Robert Estivill
  • 11,585
  • 7
  • 36
  • 60
4

You can do making a setter and getter of current view by id only

private int currentViewId = -1;

    public void setCurrentViewById(int id)
    {
        setContentView(id);
        currentViewId = id;
    }

    public int getCurrentViewById()
    {
        return currentViewId;
    }

And then in

protected void onCreate(Bundle savedInstanceState)
{
        super.onCreate(savedInstanceState);
        setCurrentViewById(R.layout.main_layout);

}

Hope this helps.

  • 1
    This will not work. super.onCreate() has to be the first line to be executed within the method. http://stackoverflow.com/questions/8514405/oncreate-not-called – Robert Estivill Nov 16 '12 at 03:20
  • There's no problem if super.onCreate() to be called at any line. BTW I'll move that statement for you. –  Nov 16 '12 at 03:25
3

In an Activity, you can do

View rootView = null;
View currentFocus = getWindow().getCurrentFocus();
if (currentFocus != null)
    rootView = currentFocus.getRootView();

As described above, there is also

View decorView = getWindow().getDecorView();

as well as

View decorView = getWindow().peekDecorView();

The difference between the latter two is that peekDecorView() may return null if the decor view has not been created yet, whereas getDecorView() will create a new decor view if none exists (yet). The first example may also return null if no view currently has focus.

I haven't tried out whether the root view and the decor view are the same instance. Based on the documentation, though, I would assume they are, and it could be easily verified with a few lines of code.

user149408
  • 4,098
  • 1
  • 25
  • 46
1

if you have two content views then you can put a tag inside the relative layout of each one. and then get the view by tag name. if tag name is the one desire then blablabla. Hope this help for whoever is searching for a solution.

ARMAGEDDON
  • 849
  • 2
  • 11
  • 23