Archive

Posts Tagged ‘screen’

Detect screen type and apply the correct orientation to application


Long ago, I had to detect screen type in order to apply correct orientation for my application.

In some devices, the width is smaller than the height, however, some others having the width is greater. As you can see this property clearly in the differences between smartphone and tablets.

This is my code.

public void onCreate(Bundle savedState) {
    //...

    if(isScreenLarge()) {
        // width > height, better to use Landscape
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    } else {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
}

public boolean isScreenLarge() {
    final int screenSize = getResources().getConfiguration().screenLayout
            & Configuration.SCREENLAYOUT_SIZE_MASK;
    return screenSize == Configuration.SCREENLAYOUT_SIZE_LARGE
            || screenSize == Configuration.SCREENLAYOUT_SIZE_XLARGE;
}

Cheers,

Pete Houston

Categories: Tricks & Tips Tags: , ,

The less-known over screen orientation

September 19, 2011 Leave a comment

I’ve just found a really interesting problem today at work, it’s about screen orientation.

I was trying to create a very simple application, which:

+ contains one button to change screen orientation state: PORTRAIT to LANDSCAPE, and vice versa.

+ the text on button determines the action going to be done if click. If the current is PORTRAIT then the text should be LANDSCAPE, and vice versa.

OK, so here is one very simple implementation, easy-read, easy-understood:

package pete.android.study;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
    // the constant strings defining text to display on button
	static final String ORIENT_PORTRAIT = "Set Portrait";
	static final String ORIENT_LANDSCAPE = "Set Landscape";
	// determine screen orientation state changed
	boolean mState = false;
	// the main button
	Button mBtnSet = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // load button from layout
        mBtnSet = (Button)findViewById(R.id.btnSet);
        // set default text display
        mBtnSet.setText(ORIENT_LANDSCAPE);
        // handle click event
        mBtnSet.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// state FALSE: switch to LANDSCAPE
				if(!mState) {
					setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
					mBtnSet.setText(ORIENT_PORTRAIT);
				}
				// state TRUE: switch to PORTRAIT
				else {
					setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
					mBtnSet.setText(ORIENT_LANDSCAPE);
				}
				// update state
				mState = !mState;
			}
		});
    }
}

However, there has a problem inside. First, try to build and run it.

The Initial Screen

The Initial Screen

By default, the screen is set as PORTRAIT, and the text is “Set Landscape“, which is correct.

Next, try to click on button, this will happen:

After First Click on Button

After First Click on Button

The screen orientation is changed to LANDSCAPE, however, the text on button doesn’t change at all. That’s so surprising!

And keep clicking on button one more time:

The 2nd Click

The 2nd Click

Now, the text changes to “Set Portrait“, well, the orientation doesn’t change, but on the third click, the screen interface backs to the first initial state.

It’s interesting, isn’t it?

The question in mind is why it is…

First, reading this one on Google Android References – Activity#ConfigurationChanges

Reading clearly, you’ll find that, everytime the screen orientation changes, the Activity is auto destroyed and recreated again, which means the text “Set Landscape” is reset as the initial state, a re-call of onCreate() method.

That’s the problem!

The solution is very simple, by adding the attribute – android:configChanges=”orientation” – , it will avoid re-create the activity and keep the current state.

Note: if you always to handle screen orientation without attribute of handling orientation, sometimes you might get exception, or errors in your application as well, even crash-out and freeze. So always keep this in mind :).

Hope you having fun with this little tips!

 

Cheers,

Pete Houston

 

 

 

Capture Emulator Screen (using DDMS)

June 27, 2011 1 comment

There is a supported function in capturing screen of Android Emulator. Today, one of my colleagues told me, I was so surprised and got so sicked of the every past days, I took screenshots by Windows screen capture function, then cropping image using MS-Paint. Couldn’t believe this!!!!

Oh well, it’s on DDMS, just access menu Device->Screen Capture, and things are automatically.

DDMS: Screen Capture

DDMS: Screen Capture

I’d like to share to everyone of you, so you don’t do stupid things like I did.

Hope you like it!

 

Cheers,

Pete Houston

 

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