Android:How to programmatically set an Activity's theme to Theme.Dialog

91,646

Solution 1

Would like to give a work around for this problem.

Problem : How to use the same activity as both dialog and full screen based.

Solution :

  1. Define your activity in your AndroidManifest.xml with the theme @android:style/Theme.Dialog
  2. In your respective .Java file, check for an intent extra that defines dialog mode.
  3. If it does not exist, set the Theme to android.R.style.Theme. This is the default theme which is applied if you do not define any theme.

Code :

boolean fDialogMode = getIntent().hasExtra("dialog_mode");

if( ! fDialogMode ) {
    super.setTheme(android.R.style.Theme);
}

Alternate Solution:

A more complex solution is to use AlertDialog as below:

  1. Define a ListAdapter class extended from ArrayAdapter.
  2. return 1 in getCount function

    @Override
    public int getCount() { return 1; }
    
  3. In the getView function, inflate the layout of the activity you need and do any customization before returning the view.

    @Override
    public View getView( int position, View view, ViewGroup group ) {
        View v = view;
        if( v == null ) {
            v = getSystemService(Context.LAYOUT_INFLATER_SERVICE).inflate( <layout res id>, null );
        }
    
    ... Do any customization here    ....
    
          return v;
    }
    

This is definitely a second choice option by if you are not doing too much processing in the activity class this could be an option.

Only reason to consider this solution could be that the logic to show it in a dialog is isolated to the places where it is used as a dialog.

Both the options worked for me but for obvious reasons I am taking the first option. :-)

Solution 2

you can use setTheme(..) before calling setContentView(...)and super.oncreate() and it should work fine

Solution 3

Like several others, calls to setTheme in onCreate (before or after my call to super.onCreate) did not work. However, by overriding setTheme, I was able to specify a theme other than that stated in Manifest.xml. Specifically, the following worked without issue:

@Override
public void setTheme(int resid) {
    boolean changeTheme = true;
    super.setTheme(changeTheme ? android.R.style.Theme_Dialog : resid);
}

I found the above in the discussion at: https://code.google.com/p/android/issues/detail?id=4394

Solution 4

Call Activity.setTheme() in onCreate() before you call setContentView().

Solution 5

use setTheme before calling super.onCreate(savedInstance)

Share:
91,646
100rabh
Author by

100rabh

Leading all phases of diverse Java, Big data &amp; Mobile technology Products with hands-on technical edge . Spark a conversation over LinkedIn

Updated on June 17, 2020

Comments

  • 100rabh
    100rabh almost 4 years

    So I have an Activity (say TestActivity) which needs to act as a normal unthemed Activity as well as a Theme.Dialog at other place. I am trying to reuse same TestActivity for both the tasks.

    All I am looking for setting the theme dynamically. The code is simple: Here is my activity's onCreate that works with a black background

    public void onCreate(Bundle icicle) {
        if (Utility.isDialog == true)
            setTheme(android.R.style.Theme_Dialog);
        super.onCreate(icicle);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
    .....
    

    and here is the Manifest Entry

    <activity android:name=".TestActivity"/>
    

    And in the meantime I found a post that says it can't be done here is the post http://code.google.com/p/android/issues/detail?id=4394 .But there is a strong feeling that it can be done.

    All suggestions are welcome.

  • 100rabh
    100rabh about 13 years
    Reuben though it works (& I have tried that earlier too)but I see Black Background instead of the calling Activity
  • Reuben Scratton
    Reuben Scratton about 13 years
    Then your theme properties are incorrect. What is your android:windowBackground set to?
  • 100rabh
    100rabh about 13 years
    Reuben I have not defined a custom style.But I will now implement a custom style with windowBackground to color/transparent
  • 100rabh
    100rabh about 13 years
    Reuben even the custom style didn't worked.For your answer to windowBackgound see this post stackoverflow.com/questions/2176922/…
  • 100rabh
    100rabh about 13 years
    see the updated post with the code.Do let me know for more details.
  • Ahmed Salem
    Ahmed Salem over 12 years
    you are not calling setContentView , and i know that it can be done because i did it in my activity
  • Bradley Uffner
    Bradley Uffner over 12 years
    Moving the call to setTheme before the super.onCreate made it work for me, thank you!
  • Joris Weimar
    Joris Weimar about 12 years
    Just a FYI: This does work, but it creates an enormous lag in creating a new activity for some reason (on Kindle Fire) and it makes my app crash unexpectedly on the Nook Color.
  • Ahmed Salem
    Ahmed Salem about 12 years
    i never tried it on Kindle before , thx for the info , will have to test it when i get one
  • Cookster
    Cookster almost 12 years
    This solution doesn't work for all devices. Consider the Motorola Xoom, the result is a dialog on top of a black background rather than a transparent one.
  • Ahmed Salem
    Ahmed Salem almost 12 years
    @user933237 that is interesting , i also found out that what you described happens on ACER iconia tablet
  • Dmitry Zaytsev
    Dmitry Zaytsev over 11 years
    Great explanation, thanks! If someone don't get it at the first time (like me :D) - read @mozarty solution first to understand where exactly you should put this code.
  • android developer
    android developer over 10 years
    is it possible to call the setTheme after setContentView, if i call setContentView again (this time after setTheme) ? example of a scenario: user sees the activity, press a button, and it changes the theme by calling setTheme and then setContentView.
  • Steve B
    Steve B about 9 years
    However, with Android invoking setTheme() before the onCreate() call, one can't use the data normally sent to an activity to decide whether to change the theme. Some additional hack is needed to make that decision.
  • Regolith
    Regolith almost 5 years
    please take some time to explain how this solves the problem so that other with similar problem can know the issue clearly.