Android Dialog Transparent

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);

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

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.

Make a transparent Dialog in android

Finally I solved this issue using this line of code into onCreateView callback.

getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0));

I found it from here

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 another TextView 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>

How to create a DialogFragment with a transparent part?

It's easy.. my friend

Sample Image

just set default dialog background to transparent as I did in onStart()

class TestDialog: DialogFragment() {

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.my_dialog, container)
}

override fun onStart() {
super.onStart()
// if you want your dialog's width/height match device screen's width/height
// dialog?.window?.setLayout(
// ViewGroup.LayoutParams.MATCH_PARENT,
// ViewGroup.LayoutParams.WRAP_CONTENT
// )

dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
}
}

Your dialog .xml

<?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="wrap_content">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:padding="10dp"
android:orientation="vertical"
android:background="#fff"
android:gravity="center">

<TextView
android:layout_marginTop="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text long text"
/>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"/>

</LinearLayout>

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="@drawable/background_circle"
android:padding="3dp"
android:src="@drawable/ic_account_circle_black_54dp"/>

</RelativeLayout>

background_circle.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">

<solid
android:color="#fff"/>

</shape>

How can I make the layout background of AlertDialog transparent?

Since I cant just post a link I will copy the answer too, but I found my answer here:
How to make custom dialog with rounded corners in android

Create an XML file in drawable, say dialog_bg.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="@color/white"/>
<corners
android:radius="30dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>

set it as the background in your layout XML:

android:background="@drawable/dialog_bg"

Set the background of the dialog's root view to transparent, because Android puts your dialog layout within a root view that hides the corners in your custom layout.

Java:

dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

Kotlin:

dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))


Related Topics



Leave a reply



Submit