4

In onCreate() method, we can can use setContentView(R.layout.something) to associate a layout to an activity. Is there anyway I could retrieve something later for an activity?

I know I can use this.findViewById(android.R.id.content).getRootView() to get activity's content view but not sure if there anyway from there I can get something?

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
media
  • 433
  • 5
  • 19
  • By "something", do you mean the View's `id` property? If so, it's simply a case of calling `getId()`, which will return the `int` value. To get it as a `String`, use Blackbelt's answer in this question: http://stackoverflow.com/questions/14647810/easier-way-to-get-views-id-string-by-its-id-int – PPartisan Mar 06 '16 at 19:04

5 Answers5

2

When you want to solve it generally for all your activities:

public class BaseActivity extends AppCompatActivity {

    private int layoutId;

    @Override
    public void setContentView(int layoutResID) {
        this.layoutId = layoutResID;
        super.setContentView(layoutResID);
    }

    protected String getLayoutName() {
        return getResources().getResourceEntryName(this.layoutId);
    }
}

public class MainActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.d("TAG", getLayoutName());
    }
}

Or you can do it simply so:

public class MainActivity extends AppCompatActivity {

    private String layoutName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.layoutName = getResources().getResourceEntryName(R.layout.activity_main);

        Log.d("TAG", layoutName);
    }
}
yital9
  • 6,544
  • 15
  • 41
  • 54
  • This doesn't answer my question. The problem is I don't have `R.layout.activity_main` and I want to retrieve this later and I can't change the source code as you suggested in the first part. – media Mar 06 '16 at 19:29
1

You can get the root view of your Activity using:

this.findViewById(android.R.id.content)

If you need to get view that you added to your activity using setContentView() you can use

final ViewGroup viewGroup = (ViewGroup) ((ViewGroup) this
            .findViewById(android.R.id.content)).getChildAt(0);

But you can also use the same id used in the root element of your xml.

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
0

use this code to get layout id:

this.findViewById(android.R.id.content).getRootView().getId()

and this to get String:

getResources().getResourceName()
0

Since Activity extends from View, you shouldn't need to do this. You are, in a way, already referencing the root view inside the activity. That is what allows you to call findViewById() any time. If you wanted to find the root element, make sure your root (LinearLayout or RelativeLayout) has an id, and you could do something like:

LinearLayout rootLayout = (LinearLayout) findViewById(R.id.my_root);
AdamMc331
  • 16,492
  • 10
  • 71
  • 133
-1

You can use setConetentView(R.layout.content) in any method in any activity, as long as content.xml exists as a layout file in the layout directory.

An activity is a single, focused thing that the user can do. Almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI with setContentView(View). While activities are often presented to the user as full-screen windows, they can also be used in other ways: as floating windows (via a theme with windowIsFloating set) or embedded inside of another activity (using ActivityGroup). There are two methods almost all subclasses of Activity will implement:

onCreate(Bundle) is where you initialize your activity. Most importantly, here you will usually call setContentView(int) with a layout resource defining your UI, and using findViewById(int) to retrieve the widgets in that UI that you need to interact with programmatically. onPause() is where you deal with the user leaving your activity. Most importantly, any changes made by the user should at this point be committed (usually to the ContentProvider holding the data). To be of use with Context.startActivity(), all activity classes must have a corresponding declaration in their package's AndroidManifest.xml.

Source: http://developer.android.com/reference/android/app/Activity.html

Ruchir Baronia
  • 7,406
  • 5
  • 48
  • 83
  • 1
    I think this does not answer my question, What I'm asking is that let's say you use `setConetentView(R.layout.content)` and later given an `Activity` is there any way you could retrieve `R.layout.content` you passed to `setConetentView`? – media Mar 06 '16 at 19:01