Thursday, 12 January 2012

DialogFragment

android.app.DialogFragment, was introduced from API Level 11 (Android 3.0), displays a dialog window, floating on top of its activity's window.

DialogFragment

- Create a new MyDialogFragment.java class extends android.app.DialogFragment.
package com.exercise.AndroidDialogFragment;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;

public class MyDialogFragment extends DialogFragment {

static MyDialogFragment newInstance() {

String title = "My Fragment";

MyDialogFragment f = new MyDialogFragment();
Bundle args = new Bundle();
args.putString("title", title);
f.setArguments(args);
return f;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String title = getArguments().getString("title");
Dialog myDialog = new AlertDialog.Builder(getActivity())
.setIcon(R.drawable.ic_launcher)
.setTitle(title)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
((AndroidDialogFragmentActivity)getActivity()).okClicked();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
((AndroidDialogFragmentActivity)getActivity()).cancelClicked();
}
})
.create();

return myDialog;
}

}


- Modify main.xml layout to add a button to open the dialog.
<?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" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/opendialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Open Dialog" />

</LinearLayout>


- Main activity, AndroidDialogFragmentActivity.java
package com.exercise.AndroidDialogFragment;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class AndroidDialogFragmentActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


Button buttonOpenDialog = (Button)findViewById(R.id.opendialog);
buttonOpenDialog.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
OpenDialog();
}});

}

void OpenDialog(){
MyDialogFragment myDialogFragment = MyDialogFragment.newInstance();
myDialogFragment.show(getFragmentManager(), "myDialogFragment");
}

public void okClicked() {
Toast.makeText(AndroidDialogFragmentActivity.this,
"OK Clicked!",
Toast.LENGTH_LONG).show();
}

public void cancelClicked() {
Toast.makeText(AndroidDialogFragmentActivity.this,
"Cancel Clicked!",
Toast.LENGTH_LONG).show();
}
}



Download the files.

No comments:

Post a Comment