How to set dialog to show with full screen in Android using Kotlin?
This example demonstrates how to set dialog to show with full screen in Android using Kotlin.
Step 1 − Create a new project in Android Studio, go to File? New Project and fill all required details to create a new project.
Step 2 − Add the following code to res/layout/activity_main.xml.
Example
Step 3 − Add the following code to src/MainActivity.kt
import android.app.Dialog import android.os.Bundle import android.view.View import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() < override fun onCreate(savedInstanceState: Bundle?) < super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) title = "KotlinApp" >fun clickHere(view: View) < val dialog = Dialog(this, android.R.style.Theme_Black_NoTitleBar_Fullscreen) dialog.setContentView(R.layout.activity_main) dialog.show() >>
Step 4 − Add the following code to androidManifest.xml
Let’s try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project’s activity files and click the Run icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen
Click here to download the project code.
Full Screen Dialog Fragment in Android
In this post, we’ll learn about full screen dialog fragment Android. For better understanding, I will create a sample app that app contains full screen dialog in Android. Additionally, we’ll learn how to pass data dialog to fragment or also. So let’s get started.
Full Screen Dialog Fragment (Demo App)
1. Open android studio and create a new project
Create a new android project with Kotlin. Once project get synced up open style file from res=>value=>style.xml. Now create a style for app dialog named is DialogTheme
2. Let’s create a subclass of dialog fragment
Create a new Kotlin class named is FullScreenDialogExample. Here we are passing a CallbackListener inside constructor just for communicating for activity or fragment.
package com.fullscreendialog import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.fragment.app.DialogFragment import kotlinx.android.synthetic.main.layout_full_screen_dialog.* class FullScreenDialogExample(private val callbackListener: CallbackListener) : DialogFragment() < override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? < isCancelable = false return inflater.inflate(R.layout.layout_full_screen_dialog, container, false) >override fun getTheme(): Int < return R.style.DialogTheme >override fun onViewCreated(view: View, savedInstanceState: Bundle?) < super.onViewCreated(view, savedInstanceState) button.setOnClickListener < //send back data to PARENT fragment using callback callbackListener.onDataReceived(editText.text.toString()) // Now dismiss the fragment dismiss() >> >
3. Open layout_full_screen_dialog layout file and put below code
This layout file we are taking one edit text and button. So in this full screen dialog, we will take input from edit text and send back to the parent (activity or fragment where you want )
4. Here is CallbackListener
Create new Interface named is CallbackListener
package com.fullscreendialog interface CallbackListener
5. Open activity layout file and update below code
6. Show dialog using below code
7. Let’s put it into the main activity and see final code look like below
package com.fullscreendialog import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity(), CallbackListener < override fun onCreate(savedInstanceState: Bundle?) < super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) buttonShowDialog.setOnClickListener < showDialog() >> private fun showDialog() < val dialogFragment = FullScreenDialogExample(this) dialogFragment.show(supportFragmentManager, "signature") >override fun onDataReceived(data: String) < textView.text = data >>
Conclusion
In this android example, We have learned the implementation of full screen dialog in android. I hope it’s helpful for you, then help me by sharing this post with all your friends.
Get Solution Code
Keep in touch
If you want to keep in touch and get an email when I write new blog posts, follow me on facebook or subscribe to us. It only takes about 10 seconds to register.
Dialog Fragment with Scroll view — doesn’t scroll | Android studio Project
Sometimes Android projects require to show ScrollView inside a dialog. This ScrollView contains different widgets like Buttons, TextView, and many more widgets.
Here we are showing the Dialog with DialogFragment class.
- Show DialogFragement
- Show Full-Screen Dialog
- Handle Back Button Event for Fragment
- Handle scroll view doesn’t scroll issue
Let’s get started
Step 1: Create an Android project with Android studio.
step 2: Create a class and extends it with DialogFragment class
public class FragmentDialogEx extends DialogFragment < public FragmentDialogEx() < // Required empty public constructor >public static FragmentDialogEx newInstance() < return new FragmentDialogEx(); >@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) < View view = inflater.inflate(R.layout.layout_dialog_fragment, container, false); return view; >@Override public void onActivityCreated(@Nullable Bundle savedInstanceState) < super.onActivityCreated(savedInstanceState); >@Override public void onDismiss(DialogInterface dialog) < super.onDismiss(dialog); >>
Show DialogFragement
Now to show DialogFragment we need to use create the DialogFragment instance and apply the show() method.
FragmentDialogEx doubtFilterFragmentDialog = FragmentDialogEx.newInstance(); doubtFilterFragmentDialog.show(getSupportFragmentManager(), "Show Filters");
Show Full-Screen Dialog
To show Dialog in the full-screen mode we need to add LayoutParams to the Dialog window
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.setContentView(root); dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.WHITE)); dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
Handle Back Button Event for Fragment
When the Dialog is Showing on the same screen and we want to dismiss dialog on back button pressed instead of finish the activity we need to override DialogFragment onBackPressed() method
Dialog dialog = new Dialog(getActivity(),getTheme()) < @Override public void onBackPressed() < dismiss(); >>;
Handle scroll view doesn’t scroll issue
Some time Fragment dialog with scroll view doesn’t scroll
to resolve this issue we need to do the same thing as with activities by adjusting windowSoftInputMode in the dialog theme
We should set a custom theme for our fragment dialog by inheriting the current android dialog and adding the windowSoftInputMode option
Create dialog with
Dialog dialog = new Dialog(getActivity(), R.style.DialogFragmentStyle);
FragmentDialog Complete Example
MainActivity
package com.example.dialogfragment; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; public class MainActivity extends AppCompatActivity < @Override protected void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); >public void showDialog(View view) < FragmentDialogEx doubtFilterFragmentDialog = FragmentDialogEx.newInstance(false); doubtFilterFragmentDialog.show(getSupportFragmentManager(), "Show Dialog"); >public void showDialogFull(View view) < FragmentDialogEx doubtFilterFragmentDialog = FragmentDialogEx.newInstance(true); doubtFilterFragmentDialog.show(getSupportFragmentManager(), "Show Dialog Full Screen"); >>
FragmentDialogEx
package com.example.dialogfragment; import android.app.Dialog; import android.content.DialogInterface; import android.content.Intent; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.widget.ImageView; import android.widget.RelativeLayout; import androidx.annotation.Nullable; import androidx.fragment.app.DialogFragment; public class FragmentDialogEx extends DialogFragment < static boolean isFull; public FragmentDialogEx() < // Required empty public constructor >public static FragmentDialogEx newInstance(boolean isFulls) < isFull=isFulls; return new FragmentDialogEx(); >@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) < View view = inflater.inflate(R.layout.layout_dialog_fragment, container, false); ImageView _back_filter=view.findViewById(R.id._back_filter); _back_filter.setOnClickListener(new View.OnClickListener() < @Override public void onClick(View view) < dismiss(); >>); return view; > @Override public Dialog onCreateDialog(final Bundle savedInstanceState) < // creating the fullscreen dialog final Dialog dialog = new Dialog(getActivity(),getTheme())< @Override public void onBackPressed() < dismiss(); >>; if(isFull) < // the content final RelativeLayout root = new RelativeLayout(getActivity()); root.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.setContentView(root); dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.WHITE)); dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); >return dialog; > @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) < super.onActivityCreated(savedInstanceState); >@Override public void onDismiss(DialogInterface dialog) < super.onDismiss(dialog); >>
layout_fragment_dialg.xml
Conclusion: We have learned how to show simple FragmentDialog in Android. Handle Back Button events to dismiss the Dialog, Show Dialog in Full-Screen Mode, Show Scrollview inside Dialog Android.