2

I have a main Activity that calls a popup window:

AddProduct.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent popup = new Intent(SingleVoucher.this, PopUp.class);
                if (additionalProduct!=null) {
                    popup.putExtra("additionalamount", additionalQuantity);
                    popup.putExtra("additionalprice", additionalPrice);
                    popup.putExtra("additionalproduct", additionalProduct);
                }
                startActivity(popup);

            }
        });

the popup:

public class PopUp extends Activity {

private OnSubmitListener mListener;

@Override
protected void onCreate (Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.popup_window);
    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    int width = dm.widthPixels;
    int height = dm.heightPixels;
    WindowManager.LayoutParams windowManager = getWindow().getAttributes();
    windowManager.dimAmount = 0.75f;
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    TextView SubmitAdditional = (TextView) findViewById(R.id.SubmitAdditional);
    final EditText product = (EditText) findViewById(R.id.adittionalTextEdit);
    final EditText qty = (EditText) findViewById(R.id.additionalQuantityTE);
    final EditText price = (EditText) findViewById(R.id.adittionalPriceTE);
    final Intent intent = getIntent();
    final Context context = this;
    SubmitAdditional.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String additionalProduct = product.getText().toString();
            String additionalQuantity = qty.getText().toString();
            String additionalPrice = price.getText().toString();
            if (additionalProduct!=null && additionalPrice.isEmpty()==false && additionalQuantity.isEmpty()==false) {
                intent.putExtra("additionalamount", additionalQuantity);
                intent.putExtra("additionalprice", additionalPrice);
                intent.putExtra("additionalproduct", additionalProduct);

            }
            else{
                AlertDialog.Builder alert = new AlertDialog.Builder(context);
                alert.setTitle("Missing Details");
                alert.setMessage("Some of the required fields are missing. please try again");
                alert.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener(){
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });


            }

        }
    });
    getWindow().setLayout((int) (width * .8), (int) (height * .3));
}

}

and the XML of the poup window is:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/BlueGray">

    <TextView
        android:id="@+id/AdditionalTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="12dp"
        android:layout_marginTop="5dp"
        android:layout_alignParentTop="true"
        android:paddingTop="2dp"
        android:text="Product Name"
        android:textSize="18dp"
        android:textColor="@color/White">
    </TextView>

    <EditText
        android:id="@+id/adittionalTextEdit"
        android:layout_width="260dp"
        android:layout_height="50dp"
        android:layout_below="@+id/AdditionalTitle"
        android:background="@color/White"
        android:textSize="12dp"
        android:textColor="@color/Black"
        android:layout_centerHorizontal="true"
        android:gravity="top">
        </EditText>

    <TextView
        android:id="@+id/additionalQuantityTV"
        android:layout_marginLeft="12dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/adittionalTextEdit"
        android:text="Quantity"
        android:textColor="@color/White"
        android:textSize="15dp"
        android:layout_marginTop="5dp">
    </TextView>

    <EditText
        android:id="@+id/additionalQuantityTE"
        android:layout_width="100dp"
        android:layout_height="20dp"
        android:layout_toRightOf="@+id/additionalQuantityTV"
        android:layout_below="@+id/adittionalTextEdit"
        android:background="@color/White"
        android:layout_marginTop="5dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="14dp"
        android:layout_marginLeft="38dp"
        android:numeric="integer"
        android:textSize="14dp">
        </EditText>

    <TextView
        android:id="@+id/adittionalPriceTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="12dp"
        android:layout_below="@+id/additionalQuantityTV"
        android:text="Overall price"
        android:textColor="@color/White"
        android:textSize="15dp"
        android:layout_marginTop="5dp">
        </TextView>

    <EditText
        android:id="@+id/adittionalPriceTE"
        android:layout_width="100dp"
        android:layout_height="20dp"
        android:layout_toRightOf="@+id/adittionalPriceTV"
        android:layout_below="@+id/additionalQuantityTE"
        android:background="@color/White"
        android:layout_marginTop="5dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="14dp"
        android:layout_marginLeft="10dp"
        android:numeric="decimal"
        android:textSize="14dp">
</EditText>

    <TextView
        android:id="@+id/popupClose"
        android:layout_marginTop="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Cancel"
        android:layout_below="@+id/adittionalPriceTE"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="14dp"
        android:textColor="@color/White">
    </TextView>

    <TextView
        android:id="@+id/SubmitAdditional"
        android:layout_marginTop="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Submit"
        android:layout_below="@+id/adittionalPriceTE"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="14dp"
        android:textColor="@color/White">
        </TextView>
</RelativeLayout>    

I want to get the values from the EditText and send them back to the parent. How can I send the data back to the view who called the popup?

N J
  • 27,217
  • 13
  • 76
  • 96
Erez Priel
  • 65
  • 1
  • 10

3 Answers3

2

You will have to startActivityForResult()

then return result from PopUp Activity using setResult()

see this good link for statActivityForResult

Community
  • 1
  • 1
N J
  • 27,217
  • 13
  • 76
  • 96
2

You can use startActivityForResult and setResult methods to achieve this. In MainActivity start the popup activity like this

 startActivityForResult(popup,1);

In popup activity use the setResult method to pass the value back to main activity.

    Intent intent = new Intent();
    intent.putExtra("data",data); // data is the value you need in parent
    setResult(100,data);

In MainActivity use the onActivityResult method to get the data

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {     value = data.getBundleExtra("data");
Jiju Induchoodan
  • 4,236
  • 1
  • 22
  • 24
0

Try to use startActivityForResult in order to show the popup.

Here is an example: http://developer.android.com/training/basics/intents/result.html

Then in the main activity you should override onActivityResult in order to have access to the info you sent from popup.

David Rauca
  • 1,583
  • 1
  • 14
  • 17