0

This is the code I have used. My MainActivity class

public class MainActivity extends AppCompatActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void broadcastIntent(View view){
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MainActivity.this);
    LayoutInflater inflater = MainActivity.this.getLayoutInflater();
    View dialogView = inflater.inflate(R.layout.demo, null);
    dialogBuilder.setView(dialogView);
    final AlertDialog alertDialog = dialogBuilder.create();
    Window window = alertDialog.getWindow();
    window.setLayout(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
    window.setGravity(Gravity.CENTER);
    alertDialog.show();
  } 
 }

My dialog layout

<?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:gravity="center"
android:background="#00000000"
android:orientation="vertical">

<RelativeLayout
    android:layout_width="150dp"
    android:layout_height="100dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true">

    <TextView
        android:id="@+id/txtJioMoney"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:text="First"
        android:textColor="#3F51B5"
        android:textSize="18sp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Second"
        android:textColor="#3F51B5"
        android:id="@+id/txtCashOnPick"
        android:gravity="center"
        android:textSize="18sp"
        android:layout_marginBottom="20dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

      </RelativeLayout>
   </RelativeLayout>

My main layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">


<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button2"
    android:text="Broadcast Intent"
    android:onClick="broadcastIntent"
    android:background="@color/colorPrimary"
    android:layout_below="@+id/textView1"
    android:textColor="@android:color/white"
    android:padding="10dp"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="103dp" />

  </RelativeLayout>

I am using the AppCompatActivity so I cannot able to use @android:style/Theme.Translucent in my manifest. I want my dialog to be fully transparent I don't need that white background.

sample Screenshot

halfer
  • 19,824
  • 17
  • 99
  • 186
Balaji
  • 117
  • 12

1 Answers1

2

There has been many answers to this question, next time try to search on stackoverflow or google before you post your question.

set Transparent background to alertdialog in android

Dialog with transparent background in Android

android AlertDialog with transparent background

Additionally, instead of using the xml style, you could also try to use .setAlpha(0) on your Relative layout programmatically.

Community
  • 1
  • 1
Vladimir Marton
  • 569
  • 4
  • 31