media
media

Reputation: 453

How to get the name of layout associated with an activity?

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?

Upvotes: 4

Views: 8998

Answers (5)

yital9
yital9

Reputation: 6702

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);
    }
}

Upvotes: 2

Gabriele Mariotti
Gabriele Mariotti

Reputation: 363637

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.

Upvotes: 1

AdamMc331
AdamMc331

Reputation: 16691

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);

Upvotes: 0

user6011904
user6011904

Reputation:

use this code to get layout id:

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

and this to get String:

getResources().getResourceName()

Upvotes: 0

Ruchir Baronia
Ruchir Baronia

Reputation: 7561

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

Upvotes: -1

Related Questions