In my application, whenever the orientation of my screen changes, I want to display different images that will fit the screen. How can I do that?
Implement the method onConfigurationChanged in your Activity, there you can check the orientation and change the UI's look.
The parameter: newConfig.orientation will tell you the current orientation.
Try something like:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
ImageView header = (ImageView) this.findViewById(R.id.header);
if ( newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE ) {
header.setImageResource(R.drawable.header480);
}
else if ( newConfig.orientation == Configuration.ORIENTATION_PORTRAIT ) {
header.setImageResource(R.drawable.header320);
}
}
You can also use this kind of code to actually get the screen size in real time:
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
...
Related
I have a activity in which I load a fragment. Lets say I have two sections inside fragment layout(section A and B) , but while I switch to landscape then I have to show only section A . How will i know the orientation change inside fragment.
you can override onConfigurationChanged to be notified when the orientation changed add code below in your fragment class:
#Override public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(newConfig.orientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){
//landscape
}else{
//portrait
}
}
When the orientation of the screen is changed the Fragment will be destroyed and created again, so onDestroy() and onCreate() are called. in your onCreate() or onCreateView() (or wherever you need to) you can check for the current screen rotation with the following code:
int screenRotation = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation();
if(screenRotation == Surface.ROTATION_90 || screenRotation == Surface.ROTATION_270) {
//LANDSCAPE
} else {
//PORTRAIT
}
For this functionality android provides Sliding pane layout SlidingPaneLayout
and watch this tutorials also official google developers tutorial https://www.youtube.com/watch?v=Jl3-lzlzOJI&t=1007s
I'm setting different height for my custom listview items based on screen orientation, and as in code below I listen to screen orientation changes, and set a global value according to it, and when getView(...) gets called on listview items I set the height of the converted view.
My question is, is there a better solution than this?
How bad this approach affect the UI loading speed process?
I'm targeting API14+
P.S: (200 & 300) below are added here as example, they are not fixed at runtime, they are changed during runtime according to screen dpi.
int mConvertViewHeight;
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
mConvertViewHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200, getResources().getDisplayMetrics());
}
else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
{
mConvertViewHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 300, getResources().getDisplayMetrics());
}
}
In my listview custom array adapter
#Override
public View getView(final int aConvertViewPosition, View aConvertView, ViewGroup parent)
{
mParams = aConvertView.getLayoutParams();
mParams.height = mMainActivity.mConvertViewHeight;
}
If you need just different item view in portrait/landscape mode, you can create different layouts in layout-land and layout-port folders
Is there some differences between getRequestedOrientation and getResources().getConfiguration() to get the orientation of the android phone screen ??
if you are in activity then use
getResources().getConfiguration().orientation
else if not in activity then use your activity instance by passing it,
your_activity_instance.getResources().getConfiguration().orientation
Another way:
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
int orientation = display.getOrientation();
if(myCurrentActivity.getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_PORTRAIT)
{
// code to do for Portrait Mode
} else {
// code to do for Landscape Mode
}
I am developing an app in which I need to provide different activity's background image on different orientation. So I've approached in this way:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setLanguage();
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
// set background for landscape
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
// set background for portrait
}
}
And
android:configChanges="locale|orientation|screenSize"
and in onCreate() I set the background image for portrait assuming user will launch the app staying in portrait mode.
Everything works fine as when users change their mode, the corresponding background is set and so on.
But if a user starts this app when the phone is in landscape mode, as It is shown the portrait image at the first launch as I assumed before user will launch app in portrait mode.
So how can I solve this problem? In one sentence, what is the best way to set different background image for different orientation ? am I in a right track?
In onCreate of your activity you can check for the current orientation using the this
Configuration newConfig = getResources().getConfiguration();
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
// set background for landscape
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
// set background for portrait
}
In one sentence, what is the best way to set different background image for different orientation ?
Step #1: Delete everything you've done, most notably the whole android:configChanges stuff. Ignore the background image for now, and get the rest of your configuration change logic working.
Step #2: Create -land versions of the requisite resource directories, for whatever densities of this image that you have (e.g., res/drawable-land-hdpi/ to match your res/drawable-hdpi/)
Step #3: Move the landscape versions into the -land directories, naming them the same as their portrait equivalents (e.g., res/drawable-hdpi/background.png and res/drawable-land-hdpi/background.png)
Step #4: Just refer to common resource name in your android:background attribute (e.g., #drawable/background)
This way:
You stick to better configuration-change behavior, and
Android will give you the correct background at the correct time
You can check for orientation in onCreate
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int orientation = display.getRotation();
if (orientation == Surface.ROTATION_0 || orientation == Surface.ROTATION_180)
{
// Portrait
}
else
{
// landscape
}
check orientation changes after setContentView method
int orientation = getResources().getConfiguration().orientation;
if (orientation == 1){
utility.toast("portrait");
}else {
utility.toast("landscape");
}
You would think that there would be a straight forward solution. The Android docs state:
The orientation sensor was deprecated in Android 2.2 (API level 8).
Instead of using raw data from the orientation sensor, we recommend
that you use the getRotationMatrix() method in conjunction with the
getOrientation() method to compute orientation values.
Yet, they don't provide a solution on how to implement getOrientation() and getRotationMatrix(). I've spent several hours reading through posts here on developers using these methods but they all have partially pasted code or some weird implementation. Googling hasn't provided a tutorial. Can someone please paste a simple solution using these two methods to generate the orientation??
Here is the implementation for getOrientation():
public int getscrOrientation()
{
Display getOrient = getWindowManager().getDefaultDisplay();
int orientation = getOrient.getOrientation();
// Sometimes you may get undefined orientation Value is 0
// simple logic solves the problem compare the screen
// X,Y Co-ordinates and determine the Orientation in such cases
if(orientation==Configuration.ORIENTATION_UNDEFINED){
Configuration config = getResources().getConfiguration();
orientation = config.orientation;
if(orientation==Configuration.ORIENTATION_UNDEFINED){
//if height and widht of screen are equal then
// it is square orientation
if(getOrient.getWidth()==getOrient.getHeight()){
orientation = Configuration.ORIENTATION_SQUARE;
}else{ //if widht is less than height than it is portrait
if(getOrient.getWidth() < getOrient.getHeight()){
orientation = Configuration.ORIENTATION_PORTRAIT;
}else{ // if it is not any of the above it will definitely be landscape
orientation = Configuration.ORIENTATION_LANDSCAPE;
}
}
}
}
return orientation; // return value 1 is portrait and 2 is Landscape Mode
}
And you can also refer this example which represent the use of both the methods:
getOrientation and getRotationMatrix
http://www.codingforandroid.com/2011/01/using-orientation-sensors-simple.html
public int getScreenOrientation() {
// Query what the orientation currently really is.
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
return 1; // Portrait Mode
}else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
return 2; // Landscape mode
}
return 0;
}
protected void onResume() {
// change the screen orientation
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
setContentView(R.layout.portrait);
} else if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
setContentView(R.layout.landscape);
} else {
setContentView(R.layout.oops);
}
}