Implement Interactive Dialogfragment

Posted on

This example alteration from in conclusion example of “Create DialogFragment amongst AlertDialog.Builder“, to implement interactive DialogFragment. Something sent from top dog activity to DialogFragment, together with something editable inward DialogFragment, together with something sent from DialogFragment dorsum to top dog activity.


MainActivity.java

package com.blogspot.android_er.androiddialogfragment;  import android.app.AlertDialog; import android.app.Dialog; import android.app.DialogFragment; import android.app.Fragment; import android.app.FragmentTransaction; import android.content.DialogInterface; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.Toast;  world degree MainActivity extends AppCompatActivity {      EditText inputTextField;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          inputTextField = (EditText)findViewById(R.id.inputtext);         Button btnOpen = (Button)findViewById(R.id.opendialog);         btnOpen.setOnClickListener(new View.OnClickListener() {             @Override             world void onClick(View v) {                 showDialog();             }         });     }       void showDialog() {         FragmentTransaction ft = getFragmentManager().beginTransaction();         Fragment prev = getFragmentManager().findFragmentByTag("dialog");         if (prev != null) {             ft.remove(prev);         }         ft.addToBackStack(null);          String inputText = inputTextField.getText().toString();          DialogFragment newFragment = MyDialogFragment.newInstance(inputText, inputTextField);         newFragment.show(ft, "dialog");      }      world static degree MyDialogFragment extends DialogFragment {          String mText;         static EditText backTextField;          static MyDialogFragment newInstance(String text, EditText editText) {             backTextField = editText;             MyDialogFragment f = novel MyDialogFragment();              Bundle args = novel Bundle();             args.putString("text", text);             f.setArguments(args);              provide f;         }          @Override         world Dialog onCreateDialog(Bundle savedInstanceState) {             mText = getArguments().getString("text");              //create custom LinearLayout programmatically             LinearLayout layout = novel LinearLayout(getActivity());             layout.setLayoutParams(new LinearLayout.LayoutParams(                     LinearLayout.LayoutParams.MATCH_PARENT,                     LinearLayout.LayoutParams.WRAP_CONTENT));             layout.setOrientation(LinearLayout.VERTICAL);              in conclusion EditText editField = novel EditText(getActivity());             editField.setHint("type something");             Button btnAccept = novel Button(getActivity());             btnAccept.setText("Accept");             in conclusion EditText textField = novel EditText(getActivity());             btnAccept.setOnClickListener(new View.OnClickListener() {                 @Override                 world void onClick(View v) {                     textField.setText(editField.getText().toString());                 }             });              layout.addView(editField);             layout.addView(btnAccept);             layout.addView(textField);              provide novel AlertDialog.Builder(getActivity())                     .setIcon(R.mipmap.ic_launcher)                     .setTitle("Alert Dialog")                     .setMessage(mText)                     .setPositiveButton("OK",                             novel DialogInterface.OnClickListener() {                                 world void onClick(DialogInterface dialog, int whichButton) {                                     Toast.makeText(getActivity(),                                             textField.getText(),                                             Toast.LENGTH_LONG).show();                                     backTextField.setText(textField.getText());                                 }                             }                     )                     .setNegativeButton("Cancel", null)                     .setView(layout)                     .create();         }      } }  

layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout     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:padding="16dp"     android:orientation="vertical"     tools:context=".MainActivity"     android:background="#808080">      <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center_horizontal"         android:autoLink="web"         android:text="https://rootandroid.org//"         android:textStyle="bold" />      <EditText         android:id="@+id/inputtext"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:hint="Type something"/>     <Button         android:id="@+id/opendialog"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="Open DialogFragment"         android:textAllCaps="false"/>  </LinearLayout>