1

I have created an activity, that launches a broadcast receiver, The Broadcast receiver launches another activity. The second activity just has a TextView, I have defined a transparent background for the 2nd activity.

But the activity opens over the 1st activity, and displays black background,and not the 1st activity in the background.

Kindly guide me how can i achieve Alert Dialog like feel in this, As i want to display an alert dialog from Broadcast receiver, so this is the only possible way i found to do this.

public class MainActivity extends AppCompatActivity {

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


                Intent intent = new Intent();
                            intent.setAction("com.myapp.serviceIntent");
                            sendBroadcast(intent);

Broadcast

public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();

        Intent i= new Intent(context.getApplicationContext(), AlertActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);


    }
dev90
  • 7,187
  • 15
  • 80
  • 153

3 Answers3

8

Add this code in your style.xml file

 <style name="Theme.AppCompat.Translucent" parent="Theme.AppCompat.NoActionBar">
    <item name="android:background">#33646464</item> <!-- Or any transparency or color you need -->
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowAnimationStyle">@android:style/Animation</item>
</style>

Now add this line in your manifest.xml file under your activity tag

<activity android:name=".AlertActivity"
        android:theme="@style/Theme.AppCompat.Translucent"></activity>
Shaz Hemani
  • 469
  • 2
  • 10
2

You can use Full screen dialog fragments with transparent background Set these parameters and set transparency to parent view of your layout

@Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(STYLE_NO_FRAME, android.R.style.Theme_Holo_Light);

}

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog dialog = super.onCreateDialog(savedInstanceState);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

    return dialog;
}
Cyph3rCod3r
  • 1,978
  • 23
  • 33
1

An use the code below to show the dialog

    <activity
        android:name=".YourActivity"
        android:launchMode="singleTask"
        android:showOnLockScreen="true"
        android:screenOrientation="portrait"
        android:theme="@style/Theme.AppCompat.Light.Dialog">
    </activity>


private void showErrorDialog(String error) {
    builder = null;
    builder = new AlertDialog.Builder(this);
    builder.setTitle("Title");
    builder.setMessage(error);
    builder.setCancelable(false);
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {

            dialogInterface.dismiss();
        }
    });
    dialog = builder.create();
    dialog.show();

}
RaghavPai
  • 652
  • 9
  • 14