Saturday, April 16, 2016

Dialog Fragment Part-III - Inter Fragment Communication (Retrieve data from dialog and send to Activity)

Hi ... Dear All ..Today I am uploading a superb example of Dialog Fragment from which we can take inputs and send it back to the Activity.
*****  for this we have to establish a interface between Activity and Dialog Fragment. 
Please check.
I am using Android Studio 1.5.1
Minimun SDK API 19
Target SDK API 23
Please Like us & put your valuable suggestions in comment box.

A. Output will be like   
a.


 

B. XML files 
a. activity_main.xml
<?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:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context="com.exam.ravi.dialogex2.MainActivity">


    <Button        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Show Dialog"

        android:id="@+id/button1"

        android:onClick="showDialog"

        android:layout_centerVertical="true"

        android:layout_centerHorizontal="true" />

    <TextView        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:textAppearance="?android:attr/textAppearanceLarge"

        android:id="@+id/txtrec"        android:layout_alignParentTop="true"

        android:layout_alignParentStart="true"

        android:layout_marginTop="40dp"

        android:layout_alignParentEnd="true" />

</RelativeLayout>

b. Dialog Fragment Layout frag_lay.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent" android:layout_height="match_parent">

    <TextView        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:textAppearance="?android:attr/textAppearanceLarge"

        android:text="Enter Name"

        android:id="@+id/textView"

        android:layout_alignParentTop="true"

        android:layout_alignParentStart="true"

        android:layout_marginTop="42dp" />

    <Button        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Yes"        android:id="@+id/yesbut"

        android:layout_below="@+id/textView"

        android:layout_alignParentStart="true"

        android:layout_marginTop="67dp" />

    <Button        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="No"        android:id="@+id/nobut"

        android:layout_alignTop="@+id/yesbut"

        android:layout_alignParentEnd="true" />

    <EditText        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/editText"

        android:layout_alignTop="@+id/textView"

        android:layout_alignParentEnd="true"

        android:layout_toEndOf="@+id/textView" />
</RelativeLayout>

C. Java Files

a. MainActivity.java

package com.exam.ravi.dialogex2;

import android.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity 
                         implements  MyDialog.InterProcessComm {

   TextView rec;
    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rec= (TextView) findViewById(R.id.txtrec);

    }
public void showDialog(View view)
{
    FragmentManager fragmentManager=getFragmentManager();
    MyDialog myDialog=new MyDialog();
    myDialog.show(fragmentManager,"Ravi_Dialog");
}

    @Override    public void onDialogMsg(String msg) {
        rec.setText(msg);
    }
}

b. MyDialog.java

package com.exam.ravi.dialogex2;

import android.app.Activity;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MyDialog extends DialogFragment implements  View.OnClickListener{
    Button yes,no;
    InterProcessComm communicator;
    EditText name;
    String sendData;
    @Override    public void onAttach(Activity activity) {
        super.onAttach(activity);
        communicator=(InterProcessComm) activity;

    }

    @Override

    public View onCreateView(LayoutInflater inflater,
                       ViewGroup container, Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.frag_lay, container);
        name= (EditText) view.findViewById(R.id.editText);
        yes= (Button) view.findViewById(R.id.yesbut);
        no= (Button) view.findViewById(R.id.nobut);
        yes.setOnClickListener(this);
        no.setOnClickListener(this);
        setCancelable(false);
        getDialog().setTitle("Input Taken Dialog.....");
        return view;
    }

    @Override    public void onClick(View v) {
        if(v.getId()==R.id.yesbut)
        {
            sendData=name.getText().toString();
            communicator.onDialogMsg(sendData);
            dismiss();

        }
        else        {
           Toast.makeText(getActivity(),"Ohhhh Cancelled",Toast.LENGTH_LONG).show();
            dismiss();

        }
    }
    interface InterProcessComm
    {
        public void onDialogMsg(String msg);
    }
}

No comments:

Post a Comment