Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am new in Android Development. I want to open the Alert Dialog for Number Picker from Main Activity, and then take input from Alert Dialog and show it in the Main view.

I have written code from taking some references and its working correct. But i don't want to use " implements NumberPickerFragment.NoticeDialogListener" in main activity. Please help me, how can i return the value to main activity.

My code for Main Activity is:

    package com.pinnacleappdesign.pinnacleappdesign;

import android.os.Bundle;
import android.app.Activity;
import android.app.DialogFragment;
import android.view.Menu;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements NumberPickerFragment.NoticeDialogListener{

    int memoryIndex = 5;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        Button firstPaneButton = (Button)findViewById(R.id.first_pane_button1);

        firstPaneButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v){
               DialogFragment newFragment = new NumberPickerFragment();

                Bundle args = new Bundle();

                args.putInt("currentMemoryIndex", memoryIndex);
                newFragment.setArguments(args);
                newFragment.show(getFragmentManager(), "numberPicker");
            }       

        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


   public void onDialogPositiveClick(int newMemoryIndex) {
    this.memoryIndex = newMemoryIndex;

    /** Getting the reference of the textview from the main layout */
    TextView tv = (TextView) findViewById(R.id.tv_android);

    /** Setting the selected android version in the textview */
    tv.setText("Your Choice : " + this.memoryIndex);        
   }

}

My code for NumberPickerFragment.java is:

 package com.pinnacleappdesign.pinnacleappdesign;

    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.LayoutInflater;
    import android.view.View;
    import android.widget.NumberPicker;
    import android.widget.Toast;

public class NumberPickerFragment extends DialogFragment{

       /* The activity that creates an instance of this dialog fragment must
     * implement this interface in order to receive event callbacks.
     * Each method passes the DialogFragment in case the host needs to query it. */
    public interface NoticeDialogListener {
        public void onDialogPositiveClick(int newMemoryIndex);
    }

    // Use this instance of the interface to deliver action events
    NoticeDialogListener mListener;

    // Override the Fragment.onAttach() method to instantiate the NoticeDialogListener
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        // Verify that the host activity implements the callback interface
        try {
            // Instantiate the NoticeDialogListener so we can send events to the host
            mListener = (NoticeDialogListener) activity;
        } catch (ClassCastException e) {
            // The activity doesn't implement the interface, throw exception
            throw new ClassCastException(activity.toString()
                    + " must implement NoticeDialogListener");
        }
    }

    public Dialog onCreateDialog(Bundle savedInstanceState) {

        Bundle bundle = getArguments();
        int currentMemoryIndex = bundle.getInt("currentMemoryIndex");

        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

       // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();

        // Inflate and set the layout for the dialog
        // Pass null as the parent view because its going in the dialog layout

        View DialogView = inflater.inflate(R.layout.number_picker, null);

        final NumberPicker np = (NumberPicker)DialogView.findViewById(R.id.numberPicker1);

        np.setMinValue(1);
        np.setMaxValue(100);
        np.setWrapSelectorWheel(false);
        np.setValue(currentMemoryIndex);

        builder.setTitle(R.string.dialog_title)
               .setView(DialogView)
               .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                    // User confirmed the dialog
                    int position = np.getValue();           
                    mListener.onDialogPositiveClick(position);  
                   }
               })
               .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // User cancelled the dialog
                   }
               });
        // Create the AlertDialog object and return it
          return builder.create();
    }

}
share|improve this question

1 Answer 1

What is wrong with your current method? That appears to be the method Android encourages developers to use as seen here: Communicating with Fragments

You can also do something like this within your NumberPickerFragment:

((MainActivity) getActivity()).yourMethod();

But IMO it is much cleaner and re-usable to use the defined Interface method.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.