4

I have an activity with ListView that has:

android:theme="@android:style/Theme.Dialog"

in Manifest. When I open it and when it has only one line in ListView, the window that opens is very small. How do I make the window take the whole screen?

OkyDokyman
  • 3,786
  • 7
  • 38
  • 50

6 Answers6

7

Use this in your onCreate method of the Activity to make it full screen.

   @Override
protected void onCreate(Bundle arg0) {
    super.onCreate(arg0);
    setContentView(R.layout.myxml);

    LayoutParams params = getWindow().getAttributes(); 
            params.height = LayoutParams.MATCH_PARENT;
            params.width  = LayoutParams.MATCH_PARENT;
           getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
   } 
PravinCG
  • 7,688
  • 3
  • 30
  • 55
  • Solution do not work. Maybe it's because of the Dialog Theme with the ListView? Any ideas? – OkyDokyman May 11 '11 at 20:03
  • Thanks! My Activity (made to look like a dialog via the Manifest) had a fairly complicated LinearLayout and was displaying it just a few letters wide. But wow, it worked for me! But why? – SMBiggs Oct 19 '12 at 05:48
  • Please check comments below for changes. The answer is over two years old and may require some edits. – PravinCG Apr 18 '14 at 05:51
3

I have found that setting the window size does work, but you have to do it a bit later. In this example the window width is set to 90% of the display width, and it is done in onStart() rather than onCreate():

@Override
protected void onStart() {
   super.onStart();
   // In order to not be too narrow, set the window size based on the screen resolution:
   final int screen_width = getResources().getDisplayMetrics().widthPixels;
   final int new_window_width = screen_width * 90 / 100; 
   LayoutParams layout = getWindow().getAttributes();
   layout.width = Math.max(layout.width, new_window_width); 
   getWindow().setAttributes(layout);
}
ahcox
  • 9,349
  • 5
  • 33
  • 38
1

Similar to the answer from PravinCG but it can be done with one line in onCreate()...

getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
Squonk
  • 48,735
  • 19
  • 103
  • 135
0

Just a small update. Used MATCH_PARENT instead of the deprecated FILL_PARENT. PravinCG's answer worked great for me.

Kaptkaos
  • 333
  • 3
  • 6
0

Yeezz ! I figured it out ! The problem is that the margin sizes are not calculated in the window widht. So If you set the layout margin to 0 and move that part to the padding of the layout the problem will be solved.

0

Use the suggested code before setcontentview() call. It will work.

nawab
  • 362
  • 4
  • 9