Modern mobile devices are basically meant to be rotated. There is actually two concrete and fairly different ways to handle it. Indeed, we can rotate the graphical interface of our activities because the user has rotated his/her phone, or we can do it programmatically to fit our needs. Thus, we will see both ways of handling rotation.
If you are only using built-in elements, they will all have a default look and feel for the landscape mode. Therefore, if you rotate the example used in the previous AutoCompleteTextView
example, you will see something looking like the following screenshot:
The thing is that you can create a new folder named Layout-land
under the Resource
folder and create another .axml
file inside it. To summarize, you will have two different Main.axml
files for MainActivity.axml
. One will be under the Layout
folder and the other will be under the Layout-land
folder. When the phone is in the portrait mode, the .axml
file under layout will be used, whereas the one under the Layout-land
folder will be used when the phone is rotated.
Copy the corresponding .axml
file under the Layout-land
folder that you just created under the Resource
folder, and modify something in it similar to the text value with something remarkable, such as a land
prefix:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="5dp" android:id="@+id/linearLayout1"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="States-land" android:id="@+id/textView1"/> <AutoCompleteTextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/autoCompleteTextView1" android:layout_marginLeft="5dp" /> </LinearLayout>
When rotating your phone or your emulator, you will see the text change, as shown in the following screenshot:
Clearly, there is a simple way to get things done programmatically. First of all, you can test the current orientation using the following code:
TextView tv = FindViewById<TextView> (Resource.Id.textView1); var phoneMode = WindowManager.DefaultDisplay.Rotation; if (phoneMode == SurfaceOrientation.Rotation0) { tv.Text = "Not in Landscape"; } else if (phoneMode == SurfaceOrientation.Rotation180) { tv.Text = "In Landscape"; }
You can even force the phone into landscape (or the portrait mode) by using the phoneMode
variable with the desired variable as follows:
surfaceOrientation = SurfaceOrientation.Rotation180;
Android is known to have the not-so-good habit of restarting activities when the phone gets rotated. In general manners, we do not want our activities to be restarted in order to save the state of our activities without saving it, making the activity look responsive even during rotation. The only way do this is to redefine the declaration of the activity with the following code:
[Activity (Label = "GraphicalInterface", MainLauncher = true, ConfigurationChanges=Android.Content.PM.ConfigChanges.Orientation)]
Once you have completed this, you must redefine the method: OnConfigurationChanged()
:
public override void OnConfigurationChanged (Android.Content.Res.Configuration newConfig) { base.OnConfigurationChanged (newConfig); TextView tv = FindViewById<TextView> (Resource.Id.textView1); if (newConfig.Orientation == Android.Content.Res.Orientation.Portrait) { tv.Text = "Not in Landscape"; } else if (newConfig.Orientation == Android.Content.Res.Orientation.Landscape) { tv.Text = "In Landscape"; } }
Notice that the way of detecting the orientation of the phone slightly differs in this redefinition. Also, as mentioned when studying Active Lifestyles, we must always call the base implementation in order to avoid fatal failures.
As usual, we can save states during rotation using code that overrides the OnSaveInstanceState()
method (shown in the following code sample) and calls the bundle.GetInt ("someValue");
method to retrieve data:
protected override void OnSaveInstanceState (Bundle outState) { outState.PutInt ("SomeKey", SomeValue); base.OnSaveInstanceState (outState); }
18.118.145.114