Escolar Documentos
Profissional Documentos
Cultura Documentos
2. Next a dialog box Application Name is appearing. Set the value as below
3. Click Next
7. Then create xml file. In the res/layout folder, right-click on it and select New >
Android XML File
8. Name the new Android XML file as fragment1.xml and click Finish.
9. Open fragment1.xml file (located in the res/layout folder). Modify the code as below
<?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:background="#00FF00"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is fragment #1"
android:textColor="#000000"
android:textSize="25sp" />
</LinearLayout>
11. Also in the res/layout folder, add another new file and name it fragment2.xml. Modify it as
follows:
<?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:background="#FFFE00"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is fragment #2"
android:textColor="#000000"
android:textSize="25sp" />
</LinearLayout>
13. Open the activity_fragments.xml file and modify the code as below
<?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="horizontal" >
<fragment
android:id="@+id/fragment1"
android:name="com.example.fragment.Fragment1"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment
android:id="@+id/fragment2"
android:name="com.example.fragment.Fragment2"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
15. Under the com.example.fragment package name, add two Java class files and name
them Fragment1.java and Fragment2.java
16. Open the Fragment2.java file and modify the code as below
package com.example.fragment;
import
import
import
import
import
android.app.Fragment;
android.os.Bundle;
android.view.LayoutInflater;
android.view.View;
android.view.ViewGroup;
17. Open the Fragment2.java file and modify the code as below
package com.example.fragment;
import
import
import
import
import
android.app.Fragment;
android.os.Bundle;
android.view.LayoutInflater;
android.view.View;
android.view.ViewGroup;
2. Next a dialog box Application Name is appearing. Set the value as below
3. Click Next
10
6. For the next window, set the value as below and click Finish
7. Under the com.example.dialogfragment package name, add Java class files name as
Fragment1.java
11
android.app.AlertDialog;
android.app.Dialog;
android.app.DialogFragment;
android.content.DialogInterface;
android.os.Bundle;
12
.setIcon(R.drawable.ic_launcher)
.setTitle(title)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
((DialogFragmentActivity) getActivity())
.doPositiveClick();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
((DialogFragmentActivity) getActivity())
.doNegativeClick();
}
}).create();
}
}
13