Dialog with transparent background in Android
Add this code
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
Or this one instead:
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
android - Dialog with transparent background (with any color)
Ok I got the solution
First make change in theme.
Don't make custome theme. Use android.R.style.Theme_Translucent_NoTitleBar_Fullscreen
final Dialog dialog = new Dialog(BookAppointmentActivity.this, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
instead of custom theme, and add following:
window.setBackgroundDrawableResource(R.color.dialog_back);
Full code:
final Dialog dialog = new Dialog(context, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
dialog.setContentView(R.layout.customedialog);
Window window = dialog.getWindow();
window.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
window.setBackgroundDrawableResource(R.color.dialog_back);
dialog.show();
Use color from your resource file color.xml
and at last but not least don't forget to decrease the opacity of the color.
Custom dialog with white transparent background
Try this :
Use color code for white transperent : #B3ffffff
Dialog alertDialog = new Dialog(this);
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
alertDialog.setContentView(R.layout.photo_dialog);
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(mContext.getResources().getColor(R.color.white_transperent)));
alertDialog.show();
You can adjust this hash code according your transparency requirement. use link
How can I set a background transparency to a Dialog in Android?
You can make use of this class. Change the color and transparency here Color.parseColor("#66F9B639")
public class LoadingDialog {
private static LoadingDialog instance;
private Dialog dialog;
public static LoadingDialog getInstance() {
if (instance == null) {
synchronized (LoadingDialog.class) {
if (instance == null) {
instance = new LoadingDialog();
}
}
}
return instance;
}
public void show(Context context) {
if (dialog != null && dialog.isShowing())
return;
dialog = new Dialog(context);
dialog.setCanceledOnTouchOutside(false);
dialog.setCancelable(false);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#6620314c")));
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_loading);
ViewGroup.LayoutParams params = dialog.getWindow().getAttributes();
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
params.height = ViewGroup.LayoutParams.MATCH_PARENT;
dialog.getWindow().setLayout(params.width, params.height);
dialog.show();
}
public void dismiss() {
if (dialog != null && dialog.isShowing()) {
dialog.dismiss();
}
}
}
How do I add back the transparent background around my dialog activity with a custom theme
As @nima pointed out you can use getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
to make the background transparent.
This will introduce a couple of issues:
Transparency of everything
Fix this by setting a white background to the
ConstraintLayout
root layout.Transparent activity title
Fix this by removing the title from the manifest and set it to an empty one in activity with
setTitle("")
; and add anotherTextView
to represent the title in the layout:
Remove the title (android:label) from the activity in the manifest:
Also you should android:exported="true"
as it seems that this activity should be exported to other apps:
<activity
android:name=".activity.LockScreenActivity"
android:theme="@style/Theme.AppCompat.DayNight.Dialog"/>
Set the title & background:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lock_screen);
// ...........
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
setTitle("");
}
Layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:paddingHorizontal="16dp"
android:paddingTop="16dp"
tools:context=".LockScreenActivity">
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Application Blocked"
android:textSize="22sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="@id/textView2"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="214dp"
android:layout_height="136dp"
android:gravity="center_vertical"
android:padding="20dp"
android:text="@string/aplicacao_bloqueada_text"
android:textAlignment="viewStart"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView1" />
</androidx.constraintlayout.widget.ConstraintLayout>
UPDATE:
When I use my custom theme, as opposed to Theme.AppCompat.DayNight.Dialog, I still get the black screen
This is because the android:backgroundDimAmount
is set to 50 in your custom theme; the valid range for this attribute is 0 through 1; any value greater than that will be considered 1...
0 means there is no dimming (completely transparent, and 1 meaning 100% opaque/black.
So to fix this issue, I guess you mean by 50 as the half way, so you'd set it to 0.5 instead:
<style name="dialog_theme" parent="Theme.AppCompat.DayNight.Dialog">
<item name="android:textColor">@color/white</item>
<item name="android:windowBackground">@color/dark_gray</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:backgroundDimAmount">0.5</item>
</style>
BottomSheetDialog with transparent background
So, I figured out 2 solutions.
Best one:
Create an activity with transparent background just for your bottom sheet.
Implement your own layout with a coordinator layout and a bottom sheet.
Set the margin you want.
Set the content you want.
Not tested yet.
Lazy one:
Extends BottomSheetDialogFragment, in onActivityCreated
add:
Resources resources = getResources();
// Set margin for Landscape Mode. Maybe a more elegant solution will be to implements our own bottom sheet with our own margins.
if (resources.getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
assert getView() != null;
View parent = (View) getView().getParent();
CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) parent.getLayoutParams();
layoutParams.setMargins(
resources.getDimensionPixelSize(R.dimen.bottom_sheet_margin_left), // 64dp
0,
resources.getDimensionPixelSize(R.dimen.bottom_sheet_margin_right), // 64dp
0
);
parent.setLayoutParams(layoutParams);
}
How can I make the background of my dialog in android transparent?
Add this code where you are initializing your dialog
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
or you can also add this code instead
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
Related Topics
Why Does Layoutinflater Ignore the Layout_Width and Layout_Height Layout Parameters I'Ve Specified
How to Sign Android App With System Signature
Android - Android.View.Inflateexception: Binary Xml File Line #8: Error Inflating Class Fragment
API Key for Gcm Is Suddenly Invalid? Unauthorized (401) Error
Failed to Load Appcompat Actionbar with Unknown Error in Android Studio
Warning: This Asynctask Class Should Be Static or Leaks Might Occur
Android Material Design Button Styles
How to Configure Gradle to Work "Offline" (Using Cached Dependencies)
Convert View to Bitmap on Android
Difference Between Fragmentpageradapter and Fragmentstatepageradapter
How to Find Serial Number of Android Device
Programmatically Switching Off Android Phone
Failed to Resolve: Com.Google.Firebase:Firebase-Core:11.2.0
How to Create Circular Progressbar in Android