kandroidj
kandroidj

Reputation: 13932

Setting Theme Programmactically Causes black background

When I run my android application I am calling a method to check if the app is being run on a tablet using:

public boolean isTablet(Context context){
  boolean xlarge = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == 4);
  boolean large = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK)== Configuration.SCREENLAYOUT_SIZE_MASK);
  return(xlarge || large);
} 

if the method returns true(i.e. the device satisfies one of these conditions)

I set my theme to a Dialog theme via:

setTheme(R.style.MyTheme);

where MyTheme is a theme that inherits from the parent Theme.Holo.Light.Dialog

This logic works fine however it gives me a weird effect in the background. The Calling intent is completely blacked out, whereas if i just set the theme in the manifest the background is only slightly greyed out.

Update - code added

 private Context mClassContext = this;
 @Override
 public void onCreate(Bundle savedInstanceState){
     if(isTablet(mClassContext)){
       setTheme(R.style.MyTheme);
     }
  super.onCreate(savedInstanceState);
  setContentView(R.layout.myLayout);
}

How do I replicate this?

Upvotes: 7

Views: 1166

Answers (1)

kandroidj
kandroidj

Reputation: 13932

I have seem to found the answer to my own question.

To avoid the black background:

In the android manifest set all your activities that could be dialogs(if it is a tablet) to the dialog theme:

then in onCreate add this else case to change it for non-tablet devices(i.e phones)

if(isTablet(mContext)){
setTheme(R.style.myDialogTheme);}
else{ 
  setTheme(R.style.MyTheme);
}

Upvotes: 13

Related Questions