32

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 ?

ThunderStruct
  • 1,504
  • 6
  • 23
  • 32
Andy Jacobs
  • 15,187
  • 13
  • 60
  • 91
  • PlayersActivity already is your java class for controlling your components. How would class FirstActivity differ from PlayersActivity? – RickNotFred Feb 04 '10 at 13:00
  • to splits up my functionality in different classes so i can reuse that specific layout in othere viewflippers – Andy Jacobs Feb 04 '10 at 13:10

4 Answers4

53

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.

D. Wonnink
  • 306
  • 4
  • 19
RickNotFred
  • 3,381
  • 2
  • 24
  • 26
  • 1
    Step 3 assumes your class is an activity. – RickNotFred Feb 04 '10 at 16:57
  • 1
    i did add in the application node (and followed the other steps to) but it still doesn't call the class do i define it right in the manifest ? – Andy Jacobs Feb 05 '10 at 08:20
  • You need to specify the fully qualified name. Assuming the class resides in the same package as your manifest, you can shorten by specifying as (note the leading ".") – RickNotFred Feb 05 '10 at 11:54
  • @RickNotFred: I'd done all this as mentioned. but still there's problem at line `setContentView(R.layout.FirstActivity);` `FirstActivity` is not resolved or is not a field. – Mohammad Faisal Oct 21 '12 at 06:48
6

1) Create an xml file (say foo.xml).
2) Put foo.xml in res/layout directory.
3) Edit foo.xml and put some android layout code and save it. e.g.,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent" 
              android:layout_height="fill_parent" 
              android:orientation="vertical" >
    <ViewFlipper android:id="@+id/viewFlipper1" 
                 android:layout_width="match_parent" 
                 android:layout_height="wrap_content"></ViewFlipper>
</LinearLayout>

4) In your new activity class put

setContentView(R.layout.foo);

For creating an activity see this answer

I guess the problem with your xml file is that you had not specified any layout for the activity.

Community
  • 1
  • 1
Mohammad Faisal
  • 5,783
  • 15
  • 70
  • 117
3

Not so hard to link 2 layouts just do :

@Override
    public void onClick(View args0) {
    setContentView(R.layout.aardelayout);
}
CaptainStony
  • 51
  • 1
  • 8
0

Change the name from FirstActivity to firstactivity. Layout does not accept caps.

General Grievance
  • 4,555
  • 31
  • 31
  • 45