Today problem is that on Android 2.3.5 showing custom dialog is wrong. Dialog width is wider than it should be.
I tried using 20dp instead of wrap_content in TextView as in LinearLayout with no success. I would like get rectangle shape with wrap_content as it is in Android 4.
Here is xml code for this custom dialog's view.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Please wait" >
</TextView>
</LinearLayout>
Here is a a custom dialog's code, anything else is not important.
AlertDialog.Builder builder = new AlertDialog.Builder(context);
AlertDialog dialog;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Service.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.dialog_progressdialog,
null);
builder.setView(layout);
//initializing textview
dialog = builder.create();
//do other stuff
On android 4.0.3 I get rectangle, so it looks ok - it is as I wanted. What might be the problem, does anyone find this kind of problem? Sorry, I don't have sample for Android 4 - no phone today to make sample.
If I put inside also progressbar I am able to create square in android 4, but it is impossible to do it in Android 2.3.
Android 2.3.5
Android 4.0.4
I dont know whats wrong with your code. I want to have the ProgressDialog at the center of the screen. I have implemented it in to the Android 2.2.* and it works perfect in all devices even in android 4.*
I addition i have use style to the custom progressDialog. Please see below my code and you will come to know about it.
Java code:
final Dialog dialog = new Dialog(SplashActivity.this,R.style.CustomDialogTheme);
dialog.getWindow();
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.internet_dialog);
dialog.setCancelable(false);
Button retryBtn = (Button) dialog.findViewById(R.id.retryBtn);
Button cancel = (Button) dialog.findViewById(R.id.cancelBtn);
retryBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
dialog.dismiss();
// Some task
}
});
cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
dialog.dismiss();
// Some task
}
});
dialog.show();
}
CustomDialogTheme
<style name="CustomDialogTheme" parent="#android:style/Theme.Dialog">
<item name="android:windowIsFloating">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:fitsSystemWindows">true</item>
<!-- <item name="android:colorBackgroundCacheHint">#00000000</item>
<item name="android:background">#00000000</item> -->
</style>
And XMl file of my layout is: internet_dialog.xml
<?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:background="#00000000"
android:orientation="vertical" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/popup_shape"
android:layout_margin="10dp"
android:orientation="vertical"
>
<TextView
android:id="#+id/net_popup_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="#string/internet_popup_heading"
android:layout_marginTop="15dp"
android:textColor="#ffffff"
android:textSize="20dp" />
<TextView
android:id="#+id/net_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="20dp"
android:gravity="center"
android:text="#string/internet_popup_msg"
android:textColor="#ffffff"
android:textSize="14dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="horizontal"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:padding="5dp" >
<Button
android:id="#+id/retryBtn"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginRight="2dp"
android:background="#drawable/blue_button"
android:textColor="#000000"
android:textStyle="bold"
android:text="#string/internet_popup_retry" />
<Button
android:id="#+id/cancelBtn"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_marginLeft="2dp"
android:layout_height="wrap_content"
android:background="#drawable/blue_button"
android:textColor="#000000"
android:textStyle="bold"
android:text="#string/internet_popup_exit" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
here, i havent give ant fix height/width but it seems perfect in all the layout of the any device.
try to adding style to your dialog and see result.
Hope it will help you.
Feel free for comments. Enjoy Coding... :)
Just to ensure that your code behaves same on all the platforms, you can add layout params to your dialog view like this:
layout.setLayoutparams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
dialog.setView(layout);
Ok its a work around, but if you are out of ideas , as I am for this issue, you can try:
calulate length of the text View by using Paint.measureText(); and set it to dialog view param.
layout.setLayoutparams(new LinearLayout.LayoutParams(LENGTH_CALCULATED, LayoutParams.WRAP_CONTENT));
try this
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(layout);
builder.setTitle("Title");
alertDialog = builder.create();
alertDialog.show();
alertDialog.getWindow().setLayout(600, 400); //Controlling width and height.
Also try this
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);
Related
I have an alertdialog with a custom layout, including a scrollview.
When the dialog appears, the scrollview is "cropped", and appears to be anchored to the top of the Screen, rather than the top of the dialog
I have searched SE for similar problems, and cannot find any.
Using a popup Window has exactly the same issue.
Here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scroller"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/rock">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#drawable/scrollbutton"
android:padding="12dp"
android:text="Room Number"
android:textColor="#android:color/black"
android:textSize="22sp" />
<TextView
android:id="#+id/contents"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/scrollbutton"
android:gravity="center"
android:padding="12dp"
android:text="Here be Monsters" />
</LinearLayout>
</ScrollView>
And here is the dialog code:
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.contents, null);
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setView(layout);
AlertDialog alert = alertDialog.create();
alert.requestWindowFeature(Window.FEATURE_NO_TITLE);
alert.show();
Window window = alert.getWindow();
window.setLayout((int)(screenWidth*.8), (int)(screenHeight*.8));
I have tried adding the ScrollView as the root_view in the inflater, and also adding a LinearLayout around the ScrollView and setting this as root_view, but this does not alter the results
You don't set parent to layout inflater. Let dialog manages view arranging DialogFragment
I'm making a custom dialog from a layout file
Java code:
Dialog dialog = new Dialog(CategoryActivity.this);
dialog.setContentView(R.layout.content_privacy_dialog);
TextView dialog_title = (TextView) dialog.findViewById(R.id.dialog_title);
dialog_title.setText(getString(R.string.nav_privacy));
dialog.show();
Layout file:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:background="#color/dialogBg"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:padding="24dp"
android:layout_height="wrap_content"
android:id="#+id/dialog_title"
android:text="About"
android:textSize="20sp"
android:textColor="#android:color/white"
/>
</LinearLayout>
I'm getting this result:
How can I hide the white space on top and the blue bar?
Any help will be appreciated.
try this
Dialog dialog = new Dialog(CategoryActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.content_privacy_dialog);
TextView dialog_title = (TextView) dialog.findViewById(R.id.dialog_title);
dialog_title.setText(getString(R.string.nav_privacy));
dialog.show();
Instead of using simple Dialog i would recomend you to use Material Dialogs. You can get help from this link
https://material.google.com/components/dialogs.html#dialogs-specs
I have a class that extends EditTextPreference and I use it in my own layout, such as:
<com.app.MyEditTextPreference
android:key="#string/key"
android:title="#string/title"
android:summary="#string/summary"
android:dialogTitle="#string/title"
android:dialogMessage="#string/message"
android:layout="#layout/preference_edittext"/>
My app theme inherits from Theme.AppCompat.Light.DarkActionBar and I have changed the values of the text sizes:
<style name="TextAppearance.Small">
<item name="android:textSize">18sp</item> <!-- was 14sp -->
</style>
However, when the preference dialog is displayed, the size of the message is different than the one I have defined in my style.
So how do I set the text size of the dialog message correctly?
EDIT
I copied the dialog layout from the sdk:
<LinearLayout
android:id="#+android:id/edittext_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:orientation="vertical">
<TextView
android:id="#android:id/message"
android:layout_marginBottom="48dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="4dp"
android:paddingEnd="4dp"
android:textColor="?android:attr/textColorSecondary"
style="#style/TextAppearance.Small" />
</LinearLayout>
However, I get a build error Resource is not public on the line android:id="#+android:id/edittext_container".
And if I change it to "#*android:id/edittext_container", the edit text is not visible anymore.
Is there a simpler way to just change the textsize of the message?
First create new xml file name cus.xml in layout folder.
it need for set the view for dialog at runtime .
<?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">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Name :"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="18dp" />
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="18dp" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:textSize="18dp" />
As you need dialog:
here's dialog code:
Dialog d=new Dialog(getActivity());
d.setTitle("Image Title");
d.setContentView(R.layout.cus);
d.show();
I finally managed to change the message text size by:
a) Changing the id of the LinearLayout to: #+id/edittext_container, and
b) Adding the following code in my class extending EditTextPreference:
#Override
protected void onAddEditTextToDialogView(View dialogView, EditText editText) {
ViewGroup container = (ViewGroup) dialogView
.findViewById(R.id.edittext_container);
if (container != null) {
container.addView(editText, ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
}
}
This works fine and is clearly a valid answer, however a simpler solution would be to just change the message text size through themeing.
I have implemented the following Dialog in Android but have certain problems that I cant figure out why it is happening.
I cant figure out why there is a white space at the top and the bottom corners. And how to remove it!
As i have used Dialog and not AlertDialog, this message disappears when I touch elsewhere on the screen. I want to prevent this from happening and want the message box to be closed only when the user selects any one of the two options.
How much ever I try, I am not able to get the Cancel and Erase button of the same width.
Here are the XMLs
custom_alert.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:background="#drawable/custom_alert_layout"
android:id="#+id/alert_layout"
android:paddingBottom="15dp"
android:paddingTop="10dp"
android:layout_height="wrap_content">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/alert_icon_imageview"
android:src="#drawable/alerticon"
android:layout_alignParentTop="true"
android:layout_alignLeft="#+id/alert_msg"
android:layout_alignStart="#+id/alert_msg" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/alert_title"
android:text="TITLE"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="#0a4d4a"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/alert_icon_imageview"
android:layout_toEndOf="#+id/alert_icon_imageview"
android:layout_marginLeft="20dp" />
<ImageView
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginTop="10dp"
android:layout_marginRight="4dp"
android:layout_marginLeft="2dp"
android:id="#+id/alert_divider_imageview"
android:layout_below="#+id/alert_title"
android:src="#drawable/alertdivider"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/alert_msg"
android:text="MESSAGE"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:textSize="16sp"
android:layout_below="#+id/alert_divider_imageview"
android:textColor="#ff373334"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginRight="4dp"
android:layout_marginLeft="2dp"
android:layout_marginTop="10dp"
android:id="#+id/alert_divider_imageview2"
android:layout_below="#+id/alert_msg"
android:src="#drawable/alertdivider"/>
<Button
android:layout_width="150dp"
android:id="#+id/alert_cancel"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:layout_marginTop="15dp"
android:textColor="#ffffff"
android:textStyle="bold"
android:text="CANCEL"
android:background="#drawable/custom_alert_cancel"
android:layout_height="wrap_content"
android:layout_below="#+id/alert_divider_imageview2"
/>
<Button
android:layout_width="150dp"
android:id="#+id/alert_ok"
android:text="ERASE"
android:textStyle="bold"
android:layout_marginRight="10dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="15dp"
android:textColor="#ffffff"
android:background="#drawable/custom_alert_ok"
android:layout_toRightOf="#+id/alert_cancel"
android:layout_height="wrap_content"
android:layout_below="#+id/alert_divider_imageview2" />
</RelativeLayout>
custom_alert_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" >
<shape android:shape="rectangle">
<corners android:radius="10dp"/>
<solid android:color="#139977" />
<stroke android:width="2dp" android:color="#0a4d4a" />
</shape>
</item>
</selector>
Can you post your Activity's code ?
Try this in your Activity :
final Dialog dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.alert_dialog);
Button okBtn = (Button) dialog.findViewById(R.id.ok_btn);
Button cancelBtn = (Button) dialog.findViewById(R.id.cancel_btn);
okBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
doErase();
}
});
cancelBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.setCancelable(false);
dialog.show();
to solve 3rd problem, remove buttons from xml and use inbuild button of dialog. methods like setPositiveButton() and setNegativeButton() of dialog class.
it will give you same size button.
to solve the 1st problem you can use in xml like
<item name="android:background">#android:color/transparent</item>
or in java
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
Just add a line to your dialog box which remove your actionbar and title bar of the dialog:
Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); //add this line
dialog.setContentView(R.layout.activity_main);
for solve your first problem add this code before setContentView
Window window = actDialog.getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();
window.setAttributes(wlp);
window.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
actDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
your second problem solution
actDialog.setCanceledOnTouchOutside(false);
your third problem solution here
place your two Button in LinearLayout and give layout_weight same
1.To remove white space at top:
add these two lines to your dialog ,
dialog.getWindow();
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.alert_dialog);
remove white space at bottom,in your code just remove below line
<corners android:radius="10dp"/>
from your custom_alert_layout.xml file.
2.To solve second problem:add below two lines to your code,
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
3.To get same width for buttons use LinearLayout as parent for both the buttons,and give equal weights for buttons.
For your 3rd problem
here is code
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Button
android:layout_width="match_parent"
android:id="#+id/alert_cancel"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:layout_marginRight="5dp"
android:layout_marginTop="15dp"
android:textColor="#ffffff"
android:textStyle="bold"
android:text="CANCEL"
android:background="#drawable/custom_alert_cancel"
android:layout_height="wrap_content"
/>
<Button
android:layout_width="match_parent"
android:id="#+id/alert_ok"
android:text="ERASE"
android:textStyle="bold"
android:layout_weight="1"
android:layout_marginRight="10dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="15dp"
android:textColor="#ffffff"
android:background="#drawable/custom_alert_ok"
android:layout_height="wrap_content"
/>
</LinearLayout>
The Default AlertDialog looks nice, and using setMessage() one can set its text - easy.
But when using setView(), the dialog replaces the content by the given view, which is fine.
However, I'd like to keep the default paddings/background/style. I searched for a proper style and I had a look at alert_dialog.xml, but didn't find anything helpful.
How can I apply the default style, without mimicking it?
There is an AlertDialog style, that you can set when you create your Builder.
Example:
AlertDialog.Builder builder;
if (title != null) {
builder = new AlertDialog.Builder(getActivity(), R.style.AlertDialogStyle);
builder.setTitle(title);
}
else {
builder = new AlertDialog.Builder(getActivity(), R.style.AlertDialogStyleNoTitle);
}
In your styles.xml file:
<style name="AlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorPrimary">#color/primary</item>
<item name="colorPrimaryDark">#color/primary_dark</item>
<item name="colorAccent">#color/accent</item>
<item name="android:textColorPrimary">#color/black</item>
</style>
<style name="AlertDialogStyleNoTitle" parent="AlertDialogStyle">
<item name="android:windowNoTitle">true</item>
</style>
My styles are just an example of some things you can do - you can just have one dialog style if you want. As long as you inherit from one of the Dialog.Alert styles, you should be good to go.
You will still have to set the padding up yourself. I looked at the material design guidelines, and the margins are 24dp.
Use :
AlertDialog.Builder b = new AlertDialog.Builder(this);
AlertDialog alert = b.create();
alert.setView(view, 0, 0, 0, 0);
To fix the margins you can use wrap your Layout in a FrameLayout and add the margins in your Layout like this:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="#dimen/dialog_margin_title"
android:layout_marginBottom="#dimen/dialog_margin"
android:layout_marginLeft="#dimen/dialog_margin"
android:layout_marginStart="#dimen/dialog_margin"
android:layout_marginRight="#dimen/dialog_margin"
android:layout_marginEnd="#dimen/dialog_margin"
android:orientation="vertical">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Please enter the email address of the person you would like to follow, this person will be notified." />
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textEmailAddress" />
</LinearLayout>
</FrameLayout>
Basing on the source code of AppCompat
https://chromium.googlesource.com/android_tools/+/HEAD/sdk/extras/android/support/v7/appcompat/res/layout/abc_alert_dialog_material.xml
this is an example of layout for a Dialog custom view that looks good
<?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="wrap_content"
android:orientation="horizontal"
android:paddingBottom="#dimen/abc_dialog_padding_top_material"
android:paddingLeft="?attr/dialogPreferredPadding"
android:paddingRight="?attr/dialogPreferredPadding"
android:paddingTop="#dimen/abc_dialog_padding_top_material">
<Spinner
android:id="#+id/spinner1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_weight="1" />
<Spinner
android:id="#+id/spinner2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
Note that it is not raccomanded to use private values, such as abc_dialog_padding_top_material, but I didn't find a better solution.