I used the following piece of code in onCreate() method... But still it is displaying both messages while transists from lands开发者_开发问答cape to protrait. Please help me to getrid of it..
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WindowManager wm=getWindowManager();
Display dis=wm.getDefaultDisplay();
if(dis.getWidth()>dis.getHeight())
Toast.makeText(getBaseContext(), "Landscape", Toast.LENGTH_SHORT).show();
else
Toast.makeText(getBaseContext(), "Portarit", Toast.LENGTH_SHORT).show();
}
use getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE
instead; width and height of the screen are fixed when you ask the defaultDisplay(). Otherwise you could do with getResources().getDisplayMetrics().widthPixels
and getResources().getDisplayMetrics().heightPixels
.
精彩评论