'How to connect a layout view with an activity
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 ?
Solution 1:[1]
Say your new xml file is foo.xml:
- Put
foo.xmlfile in yourres/layoutdirectory. - In your new class use
setContentView(R.layout.foo); - Specify your new class in your manifest file.
See also the topic on declaring layout.
Solution 2:[2]
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.
Solution 3:[3]
Not so hard to link 2 layouts just do :
@Override
public void onClick(View args0) {
setContentView(R.layout.aardelayout);
}
Solution 4:[4]
Change the name from FirstActivity to firstactivity.
Layout does not accept caps.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | D. Wonnink |
| Solution 2 | Community |
| Solution 3 | CaptainStony |
| Solution 4 | General Grievance |
