jeudi 7 mai 2015

Same design for Phone Portrait, Tab portrait but Different design for Tab Landscape

In my application I am having two different design For portrait mode and Landscape mode(Only for Tab 7.5" and grater). I have designed the layout separately

res/layout-sw720dp-land 
res/layout
res/layout-sw720dp-port

For all the layout I am going to use same file name. For example sw720dp will have detail_item.xml means then res/layout-sw720dp-port & res/layout will have same name as detail_item.xml. But design will different between them.

If a tab is having 7.5" or grater then it will have Landscape mode, In all the case it will have Portrait mode. For checking that I am 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 any activity I am going to call this check the value on shared preference, then I will set the orientation for 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 the orientation I am going to setlayout and going to declare and initialize the views. This works fine on if am holding the tab on Landscape. Incase of in the mean time if If I am rotating the tab to portait then the layout loads portrait layout, not the landscape. How to fix this issue.

Aucun commentaire:

Enregistrer un commentaire