Search code examples
androidandroid-dialogfragmentgettext

Dialog Fragment to activity data transfer


I have a DialogFragment with two buttons and two text field.

I only want that when I enter data in both text fields and press "ok" button then it can match the data of both fields and save result to a String. Toast msg is working correctly but how can I get data from fragment textfield?? Here is my Fragment code

public class chngpswd extends DialogFragment implements View.OnClickListener {

    Button ok,cancel;

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
        View v=inflater.inflate(R.layout.activity_chngpswd,null);

        ok=(Button)v.findViewById(R.id.ok);
        cancel=(Button)v.findViewById(R.id.cancel);
        ok.setOnClickListener(this);
        cancel.setOnClickListener(this);
        setCancelable(false);
        return v;
    }

    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.ok)
        {
            dismiss();
            Toast.makeText(getActivity(),"ok", Toast.LENGTH_LONG).show();
        }
        else {

            dismiss();
            Toast.makeText(getActivity(),"cancelled", Toast.LENGTH_LONG).show();
        }
    }
}

Code for xml used in the Fragment

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.adnaninayat.myapplication.chngpswd"
    android:weightSum="1">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Cancel"
        android:id="@+id/cancel"
        android:layout_marginTop="48dp"
        android:layout_below="@+id/pass"
        android:layout_centerHorizontal="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="OK"
        android:id="@+id/ok"
        android:layout_alignTop="@+id/cancel"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="numberPassword"
        android:ems="10"
        android:id="@+id/pass"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:hint="Enter New Password"
        android:maxLength="4" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="numberPassword"
        android:ems="10"
        android:id="@+id/cpass"
        android:layout_above="@+id/ok"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:hint="Confirm Password"
        android:maxLength="4" />

</RelativeLayout>

Solution

  • You have to save references to the text fields in your onCreateView() (like you do with buttons) and use them to get the fields data:

    Button ok, cancel;
    EditText pass, cpass;
    
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
        View v=inflater.inflate(R.layout.activity_chngpswd,null);
    
        ok=(Button)v.findViewById(R.id.ok);
        cancel=(Button)v.findViewById(R.id.cancel);
    
        pass = (EditText)v.findViewById(R.id.pass);
        cpass = (EditText)v.findViewById(R.id.cpass);
    
        ok.setOnClickListener(this);
        cancel.setOnClickListener(this);
        setCancelable(false);
        return v;
    }
    
    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.ok)
        {
            dismiss();
            String passData = pass.getText().toString();
            Toast.makeText(getActivity(),"ok: " + passData, Toast.LENGTH_LONG).show();
        }
        else {
    
            dismiss();
            Toast.makeText(getActivity(),"cancelled", Toast.LENGTH_LONG).show();
        }
    }