Home > Tricks & Tips > Retrieve screen resolution (in pixels)

Retrieve screen resolution (in pixels)


Sometimes, you surely need to get screen resolution to do something and this is what you need.

package pete.android.study;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.widget.Toast;

public class MainActivity extends Activity  {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// Display object holds information about screen
		Display display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

		if(event.getAction() == MotionEvent.ACTION_DOWN) {
			String text = "W: " + display.getWidth() + " | H: " + display.getHeight();
			Toast.makeText(this, text, Toast.LENGTH_LONG).show();
		}

		return super.onTouchEvent(event);
	}
}

Enjoy it!

Cheers,
Pete Houston

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: