0

enter image description here

I am developing an app in which i have to show a button on the screen which is shown in the screenshot. But i want to show this button on some other activity. e.g. I have an activity which is on the screen. now after two minutes i want to show a custom alert dialog i.e. this screen which is shown in the screenshot but instead of showing only button, the alert dialog is showing whole screen as a dialog i.e with the white background. How can i get rid of this background so that only this button can be shown on the screen as an alert dialog? My code for this custom layout is:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
    android:text="Unable to Snooze"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="15dp"
    android:id="@+id/button"
    android:background="#088a68"
    android:textColor="#FFFFFF"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="15dp" />

and activity code for alert dialog is:

new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {

            AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(ActivityA.this);
            LayoutInflater inflater = ActivityA.this.getLayoutInflater();
            View dialogView = inflater.inflate(R.layout.alert, null);
            dialogBuilder.setView(dialogView);

            AlertDialog alert11 = dialogBuilder.create();
            alert11.show();

        }
    }, 120000);

NOTE I want to show the button on the bottom of the screen as an alertdialog

Arslan Ali
  • 371
  • 5
  • 20

3 Answers3

1

Edit:

Use Dialog instead of AlertDialog.Builder and so use setContentView instead setView. And check this one: Dialog with transparent background in Android

Old Answer:

If you are gonna show this button on multiple activities, you'd better use DialogFragment. Also you can set fragments background transparent and you can adjust size of the dialog to fit button.

How to use Dialog Fragment : https://stackoverflow.com/a/20405684/1781718

Community
  • 1
  • 1
dcanbatman
  • 165
  • 1
  • 13
1

Try this one:-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btn_snooze"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Unable to snooze"/>
</LinearLayout>

& Call this method wherever you need

public void showDialog() {

        dialog = new Dialog(MainActivity.this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.getWindow().setBackgroundDrawable(
                new ColorDrawable(android.graphics.Color.TRANSPARENT));
        dialog.setContentView(R.layout.dialog_layout);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(true);

        btn_snooze = (Button) dialog.findViewById(R.id.btn_snooze);

        btn_snooze.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.cancel();
            }
        });

        dialog.show();
    }
priyanka kataria
  • 417
  • 5
  • 17
0

Try with:

AlertDialog alert11 = dialogBuilder.create();
// Add this
alert11.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

alert11.show();
Borislav Kamenov
  • 561
  • 4
  • 12