Android detect Screen layout Size and Resolution with example


Introduction
               Many of our applications requires android mobile screen size and resolution for different purposes. In this tutorial lets see how to get android mobile screen size and resolution programatically. This is going to be so simple with very few lines of code, now lets go ahead and start.

Step 1: Get mobile screen resolution
               We will be using two classes to get this done, one is the Display class and the other one is Point class. Create Display class object and Point class object, using which we can find the resolution Width and Height as follows :


Display display = getWindowManager().getDefaultDisplay();
  Point size = new Point();
  display.getSize(size);
  int width = size.x;
  int height = size.y;

Set this Width and Height to a textView which will display the mobile's resolution.

Step 2: Get Android Screen Size
              The SCREENLAYOUT_SIZE_MASK bits define the overall size of the screen. They may be one of SCREENLAYOUT_SIZE_SMALL, SCREENLAYOUT_SIZE_NORMAL, SCREENLAYOUT_SIZE_LARGE, or SCREENLAYOUT_SIZE_XLARGE.
              

int screenSize = getResources().getConfiguration().screenLayout
    & Configuration.SCREENLAYOUT_SIZE_MASK;  
  switch (screenSize) {
  case Configuration.SCREENLAYOUT_SIZE_LARGE:
   Toast.makeText(this, "Large screen", Toast.LENGTH_LONG).show();
   break;
  case Configuration.SCREENLAYOUT_SIZE_NORMAL:
   Toast.makeText(this, "Normal screen", Toast.LENGTH_LONG).show();
   break;
  case Configuration.SCREENLAYOUT_SIZE_SMALL:
   Toast.makeText(this, "Small screen", Toast.LENGTH_LONG).show();
   break;
  default:
   Toast.makeText(this,
     "Screen size is neither large, normal or small",
     Toast.LENGTH_LONG).show();
  }

We can display these values in a Toast using Switch case. Below is the screen shot of the output of resolution and screen layout code.



activity_main.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/text_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

MainActivity.java


import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.res.Configuration;
import android.graphics.Point;
import android.view.Display;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("NewApi")
public class MainActivity extends Activity {

  @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

   Display display = getWindowManager().getDefaultDisplay();
  Point size = new Point();
  display.getSize(size);
  int width = size.x;
  int height = size.y;

   TextView text = (TextView) findViewById(R.id.text_id);

   text.setText("Width :" + String.valueOf(width) + "    Height :"
    + String.valueOf(height));

   int screenSize = getResources().getConfiguration().screenLayout
    & Configuration.SCREENLAYOUT_SIZE_MASK;
  switch (screenSize) {
  case Configuration.SCREENLAYOUT_SIZE_LARGE:
   Toast.makeText(this, "Large screen", Toast.LENGTH_LONG).show();
   break;
  case Configuration.SCREENLAYOUT_SIZE_NORMAL:
   Toast.makeText(this, "Normal screen", Toast.LENGTH_LONG).show();
   break;
  case Configuration.SCREENLAYOUT_SIZE_SMALL:
   Toast.makeText(this, "Small screen", Toast.LENGTH_LONG).show();
   break;
  default:
   Toast.makeText(this,
     "Screen size is neither large, normal or small",
     Toast.LENGTH_LONG).show();
  }
 }

  @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

}

Source code of this application
AndroidScreenSize.zip




Reactions:

0 comments :

Post a Comment