View turns blank when sliding out G1 keyboard

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

View turns blank when sliding out G1 keyboard

Postby acrosser » Tue Mar 17, 2009 12:38 am

I hate to post my problem and then copy & paste a bunch of code for some poor-yet-helpful soul [who knows more about Android development than I do] to wade through, but I've searched high and low for an answer and can't seem to find one!

I'm working on a New Game - Player Selection layout. It consists of a single RelativeLayout inside a ScrollView that takes up the whole width and height. The RelativeLayout will have several TextViews, EditTexts, and Spinners. These will allow the user to select the number, type and name for each player in the game.

The layout looks just fine in Portrait screen orientation. But once I slide out the hardware keyboard of the G1 to type in a player's name, the entire view goes completely blank / black (well, the dark gray color of the background).

All widgets/views disappear, and I can only see the Android status bar at top and the application title bar.
If I slide the keyboard back in, the screen remains the same... all dark gray with none of the TextViews, EditTexts, Spinners visible. If I press Home, then go back into the application, then after a few seconds the view is back to how it should look... but only in portrait mode with the keyboard slid in. I can't get it to display the widgets in landscape mode.

Here's sky_new_game.xml:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  4. <ScrollView xmlns:android=""
  5.    android:layout_width="fill_parent"
  6.    android:layout_height="wrap_content"
  7.    android:scrollbars="none">
  11. <RelativeLayout
  12. android:id="@+id/new_game_rel_layout"
  13. android:layout_width="fill_parent"
  14. android:layout_height="fill_parent"
  15. xmlns:android=""
  16. >
  18. <TextView
  19. android:id="@+id/txt_new_game_title"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:text="@string/new_game_title"
  23. android:layout_alignParentTop="true"
  24. android:layout_alignParentLeft="true" android:paddingBottom="20px" android:textSize="20sp" android:textStyle="bold" android:paddingLeft="5px"/>
  27. <!--  PLAYER 1 -->
  28. <TextView
  29. android:id="@+id/txt_p1"
  30. android:layout_width="wrap_content"
  31. android:layout_height="wrap_content"
  32. android:text="@string/p1"
  33. android:layout_below="@+id/txt_new_game_title"
  34. android:layout_alignParentLeft="true"
  35. android:paddingBottom="5px"
  36. android:paddingLeft="5px"
  37. android:paddingTop="10px"
  38. android:textSize="16sp"
  39. android:textStyle="bold"
  40. android:paddingRight="30px"/>
  41. <Spinner
  42. android:id="@+id/spin_p1"
  43. android:layout_width="fill_parent"
  44. android:layout_height="wrap_content"
  45. android:layout_below="@+id/txt_new_game_title"
  46. android:layout_toRightOf="@+id/txt_p1"
  47. android:drawSelectorOnTop="true"
  48. android:prompt="@string/p1_prompt"
  49. />
  50. <TextView
  51. android:id="@+id/txt_p1_name"
  52. android:layout_width="wrap_content"
  53. android:layout_height="wrap_content"
  54. android:text="@string/p_name"
  55. android:layout_below="@+id/spin_p1"
  56. android:layout_alignParentLeft="true"
  57. android:paddingBottom="5px"
  58. android:paddingLeft="5px"
  59. android:paddingTop="10px"
  60. android:textSize="16sp"
  61. android:textStyle="bold"
  62. android:paddingRight="30px"/>
  63. <EditText
  64. android:id="@+id/edit_txt_p1_name"
  65. android:layout_width="fill_parent"
  66. android:layout_height="wrap_content"
  67. android:text="@string/p1_name"
  68. android:textSize="18sp"
  69. android:layout_below="@+id/spin_p1"
  70. android:layout_alignLeft="@+id/spin_p1"
  71. />
  73. <Button
  74. android:id="@+id/but_start_game"
  75. android:layout_width="wrap_content"
  76. android:layout_height="wrap_content"
  77. android:layout_alignParentRight="true"
  78. android:layout_below="@+id/edit_txt_p1_name"
  79. android:text="@string/start_game"
  80. />
  82. </RelativeLayout>
  84. </ScrollView>
Parsed in 0.006 seconds, using GeSHi

And to display this layout I am simply calling setContentView():

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. setContentView(R.layout.sky_new_game);
Parsed in 0.011 seconds, using GeSHi

I know you can create the folder layout-land under the res folder, and put an alternate layout XML file in there to handle the landscape orientation, but I'd like to avoid this if possible because as far as I can tell the original version I posted above should work just fine. It's using a RelativeLayout which should adjust to the different screen resolution, unless I am mistaken.

Does this problem have to do with using a ScrollView? Is there something else I am missing? Any help would be greatly appreciated! :)
Devices: AT&T Fuze (Raphael / HTC Touch Pro), T-Mobile/HTC/Google G1
Location: Oregon, USA
Posts: 2
Joined: Wed Feb 25, 2009 7:48 am


Figured it out

Postby acrosser » Tue Mar 17, 2009 3:28 am

...Guess I hadn't searched hard enough for an answer! I determined what the problem was. Hopefully this will help anyone in the future.

I found the solution here:

"Sure. Put one layout in res/layout and another in res/layout-land. Here, layout-land effectively means "use this layout when the device is in landscape orientation".

When the device is rotated, your current activity will be closed (shortly after onSaveInstanceState() is called) and then restarted with the appropriate layout set. Reload your widgets from the instance state supplied to onCreate()/onRestoreInstanceState(), and you're set. "

Because I had copied and pasted code from another application (the Snake app from the Samples) that overrode the following functions:

public void onCreate(Bundle savedInstanceState),
protected void onPause(), and
public void onSaveInstanceState(Bundle outState)

...the OS was closing my activity and restarting it. But I hadn't finished implementing all three functions above, and so my activity was getting restarted but I didn't have any code running to reload the widgets and saved data.

For now I am just going to comment out onPause() and onSaveInstanceState() and implement them later, so the OS will handle orientation changes for me (until I can get more done, to the point where I need to save and restore states).
Devices: AT&T Fuze (Raphael / HTC Touch Pro), T-Mobile/HTC/Google G1
Location: Oregon, USA
Posts: 2
Joined: Wed Feb 25, 2009 7:48 am


Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 3 guests