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();
Related
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.
Here is my code for showing a custom fullscreen dialog.
My problem is when the root view of Dialog is LinearLayout, the dialog is not fullscreen but if it is RelativeLayout then dialog fullscreen
public void showDialog(Context context){
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_increase_repeattime);
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
Button dialogButton = (Button) dialog.findViewById(R.id.btn_dialog);
dialogButton.setOnClickListener(new View.OnClickListener() {
...
});
dialog.show();
}
Here is dialog layout xml using LinearLayout
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffffff">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="OK"
android:id="#+id/btn_dialog"
android:gravity="center_vertical|center_horizontal"
android:layout_centerHorizontal="true"
/>
</LinearLayout>
Using RelativeLayout
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffffff">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="OK"
android:id="#+id/btn_dialog"
android:gravity="center_vertical|center_horizontal"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
Why this happened? Any help or suggestion would be great appreciated.
I creating camerapopup.xml in my application . I want to display this pop up window at the bottom of screen for all screen size . I have tried to creating different layout folders (layout-large , layout-small,layoutxlarge etc )but does not work at all How to set the popup window for all screen size at the bottom .Can someone help me how to work with that.Thanks in advanced .
Here is my xml file .
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/popup_element"
android:layout_width="fill_parent"
android:layout_height="180dp"
android:background="#android:color/transparent"
android:orientation="vertical"
android:padding="3dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="1dp"
android:background="#android:color/transparent"
android:orientation="vertical">
<TextView
android:id="#+id/Title"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:background="#drawable/curve_shap"
android:gravity="center_horizontal|center_vertical"
android:text="Take Photo"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/darker_gray"
android:textSize="14sp" />
<Button
android:id="#+id/button_Camera"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:background="#drawable/curve_shap"
android:text="Camera"
android:textColor="#3A86CF" />
<Button
android:id="#+id/button_Gallery"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:background="#drawable/curve_shap"
android:text="Gallery"
android:textColor="#3A86CF" />
</LinearLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="45dp"
android:layout_marginTop="4dp"
>
<Button
android:id="#+id/btnCancelCamera"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_gravity="center_horizontal"
android:background="#drawable/curve_shap"
android:text="Cancel"
android:textColor="#3A86CF"
android:textSize="16sp"
android:textStyle="bold" />
</RelativeLayout>
</LinearLayout>
Here is my Activity code
imgProfilePic.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.camera_popup, (ViewGroup) findViewById(R.id.popup_element));
popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setWidth(720);
popupWindow.setHeight(350);
popupWindow.setFocusable(true);
popupWindow.showAtLocation(popupView, Gravity.BOTTOM, 0, 0);
popupWindow.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
Button btnCamera = (Button) popupView.findViewById(R.id.button_Camera);
Button btnGallery = (Button) popupView.findViewById(R.id.button_Gallery);
Button btnDismiss = (Button) popupView.findViewById(R.id.btnCancelCamera);
}
});
Use this in all your java file
AlertDialog.Builder builder = new AlertDialog.Builder(context);
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
View dialogLayout = inflater.inflate(R.layout.ppopup_element,
null);
final AlertDialog dialog = builder.create();
dialog.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
dialog.setView(dialogLayout, 0, 0, 0, 0);
dialog.setCanceledOnTouchOutside(true);
dialog.setCancelable(true);
WindowManager.LayoutParams wlmp = dialog.getWindow()
.getAttributes();
wlmp.gravity = Gravity.BOTTOM;
Button btnCamera = (Button) dialogLayout.findViewById(R.id.button_Camera);
Button btnGallery = (Button) dialogLayout.findViewById(R.id.button_Gallery);
Button btnDismiss = (Button) dialogLayout.findViewById(R.id.btnCancelCamera);
builder.setView(dialogLayout);
dialog.show();
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();
I have been trying to set a custom AlertDialog but was not able to get correctly.
prompt.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffe3e3"
xmlns:tools="http://schemas.android.com/tools"
tools:ignore="HardcodedText">
<TextView
android:id="#+id/prompttitile"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|left"
android:padding="5dp"
android:text="Error Title Here"
android:textColor="#c50200"
android:textSize="16sp"
android:textStyle="normal"
android:visibility="visible"
/>
<TextView
android:id="#+id/promptmessage"
android:layout_below="#+id/prompttitile"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:padding="5dp"
android:text="Error Title Here"
android:textColor="#c50200"
android:textSize="16sp"
android:textStyle="normal" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/promptmessage"
android:weightSum="2"
android:padding="10dp"
android:background="#333"
>
<Button
android:text="#android:string/no"
android:id="#+id/promptno"
android:layout_width="0sp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:padding="7dp"
android:textColor="#android:color/black"
android:textSize="14sp"
android:textStyle="bold"
android:background="#drawable/button_selector"
android:layout_marginRight="5dp"
android:gravity="center"
/>
<Button
android:text="#android:string/ok"
android:id="#+id/promptok"
android:layout_width="0sp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:padding="7dp"
android:textColor="#android:color/black"
android:textSize="14sp"
android:textStyle="bold"
android:background="#drawable/button_selector"
android:gravity="center"
/>
</LinearLayout>
</RelativeLayout>
I am getting black color background at the top of the AlertDialog and I want to only the AlertDialog to be displayed.
Dialog code
Dialog d=new Dialog(mContext);
d.setContentView(R.layout.prompt);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
d.getWindow().setAttributes(lp);
d.show();
Dialog d=new Dialog(this);
//Add this
d.requestWindowFeature(Window.FEATURE_NO_TITLE);
d.setContentView(R.layout.prompt);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
d.getWindow().setAttributes(lp);
d.show();
Try this
Dialog dialog = new Dialog(MainAppActivity.this, R.style.PauseDialog);
dialog.setContentView(R.layout.prompt);
dialog.setCancelable(true);
dialog.show();
and in style.xml
<style name="PauseDialog" parent="#android:style/Theme.Translucent.NoTitleBar"></style>