Ankur_009
Ankur_009

Reputation: 3710

Make dialog width to the full screen

Yup this is asked several way but none of the method is working.

I am trying to expend the dialog width to full screen.

Dialog Layout:

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

    <android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/constraint_layout"
        android:layout_width="match_parent"
        android:layout_height="300dp">

        <android.support.constraint.Guideline
            android:id="@+id/guideline3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintGuide_percent="0.08" />

        <TextView
            android:fontFamily="@font/gotham_medium_regular"
            android:textAllCaps="true"
            android:id="@+id/textView6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:text="Login"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
            android:textStyle="bold"
            app:layout_constraintStart_toStartOf="@+id/guideline3"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:fontFamily="@font/gotham_light"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
            android:layout_marginTop="8dp"
            app:layout_constraintStart_toStartOf="@+id/guideline3"
            app:layout_constraintTop_toBottomOf="@+id/textView6"
            android:id="@+id/textView7"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Enter your phone number to proceed" />

        <android.support.design.widget.TextInputLayout
            android:id="@+id/text_input"
            android:layout_marginTop="24dp"
            android:layout_width="0dp"
            app:layout_constraintTop_toBottomOf="@+id/textView7"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="@+id/guideline4"
            app:layout_constraintStart_toStartOf="@+id/guideline3">

            <android.support.design.widget.TextInputEditText
                android:id="@+id/userPhone"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fontFamily="@font/gotham_light"
                android:hint="PHONE NUMBER"
                android:inputType="phone"
                android:text="+91" />
        </android.support.design.widget.TextInputLayout>



        <android.support.constraint.Guideline
            android:id="@+id/guideline4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintGuide_percent="0.92" />


        <Button
            android:layout_marginBottom="@dimen/activity_horizontal_margin"
            android:id="@+id/continueButton"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:background="@drawable/button_background"
            android:fontFamily="@font/gotham_light"
            android:text="Continue"
            android:textColor="@android:color/white"
            app:layout_constraintEnd_toEndOf="@+id/guideline4"
            app:layout_constraintStart_toStartOf="@+id/guideline3"
            app:layout_constraintTop_toBottomOf="@+id/text_input" />


    </android.support.constraint.ConstraintLayout>

Dialog code:

 val dialogBuilder = AlertDialog.Builder(ContextThemeWrapper(context,R.style.ThemeDialog))
        val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        val dialogView = inflater.inflate(R.layout.mobile_bottom_sheet_layout, null)

        val phone = dialogView.findViewById<EditText>(R.id.userPhone)
        dialogBuilder.setView(dialogView)
        val alertDialog = dialogBuilder.create()

        val countinueButton = dialogView.findViewById<Button>(R.id.continueButton) as Button

        countinueButton.setOnClickListener {

            if(phone.text.toString().isEmpty()){
                phone.error=getString(R.string.empty)
                phone.requestFocus()
            }else if(phone.text.toString().length!=13){
                phone.error=getString(R.string.phone_length_error)
                phone.requestFocus()
            }else{

                checkIfTrainerExists(phone.text.toString(),this@LoginActivity);
            }

        }

        alertDialog.show()

Things I have tried:

  1. https://stackoverflow.com/a/28519059/1640009
  2. https://stackoverflow.com/a/2680670/1640009
  3. https://stackoverflow.com/a/6631310/1640009

I have tried the other solutions also. But none is working.

Upvotes: 5

Views: 7941

Answers (6)

Nabin Khatiwada
Nabin Khatiwada

Reputation: 478

Have a look at this sample Full Width Dialog. This may help you.

enter image description here

Create styles for the dialog as below:

 <style name="FullWidth.Dialog" parent="Theme.AppCompat.DayNight.Dialog">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">false</item>
    <item name="android:windowFullscreen">false</item>
    <item name="android:background">@android:color/transparent</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <!--<item name="android:windowAnimationStyle">@style/DialogAnimation</item>-->
</style>

Add style to your dialog on onCreateDialog as:

public class ChangePasswordDialog extends DialogFragment {

private Dialog dialog;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    dialog = new Dialog (getActivity(), R.style.FullWidth_Dialog); //this does the trick.
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    View view = LayoutInflater . from (getActivity()).inflate(
        R.layout.dialog_change_password,
        null
    );
    dialog.setContentView(view);
    dialog.show();
    return dialog;
  }
}

Upvotes: 5

Cristian Cardoso
Cristian Cardoso

Reputation: 675

If you use a ConstraintLayout it will not work the correct match constraint. In order to obtain your layout params apply them on onAttachedToWindow method.

class DialogDownloadFinished(context : Context) : Dialog(context) {

    override fun onCreate(savedInstanceState: Bundle?) {
       super.onCreate(savedInstanceState)
       val dialogView =  DialogDownloadFinishedBinding.inflate(LayoutInflater.from(context),null,false)
       requestWindowFeature(Window.FEATURE_NO_TITLE)
       setContentView(dialogView.root)
    }

    override fun onAttachedToWindow() {
       super.onAttachedToWindow()
       window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
       window.setGravity(Gravity.TOP)
    }
}

Upvotes: 1

Kishan Solanki
Kishan Solanki

Reputation: 14618

For simple AlertDialog in kotlin, you can use

alertDilog.getWindow().setLayout(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)

after alertDilog.show()

Here AlertDialog is android.support.v7.app.AlertDialog

Upvotes: 3

Chetan Kumar Patel
Chetan Kumar Patel

Reputation: 287

Try this

Make custom dialog component

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;

public class CustomDialog extends Dialog {

     public CustomDialog(@NonNull Context context) {
        super(context);
    }

    public CustomDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_layout);// use your layout in place of this.

    }
}

Write below code where you want.

CustomDialog dialog=new CustomDialog(MainActivity.this, android.R.style.Theme_Material_Light_NoActionBar_Fullscreen);
                dialog.show();

Screenshot

Upvotes: 1

Sami Ikhleaf
Sami Ikhleaf

Reputation: 156

Try using Dialog instead of AlertDialog so you can create your custom dialogs as you like.

Also you can of this:

public class CustomDialog extends Dialog{

    public void onCreate(Bundle savedInstance) {

        super.onCreate(savedInstance);

        setContenctView(R.layout.custom_layout);

        // You can manipulate the Dialog using         
        //Window window = getDialog().getWindow();


    }      

}

Upvotes: 1

mbo
mbo

Reputation: 4691

You could create a custom FragmentDialog and do

@Override
public void onResume() {
    // Resize the dialog to full screen.
    if (getDialog() != null && getDialog().getWindow() != null) {
        Window window = getDialog().getWindow();
        window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
    }
    super.onResume();
}

Tutorials for FragmentDialogs:

Upvotes: 4

Related Questions