Space Ghost
Space Ghost

Reputation: 765

Can I access my XML layout programmatically through findViewById

For example, my layout looks like this

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">
</RelativeLayout>

can I do

RelativeLayout rl = (RelativeLayout)findViewById(R.layout.activity_main); rl.someMethod();

When I do try I get a NPE, but I'm curious if accessing it like this is feasible.

Upvotes: 0

Views: 912

Answers (5)

Sergi Pasoevi
Sergi Pasoevi

Reputation: 2851

You can access your RelativeLayout programmatically. What you are doing wrong is that you are trying to access the layout file in findViewById. What you should do instead, is to specify the id of the relative layout, not the name of the file which contains it. For this, first set id to your relative layout with

android:id="@+id/someRelativeLayout"

Then after having called setContentView(R.layout.activity_main) do:

RelativeLayout rl = (RelativeLayout)findViewById(R.id.someRelativeLayout); 
rl.someMethod();

Upvotes: 1

Sergey  Pekar
Sergey Pekar

Reputation: 8745

If you want to get acces to created layout add id to your xml and use findViewById Like this:

In xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:id="@+id/id">
</RelativeLayout>

In java

RelativeLayout rl = (RelativeLayout)findViewById(R.id.id);

If you want to create layout from xml you should use this code

LayoutInflater inflater = (LayoutInflater) this.getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

RelativeLayout layout = inflater.inflate(R.layout.activity_main, parent_view, false); //parent view is your activity view

Upvotes: 1

Raghunandan
Raghunandan

Reputation: 133560

You need to have

android:id="@+id/relativeLayout"

Then

setContentView(R.layout.activity_main); // set layout to the activity
RelativeLayout rl = (RelativeLayout)findViewById(R.id.relativeLayout); 
// then initialize views 

You get NullPointerException as findViewById din't find the view with the id mentioned which is integer value. Also you had R.layout.activity_main.

I do not know what is someMethod(). You need to look at docs of RelativeLayout the public methods

http://developer.android.com/reference/android/widget/RelativeLayout.html

Upvotes: 1

Piotr Chojnacki
Piotr Chojnacki

Reputation: 6867

Of course it is possible to access this layout as you do it.

You're getting NullPointerException though because you're accessing activity_main which doesn't exist. Your layout in XML file doesn't have its id specified, and while you're trying to get activity_main, it should have id with the same name:

android:id="@+id/activity_main"

Upvotes: 1

Shadow
Shadow

Reputation: 6899

No you can't. You always want to create an id for RelativeLayout and mention that id

   <RelativeLayout
   android:id="@+id/a"
    />

RelativeLayout rl = (RelativeLayout)findViewById(R.id.a);

Upvotes: 1

Related Questions