In many applications, you may have seen that whenever we have to make choices some kind of elevated dialog box appears and ask the user for some input or choice. In this article, we are going to see the same that how we can pass data from a dialog box to activity in android studio.
What we are going to build in this article?
In this article, we will see that a dialog box appears and ask the user to type a message and the same message will be displayed in the activity. Here is a sample video to understand what we are going to build in this article and what actually a Dialog Fragment is
Step by Step Implementation
Step 1: Create a new project
- Open a new project.
- We will be working on Empty Activity with language as Java. Leave all other options unchanged.
- You can change the name of the project at your convenience.
- There will be two default files named activity_main.xml and MainActivity.java.
If you don’t know how to create a new project in Android Studio then you can refer to How to Create/Start a New Project in Android Studio?
Step 2: Working with XML files
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
XML
<? xml version = "1.0" encoding = "utf-8" ?> < RelativeLayout android:layout_width = "match_parent" android:layout_height = "match_parent" tools:context = ".MainActivity" > < RelativeLayout android:layout_width = "match_parent" android:layout_height = "match_parent" android:id = "@+id/activity_container" > < TextView android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_centerHorizontal = "true" android:text = "This is an Activity" android:id = "@+id/heading" android:textSize = "18sp" android:textColor = "#000" /> < Button android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "open dialog" android:layout_centerHorizontal = "true" android:id = "@+id/open_dialog" android:layout_below = "@+id/heading" android:layout_marginTop = "20sp" /> < TextView android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:id = "@+id/input_display" android:layout_below = "@+id/open_dialog" android:layout_marginTop = "20dp" android:layout_centerHorizontal = "true" android:textSize = "15sp" android:textColor = "#000" /> </ RelativeLayout > </ RelativeLayout > |
Follow the path app > res > layout > right-click > new > Layout resource File > Name it as “dailog_fragment.xml“. . Below is the code for dailog_fragment.xml file-
XML
<? xml version = "1.0" encoding = "utf-8" ?> < RelativeLayout android:layout_width = "match_parent" android:layout_height = "match_parent" > < RelativeLayout android:layout_width = "match_parent" android:layout_height = "200dp" android:padding = "10dp" android:background = "#DDB4B4" > < TextView android:layout_width = "match_parent" android:layout_height = "wrap_content" android:text = "Your Custom Dialog Heading" android:textSize = "18sp" android:textColor = "#000" android:id = "@+id/heading" /> < EditText android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_below = "@id/heading" android:layout_marginTop = "20dp" android:background = "@null" android:textColor = "#000" android:hint = "enter some text..." android:id = "@+id/input" /> < TextView android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "OK" android:layout_alignParentBottom = "true" android:layout_alignParentRight = "true" android:layout_marginRight = "20dp" android:textSize = "18sp" android:textColor = "#33bbff" android:id = "@+id/action_ok" /> < TextView android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "CANCEL" android:layout_alignParentBottom = "true" android:layout_alignParentLeft = "true" android:layout_marginRight = "20dp" android:textSize = "18sp" android:textColor = "#33bbff" android:id = "@+id/action_cancel" /> </ RelativeLayout > </ RelativeLayout > |
Step 3: Working with java files
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file.
Java
package com.example.dialogfragmenttoactivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity implements DialogFragment.OnInputListener { private static final String TAG = "MainActivity" ; @Override public void sendInput(String input) { Log.d(TAG, "sendInput: got the input: " + input); mInput = input; setInputToTextView(); } private Button mOpenDialog; public TextView mInputDisplay; public String mInput; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); mOpenDialog = findViewById(R.id.open_dialog); mInputDisplay = findViewById(R.id.input_display); mOpenDialog.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { Log.d(TAG, "onClick: opening dialog." ); DialogFragment dialog = new DialogFragment(); dialog.show(getFragmentManager(), "MyCustomDialog" ); } }); } private void setInputToTextView() { mInputDisplay.setText(mInput); } } |
Follow the path app > java > right-click > new > java class. Name it as DialogFragment.java, below is the code for DialogFragment.java file-
Java
package com.example.dialogfragmenttoactivity; import android.content.Context; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.EditText; import android.widget.TextView; public class DialogFragment extends android.app.DialogFragment { private static final String TAG = "DialogFragment" ; public interface OnInputListener { void sendInput(String input); } public OnInputListener mOnInputListener; private EditText mInput; private TextView mActionOk, mActionCancel; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate( R.layout.dailog_fragment, container, false ); mActionCancel = view.findViewById(R.id.action_cancel); mActionOk = view.findViewById(R.id.action_ok); mInput = view.findViewById(R.id.input); mActionCancel.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { Log.d(TAG, "onClick: closing dialog" ); getDialog().dismiss(); } }); mActionOk.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { Log.d(TAG, "onClick: capturing input" ); String input = mInput.getText().toString(); mOnInputListener.sendInput(input); getDialog().dismiss(); } }); return view; } @Override public void onAttach(Context context) { super .onAttach(context); try { mOnInputListener = (OnInputListener)getActivity(); } catch (ClassCastException e) { Log.e(TAG, "onAttach: ClassCastException: " + e.getMessage()); } } } |
Here is the final output of our application.
Output: