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
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
I am working with OpenCV in android. I am forced to use
android:screenOrientation="landscape"
in my AndroidManifest.xml
The problem I am facing is that I want to rotate only one Button according to screen orientation. The rest must remain un rotated.
Is that possible?
Will the line I added to Manifest File prevent detection of all future rotations?
Yes the line added prevent all rotations.
You can create a new folder under /res , called 'layout-land' (without quotes), copy your layout to these folder and modify. Android read layout folder to protait orientation, and layout-land to show the layout landscape orientation.
you need to provide layout for that.
for Example.
res
layout
main.xml
layout-land
main.xml
Now create your layout you want under -land folder. And it will load when phone is rotate.
Feel free to ask anything.
You can leverage the Position Sensor as described in this article (Position Sensor) to determine the orientation of your phone.
With that, you can get the angle in onSensorChanged() as following.
#Override
public void onSensorChanged(SensorEvent event) {
float azimuth_angle = event.values[0];
float pitch_angle = event.values[1];
float roll_angle = event.values[2];
// Do something with these orientation angles.
}
Hope this helps.
This question already has answers here:
How to support different screen size in android
(9 answers)
Closed 8 years ago.
First, I must say that i am beginner. But have made an application, that works on my Nexus 7 tablet. But i want to make it to work in all devices. Yes, I know, that question has been there before. But I want your opinion, will it work if etc...
public void setLayout()
{
DisplayMetrics dm = new DisplayMetrics(); //avoid this it just gets screen size.
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width=dm.widthPixels;
int height=dm.heightPixels;
int dens=dm.densityDpi;
double wi=(double)width/(double)dens;
double hi=(double)height/(double)dens;
double x = Math.pow(wi,2);
double y = Math.pow(hi,2);
double ss = Math.sqrt(x+y);
if(ss <7) //n7 If screen size is nexus 7, application will set nexus7 layout
{
setContentView(R.layout.screen7);
}
else
{
setContentView(R.layour.default); //user is not using nexus 7 so we will set default layout
}
But i need to run this method in every activity, that need to set layout. Like in MainActivity, instead of using
setContentView(R.layout.activity_main);
I want to use setLayout(); //so method checks what screen size user is using so we can show the best layout available.
I hope you understand what i want to make ;)
Thank you :)
Please let me know, if you want LogCat error.
I just wanted to say that this way you will rediscover fire. You are taking the wrong path. u don't need to do these things in java. You can do all this in just XML files , android than does it for you. Try to use relational positioning of views , not exact pixels. Android has a nice measure called dp and it signifies to some extent as if u are telling the system where to position things in percentage.
Here is a link that helped me understand better(ignore the small dimensions):
http://android-developers.blogspot.com/2011/07/new-tools-for-managing-screen-sizes.html
Another advice i have to give you is try to use RelativeLayout and LinearLayout , you can use match_parent , wrap_content or u can use layout_gravity atribute for your positioning.
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.
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.