Android:How to programmatically set an Activity's theme to Theme.Dialog
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 :
- Define your activity in your AndroidManifest.xml with the theme
@android:style/Theme.Dialog
- In your respective
.Java
file, check for anintent
extra that definesdialog
mode. - If it does not exist, set the
Theme
toandroid.R.style.Theme
. This is the defaulttheme
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:
- Define a
ListAdapter
class extended fromArrayAdapter
. return
1
ingetCount
function@Override public int getCount() { return 1; }
In the
getView
function,inflate
thelayout
of theactivity
you need and do any customization before returning theview
.@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. :-)
you can use setTheme(..)
before calling setContentView(...)
and super.oncreate()
and it should work fine
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