Is there any way to auto fit the screen when changing the orientation?
Because when I view my App in Portrait mode all the images seems fined but when I change it into Landscape the images becomes pix-elated or has been stretched to much. So I'm wondering if there is auto fit screen.
thanks for any thoughts.
Specify different layouts with same name in layout-land folder and use suitable drawables.
For more help see this tutorial
http://www.androidpeople.com/android-portrait-amp-landscape-differeent-layouts
Display display;
int width;
int height;
in onCreate() add
width = display.getWidth();
height = display.getHeight();
in onResume() add
if(width<height){
//you can add your layout here (landscape)
}else{
//you can add your layout here (portrait)
}
you must have two layouts, one for portrait and another for landscape orientation. You can use the same images if you use two layouts.
There are two possibilities,
Option 1 : You can have different XML layouts in layout-port ,layout-land,drawable,drawable-land. In those XML layouts, you can fix different sized images that matches your screen density.For different set of images, you can review on ldpi,mdpi,hdpi,xhdpi.
Option 2 : You can go with Nine patch images.
If you find a better solution,Share it.
Cheers.
You can use following snippet to recognize the configuration has been changed
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen and do your action here
}
Cheers.
Related
Basically my goal is to set some custom View size exactly to 1/3 of the screen for portrait orientation and 1/6 for landscape
The first thought that came to my mind is to simply calculate some mSize variable and set it to the View in OnMeasure like this:
mSize = (AContext.getScreenSize().x / (orientation == Configuration.ORIENTATION_PORTRAIT ? 3 : 6)
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(mSize|MeasureSpec.EXACTLY, mSize|MeasureSpec.EXACTLY);
}
And it's working just fine, till i rotate the screen. The thing is that when screen is rotating View initialization and sizing are called before the orientation value will actually change, so the result is that it goes one third to landscape also, if it was opened with portrait orientation firstly, and other way around.
I'm aware that i can do something like :
android:configChanges="orientation|screenSize"
In AndroidManifest for my Activity and override onConfigurationChanged to handle the rotation and change mSize value there, but, it disables auto choosing between portrait and landscape layouts(from .xml files on inflating). So i'm ending up with the same xml file for both orientations.
Is there a way to keep orientation value from context.getResources().getConfiguration().orientation consistent?
Or i need to do some workaround with onConfigurationChange to enable layout choosing?(i don't want to do it maually in code thou)
Or is there a better way to achieve my goal, and i'm just doing it wrong from the start?
Please help me.
first I would say to not use android:configChanges="orientation|screenSize" on this situation. It makes no sense such a thing on a custom view.
Secondly I believe the main mistake is the way you're capturing the 3, or 6 values. As you can achieve it much simpler using XML.
res/values/integers.xml
<integer name="view_fraction">3</integer>
res/values-land/integers.xml
<integer name="view_fraction">6</integer>
Then during your view constructor you simply call:
int val = context.getResources().getInteger(R.integer.view_fraction)
Furthermore, I will offer some other suggested improve on your view size calculation by suggesting you to simply use code that Google provides us, instead of trying to re-code it. Using the classes from android.support.percent
If your custom view extends from FrameLayout or RelativeLayout I would suggest you to instead extend from their percent counterpart PercentFrameLayout and PercentRelativeLayout.
If the custom view does not extends from one of those, you can use their helper PercentLayoutHelper following the guide on their page.
That way you can easily dinamically assing percentage of view size on your XML layout
as new to android and have a requirement that imageview src image change when changing the phone oreintation(portrait to landscape and vice versa) .set the image for imageview is ok for me but how can the above requirement can be achieve.is this thing possible in android.
Thanks .A little help will be very much appreciate.
here i am mention the two layout which contain some imageview
1 the portrait mode of design
2.the landscape mode of design
if anything more require please ask me.
the app is design for the android version 2.3 and api level 10
Try this.
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
ImageView imageView = (ImageView)findViewById(R.id.imageView);
if(getResources().getConfiguration().orientation == 2) {
imageView.setImageDrawable(R.drawable.landscapeimage);
} else if(getResources().getConfiguration().orientation == 1){
imageView.setImageDrawable(R.drawable.portraitimage);
}
}
you can create Layout-land and from that you can copy paste your code from the layout folder , and change the imageview to whatever you like. now when the user change orientation it will go to the layout-land. hope that help you.
write this in onStart()
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
if(width<height){
//in portrait mode
}
else{
//in landscape mode.
}
you have to create new layout and put it to its designated folder such like:
- layout layout-800x400
- layout-land
- layout-land-800x400
- layout-port
- layout-port-1232x800
etc.
in each folder are adjusted layout depends on what design would you want it to be like. Now, if its not working, you have to identify the dimension or resolution of your device and add it as your layout folder like sample above,coz maybe the device was unable to find the layout folder for it.
for more reference check this link
http://developer.android.com/guide/practices/screens_support.html
I have a Table containing a number of images per row. The number of images per row is decided on the fly based on the image width and screen width. When I use the phone normally, images are spaced on the phone screen. When the phone orientation changes, all the images appear on the same row. Should I explicitly handle the orientation changes for this case?
// get the number of images per column based on the screen
// resolution
Display display = getWindowManager().getDefaultDisplay();
int screenWidth = display.getWidth();
Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.emo_im_happy);
numberOfImagesPerRow = screenWidth / (drawable.getIntrinsicWidth() + 10);
int numberOfRows = (int) Math.ceil(numberOfEmotions
/ numberOfImagesPerRow);
It depends when that code is run, but you generally don't need to do anything special.
When Android detects an orientation change, the system will re-create your Activity and it will get a chance to go through onCreate and re-layout and re-render everything according to the new configuration.
Using android android:configChanges should be used with care and not be the first option you think of, since the system will do a better job at selecting appropriate resources for you after a configuration change (and you'll have to handle that code path for other forms of configuration change anyways).
See:
http://developer.android.com/guide/topics/resources/runtime-changes.html
Yes. One way is to put android:configChanges="orientation|keyboardHidden" on your <activity> in the AndroidManifest.xml file, and then override onConfigurationChanged to detect the orientation change.
If you want to persist the number of images displayed in one row you can use the GridView. In this way you can control the number of columns per line.
<GridView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:isScrollContainer="true"
android:numColumns="3"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
/>
In My Application i have One button like this :
The Resolution of that button is 192x32. And when i put this button in to drawable-mdpi, it seems good to layout. Now for other screen resolution for multiple screen support which size of button i have to make to see the Good Layout Design according to other Devices screen ?
I mean for drawable-ldpi and drawable-hdpi, which resolution i have to make for this button ? How to do Such calculation for to make this button size to fit for all the screen size ?
Please help me for this.
Thanks.
No need to create multiple buttons for multiple screen support.
Instead create a single button and set the width and height at run time.This is achieved by getting the display width and height. Use the bellow code to get the display H & W values.
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
based in the above values set the button width and height at runtime.
Example:
Button bt=(Button)findViewById(R.id.button);
bt.setWidth(width);//screen width(fill_parent)
bt.setHeight(height/6);//1/6 of the screen height
The above code set the button width to screen(display) width size and height to 1/6 of the screen.
There is much information on this subject on the android developers website. In particular, there is a list of the various dpi levels and what range of DPIs that they correspond to.
Also, Rather than providing different images for different resolutions, you could make the image a nine-patch, and have it auto-magically expand to fit the button. Although if you want to keep the highlighting in the background proportional, it might be somewhat difficult to make it expand vertically.
I have an android application, I would like to know if I can have 1 layout (1 layout xml file) for both landscape and portrait mode. But I want a different background for each mode? Is that possible? Do I need 2 xml file points to different background image? Or I can achieve what I want using 1 xml file?
Thank you.
I have achieved the same by creating drawable-land-hdpi folder and copying my landscape background into it.
I did not want to change my code, I just have one XML layout file and two different background.jpg images in two folders drawable-land-hdpi and drawable-hdpi.
If you're willing to use code to accomplish this, you might try inserting something like
View view = (View) findViewById(R.id.background_carrying_view);
int orientation = getResources().getConfiguration().orientation;
if(orientation == Configuration.ORIENTATION_LANDSCAPE) {
view.setBackgroundResource (R.drawable.background_land);
} else {
view.setBackgroundResource (R.drawable.background_port);
}
into your onCreate() and/or onConfigurationChange().
With this method, you would use the same layout for both landscape and portrait.