Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 870
kandroidj Avatar asked Nov 06 '12 19:11

kandroidj


1 Answers

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);
}
like image 142
kandroidj Avatar answered Oct 14 '22 21:10

kandroidj