in application having 2 different design portrait mode , landscape mode(only tab 7.5" , grater). have designed layout separately
res/layout-sw720dp-land res/layout res/layout-sw720dp-port
for layout going use same file name. example sw720dp have detail_item.xml means res/layout-sw720dp-port & res/layout have same name detail_item.xml. design different between them.
if tab having 7.5" or grater have landscape mode, in case have portrait mode. checking using deviceconnected function.
public static void deviceconnected(final activity activity) { sharedpreferences mprefs; sharedpreferences.editor meditor; mprefs = activity.getsharedpreferences("database_value", context.mode_world_writeable); meditor = mprefs.edit(); double screeninch = 7.5; string ua = new webview(activity).getsettings().getuseragentstring(); if (ua.contains("mobile")) { activity.setrequestedorientation(activityinfo.screen_orientation_portrait); // meditor.putstring("device_mode", "tab-landscape"); meditor.putstring("device_mode", "phone"); meditor.commit(); } else { displaymetrics dm = new displaymetrics(); activity.getwindowmanager().getdefaultdisplay().getmetrics(dm); int width = dm.widthpixels; int height = dm.heightpixels; int dens = dm.densitydpi; double wi = (double) width / (double) dens; double hi = (double) height / (double) dens; double x = math.pow(wi, 2); double y = math.pow(hi, 2); double screeninches = math.sqrt(x + y); log.i("scrren", "" + screeninches); // toast.maketext(activity, "" + screeninch, 1000).show(); if (screeninches >= screeninch) { activity.setrequestedorientation(activityinfo.screen_orientation_sensor_landscape); meditor.putstring("device_mode", "tab-landscape"); meditor.commit(); } else { activity.setrequestedorientation(activityinfo.screen_orientation_portrait); // meditor.putstring("device_mode", "tab-landscape"); meditor.putstring("device_mode", "tab-portrait"); meditor.commit(); } }
before starting activity going call check value on shared preference, set orientation activity.
string device_mode = mprefs.getstring("device_mode", ""); // set default orientation. if (device_mode.equals("phone")) { setrequestedorientation(activityinfo.screen_orientation_sensor_portrait); } else if (device_mode.equals("tab-portrait")) { setrequestedorientation(activityinfo.screen_orientation_sensor_portrait); } else if (device_mode.equals("tab-landscape")) { setrequestedorientation(activityinfo.screen_orientation_sensor_landscape); } else { finish(); }
after setting orientation going setlayout , going declare , initialize views. works fine on if holding tab on landscape. incase of in mean time if if rotating tab portait layout loads portrait layout, not landscape. how fix issue.
Comments
Post a Comment