I am writing an application which in portrait view has a gallery at the top and when you click a picture it will inflate and fill the entire screen.
This works however in landscape mode the gallery covers up most of the picture and it in general looks crappy.
I made a GridView for landscape mode. The problem I am having now is to change it from the gallery activity to the Gridview activity when the orientation changes. Any ideas?
The right way to do it is to define two layouts, one in layout-port and one in layou-land directories. That is, you will have the following two layouts:
res/layout-port/main.xml and res/layout-land/main.xml.
In your software you simply write secContentView(R.layout.main); and android will take care of applying the right layout upon device rotation.
Make an extra layout xml file for landscape mode and put it into the folder "layout-land"
Add this to your Activity:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
startActivity(newActivity)
}
Note that this is a bad idea. Actually, it's a really bad idea. A much better alternative would be to either force one orientation or to create a layout that looks great on both orientations.
Related
I have an app in which I override the onConfigurationChanged because I don't want the activity to be destroyed and rebuilt. it works fine.
Now I want a certain part of the layout to change when the orientation is changed. Regularly I would do this with the layout-land folder and that would work, but now it doesn't. The portrait layout stays even when I change to landscape, though it is obviously redrawn, because the items in it change their locations accourding to the new orientation limits.
I think that's because the layout is only redrawn but the resource isn't re-selected from the res folders.
Any idea? (I wouldn't want to use setLayoutResource or something alike, I would rather to use the more general solution).
thanks.
Without onConfigurationChanged android redraw the scene by it's own (it setLayouResource according to the orientation).
When you write onConfigurationChanged you say to OS that you will handle all thing by hands and with onConfigurationChanged android only rotate the corresponding view. If you want to use layout from layout-land the only way is to call setLayouResource (or move views (change it's layout parameters) dynamically - but this way is not very good) and restore all of your data.
In AndroidManifest insert the parameter: android:configChanges="orientation" on the activity.
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.yourlayout);
}
and provide two versions of yourlayout.xml: layout/yourlayout.xml and layout-land/yourlayout.xml. You can add more - for different sizes, etc.
Don't forget to set android:configChanges="orientation|screenSize", otherwise your activity may be reloaded.
Should I put xml landscape layout files for ALL the activities in the same layout-land folder?
If there are 3 activities and 3 landscape layout, then how should I name them in layout land folder to be recognized?
I have 2 activities - A and B, they each have 2 lanscape layout in layout-land folder but only activity A can find the layout for landscape, whereas activity B finds the landscape layout for activity A and use it. Why? How to map the landscape layout to its activities?
//ACTIVITY- A
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent bmr= new Intent(TdeeActivity.this,BMRActivity.class);
startActivityForResult(bmr,CALLED_ACTIVITY);
}
#Override
protected void onResume() {
super.onResume();
setContentView(R.layout.activity_tdee);
Button ok=(Button)findViewById(R.id.btnOk);
rdActLevel=(RadioGroup) findViewById(R.id.rd);
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
showResult();
}
});
}
You give them the same name as the one in portrait mode, as long as you put them in the right folder (layout-land) they will be loaded automatically.
And yes you do need to provide a different layout for each activity in landscape if it makes sense.
Android Activity Automatically recognized the layout land. If you put same name layout in android layout-land and portrait mode and load this layout in you activity class then then android automatically recognized it.
Thanks.
It is not a rule, but it is better to anticipate the user experience. If you did not include one landscape layout, Android will resize it automatically when phone is rotated. This may cause bad user experience (like image stretching, bad Widget size or overlapping). If you test your App on many screen sizes in both orientations and are satisfied with the result, then you do not need to add one for every layout.
For the Activity relation, there is no link between number of Activities and number of Layouts (you may have many Activities with one Layout, with minimal changes done via code). It really depends on what each Layout represents.
Fore the last question, you need to name the xml files with the exact same name in layout-land as in layout folder for Android to map them correctly.
I have no idea of this is actually possible, but i want the device to have different activities for landscape and portrait.
I have a listview, and of all the items in my listview i have the coordinates. So i thought, it would be nice if you put your device in landscape, and show a mapview with icons placed on the locations of the items.
I know you can create different layouts for different orientations, but only creating an mapview and using that as landscape layout wont do the trick i guess. Is there something possible in an equivalent way for activities? I couldn't find it, so probably not.
Else i think this might work:
I thought myself of a switch in the activity: on portrait --> do this and load this layout, on landscape --> do that and use the other layout. But this would only work once if put in the oncreate. But than the orientationlistener would do the trick. Anyone knows of this is possible?
There is a method in activity lifecycle
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Check the Orientation of Device
// Start Other Activity
...
...
...
}
you can use this method to change the activity if the device orientation changes.
IMHO we can't use 2 different activities here, but there is a way,
please check out how to detect orientation dynamically here,
check-orientation-on-android-phone
once you are through this, you can include code for landscape mode here in the same activity.
I have created two folders layout and layout-land with two xml files, one for portrait and the other for landscape. Both of the xmls work but here is the problem.
My first screen is a login screen and my second screen is a main screen. If I login in portrait and then turn my phone landscape at the main screen. The layout will landscape turn but it uses the portrait xml for the main screen.
The same error occurs if I start in landscape and try to move to portrait later on.
It seems like whatever layout I do for my main then that's the layout that will be used for the rest of the app. Is there anyway to go around this?
Also. I'm already using android:configChanges="orientation" in my manifest for the activities.
If you are using android:configChanges="orientation", then you can override onConfigurationChanged to inflate the new layout after a configuration change.
#Override
protected void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(...);
}
Make sure you have a good reason for preventing the Activity from being recreated on an orientation change... and most importantly don't just do it because orientation changes are crashing your app. Handling the configuration change yourself can make it much more difficult to use alternative resources, because the system does not automatically apply them for you. This technique should be considered a last resort when you must avoid restarts due to a configuration change and is not recommended for most applications.
Using android:configChanges="orientation" means you will be handling the orientation change in code. If you want it to automatically switch layouts, you shouldn't have that there.
Make sure both xml files present in two different folders have same name.
I build my Android Application and want do add layouts for different orientations now. I created a layout-land folder and put a different layout for my first Starter Activity "myStartActivity" (with the same name as the layout i used before for both orientations) in there.
Depending on my Screen Orientation BEFORE i start the app the right layout is chosen: "myLayout.xml" from within the "layout"-folder when i start in portrait and the "myLayout.xml" from within the "layout-land"-folder when i start in landscape.
The Problem is, that when i rotate the device when I'm already in the Activity, after rotation I dont get the new layout. For example: rotating from portrait to landscape it stills shows "myLayout.xml" from within the "layout"-folder and not the "layout-land"-folder as it should.
I didnt overwrite any OnConfigurationChange Methods or anything. All I do in "myStartActivity" is instantiate some buttons and give them some listeners. I wanna use a different layout in landscape to change the ordering of the buttons.
In my case the described problem happens only when I have android:configChanges="orientation"
in the activity in the manifest.
Otherwise the correct layout is automatically used when rotating.
What you could do is use Activity.getResources().getConfiguration().orientation
Then depending on the orientation result, set the content view in your oncreate() method which is called on rotation.
protected void onCreate(Bundle savedInstanceState) {
int result = this.getResources().getConfiguration().orientation;
if (result == 1){
//set content view to portrait
setContentView(R.layout.portrait);
}
else{
//set content view to landscape}
setContentView(R.layout.landscape);
}
Or stick in a case statement :)
If you're testing on the emulator only, there may be problems with detecting orientation change. I have experienced that at least.