Add API demo for DialogFragment.

Change-Id: Id02cba60d661f747c36bc113bd81a2b6fe45573a
This commit is contained in:
Dianne Hackborn
2010-07-22 12:18:24 -07:00
parent 5a48825cc0
commit cdb8af8170
5 changed files with 285 additions and 0 deletions

View File

@@ -211,6 +211,13 @@
<!-- Fragment Samples -->
<activity android:name=".app.FragmentAlertDialog" android:label="@string/fragment_alert_dialog">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.SAMPLE_CODE" />
</intent-filter>
</activity>
<activity android:name=".app.FragmentAnim" android:label="@string/fragment_anim">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
@@ -226,6 +233,13 @@
</intent-filter>
</activity>
<activity android:name=".app.FragmentDialog" android:label="@string/fragment_dialog">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.SAMPLE_CODE" />
</intent-filter>
</activity>
<activity android:name=".app.FragmentLayout" android:label="@string/fragment_layout">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

View File

@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2010 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- Top-level content view for the simple fragment sample. -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:padding="4dip"
android:gravity="center_horizontal"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical|center_horizontal"
android:gravity="center_vertical|center_horizontal" />
<Button android:id="@+id/show"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_weight="0"
android:text="@string/show">
<requestFocus />
</Button>
</LinearLayout>

View File

@@ -83,6 +83,8 @@
<string name="redirect_getter">Enter the text that will be used by the main activity. Press back to cancel.</string>
<string name="apply">Apply</string>
<string name="fragment_alert_dialog">App/Fragment/Alert Dialog</string>
<string name="fragment_anim">App/Fragment/Anim</string>
<string name="fragment_context_menu">App/Fragment/Context Menu</string>
@@ -90,6 +92,9 @@
menu; long press the button to see.</string>
<string name="long_press">Long press me</string>
<string name="fragment_dialog">App/Fragment/Dialog</string>
<string name="show">Show</string>
<string name="fragment_layout">App/Fragment/Layout</string>
<string name="fragment_list_array">App/Fragment/List Array</string>

View File

@@ -0,0 +1,74 @@
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.apis.app;
import com.example.android.apis.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class FragmentAlertDialog extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_dialog);
View tv = findViewById(R.id.text);
((TextView)tv).setText("Example of displaying an alert dialog with a DialogFragment");
// Watch for button clicks.
Button button = (Button)findViewById(R.id.show);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog();
}
});
}
void showDialog() {
DialogFragment newFragment = new MyAlertDialogFragment();
newFragment.show(this, "dialog");
}
public static class MyAlertDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setIcon(R.drawable.alert_dialog_icon)
.setTitle(R.string.alert_dialog_two_buttons_title)
.setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked OK so do some stuff */
}
})
.setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked Cancel so do some stuff */
}
})
.create();
}
}
}

View File

@@ -0,0 +1,153 @@
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.apis.app;
import com.example.android.apis.R;
import android.app.Activity;
import android.app.DialogFragment;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class FragmentDialog extends Activity {
int mStackLevel = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_dialog);
View tv = findViewById(R.id.text);
((TextView)tv).setText("Example of displaying dialogs with a DialogFragment. "
+ "Press the show button below to see the first dialog; pressing "
+ "successive show buttons will display other dialog styles as a "
+ "stack, with dismissing or back going to the previous dialog.");
// Watch for button clicks.
Button button = (Button)findViewById(R.id.show);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog();
}
});
if (savedInstanceState != null) {
mStackLevel = savedInstanceState.getInt("level");
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("level", mStackLevel);
}
void showDialog() {
mStackLevel++;
DialogFragment newFragment = new MyDialogFragment(mStackLevel);
FragmentTransaction ft = openFragmentTransaction();
Fragment prev = this.findFragmentByTag("dialog");
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);
newFragment.show(this, ft, "dialog");
}
static int getStyleForNum(int num) {
switch ((num-1)%6) {
case 1: return DialogFragment.STYLE_NO_TITLE;
case 2: return DialogFragment.STYLE_NO_FRAME;
case 3: return DialogFragment.STYLE_NO_INPUT;
case 4: return DialogFragment.STYLE_NORMAL;
case 5: return DialogFragment.STYLE_NORMAL;
}
return DialogFragment.STYLE_NORMAL;
}
static int getThemeForNum(int num) {
switch ((num-1)%6) {
case 4: return android.R.style.Theme_Light;
case 5: return android.R.style.Theme;
}
return 0;
}
static String getNameForNum(int num) {
switch ((num-1)%6) {
case 1: return "STYLE_NO_TITLE";
case 2: return "STYLE_NO_FRAME";
case 3: return "STYLE_NO_INPUT (this window can't receive input, so "
+ "you will need to press the bottom show button)";
case 4: return "STYLE_NORMAL with light fullscreen theme";
case 5: return "STYLE_NORMAL with dark fullscreen theme";
}
return "STYLE_NORMAL";
}
public static class MyDialogFragment extends DialogFragment {
int mNum;
public MyDialogFragment() {
mNum = -1;
}
public MyDialogFragment(int num) {
super(getStyleForNum(num), getThemeForNum(num));
mNum = num;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
mNum = savedInstanceState.getInt("num");
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("num", mNum);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_dialog, container, false);
View tv = v.findViewById(R.id.text);
((TextView)tv).setText("Dialog #" + mNum + ": using style "
+ getNameForNum(mNum));
// Watch for button clicks.
Button button = (Button)v.findViewById(R.id.show);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
((FragmentDialog)getActivity()).showDialog();
}
});
return v;
}
}
}