0

I have been trying to compare my current view from the activity with other random layout file using:

if (this.findViewById(android.R.id.content).getRootView().getId() == 
        findViewById(R.layout.layout_1).getId())

The above code is not working.

The respective ID's displayed are -1 and 22566789

Is it possible to compare them? Please let me know any other method to compare them.

Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
Anurag Sharma
  • 241
  • 1
  • 4
  • 13

2 Answers2

0

The problem with the code you showed is that you call findViewById(R.layout.layout_1). This should be returning null. R.layout.layout_1 is not a View ID, it is a layout ID, and thus findViewById() should fail.

If you know the ID of the root view in your second layout, use that (in the format R.id.my_root_view).

If you want to compare the actual layout IDs, I would recommend storing the current layout ID whenever you call setContentView() (assuming this is an Activity).

Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
0

Simple ex I wrote for you;

public void findId(View root, int id){
    if(root instanceof ViewGroup){
        for(int i = 0; i < ((ViewGroup)root).getChildCount(); ++i){
            findId(((ViewGroup) root).getChildAt(i), id);
        }
    }

    if(root.getId() == id)
    {
        Log.i("view","in it");
    }
}

Call method;

    View root = getWindow().getDecorView().findViewById(android.R.id.content); // activity contentview

    findId(root, R.id.textView1);
nurisezgin
  • 1,530
  • 12
  • 19