I'm trying to make a custom dialog displaying the DatePicker, but the element adds an extra margin/padding to the right of the view. This happens only on Android 7.1.1, Nexus 6P, but works perfectly fine on other lower resolution phones. How do I remove the extra padding?
My XML layout code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<DatePicker
android:id="#+id/date_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:endYear="2012"
android:startYear="1930" />
</LinearLayout>
It looks like this on Android 7.1.1:
Edit: Added my Java code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_attributes);
assert getSupportActionBar() != null;
getSupportActionBar().setTitle("Register");
final TextView birthDateSelectedTextView = (TextView) findViewById(R.id.text_view_birth_date);
Button dialogOpenButton = (Button) findViewById(R.id.button_dialog_date_picker);
dialogOpenButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog dialog = new Dialog(UserAttributesActivity.this);
final AlertDialog.Builder builder = new AlertDialog.Builder(UserAttributesActivity.this);
LayoutInflater inflater = getLayoutInflater();
View dialogView = inflater.inflate(R.layout.dialog_date_picker, null);
builder.setView(dialogView);
final DatePicker datePicker = (DatePicker) dialogView.findViewById(R.id.date_picker);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
day = datePicker.getDayOfMonth();
month = datePicker.getMonth() + 1;
year = datePicker.getYear();
String date = "" + day + "/" + month + "/" + year;
birthDateSelectedTextView.setText(date);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
}
});
}
Add Style with AlertDialog.Builder like as follow.
AlertDialog.Builder builder = new AlertDialog.Builder(UserAttributesActivity.this, android.R.style.Theme_DeviceDefault_Light_Dialog_NoActionBar);
#Ankita is right, but if you are using AppCompat things are a bit more involved:
Define a new style and extend AppCompat's Light.Dialog style
<style name="DatePickerDialogStyle" parent="#style/Theme.AppCompat.Light.Dialog">
...
</style>
Then add attributes to mimic the NoActionBar part:
<style name="DatePickerDialogStyle" parent="#style/Theme.AppCompat.Light.Dialog">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
Lastly, now that you are no longer getting app compat colors from your AppTheme, copy them over to your new theme:
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="colorPrimary">#color/primary</item>
<item name="colorPrimaryDark">#color/primary_dark</item>
<item name="colorAccent">#color/accent</item>
</style>
<style name="DatePickerDialogStyle" parent="#style/Theme.AppCompat.Light.Dialog">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="colorPrimary">#color/primary<item>
<item name="colorPrimaryDark">#color/primary_darkitem>
<item name="colorAccent">#color/accent</item>
</style>
Then apply this new theme:
AlertDialog.Builder builder
= new AlertDialog.Builder(UserAttributesActivity.this,
DatePickerDialogStyle);
Use this
DatePickerDialog.Builder builder = null;
// 5.0+ 需要更换主题以去除右侧白边
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder = new DatePickerDialog.Builder(this.getActivity(),
android.R.style.Theme_DeviceDefault_Light_Dialog_NoActionBar);
}
// 4.4 不需要,相反加上会有白边
else {
builder = new DatePickerDialog.Builder(this.getActivity());
}
If you are using a class like androidx.fragment.app.DialogFragment(). Do this:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Set the dialog style.
setStyle(STYLE_NO_TITLE, android.R.style.Theme_DeviceDefault_Light_Dialog_NoActionBar)
}
Related
I use three dialogs in my app, one DatePickerDialog in a dialogfragment, one dialogfragment with a custom layout through an alertdialog and one alertdialog without a dialogfragment. My dateopickerdialog looks like this. Notice the titlepart which is completely orange.
My second dialogfragmen does not have the titlepart covering the whole upper part of the window which I would like to have.
Here is the relevant part of my style.xml
<style name="ScheduleCompareTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/primaryColor</item>
<item name="colorPrimaryDark">#color/primaryDarkColor</item>
<item name="colorAccent">#color/primaryLightColor</item>
<item name="android:dialogTheme">#style/ScheduleCompareDialogTheme</item>
<item name="android:alertDialogTheme">#style/ScheduleCompareDialogTheme</item>
</style>
<style name="ScheduleCompareDialogTheme">
<item name="android:windowTitleStyle">#style/ScheduleCompareDialogTitle</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowCloseOnTouchOutside">false</item>
<item name="android:buttonBarButtonStyle">#style/Widget.MaterialComponents.Button.TextButton</item>
</style>
<style name="ScheduleCompareDialogTitle">
<item name="android:background">#color/primaryLightColor</item>
<item name="android:textAppearance">#style/DialogWindowTitleText</item>
</style>
<style name="DialogWindowTitleText">
<item name="android:textColor">#color/primaryTextColor</item>
<item name="android:textSize">24sp</item>
</style>
The first part is my base theme, the second is the style used for dialogs and alertdialogs, the third part for the dialogtitle and the fourth for the title text. The styling works but somehow not the whole title area background is colored orange. Judging from the second picture I assumed there was some standard padding applied so I set the padding in the style name="ScheduleCompareDialogTheme" to 0dp. This produced the following effect.
So it works but only for the toppadding. Setting the paddingLeft and paddingRight explicitly to 0 dp did not produce any results.
Searching the internet I found something using android:topDark but this also produced no effect.
Does someone know how to extend the orange rectangle to cover the whole of the top area?
Aditionally I would like to mention that the datepickerdialog was already styled like it is shown just using the basetheme.
EDIT:
This is the layoutfile for the customdialog. I removed some code which is not shown in the picture, but is used when someone with more rights uses the app.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
xmlns:app="http://schemas.android.com/apk/res-auto">
.......
<RadioButton
android:id="#+id/radio_teachers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Docenten" />
.......
<RadioButton
android:id="#+id/radio_students"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Leerlingen" />
........
<AutoCompleteTextView
android:id="#+id/acl_textinput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:hint="Zoeknaam"
app:layout_constraintTop_toBottomOf="#id/check_own_properties"
app:layout_constraintLeft_toLeftOf="#id/check_own_properties"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="50dp"
android:dropDownHeight="150dp"
android:inputType="textNoSuggestions"/>
</android.support.constraint.ConstraintLayout>
And the relevant part of the dialogfragment
public class OverlaySchedulePickerDialogFragment extends DialogFragment
implements CompoundButton.OnCheckedChangeListener,
View.OnClickListener {
private final String DEBUGTAG = "ScheduleDialog";
private Context mContext;
private ScheduleViewModel mScheduleViewModel;
private AlertDialog mDialog;
//variables for the layout
RadioGroup radiogroupBranches, radiogroupMainLeft, radiogroupMainRight;
RadioButton radioTeachers,radioStudents,radioLocations,radioGroups;
List<RadioButton> radioBranches;
CheckBox checkOwnProperties;
AutoCompleteTextView aclInputvalue;
StringsAdapter aclAdapter;
.....
#NonNull
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
mScheduleViewModel = ViewModelProviders.of(getActivity()).get(ScheduleViewModel.class);
mScheduleViewModel.loadDataFromDB(Constants.GETBRANCHESFROMDB, null);
LayoutInflater inflater = getActivity().getLayoutInflater();
View fragmentLayout = inflater.inflate(R.layout.dialogfragment_secondschedulepicker, null);
//get references to the layoutelements
radiogroupMainLeft = fragmentLayout.findViewById(R.id.radiogroup_main_left);
radiogroupMainRight = fragmentLayout.findViewById(R.id.radiogroup_main_right);
radiogroupBranches = fragmentLayout.findViewById(R.id.radiogroup_branches);
radioTeachers = fragmentLayout.findViewById(R.id.radio_teachers);
radioStudents = fragmentLayout.findViewById(R.id.radio_students);
aclInputvalue = fragmentLayout.findViewById(R.id.acl_textinput);
//set listeners for the checkbox and radiobuttons
checkOwnProperties.setOnCheckedChangeListener(this);
radioStudents.setOnClickListener(this);
radioTeachers.setOnClickListener(this);
radioGroups.setOnClickListener(this);
radioLocations.setOnClickListener(this);
radioTeachers.performClick();
//set properties of the AutoCompleteText
aclAdapter = new StringsAdapter(mContext, R.layout.autocomplete_listitem,new ArrayList<>());
aclInputvalue.setAdapter(aclAdapter);
aclInputvalue.setThreshold(2);
aclInputvalue.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
if(textView.getId() == aclInputvalue.getId() && actionId == IME_NULL && keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
if (mDialog.getButton(DialogInterface.BUTTON_POSITIVE).isEnabled()) {
mDialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick();
return true;
} else {
if (aclAdapter.suggestions.size() > 0) {
String text = aclAdapter.suggestions.get(0);
aclInputvalue.setText(text);
aclInputvalue.setSelection(aclInputvalue.getText().length());
return true;
}
}
}
return false;
}
});
//add textWatcher to validate the entered text
aclInputvalue.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void afterTextChanged(Editable editable) {
String stringToValidate = editable.toString();
if(mStringList!=null) {
mDialog.getButton(DialogInterface.BUTTON_POSITIVE)
.setEnabled(mStringList.contains(stringToValidate));
}
}
});
//build the dialog
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext);
dialogBuilder.setView(fragmentLayout);
dialogBuilder.setTitle(R.string.overlayschedulepickerdialog_title);
dialogBuilder.setPositiveButton(R.string.dialog_positive_button_text, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
//Input is valid so transfer to mScheduleViewModel and ask for secondSchedule
int position = mStringList.indexOf(aclInputvalue.getText().toString());
mScheduleViewModel.setOverlaySchedule(mScheduleType,position);
}
});
dialogBuilder.setNegativeButton(R.string.dialog_negative_button_text, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
mDialog = dialogBuilder.create();
mDialog.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialog) {
((AlertDialog)dialog).getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(false);
}
});
return mDialog;
}
END EDIT
I have used this line to add style to dialog box
AlertDialog dialog = new AlertDialog.Builder(getActivity(), R.style.AlertDialogStyle)
I am able to style the Title name however the text color in the list remains black color only.
public class NotificationModeDialog extends DialogFragment {
public ArrayList<String> list = new ArrayList<>(); //initialized the array of list
public int i;
final String notificationListItems[]={
"CallAlert","address","name","Message","id","contact","ymail","Gmail"};//the data to be displayed in dialog box
#NonNull
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final boolean[] notificationSelected={false,false,false,false,false,false,false,false};//initializing the starting state
//initializing the alertDialog here AlertDialogStyle is added for styling
AlertDialog dialog = new AlertDialog.Builder(getActivity(), R.style.AlertDialogStyle)
.setTitle("Notification Mode")
.setMultiChoiceItems(notificationListItems, notificationSelected ,new DialogInterface.OnMultiChoiceClickListener() {
#Override
public void onClick(DialogInterface dialog, int j, boolean isChecked) {
if(isChecked)
{
list.add(notificationListItems[j]); //add items
}
else if(list.contains(notificationListItems[j]))
{
list.remove(notificationListItems[j]);//remove items
}
}
})
//when person click on "ok" this statement is called
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int j) {
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel(); //cancel the changes and turn of the dialog box
}
}).create();//this will create the dialog box
dialog.show(); //show the dialog box
return dialog; //return the dialog box to layout
}
}
This is styles.xml code.
<!-- Styling the alert BOX -->
<style name="AlertDialogStyle" parent="Theme.AppCompat.Light.Dialog">
<!-- Used for the buttons -->
<item name="colorAccent">#e4e4e4</item>
<!-- Used for the title and text -->
<item name="android:textColorPrimary">#FFFFFF</item>
<!-- Used for the background -->
<item name="android:background">#64f24c49</item>
</style>
Create a alert dialog style in styles.xml:
<style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorAccent">#color/colorAccent</item>
<item name="android:textColor">#color/white</item>
</style>
Then add the style to you current app theme style:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<!-- Alert Dialog Style -->
<item name="android:alertDialogStyle">#style/MyAlertDialogStyle</item>
</style>
How to center vertical the "Login" title?
This is the style:
<style name="ABGAlertDialog" parent="#android:style/Theme.DeviceDefault.Dialog">
<item name="android:textColor">#android:color/black</item>
<item name="android:textColorHint">#android:color/darker_gray</item>
<item name="android:background">#color/corporativo_red</item>
<item name="android:windowTitleStyle">#style/MyAlertDialogTitle</item>
</style>
<style name="MyAlertDialogTitle">
<item name="android:background">#color/corporativo_red</item>
<item name="android:colorBackground">#color/corporativo_red</item>
<item name="android:textColor">#android:color/white</item>
<item name="android:textStyle">bold</item>
<item name="android:maxLines">1</item>
<item name="android:textSize">20sp</item>
<item name="android:bottomOffset">5sp</item>
</style>
I've tried gravity, textAlignment and some others items with no result.
You can't centre a normal alert dialog's title. You will need to create a custom dialog preferably with a dialog fragment or dialog activity depending on the functionality you need.
Set your text style in the android:textAppearance
<style name="MyAlertDialogTitle">
<item name="android:background">#color/corporativo_red</item>
<item name="android:colorBackground">#color/corporativo_red</item>
<item name="android:bottomOffset">5sp</item>
<item name="android:textAppearance">#style/YourTextStyle</item>
</style>
Change both text and background color in title bar (Android)?
Textview: using the dialog title style: #android:style/TextAppearance.DialogWindowTitle
If Alert dialog is normal without any of your custom layout or theme than you ca get object by passing id with android namespace
AlertDialog.Builder alertDialogBuild = new AlertDialog.Builder(getActivity());
alertDialogBuild.setMessage("message text align center");
alertDialogBuild.setTitle("Title text Align Center");
alertDialogBuild.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog dialog = alertDialogBuild.show();
TextView title = (TextView) dialog.findViewById(getActivity().getResources().getIdentifier("alertTitle", "id", "android"));
title.setGravity(Gravity.CENTER);
TextView message = (TextView) dialog.findViewById(getActivity().getResources().getIdentifier("message", "id", "android"));
message.setGravity(Gravity.CENTER);
static void centerTitleHorizontally(final TextView title)
{
ViewTreeObserver vto = title.getViewTreeObserver();
vto.addOnGlobalLayoutListener (new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
try {
title.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} catch (NoSuchMethodError x) {
title.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
Rect bounds = new Rect();
title.getPaint().getTextBounds(title.getText().toString(), 0, title.getText().length(), bounds);
title.setPadding((title.getWidth() - bounds.width()) / 2, 0, 0, 0);
}
});
}
I display an alertdialog in my app but the left and right side of the title shows a different color. I have set my own background color in thems.xml
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="ACCAppTheme" parent="android:Theme.Holo">
<!-- Change default text colour from dark grey to black -->
<!-- <item name="android:windowBackground">#drawable/ic_logo</item> -->
<item name="android:background">#140051</item>
<item name="android:textColor">#ffffff</item>
<item name="android:textColorHint">#c0c0c0</item>
<item name="android:actionBarStyle">#style/AppTheme.ActionBar</item>
</style>
<!-- <style name="OverFlow" parent="#android:style/Widget.Holo.ActionButton.Overflow">
<item name="android:textSize">18sp</item>
</style> -->
<style name="AppTheme.ActionBar" parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">#140051</item>
<item name="android:height">50dp</item>
<item name="android:titleTextStyle">#style/AppTheme.ActionBar.Text</item>
<item name="android:textColor">#android:color/white</item>
<item name="android:showDividers">beginning</item>
</style>
<style name="AppTheme.ActionBar.Text" parent="#android:style/TextAppearance">
<item name="android:textAllCaps">false</item>
<item name="android:textColor">#android:color/white</item>
</style>
</resources>
but what appears is like this
here's the code that creates the alert dialog
public class AppMessages {
public static final int MESSAGE_INFO = 1;
public static final int MESSAGE_CRITICAL = 2;
public interface AlertButtonClickListener {
public abstract void onButtonClicked(boolean value);
}
public static void showMessage(Context context,
String message,
String title,
int messageType,
final AlertButtonClickListener target) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(message)
.setTitle(title)
.setIcon(R.drawable.ic_dialog_information)
.setCancelable(false)
.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if (target != null) {
target.onButtonClicked(true);
}
}
});
switch (messageType) {
case AppMessages.MESSAGE_CRITICAL:
builder.setIcon(R.drawable.ic_dialog_exclamation);
break;
case AppMessages.MESSAGE_INFO:
builder.setIcon(R.drawable.ic_dialog_information);
break;
}
builder.show();
}
public static void yesNo(final Context mContext,
final String title,
final String msg,
final String positiveBtnCaption,
final String negativeBtnCaption,
final boolean isCancelable,
final AlertButtonClickListener target) {
((Activity) mContext).runOnUiThread(new Runnable() {
#Override
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle(title)
.setMessage(msg)
.setIcon(R.drawable.ic_dialog_question)
.setCancelable(false)
.setPositiveButton(positiveBtnCaption,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
target.onButtonClicked(true);
}
})
.setNegativeButton(negativeBtnCaption,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int id) {
target.onButtonClicked(false);
}
});
AlertDialog alert = builder.create();
alert.setCancelable(isCancelable);
alert.show();
if (isCancelable) {
alert.setOnCancelListener(new OnCancelListener() {
#Override
public void onCancel(DialogInterface arg0) {
target.onButtonClicked(false);
}
});
}
}
});
}
}
here's how I call the it
AppMessages.showMessage(thisActivity,
"New update found. Click Ok to download it now.",
"New Update Found",
1,
new AppMessages.AlertButtonClickListener() {
#Override
public void onButtonClicked(boolean value) {
APKDownloader.downloadAPK(thisActivity);
}
});
Try this,
Customize AlertDialog Theme
Create a new Android XML resource file under res/values/
You'll want to create a new style that inherits from the default alertdialog theme:
<style name="CustomDialogTheme" parent="#android:style/Theme.Dialog">
<item name="android:bottomBright">#color/white</item>
<item name="android:bottomDark">#color/white</item>
<item name="android:bottomMedium">#color/white</item>
<item name="android:centerBright">#color/white</item>
<item name="android:centerDark">#color/white</item>
<item name="android:centerMedium">#color/white</item>
<item name="android:fullBright">#color/orange</item>
<item name="android:fullDark">#color/orange</item>
<item name="android:topBright">#color/blue</item>
<item name="android:topDark">#color/blue</item>
</style>
You can specify either colors or drawable for each section of the AlertDialog.
An AlertDialog will build it's display by combining 3 drawables/colors (top, center, bottom) or a single drawable/color (full).
In a theme of your own override the android:alertDialogStyle style (you can do this within the same XML file):
#style/CustomDialogTheme
Override your application's theme in the AndroidManifest within the application tag:
<application
android:icon="#drawable/icon"
android:label="#string/app_name"
android:theme="#style/MyTheme">
Now that you've defined your application's theme, any attributes overridden in your theme will resonate throughout your app.
For example if you also wanted to change the color of your application's title bar:
You would first define a new style that inherits from the default attribute
#color/blue
and just add the new overridden item to your theme:
<style name="MyTheme">
<item name="android:windowTitleBackgroundStyle">#style/MyBackground</item>
<item name="android:alertDialogStyle">#style/CustomDialogTheme</item>
</style>
I need the dialog to fill the screen except for some space at the top and the bottom.
I've search for a solution but couldn't find one probably because I'm declaring it in an onClickListener.
Can someone please give a solution?
Activity code:
sort.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
AlertDialog.Builder sort = new AlertDialog.Builder(HomeScreen.this);
// Get the layout inflater
LayoutInflater inflater = HomeScreen.this.getLayoutInflater();
View sortView = inflater.inflate(R.layout.sort_layout, null);
sort.setView(sortView);
sort.create().show();
}
});
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="fill_parent"
android:layout_marginBottom="50dp"
android:layout_marginTop="50dp"
android:background="#color/white"
android:orientation="vertical" android:layout_gravity="center">
+ some more stuff here I dont think it's relevant
</LinearLayout>
here set your screen width and width to the dialog
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = width;
lp.height = height;
dialog.getWindow().setAttributes(lp);
or in your styles create a style like this then
<style name="DialogTheme" parent="android:Theme.Dialog">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">fill_parent</item>
<!-- No backgrounds, titles or window float -->
<item name="android:windowBackground">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">false</item>
</style>
create a dialog object like this
dialog = new Dialog(this, R.style.DialogTheme);
Edit ::
get width and height like this for any device it will fills..
WindowManager manager = (WindowManager) getSystemService(Activity.WINDOW_SERVICE);
int width, height;
LayoutParams params;
if (Build.VERSION.SDK_INT > VERSION_CODES.FROYO) {
width = manager.getDefaultDisplay().getWidth();
height = manager.getDefaultDisplay().getHeight();
} else {
Point point = new Point();
manager.getDefaultDisplay().getSize(point);
width = point.x;
height = point.y;
}
first Make custom style for dialog in style.xml file:
<style name="full_screen_dialog" parent="#android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:windowBackground">#android:color/transparent</item>
</style>
Then after set this theme to your Alert Dialog:
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.full_screen_dialog));
Or You can call new Activity and Give this Custom style to that activity as a theme in your manifest file...
This may helpful for someone.
I want a dialog to take full width of screen. searched a lot but nothing found useful. Finally this worked for me:
mDialog.setContentView(R.layout.my_custom_dialog);
mDialog.getWindow().setBackgroundDrawable(null);
after adding this, my dialog appears in full width of screen.
Make object like below theme
Dialog objD = new Dialog(this, android.R.style.Theme_Translucent_NoTitleBar);
Dialog dialog2 = new Dialog(context, R.style.DialogTheme);
dialog2.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog2.getWindow().setBackgroundDrawable(null);
dialog2.setContentView(R.layout.dialog_img);
WindowManager.LayoutParams lp = dialog2.getWindow().getAttributes();
Window window = dialog2.getWindow();
lp.copyFrom(window.getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
window.setAttributes(lp);
lp.gravity = Gravity.CENTER;
final ImageView imgprofile=(ImageView)dialog2.findViewById(R.id.img_centre);
Picasso.with(context)
.load(arrayImages.get(position).get("image"))
.resize(800,1000)
.centerInside()
.into(imgprofile, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError() {
imgprofile.setImageResource(R.drawable.user);
}
});
dialog2.show();
And in your dialog_img.xml file add an Imageview with scaleType(fitXY).
I think you shoud try this one:
Inside the dialogFragment:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
}
The simplest way of creating a full screen dialog, create a style like this:
<style name="DialogFullScreenTheme" parent="Theme.AppCompat.Light.DialogWhenLarge">
<item name="windowNoTitle">true</item>
<item name="android:statusBarColor">#color/colorPrimaryDark</item>
</style>
then:
dialog = new Dialog(this, R.style.DialogFullScreenTheme);