I have two background images. I want Image A to display when the screen is horizontal and Image B to display when the screen is vertical. Any easy way of achieving this?
Do this
Result Values for Oreintations will be:-
Portrait == 1
Landscape == 2
int i = context.getResources().getConfiguration().orientation;
if (i == Configuration.ORIENTATION_PORTRAIT) {
yourimg.setBackground(yourAimage);
} else {
yourImg.setBackground(yourBimage);
}
you can create two xml layouts here one for landscape and other for portrait. In portrait xml you can add different images for background. By this we can use two different layouts for two orientations for a single activity. see the below picture, hope it should help.
take two images A & B. save image A in drawable-land and image B in drawable-port
note that the images are saved with same names..
you can check this in graphic layout also..
Create each XML for horizontal & Vertical
Create one more folder name "layout-land"
n just coy & paste the main.xml(your layout file) and just change the background respectavely.
you'll done...
Related
I´ve made an app where´s just some text and a background image.
When I tilt my phone my background image gets streched out..
What I want to do is when I tilt the phone I get now background image.
What you need to do is create different layouts for your views in landscape and portrait mode and put the landscape ones in res/layout-land and the portrait ones in res/layout-port. Simply get your current XML file and copy it into both the portrait and landscape stalks and then just change the drawable that is referenced for the background. That way you can have one that fits well for portrait and another that you've edited to fit for landscape views.
you need to provide a different layout for that orientation.
create a layout-landscape folder, and in that layout change the background to the one you want for your landscape layout. besides you can completely customize your landscape orientation that way.
yes you can change the background
for that you have to create another xml file with same name of the xml you are using right now
then copy all the code from your previous xml to this new xml
you have to create this xml inside the land named folder,if that folder dosent exist than crete a folder name land inside your layout folder..
you just have to change one thing in your xml and here you gooo
android:background="#drawable/yournewbackgroundname"
do not change any thing in our old xml
I want to make independent layout from screen orientation.
Something like this:
(source: userapi.com)
Okay so what you want has nothing to do with the landscape mode, you just want the button text such that it seems that those are in landscape mode. I can suggest something which is kind of a hack.
For that you can have the buttons as usual with more height and less width and align them with left and the right edge of the screen.
For button text you need to make a .jpg or a .png of your text, rotate it and set that as the background for the button.(if you change button text dynamically then you need other snaps of text). This wont look good but it is one of the ways to do what you looking for.
You mean, you want to have seperate layout files for portrait and landscape views ?
Then you should create two different files each in layout and layout-land directory under res.
res
|_ layout
|_ layout-land
Android will call the appropriate layout based on the device configuration.
To retain the data, you may use
#Override
public Object onRetainNonConfigurationInstance() {
Bundle bundle = new Bundle();
return bundle;
}
and get the bundle in onCreate()
final Bundle savedBundle = (Bundle)getLastNonConfigurationInstance();
can any one tell me it is possible to show different number of columns in listview
For Example:- when i see listview in portrait mode it shows 3 columns and when i see listview in landscape mode it shows 4 columns . i try to do it but my solutions doesn't work for me , i define to different layout for protrait in layout folder and landscape in layout-land folder but it doesn't seens to work for me .
Can any one tell me is it possible to achive this .
And one More thing it possible to execute some code by checking
For Example:- if(this is android phone){
execute this code;
}
if(this is android tablet){
execute this code;
}
like iphone and ipad developer do
if(this is iphone )
{
execute this code ;
}
if(this is ipad )
{
execute this code ;
}
because i same application is used in both phone and tablet and some one wants to show different things in phones and different in tablets .
Thanks in advance
Try this below link for tutorial
http://www.technotalkative.com/android-multi-column-listview/
Thanks..!
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
this gives the dimensions of the screen and based on these values you can determine if it is a tablet..
See this link to know how to get dimensions of your device
Create more layouts with same name in /res/layout
After selecting root element click "Next" and then choose one of the Available qualifiers (orientation) and define listView with its properties.
Can we fix the orientation for one imageview in framelayout, and orientation of other children of frame layout be controlled by the sensor?
I want to fix the orientation of image in background and on top of that need to show some reults in textviews. If I fix the activity in landscape then I can't draw the text portrait wise.
You can use separate xml for portrait and landscape mode.
1.you have to create separate folders layout-land, layout-port
2.save the xml in it.
3.Be sure with both have the same name.
You can refer here
Orientation can only be applied on activities,we cannot set the orientation of views.
I have an activity that has two layouts one in the folder layout and the other one in
layout-large, (one is for the phone factor and the other one for the tablet form). How
do I detect which layout is being loaded since in the tablet form I display more data?
Thanks
EDIT:
I know that in the tablet it will load the layout-large but how do I know that I am running in something with a tablet form factor?
You can detect it programmatically:
(getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE;
getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
and with other methods and properties in getResources().getConfiguration()
A simple approach would be to keep a Text view with visibility set as GONE. Keep text property for each layout different. In your code check for this view and identify which layout has been loaded.