Nirav Dabhi
Nirav Dabhi

Reputation: 343

How to develop an application for medium screen and large resolution in andorid?

I had developed an application. Which is suitable for large screen tablet and medium screen mobile devices.

But in Micromax Canvas series it takes resoulation as a tablet because those type of devices are of large resolution.

My device have screen of around 4" But when i run at code at my device it consider the size as 5.81". May be it is because of high resolution?

Can anyone tell the solution?

My code for checking the screen size is.

   int orientation = getWindowManager().getDefaultDisplay().getOrientation();

        screen_size = global.getScreenSize();                
        Toast.makeText(getApplicationContext(), "Screen Size:" +screen_size, 500).show();        

        if(screen_size>=5.1)
        {   
            if (orientation==0)       
                setContentView(R.layout.login_large);
            else if (orientation==1)         
                setContentView(R.layout.login_large_land);
            else if (orientation==2)       
                setContentView(R.layout.login_large);
            else if (orientation==3)         
                setContentView(R.layout.login_large_land);
        } 

When i run the application it runs succesfully in tablet and Samsung devices but it doesnt run on Micromax Canvas series.

My Global Variable coding is

public double getScreenSize()
    {
     double size = 0;
         try
         {           
             DisplayMetrics dm = getApplicationContext().getResources().getDisplayMetrics();
             float screenWidth  = dm.widthPixels / dm.xdpi;
             float screenHeight = dm.heightPixels / dm.ydpi;
             size = Math.sqrt(Math.pow(screenWidth, 2) + Math.pow(screenHeight, 2));
         }
         catch(Throwable t){}
         return size;
    }

Upvotes: 0

Views: 103

Answers (2)

Nitin Misra
Nitin Misra

Reputation: 4522

It's bad practice to do it in Coding Way because some of device doesn't returns exact physical screen size but here is your solution to the problem

DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        double x = Math.pow(dm.widthPixels/dm.xdpi,2);
        double y = Math.pow(dm.heightPixels/dm.ydpi,2);
        screen_size = Math.sqrt(x+y);
        Log.d("debug","Screen inches : " + screen_size);

then you could do what you want

if(screen_size>=5.1)
        {   
            if (orientation==0)       
                setContentView(R.layout.login_large);
            else if (orientation==1)         
                setContentView(R.layout.login_large_land);
            else if (orientation==2)       
                setContentView(R.layout.login_large);
            else if (orientation==3)         
                setContentView(R.layout.login_large_land);
        } 

Upvotes: 0

Ben Pearson
Ben Pearson

Reputation: 7752

There's a couple of things here I think you can do here that will both improve code quality and most likely fix the issue you are seeing:

  1. Instead of detect orientation and load in the XML file, you should place the same XML file in different folders (layout/layout-land)
  2. Read through the documentation here, you might decide to go with sw600dp instead of 'large'

Upvotes: 2

Related Questions