Wahdat Jan
Wahdat Jan

Reputation: 4156

How to set height of Alert Dialog

I want to set height of Alert dialog in my App. Since i use more than 10 fields inside the alert dialog to fill the data with which set positive and set negative button goes invisible . Now i want to decrease the height of alert dialog.I use getwindow.setlayout method but it is still at the same height.

    package com.clone.tracking.unnamed.mytraining;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import com.clone.tracking.unnamed.mytraining.Model.Student;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.rengwuxian.materialedittext.MaterialEditText;

public class AdminControl extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    RelativeLayout rootLayout;

    FirebaseDatabase db;
    DatabaseReference students;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_admin_control);

        db=FirebaseDatabase.getInstance();
        students=db.getReference("Students");


        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showStudentRegistrationDialog();
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

    private void showStudentRegistrationDialog() {
        final AlertDialog.Builder dialog=new AlertDialog.Builder(this);


        dialog.setTitle("REGISTER");
        dialog.setMessage("Please fill All Fields");
        //AlertDialog alertDialog = builder.create();




        LayoutInflater inflater= LayoutInflater.from(this);
        View student_layout=inflater.inflate(R.layout.layout_details,null);

        final MaterialEditText edtName=student_layout.findViewById(R.id.edtName);
        final MaterialEditText edtRoll=student_layout.findViewById(R.id.edtRoll);
        final MaterialEditText edtSemester=student_layout.findViewById(R.id.edtSemester);
        final MaterialEditText edtFees=student_layout.findViewById(R.id.edtFee);
        final MaterialEditText edtDob=student_layout.findViewById(R.id.edtDOB);
      final MaterialEditText edtEmail=student_layout.findViewById(R.id.edtEmail);
       final MaterialEditText edtPhone=student_layout.findViewById(R.id.edtPhone);
        final MaterialEditText edtSex=student_layout.findViewById(R.id.edtSEX);
        final MaterialEditText edtAddress=student_layout.findViewById(R.id.edtAddress);
        final MaterialEditText edtPercent=student_layout.findViewById(R.id.edtPercentage);
        final MaterialEditText edtcollege=student_layout.findViewById(R.id.edtCollege);
        final MaterialEditText edtDepartment=student_layout.findViewById(R.id.edtDepartment);



        dialog.setView(student_layout);

        //this set the buttons below the pop up dialog

        dialog.setPositiveButton("REGISTER", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

                dialogInterface.dismiss();

                if(TextUtils.isEmpty(edtName.getText().toString()))
                {
                    Snackbar.make(rootLayout,"Name is required",Snackbar.LENGTH_SHORT).show();
                    return;
                }
                if(TextUtils.isEmpty(edtRoll.getText().toString()))
                {
                    Snackbar.make(rootLayout,"Roll Number is required",Snackbar.LENGTH_SHORT).show();
                    return;
                }
                if(TextUtils.isEmpty(edtSemester.getText().toString()))
                {
                    Snackbar.make(rootLayout,"Semester is required",Snackbar.LENGTH_SHORT).show();
                    return;
                }
                if(TextUtils.isEmpty(edtFees.getText().toString()))
                {
                    Snackbar.make(rootLayout,"Fee detail is required",Snackbar.LENGTH_SHORT).show();
                    return;
                }
                if(TextUtils.isEmpty(edtDob.getText().toString()))
                {
                    Snackbar.make(rootLayout,"Date of Birth is required",Snackbar.LENGTH_SHORT).show();
                    return;
                }

            if(TextUtils.isEmpty(edtEmail.getText().toString()))
                {
                    Snackbar.make(rootLayout,"Email address is required",Snackbar.LENGTH_SHORT).show();
                    return;
                }

                if(TextUtils.isEmpty(edtPhone.getText().toString()))
                {
                    Snackbar.make(rootLayout,"Phone Number is required",Snackbar.LENGTH_SHORT).show();
                    return;
                }
                if(TextUtils.isEmpty(edtSex.getText().toString()))
                {
                    Snackbar.make(rootLayout,"Sex is required",Snackbar.LENGTH_SHORT).show();
                    return;
                }

                if(TextUtils.isEmpty(edtAddress.getText().toString()))
                {
                    Snackbar.make(rootLayout,"Address is required",Snackbar.LENGTH_SHORT).show();
                    return;
                }
                if(TextUtils.isEmpty(edtPercent.getText().toString()))
                {
                    Snackbar.make(rootLayout,"OverAll Percentage is required",Snackbar.LENGTH_SHORT).show();
                    return;
                }
                if(TextUtils.isEmpty(edtcollege.getText().toString()))
                {
                    Snackbar.make(rootLayout,"College Name is required",Snackbar.LENGTH_SHORT).show();
                    return;
                }

                if(TextUtils.isEmpty(edtDepartment.getText().toString()))
                {
                    Snackbar.make(rootLayout,"Department name is required",Snackbar.LENGTH_SHORT).show();
                    return;
                }

               Student student=new Student();
                student.setName(edtName.getText().toString());
                student.setRollno(edtRoll.getText().toString());
                student.setSemester(edtSemester.getText().toString());
                student.setFeedetails(edtFees.getText().toString());
                student.setDob(edtDob.getText().toString());
              student.setEmail(edtEmail.getText().toString());
                student.setPhone(edtPhone.getText().toString());
                student.setSex(edtSex.getText().toString());
                student.setAddress(edtAddress.getText().toString());
                student.setPercentage(edtPercent.getText().toString());
                student.setCollegename(edtcollege.getText().toString());
                student.setDepartment(edtDepartment.getText().toString());

                String email= students.push().getKey();
                students.child(email).setValue(student);
            }
        });


        //this set the cancel button of the dialog
        dialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });
        AlertDialog alert = dialog.create();
        alert.getWindow().setLayout(600,400);
        dialog.show();



    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.admin_control, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

Upvotes: 1

Views: 1719

Answers (4)

hemdroid
hemdroid

Reputation: 141

If you are using custom view for your Alert Dialog, set the root layout height and width to wrap content and then add another child layout with fixed dimensions inside that. Then the dialog will inherit the same dimensions as the child view. And the dialog will resize according to that.

<androidx.constraintlayout.widget.ConstraintLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="400dp"
        android:layout_height="400dp"
        android:background="@drawable/popup_bg"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent">
    
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 0

Jaimin Sarvan
Jaimin Sarvan

Reputation: 142

Try to below code

AlertDialog dialog = builder.create();

                // Finally, display the alert dialog
                dialog.show();

                // Get screen width and height in pixels
                DisplayMetrics displayMetrics = new DisplayMetrics();
                getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
                // The absolute width of the available display size in pixels.
                int displayWidth = displayMetrics.widthPixels;
                // The absolute height of the available display size in pixels.
                int displayHeight = displayMetrics.heightPixels;

                // Initialize a new window manager layout parameters
                WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();

                // Copy the alert dialog window attributes to new layout parameter instance
                layoutParams.copyFrom(dialog.getWindow().getAttributes());


                // Set alert dialog width equal to screen width 70%
                 int dialogWindowWidth = (int) (displayWidth * 0.7f);
                // Set alert dialog height equal to screen height 70%
                 int dialogWindowHeight = (int) (displayHeight * 0.7f);

                // Set the width and height for the layout parameters
                // This will bet the width and height of alert dialog
                layoutParams.width = dialogWindowWidth;
                layoutParams.height = dialogWindowHeight;

                // Apply the newly created layout parameters to the alert dialog window
                dialog.getWindow().setAttributes(layoutParams);

Below is my layout_details.xml file

     <android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:cardElevation="10dp"
    >
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_margin="20dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.rengwuxian.materialedittext.MaterialEditText
            android:id="@+id/edtName"
            android:hint="Name"
            android:inputType="text"
            android:text=""
            android:textColor="@color/colorPrimary"
            android:textColorHint="@color/colorPrimary"
            android:textSize="16sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            app:met_singleLineEllipsis="true"
            app:met_baseColor="@color/colorPrimary"
            app:met_floatingLabel="highlight"
            app:met_primaryColor="@color/colorPrimary"/>
        <com.rengwuxian.materialedittext.MaterialEditText
            android:id="@+id/edtRoll"
            android:hint="Roll No."
            android:inputType="number"
            android:text=""
            android:textColor="@color/colorPrimary"
            android:textColorHint="@color/colorPrimary"
            android:textSize="16sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            app:met_singleLineEllipsis="true"
            app:met_baseColor="@color/colorPrimary"
            app:met_floatingLabel="highlight"
            app:met_primaryColor="@color/colorPrimary"/>
        <com.rengwuxian.materialedittext.MaterialEditText
            android:id="@+id/edtSemester"
            android:hint="Semester."
            android:inputType="text"
            android:text=""
            android:textColor="@color/colorPrimary"
            android:textColorHint="@color/colorPrimary"
            android:textSize="16sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            app:met_singleLineEllipsis="true"
            app:met_baseColor="@color/colorPrimary"
            app:met_floatingLabel="highlight"
            app:met_primaryColor="@color/colorPrimary"/>
        <com.rengwuxian.materialedittext.MaterialEditText
            android:id="@+id/edtFee"
            android:hint="Fee Details"
            android:inputType="number"
            android:text=""
            android:textColor="@color/colorPrimary"
            android:textColorHint="@color/colorPrimary"
            android:textSize="16sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            app:met_singleLineEllipsis="true"
            app:met_baseColor="@color/colorPrimary"
            app:met_floatingLabel="highlight"
            app:met_primaryColor="@color/colorPrimary"/>
        <com.rengwuxian.materialedittext.MaterialEditText
            android:id="@+id/edtDOB"
            android:hint="DOB(dd/mm/yyyy)"
            android:inputType="text"
            android:text=""
            android:textColor="@color/colorPrimary"
            android:textColorHint="@color/colorPrimary"
            android:textSize="16sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            app:met_singleLineEllipsis="true"
            app:met_baseColor="@color/colorPrimary"
            app:met_floatingLabel="highlight"
            app:met_primaryColor="@color/colorPrimary"/>
        <com.rengwuxian.materialedittext.MaterialEditText
            android:id="@+id/edtSEX"
            android:hint="M/F"
            android:inputType="number"
            android:text=""
            android:textColor="@color/colorPrimary"
            android:textColorHint="@color/colorPrimary"
            android:textSize="16sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            app:met_singleLineEllipsis="true"
            app:met_baseColor="@color/colorPrimary"
            app:met_floatingLabel="highlight"
            app:met_primaryColor="@color/colorPrimary"/>
        <com.rengwuxian.materialedittext.MaterialEditText
            android:id="@+id/edtAddress"
            android:hint="Address"
            android:inputType="text"
            android:text=""
            android:textColor="@color/colorPrimary"
            android:textColorHint="@color/colorPrimary"
            android:textSize="16sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            app:met_singleLineEllipsis="true"
            app:met_baseColor="@color/colorPrimary"
            app:met_floatingLabel="highlight"
            app:met_primaryColor="@color/colorPrimary"/>
        <com.rengwuxian.materialedittext.MaterialEditText
            android:id="@+id/edtEmail"
            android:hint="Email"
            android:inputType="textEmailAddress"
            android:text=""
            android:textColor="@color/colorPrimary"
            android:textColorHint="@color/colorPrimary"
            android:textSize="16sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            app:met_singleLineEllipsis="true"
            app:met_baseColor="@color/colorPrimary"
            app:met_floatingLabel="highlight"
            app:met_primaryColor="@color/colorPrimary"/>
        <com.rengwuxian.materialedittext.MaterialEditText
            android:id="@+id/edtPhone"
            android:hint="Phone number"
            android:inputType="phone"
            android:text=""
            android:textColor="@color/colorPrimary"
            android:textColorHint="@color/colorPrimary"
            android:textSize="16sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            app:met_singleLineEllipsis="true"
            app:met_baseColor="@color/colorPrimary"
            app:met_floatingLabel="highlight"
            app:met_primaryColor="@color/colorPrimary"/>
        <com.rengwuxian.materialedittext.MaterialEditText
            android:id="@+id/edtPercentage"
            android:hint="Overall percentage"
            android:inputType="text"
            android:text=""
            android:textColor="@color/colorPrimary"
            android:textColorHint="@color/colorPrimary"
            android:textSize="16sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            app:met_singleLineEllipsis="true"
            app:met_baseColor="@color/colorPrimary"
            app:met_floatingLabel="highlight"
            app:met_primaryColor="@color/colorPrimary"/>
        <com.rengwuxian.materialedittext.MaterialEditText
            android:id="@+id/edtCollege"
            android:hint="College Name"
            android:inputType="text"
            android:text=""
            android:textColor="@color/colorPrimary"
            android:textColorHint="@color/colorPrimary"
            android:textSize="16sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            app:met_singleLineEllipsis="true"
            app:met_baseColor="@color/colorPrimary"
            app:met_floatingLabel="highlight"
            app:met_primaryColor="@color/colorPrimary"/>

        <com.rengwuxian.materialedittext.MaterialEditText
            android:id="@+id/edtDepartment"
            android:hint="Department Name"
            android:inputType="text"
            android:text=""
            android:textColor="@color/colorPrimary"
            android:textColorHint="@color/colorPrimary"
            android:textSize="16sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            app:met_singleLineEllipsis="true"
            app:met_baseColor="@color/colorPrimary"
            app:met_floatingLabel="highlight"
            app:met_primaryColor="@color/colorPrimary"/>


    </LinearLayout>
</ScrollView>
</android.support.v7.widget.CardView>

Upvotes: 1

Megha Maniar
Megha Maniar

Reputation: 444

You can do it like:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(R.layout.dialog_change_password);

And in your Layout file give layout_height fixed.:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="250dp"
              android:layout_gravity="center"
              android:layout_marginLeft="@dimen/activity_horizontal_margin"
              android:layout_marginRight="@dimen/activity_horizontal_margin"
              android:background="@drawable/bg_otp_dialog"
              android:gravity="center"
              android:orientation="vertical">

Upvotes: 0

Siv
Siv

Reputation: 1066

You can set the Alert Dialoge using this method as .

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(layout);
builder.setTitle("Title");
alertDialog = builder.create();
alertDialog.show();
alertDialog.getWindow().setLayout(600, 400);

Also you can use this after calling the alertDialog.show() method.

alertDialog.show();
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();

lp.copyFrom(alertDialog.getWindow().getAttributes());
lp.width = 150;
lp.height = 500;
lp.x=-170;
lp.y=100;
alertDialog.getWindow().setAttributes(lp);

More about this can be found in this link.

Upvotes: 0

Related Questions