How to connect a layout view with an activity

Viewed 84603

In my main view, I have:

public class PlayersActivity extends Activity {
    ViewFlipper flipper;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.playercontainer);
        flipper = (ViewFlipper) findViewById(R.id.flipper);
    }
}

with this view:

<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/flipper"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <include android:id="@+id/first"  layout="@layout/first" />
    <include android:id="@+id/second"  layout="@layout/playerdetailsview" />
</ViewFlipper>

It displays the first view correctly but I want it to be connected to a java class so I created an FirstActivity class where I can control all my components in the first view but how do I attach the first.xml layout with the FirstActivity java class ?

4 Answers

Say your new xml file is foo.xml:

  1. Put foo.xml file in your res/layout directory.
  2. In your new class use setContentView(R.layout.foo);
  3. Specify your new class in your manifest file.

See also the topic on declaring layout.

Related