Custom AlertDialog size is too small - android

I'm trying to build a simple custom alert dialog. Just a header, and edit text, and a button. Problem is that the dialog is too small to properly display the widgets. I've tried to implement quite a few suggestions, but none of them solve the issue. How can I expand this to a larger size? What am I doing wrong? Thanks!
My code is:
LayoutInflater inflater = LayoutInflater.from(this);
View builderView = inflater.inflate(R.layout.enter_password, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(builderView);
AlertDialog alert = builder.create();
alert.show();
The layout xml is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical"
android:background="#drawable/background_gray_gradient" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="70dp"
android:layout_gravity="left"
android:contentDescription="#string/blank"
android:src="#raw/lock" >
</ImageView>
<TextView
android:layout_marginTop="15dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:paddingTop="5dip"
android:paddingLeft="5dp"
android:text="#string/password_to_continue_title"
android:textSize="20sp" />
</LinearLayout>
<EditText
android:id="#+id/password"
android:inputType="textPassword"
android:hint="#string/password"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/submit_password"
style="#style/ButtonText"
android:onClick="submitPassword"
android:layout_gravity="center_horizontal"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#drawable/gray_button"
android:text="#string/continue_on" />
</LinearLayout>

This answer just satisfies your requirements, https://stackoverflow.com/a/10912076/826657
Rect displayRectangle = new Rect();
Window window = this.getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(displayRectangle);
LayoutInflater inflater = LayoutInflater.from(this);
View builderView = inflater.inflate(R.layout.activity_dia, null);
builderView.setMinimumWidth((int) (displayRectangle.width() * 0.9f));
builderView.setMinimumHeight((int) (displayRectangle.height() * 0.9f));
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(builderView);
AlertDialog alert = builder.create();
alert.show();

Related

How to show custom layout alert dialog at center vertical position

I want to display custom dialog at center vertical position but it appears at top.Above the first one is my xml file and second one is output after running in real device. Here is the code of dialog.
//alert dialog code
//add theme to display on whole page
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this,
android.R.style.Theme_Holo_Light_NoActionBar_TranslucentDecor);
LayoutInflater inflater = this.getLayoutInflater();
//custom layout
View dialogView = inflater.inflate(R.layout.custom_bar, null);
dialogBuilder.setView(dialogView);
//button to close dialog box
Button closeButton = (Button)
dialogView.findViewById(R.id.closeButton);
final AlertDialog alertDialog = dialogBuilder.create();
closeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
alertDialog.dismiss();
}
});
//show dialog
alertDialog.show();
In your main layout of custom_bar put android:gravity="center"
You should write like this.
AlertDialog dialog = new AlertDialog.Builder(this, R.style.Dark).create();
if (dialog.getWindow() != null)
{
Window window = dialog.getWindow();
window.setLayout(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.CENTER);
dialog.getWindow().setBackgroundDrawableResource(android.R.color.background_dark);
}
This will show the alert dialog to the center of the screen.
Use this code to your Alert dialog to the center of the screen.
Add this in your class and change the code as per your needed.
//Internet Alert Dialog
public static void InternetAlert(final Context _A) {
final Dialog dialog = new Dialog(_A, R.style.CustomDialogStyle);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCanceledOnTouchOutside(true);
dialog.setContentView(R.layout.dialog_internet_connection);
final Button Yes = (Button) dialog.findViewById(R.id.btn_yes);
Yes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
If you want to fill the entire screen Just change the FrameLayout Height to match_parent
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="320dp"
android:layout_height="wrap_content"
android:layout_gravity="center">
<android.support.v7.widget.CardView
android:id="#+id/cv_SignOut"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="?attr/actionBarSize"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:layout_marginTop="?attr/actionBarSize"
app:cardBackgroundColor="#0D000000"
app:cardCornerRadius="6dp"
app:cardElevation="3dp"
app:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent">
<TextView
android:id="#+id/title_SignOut"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="#color/textViewColorPrimary"
android:gravity="center"
android:minHeight="?attr/actionBarSize"
android:text="#string/info"
android:textAppearance="?attr/textAppearanceSearchResultTitle"
android:textColor="#color/colorWhite" />
<TextView
android:id="#+id/signOutAlert"
style="#android:style/TextAppearance.DeviceDefault.SearchResult.Subtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/title_SignOut"
android:background="#color/colorBackground"
android:gravity="center"
android:minHeight="120dip"
android:paddingBottom="#dimen/activity_horizontal_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_horizontal_margin"
android:text="#string/info_internet_check"
android:textColor="#color/colorPrimary" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/signOutAlert"
android:background="#color/white"
android:orientation="horizontal"
android:weightSum="1">
<Button
android:id="#+id/btn_yes"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_marginRight="2dip"
android:layout_weight="1"
android:background="#color/colorBackground"
android:gravity="center"
android:minHeight="?attr/actionBarSize"
android:text="OK"
android:textAppearance="?attr/textAppearanceSearchResultTitle"
android:textColor="#color/colorAccent" />
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
</FrameLayout>
Hope this may helps :)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="abc"
android:textSize="30sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="abc"
android:textSize="30sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="abc"
android:textSize="30sp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Done" />
</LinearLayout>
</LinearLayout>
------
dialogBuilder.showAsDropDown(dialogView, 50, 400, Gravity.CENTER);
alertDialog.show();
----
Add that line to your code. it will set the popup window to center.

Android Dialog setMessage

I'm having trouble with the message of a dialog.
Everything works fine until I want to type information in the edittext and the keyboard pops up - then the second (and last) line of the message of the dialog is cut to half..
Looks dialog looks like
Icon and Title:
Message line 1
Message line 2 (<- half visibile with keyboard)
ScrollView with some edittexts
Negative button | Positive button
Edit: Code
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(mContext.LAYOUT_INFLATER_SERVICE);
View viewGroup = inflater.inflate(R.layout.layout_dialog_daily_report, null);
ScrollView layout = (ScrollView) viewGroup.findViewById(R.id.dialog_parent);
final EditText 1 = (EditText) viewGroup.findViewById(R.id.dialog_1);
final EditText 2 = (EditText) viewGroup.findViewById(R.id.dialog_2);
final EditText 3 = (EditText) viewGroup.findViewById(R.id.dialog_3);
final EditText 4 = (EditText) viewGroup.findViewById(R.id.dialog_4);
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setIcon(R.drawable.icon);
builder.setTitle(R.string.dialog_title);
builder.setMessage(R.string.dialog_message);
builder.setView(layout);
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
dialog.show();
Layout:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/dialog_parent"
android:margin="7dp"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:ellipsize="end"
android:text="#string/dialog_1" />
<EditText
android:id="#+id/dialog_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:ellipsize="end"
android:text="#string/dialog_2" />
<EditText
android:id="#+id/dialog_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:ellipsize="end"
android:text="#string/dialog_3" />
<EditText
android:id="#+id/dialog_3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:ellipsize="end"
android:text="#string/dialog_4" />
<EditText
android:id="#+id/dialog_4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4" />
</LinearLayout>
</LinearLayout>
As you can see the buttons are not visible, I dont know why the scrollview isnt scrollable anymore
How can I prevent that the Dialog.setMessage text is cut off on showing the keyboard?
add next line into your Manifest:
android:windowSoftInputMode="adjustResize"
I believe the errors are mostly in xml,try removing android:elipsize,maybe giving the layout_width's a value other that 0,you can try

custom alertDialog in android

I'm working with custom alertDialog in android and found a problem. My code is simple:
LayoutInflater inflatre = getLayoutInflater();
View v = inflatre.inflate(R.layout.dialog_view, null);
AlertDialog.Builder dialog = new AlertDialog.Builder(EditPageActivity.this);
dialog.setView(v);
dialog.show();
And the layout xml is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/bg"
android:padding="10dp"
android:gravity="center" >
<Button
android:id="#+id/gallery_b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Gallery" />
<Button
android:id="#+id/photo_b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/gallery_b"
android:text="Take Photo" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/gallery_b"
android:layout_centerHorizontal="true"
android:text="Choose Image"
android:textSize="20sp"
android:textColor="#color/splash_font"
android:layout_marginBottom="10dp" />
But the output is like this:
Feeling helpless after searching google :(...can anyone describe me anything
Try this layout for Dialog, It should work.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/bg"
android:padding="10dp"
android:gravity="center">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Choose Image"
android:textSize="20sp"
android:textColor="#color/splash_font"/>
<Button
android:id="#+id/gallery_b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:text="Gallery"
android:layout_marginTop="10dp"/>
<Button
android:id="#+id/photo_b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/gallery_b"
android:text="Take Photo" />
</RelativeLayout>
Try this
Dialog dialog = new Dialog(YourActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.yourlayout); // your layout id
Button dialogButton = (Button) dialog
.findViewById(R.id.dialogButtonOK); // button in your layout
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
dialog.show();
//Try this one:
final Dialog dialog = new Dialog(mContext);
dialog.requestWindowFeature((int) Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.your_layout_here);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
dialog.getWindow().setAttributes(lp);
dialog.show();

How to set a view for AlertDialog in android?

I want to set a custom view for my alert dialog. Actually I have a grid view and I want to show a dialog when each item clicked, I built a function as ToastWarning and it builds my alert dialog. This is my xml for custom view: an imageview, textview and 2 button:
But this code is not working.why?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/linear_toast"
android:gravity="right"
android:background="#drawable/white"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="right"
>
<TextView
android:id="#+id/tv_Toast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="18sp"
android:textColor="#FFFFFF"
android:layout_margin="10dp" />
<ImageView
android:id="#+id/iv_toast"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginRight="7dp"
android:layout_marginTop="10dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="right"
android:weightSum="10"
android:padding="5dp">
<Button
android:id="#+id/bt_no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_weight="5"
android:textColor="#FFFFFF" />
<Button
android:id="#+id/bt_yes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_weight="5"
android:textColor="#FFFFFF" />
</LinearLayout>
</LinearLayout>
It's my code for building a alert dialog:
private void ToastWarning() {
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.toast, null);
iv_alert=(ImageView)view.findViewById(R.id.iv_toast);
tv_alert=(TextView)view.findViewById(R.id.tv_Toast);
bt_yes=(Button)view.findViewById(R.id.bt_yes);
bt_yes=(Button)view.findViewById(R.id.bt_no);
iv_alert.setBackgroundResource(R.drawable.facebook);
tv_alert.setTypeface(face);
bt_yes.setTypeface(face);
bt_no.setTypeface(face);
bt_yes.setText(PersianReshape.reshape("بله"));
bt_no.setText(PersianReshape.reshape("خیر"));
tv_alert.setText(PersianReshape.reshape("فیلدهای مورد نظر پر شود."));
AlertDialog MyDialog;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(view);
MyDialog=builder.create();
MyDialog.show();
}

Why is my alert dialog not showing all its contents

I am trying to customize my alert dialog with a layout. The problem is that the ALert dialog box is lunched with small size and most of the layout is trancated or words are overlapping. It is not screen issue as the dialog box is very small and does not seem to expand to cover all the contents. Why do you think this is happening?
I am using the following layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/linearLayout1"
android:layout_alignParentLeft="true"
android:orientation="horizontal" >
<TextView
android:id="#+id/tvNa"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"/>
<EditText
android:id="#+id/etNa"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:orientation="horizontal" >
<TextView
android:id="#+id/tvQ"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"/>
<EditText
android:id="#+id/etQ"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/linearLayout1"
android:layout_centerVertical="true"
android:orientation="horizontal" >
<TextView
android:id="#+id/tvP"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5" />
<EditText
android:id="#+id/etP"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="numberDecimal" />
</LinearLayout>
</RelativeLayout>
And I am using the following code:
AlertDialog.Builder alert = new AlertDialog.Builder(this);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.add_dialog, null);
alert.setView(layout);
alert.setTitle("New one");
alert.setPositiveButton("Ok",new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int whichButton) {
}
});
alert.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int whichButton) {
}
});
alert.show();
The Dialog Object has a set max size that you have not over written. You could add your layout to a ScrollView. Other than that you will have to follow the inheritances of the Dialog until you find where the max Dialog size is being set and override this. GrepCode is a good place to view the SDK source.

Categories

Resources