1

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>
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
MR AD
  • 53
  • 1
  • 9

5 Answers5

1

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();
    }
}
lorenzo-s
  • 16,603
  • 15
  • 54
  • 86
1

first initialize the edittext objects and

 EditText et1 = (EditText)v.findViewById(R.id.pass);
 EditText et1 = (EditText)v.findViewById(R.id.cpass);

and use following code to get text

String firstField = et1.getText().toString();

Hope it helps!

Mr.Popular
  • 845
  • 1
  • 8
  • 15
1

you can achieve this using a interface,by sending your data from a fragment to your activity,below i have edited your complete code and its working fine.... Here is the code with example

    Your Main Activity

    public class MainActivity extends Activity  implements SetName{
        Button showDialog;
        TextView showText;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            showDialog = (Button)findViewById(R.id.button1);
            showText = (TextView)findViewById(R.id.textView2);
            showDialog.setOnClickListener(new View.OnClickListener()   
            {
                @Override
                public void onClick(View v) {
                    showMyAlert(v);
                }
            });
        }

        public void showMyAlert(View view) {
            MyAlert myAlert = new MyAlert();
            myAlert.show(getFragmentManager(), "My New Alert");
        }

        public void setMyNameStr(String myNameStr) {

        }

        @Override
        public void setMyName(String string) {
            // TODO Auto-generated method stub

            showText.setText(string);
        }
    }

Your Dialog

public class MyAlert extends DialogFragment {

    MainActivity callBackActivity;
    Button ok, cancel;
    EditText pass, cpass;
    SetName setname;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        callBackActivity = new MainActivity();

       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);

        getEditText.setInputType(InputType.TYPE_CLASS_TEXT);
        AlertDialog.Builder builder = new      
        AlertDialog.Builder(getActivity());
        builder.setTitle("Get UserName :");
        builder.setMessage("Enter Your Name :");
        builder.setPositiveButton("Ok", this);
        builder.setNegativeButton("Cancel", null);
        builder.setView(getEditText);
        return builder.create();
    }


    @Override
    public void onAttach(Activity a) {
        super.onAttach(a);
        setname = (SetName) a;
    }
    @Override
    public void onClick(DialogInterface dialog, int which) {
        String value = pass.getText().toString();
        Log.d("Name : ", value);
        setname .setMyName(value);
        dialog.dismiss();
    }

  ok.setOnClickListener(this);
  cancel.setOnClickListener(this);
}

@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();
    }
}

Create a Interface

public interface SetName {

    void setMyName(String string);

}
Janak
  • 607
  • 5
  • 23
0

you can use callback mechanism for fragment to activity communication.

Communicating between a fragment and an activity - best practices.

Community
  • 1
  • 1
ak0692
  • 233
  • 2
  • 13
0

Add this Interface to your Fragment

public interface OnButtonClicked {
    public void onOk(String passData);
    public void onCancel();
}

On click of Button

if(v.getId()==R.id.ok){
    dismiss();
try {
    ((OnNextClicked) context).onOk(passValue);
    } catch (ClassCastException cce) {cce.printStackTrace();}
}else {
    dismiss();
    Toast.makeText(getActivity(),"cancelled", Toast.LENGTH_LONG).show();
}

implements YourFragment.OnButtonClicked this in your activity

You will get value in

@Override onOk(String passedValue){ //To do here }

Rajesh
  • 2,618
  • 19
  • 25