Jul 24, 2012

Pass back data from dialog to activity

In the article "Create custom dialog with EditText", a dialog with user editable field (EditText) was implemented. To pass back the user enter data from dialog to activity, we can create a global variable. Because both the main activity and the dialog are in the same class, such that both can access the global variable directly.

Pass back data from dialog to activity


Main code in MainActivity.java.
package com.AndroidCustomDialog;

import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
 
 static final int CUSTOM_DIALOG_ID = 0;
  
 TextView customDialog_TextView;
 EditText customDialog_EditText;
 Button customDialog_Update, customDialog_Dismiss;
 
 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); 
         }});
    }
    
    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());
      
      result = customDialog_EditText.getText().toString();
      textReturned.setText(result);
     } 
    };
    
    private Button.OnClickListener customDialog_DismissOnClickListener
     = new Button.OnClickListener(){
     
     @Override
     public void onClick(View arg0) {
      // TODO Auto-generated method stub
      dismissDialog(CUSTOM_DIALOG_ID); 
     } 
    };
     
    @Override
    protected Dialog onCreateDialog(int id) {
     // TODO Auto-generated method stub
     Dialog dialog = null;;
     
     switch(id) {
     case CUSTOM_DIALOG_ID:
      dialog = new Dialog(MainActivity.this);
      
      dialog.setContentView(R.layout.customlayout);
      dialog.setTitle("Custom Dialog");
      
      customDialog_EditText = (EditText)dialog.findViewById(R.id.dialogedittext);
      customDialog_TextView = (TextView)dialog.findViewById(R.id.dialogtextview);
      customDialog_Update = (Button)dialog.findViewById(R.id.dialogupdate);
      customDialog_Dismiss = (Button)dialog.findViewById(R.id.dialogdismiss);
      customDialog_Update.setOnClickListener(customDialog_UpdateOnClickListener);
      customDialog_Dismiss.setOnClickListener(customDialog_DismissOnClickListener);
      break; 
     }
     return dialog; 
    }
    
}


/res/layout/customlayout.xml, the layout of the dialog.
<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>


Main layout, /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>


Next:
- DialogFragment with interface to pass data back to activity


1 comment:

Infolinks In Text Ads