0

I had developed an application. The size i developed was 3.7in WVGA Nexus One.

So when display larger than this screen, it does not have problem. When i display smaller than 3.7in, there problem came.

Here is my xml, i am using fixed px for some widget.

<LinearLayout
    android:id="@+id/layout_content"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/layout_title"
    android:background="@drawable/backrepeat"
    android:gravity="center_vertical|center_horizontal" >

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical|center_horizontal" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center_vertical|center_horizontal" >

            <TableLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:gravity="center_vertical|center_horizontal" >

                <TableRow
                    android:layout_width="fill_parent"
                    android:gravity="center_vertical|center_horizontal"
                    android:paddingBottom="10px"
                    android:paddingTop="10px" >

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:gravity="center_vertical|center_horizontal" >

                        <TextView
                            android:id="@+id/fixtext_name"
                            android:layout_width="80px"
                            android:layout_height="wrap_content"
                            android:gravity="right"
                            android:paddingBottom="10px"
                            android:text="姓名 :"
                            android:textColor="#000000"
                            android:textSize="30px" />

                        <EditText
                            android:id="@+id/text_name"
                            android:layout_width="400px"
                            android:layout_height="wrap_content"
                            android:ems="15"
                            android:gravity="left"
                            android:inputType="textPersonName"
                            android:paddingBottom="10px"
                            android:textSize="30px" >
                        </EditText>
                    </LinearLayout>
                </TableRow>                        
            </TableLayout>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

This is an example of my application layout. The total width is 480px and height using

fill_parent

So, height does not have problem.

<supports-screens
    android:anyDensity="true"
    android:largeScreens="true"
    android:normalScreens="true"
    android:resizeable="true"
    android:smallScreens="true"
    android:xlargeScreens="true"
    android:requiresSmallestWidthDp="480"
    android:compatibleWidthLimitDp="480"
    android:largestWidthLimitDp="480" >
</supports-screens>

This is the support-screen attribute in manifest xml. I had set all already.

The problem was when i display in 2.7in QVGA (something like SG Mini), its only display 240px width only instead of full screen items.

I want to ask how to squeeze or re-size application base on application screen size?

If can, do i need to code same thing for all activity?

Andro Selva
  • 53,910
  • 52
  • 193
  • 240
Alan Lai
  • 1,094
  • 7
  • 18
  • 41

4 Answers4

1

You can specify a new XML file which will only work on small screen.

1) Right click on you project
2) Select new->Android XML file
3) Make a new layout, type THE SAME NAME AS YOUR ORIGINAL FILE and press NEXT (not finish)
4) You can now choose qualifiers - for instance "Size" and specify the XML only suits small screens 5) Once you have specified the qualifiers and their properties press finish.
6) You now have an XML file in a folder called layout-SOMETHING (in this case layout-small)

This new layout can be modified to fit smaller screens without changing anything in the code. Just make sure you have the same ids and the like to avoid NULLPOINTER exceptions.

Hope this helps.

ThomasKJDK
  • 367
  • 1
  • 7
  • 17
  • That mean i got 20 xml also need to create new one for suit small size? For example, original i got a.xml and b.xml then i need to create new one for small_a.xml and small_b.xml. Inside one also need to completely copy without changing – Alan Lai May 11 '12 at 03:09
  • The new layout need to put into res/layout folder? – Alan Lai May 11 '12 at 03:11
  • how about java class? if not implement, application how to determine which folder in use? – Alan Lai May 11 '12 at 03:19
  • If you make separate XML files as described above, android pick the correct file based on the device. The files need to stay in the new folders like layout-small. – ThomasKJDK May 11 '12 at 03:25
  • Alternatively you can make a 'values' file for small screens rather than layout. That way you can use f.ex. @dimen/value1 and then have value1 defined for large screens in res/values/myValues and for small screens in res/values-small/myValues – ThomasKJDK May 11 '12 at 03:28
  • for value file, i don't know how to do it – Alan Lai May 11 '12 at 03:46
  • The same approach. Right-click project->new->android XML. Select values rather than layout. – ThomasKJDK May 11 '12 at 03:50
  • If you only want to scale the XML layout you can just use values-small(dimen-type attributes) without the layout-small. Then in your layout-XML you must use f.ex: android:layout_width="@dimen/value1" – ThomasKJDK May 11 '12 at 03:52
  • if using value-small, then every XML, every widget also need to place f.ex: android:layout_width="@dimen/value1"? – Alan Lai May 11 '12 at 05:42
  • can use post an example of value and layout xml in post? – Alan Lai May 11 '12 at 05:45
1

It looks like you are trying to use "px". This might be the problem. Instead try "dip" for the width and padding attributes in your XML.

Here is a very good example that explains you about the difference between px, dp,dip etc.

What is the difference between "px", "dp", "dip" and "sp" on Android?

Community
  • 1
  • 1
Andro Selva
  • 53,910
  • 52
  • 193
  • 240
1

design new .xml file according to small emulator view. After design .xml file then use this code.

.java file try this:

DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
screenHeight = displaymetrics.heightPixels;
screenWidth = displaymetrics.widthPixels;
screendensity = displaymetrics.densityDpi;
Log.i("screenHeight",""+screenHeight);
Log.i("screenWidth",""+screenWidth);
Log.i("screendensity",""+screendensity);

then apply switch case where you will pass .xml file id.

0

final ViewTreeObserver mLayoutObserver = mLayout.getViewTreeObserver();

mLayoutObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() 
{

    @Override
    public void onGlobalLayout() 
    {
        DisplayMetrics metrics = getResources().getDisplayMetrics();

        int deviceWidth = metrics.widthPixels;

        int deviceHeight = metrics.heightPixels;

        float widthInPercentage =  ( (float) 280 / 320 )  * 100; // 280 is the width of my LinearLayout and 320 is device screen width as i know my current device resolution are 320 x 480 so i'm calculating how much space (in percentage my layout is covering so that it should cover same area (in percentage) on any other device having different resolution

        float heightInPercentage =  ( (float) 300 / 480 ) * 100; // same procedure 300 is the height of the LinearLayout and i'm converting it into percentage

        int mLayoutWidth = (int) ( (widthInPercentage * deviceWidth) / 100 );

        int mLayoutHeight = (int) ( (heightInPercentage * deviceHeight) / 100 );

        LayoutParams layoutParams = new LayoutParams(mLayoutWidth, mLayoutHeight);

        mLayout.setLayoutParams(layoutParams);
    }
});
Andriya
  • 241
  • 2
  • 14