Rabu, 25 Juli 2012

DialogFragment with interface to pass data back to activity

The articles "Create custom dialog with EditText" and "Pass back data from dialog to activity" introduced custom Dialog in pre-Honeycomb way.

Honeycomb introduced Fragments to support reusing portions of UI and logic across multiple activities in an app. In parallel, the showDialog / dismissDialog methods in Activity are being deprecated in favor of DialogFragments. (reference: http://android-developers.blogspot.hk/2012/05/using-dialogfragments.html)

This article show how to modify last article "Pass back data from dialog to activity" to do the same function in DialogFragment way, with interface to pass data from DialogFragment to main activity.

DialogFragment with interface to pass data back to activity

DialogFragment with interface to pass data back to activity


To use android.app.DialogFragment, android:minSdkVersion="11" is needed to be specified in AndroidManifest.xml.

MyDialogFragment.java
package com.AndroidCustomDialog;

import android.app.DialogFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/*
* To use android.app.DialogFragment,
* android:minSdkVersion="11" is needed to be specified in AndroidManifest.xml
*/
public class MyDialogFragment extends DialogFragment{

TextView customDialog_TextView;
EditText customDialog_EditText;
Button customDialog_Update, customDialog_Dismiss;

static MyDialogFragment newInstance() {
return new MyDialogFragment();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View dialogView = inflater.inflate(R.layout.customlayout, container, false);

customDialog_TextView = (TextView)dialogView.findViewById(R.id.dialogtextview);
customDialog_Update = (Button)dialogView.findViewById(R.id.dialogupdate);
customDialog_Dismiss = (Button)dialogView.findViewById(R.id.dialogdismiss);
customDialog_Update.setOnClickListener(customDialog_UpdateOnClickListener);
customDialog_Dismiss.setOnClickListener(customDialog_DismissOnClickListener);

customDialog_EditText = (EditText)dialogView.findViewById(R.id.dialogedittext);

return dialogView;
}


private Button.OnClickListener customDialog_UpdateOnClickListener
= new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
customDialog_TextView.setText(customDialog_EditText.getText().toString());
}
};

private Button.OnClickListener customDialog_DismissOnClickListener
= new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
EditDialogListener activity = (EditDialogListener) getActivity();
activity.updateResult(customDialog_EditText.getText().toString());

dismiss();
}
};

public interface EditDialogListener {
void updateResult(String inputText);
}

}


/res/layout/customlayout.xml.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/customdialog"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"/>
<TextView
android:id="@+id/dialogtextview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/dialogedittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/dialogupdate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Update"/>
<Button
android:id="@+id/dialogdismiss"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Dismiss"/>
</LinearLayout>


MainActivity.java
package com.AndroidCustomDialog;

import com.AndroidCustomDialog.MyDialogFragment.EditDialogListener;
import android.os.Bundle;
import android.app.Activity;
import android.app.DialogFragment;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements EditDialogListener{

String result = "";
TextView textReturned;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

textReturned = (TextView)findViewById(R.id.textreturned);

Button buttonStartDialog = (Button)findViewById(R.id.startdialog);
buttonStartDialog.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//showDialog(CUSTOM_DIALOG_ID);

DialogFragment newFragment = MyDialogFragment.newInstance();
newFragment.show(getFragmentManager(), "dialog");

}});
}

public void updateResult(String inputText) {
result = inputText;
textReturned.setText(result);

}

}


/res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world"/>
<Button
android:id="@+id/startdialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" Start Dialog "/>
<TextView
android:id="@+id/textreturned"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>


0 komentar:

Posting Komentar

Copyright © 2012 Codding News All Right Reserved