Jyosna
Jyosna

Reputation: 4446

android how to use following code for Fragments

I am trying to do an application for Fragments. I am doing an example code present at,

http://android-developers.blogspot.com/2011/02/android-30-fragments-api.html

this example has 2 xml files. I unable get where should i use that xml files. And which class should be my main activity class. Plz anybody help me.

Thank you

Upvotes: 0

Views: 1451

Answers (1)

Karthik
Karthik

Reputation: 3529

The code for the main activity is not given in the blog. As author says "The code for this activity is not interesting; it just calls setContentView() with the given layout:".
So you have to create a dummy activity which just calls setContentView() with the Layout given in the first xml file.

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

where main.xml whould contain:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <fragment class="com.example.android.apis.app.TitlesFragment"
            android:id="@+id/titles" android:layout_weight="1"
            android:layout_width="0px"
            android:layout_height="match_parent" />

    <FrameLayout android:id="@+id/details" android:layout_weight="1"
            android:layout_width="0px"
            android:layout_height="match_parent" />

</LinearLayout>

TitleFragment and DetailsFragment would go into seperate java files. The other xml and DetailsActivityis for handling Portrait mode in which case, the DetailsFragment is changed to a separate activity instead of a Fragment.

For more details refer the Example section of http://developer.android.com/guide/topics/fundamentals/fragments.html

Upvotes: 1

Related Questions