How to get Mobile Screen Resolution Width and Height programatically

Width and Height in landscape mode
Width and Height in landscape mode
Width and Height in portrait mode
Width and Height in portrait mode

To get the Width and the height of the Android Screen we can use the below code,

package com.code2care.tools.sample;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {

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

        TextView tv = (TextView) findViewById(R.id.textView);

        Display disp = getWindowManager().getDefaultDisplay();

        //Displays the Width and Height of the screen
        tv.setText("Width : " + disp.getWidth() +"\n" +" Height : "+disp.getHeight());


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Comments & Discussion

Facing issues? Have questions? Post them here! We're happy to help!