2

I would like to show an Activity (use AppCompat) as Dialog when my device is a tablet.

Here is my theme for MyAppCompatActivity

<style name="AppDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="windowNoTitle">true</item>
</style>

When I put directly this theme in my Manifest.xml like this, works fine :

<activity android:name=".MyAppCompatActivity" android:theme="@style/AppDialogTheme"/>

But when I would like set my theme programmatically, my Activity appears as dialog but the background overlay is black (and not transparent as the first method). Here is my code :

public abstract class MyAppCompatActivity extends AppCompatActivity 
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
         if(isTablet)
         {
             setTheme(R.style.AppDialogTheme);
         }

        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_layout);
    }
}
frenchdev20
  • 695
  • 2
  • 10
  • 18
  • The question is, what do you want? Light theme ? please be clear in the question. – ʍѳђઽ૯ท Jan 12 '16 at 16:27
  • I would like to MyAppCompatActivity show as dialog (with my AppDialogTheme) when my device is a tablet so I must set the theme programmatically but doesn't work with the code above – frenchdev20 Jan 12 '16 at 16:37

1 Answers1

1

Try to use bellow code for changing the background color:

<style name="AppDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="windowNoTitle">true</item>
    <item name="android:background">#FF0000</item> //Your custom background color
</style>

Of course, you could change the background color from your custom my_layout like this:

android:background="#FF0000"

And you were right, seems like that dialog appearing with dark color.But, according to Android Developers docs, if you are using on large screens, you have to add this in Manifest:

<activity android:theme="@android:style/Theme.Holo.DialogWhenLarge" >

With AppCompat:

Theme.AppCompat.Light.DialogWhenLarge
// or
Theme.AppCompat.DialogWhenLarge

And something else, check this link from Android Developers:

http://developer.android.com/reference/android/app/Dialog.html

And here is the content:

Public Constructors

Dialog(Context context) Creates a dialog window that uses the default dialog theme. Dialog(Context context, int themeResId) Creates a dialog window that uses a custom dialog style.

And honestly, I couldn't find any methods like setTheme for Dialogs there.(I'm not pretty sure, but it wasn't there).


And i'm thinking about your implementation, i think this is not a good way for creating a dialog.Check and try the below code for creating a Dialog:

public class MyAppCompatActivity extends Dialog

Instead of your codes:

public abstract class MyAppCompatActivity extends AppCompatActivity 

I think you did that in wrong way like an Activity.Check the following links for creating a dialog also:

How to create a Custom Dialog box in android?

Community
  • 1
  • 1
ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
  • Thanks for your quick reply. When I put this theme : `Theme.AppCompat.Light.DialogWhenLarge` directly in my manifest like this `` works fine but programmatically with `Activity.setTheme(int resId)`my dialog appearing still with the dark overlay. – frenchdev20 Jan 12 '16 at 17:08
  • `` you are using that Dialog as an activity! http://stackoverflow.com/questions/29032488/whats-the-differents-between-activity-which-theme-is-dialog-and-the-class-exten – ʍѳђઽ૯ท Jan 12 '16 at 17:20
  • My problem is not here ! When my device is a phone My Activity appears on fullscreen and when it is a tablet appears on Dialog (but is still an Activity). If i extend with a Dialog class how to use for fullscreen ? – frenchdev20 Jan 12 '16 at 17:32
  • Well, your questions needs to edit, edit it before they put it as `hold on` thread.and one thing, i've checked it again and you can use that as activity for full screen -> http://developer.android.com/guide/topics/ui/dialogs.html So, as i heard that from your comment, check this: http://stackoverflow.com/questions/8255985/how-to-support-different-screen-size-in-android **Your question is not clear by the way, i thought you just wanna make it light!** – ʍѳђઽ૯ท Jan 12 '16 at 17:35