Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 787
Andy Jacobs Avatar asked Feb 04 '10 10:02

Andy Jacobs


People also ask

Which function is used to include layout to an activity?

Use the <include> tag However, if you want to override layout attributes using the <include> tag, you must override both android:layout_height and android:layout_width in order for other layout attributes to take effect.

How you can create a layout file of activity in Android?

To do so, select a res/layout folder in our project and click the button for creating a new file. In the File field enter the name of the file: myscreen. xml and click Finish. New layout file should open instantly.

How can use multiple layouts in one activity in Android?

You can use as many layouts as possible for a single activity but obviously not simultaneously. You can use something like: if (Case_A) setContentView(R. layout.


2 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.

like image 127
RickNotFred Avatar answered Sep 25 '22 06:09

RickNotFred


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.

like image 24
Mohammad Faisal Avatar answered Sep 26 '22 06:09

Mohammad Faisal