1

I've tried many themes to change the activity into an alert dialog in vain. I remember accomplishing it before but unable to do it right now

here's the App's theme:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:windowBackground">@color/background</item>

</style>

here's the activity, I'm trying to change into an alert dialog:

Real activity contains more than this(buttons, linear layout, text views).

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="com.famiddle.android.thechanakyanitiplus.NotificationCenter">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/notification_center_title"
    android:gravity="center_horizontal"
    android:textColor="@color/colorAccent"
    android:layout_marginTop="8dp" />

</ScrollView>

It's java file(NotificationCenter.java)

public class NotificationCenter extends AppCompatActivity {  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // To remove status bar
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    //getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
      //      WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.activity_notification_center);}}

the button that will start this activity:

 LinearLayout notificationManager = (LinearLayout) findViewById(R.id.notification_manager);
    notificationManager.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(getApplicationContext(), NotificationCenter.class));

        }
    });

Finally, AndroidManifest file:

 <activity
        android:name=".NotificationCenter"
        android:theme="@style/Theme.AppCompat.Dialog"
        android:excludeFromRecents="true">

    </activity>

I've tried different themes mentioned here Android Activity as a dialog, but whenever I click the button, It kinda crashes the app(restart it).

Community
  • 1
  • 1
Arpit Chhabra
  • 193
  • 1
  • 12

1 Answers1

0

It's pretty simple if you ask me. Try this theme and notice the parent theme:

Theme.AppCompat.Light.Dialog. That's what is needed to turn an activity into an AlertDialog. Remove "Light" if you need a dark theme. I've also added some more "items" that matches your app's theme. I hope this will help.

    <!-- NOTIFICATION MANAGER THEME -->
<style name="AppDialogTheme" parent="Theme.AppCompat.Light.Dialog">
    <!-- Customize your theme here. -->
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:windowBackground">@color/background</item>
</style>
Ari
  • 97
  • 1
  • 1
  • 11