0

I would like to create the following layout that is suitable for every type of tablets .I know we could set different setting by placing them in drawable and editing them however is there an alternative to achieve that? The following is my intention to be created for all tablets of 7inch size: enter image description here

the following is my xml code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
    >
<TextView
    android:id="@+id/widget38"
    android:layout_width="480dp"
    android:layout_height="45dp"
    android:background="@color/White"
    android:text="Reminder"
    android:textSize="20dp"
    android:textColor="@color/Black"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true" />

<TextView
    android:id="@+id/label"
    android:layout_width="476dp"
    android:layout_height="70dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/widget38"
    android:background="@color/White"
    android:text="DID you have your Pill?"
    android:textColor="@color/Black"
    android:textSize="40dp" />

<Button
    android:id="@+id/yes"
    android:layout_width="224dp"
    android:layout_height="161dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignRight="@+id/widget38"
    android:layout_below="@+id/label"
    android:background="@color/Green"
    android:text="Yes" />

<Button
    android:id="@+id/no"
    android:layout_width="239dp"
    android:layout_height="161dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/label"
    android:layout_toRightOf="@+id/widget38"
    android:background="@color/Red"
    android:text="No/Cancel" />

</RelativeLayout>

My java code that creates alert:

Button button=(Button)findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {

            @SuppressWarnings("deprecation")
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub


                View view= getLayoutInflater().inflate(R.layout.custom, null);
                final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);


                builder.setView(view);
                // set the custom dialog components - text, image and button
                    TextView t=(TextView)view.findViewById(R.id.widget38);
                TextView text = (TextView)view.findViewById(R.id.label);

                Button yesButton = (Button) view.findViewById(R.id.yes);
                Button noButton=(Button)view.findViewById(R.id.no);



                final AlertDialog dialog=builder.create();  

                noButton.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });



                yesButton.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });

            Display display = getWindowManager().getDefaultDisplay(); 
                int width = display.getWidth();
                int height = display.getHeight();

                dialog.show();
                dialog.getWindow().setLayout(width, height);

Edited the code based on the answer and it shows correctly in eclipse IDE however when tried on the emulator it appears very different. Not able to recitify..Please help. output that appears in nexus 7 emulator however it shows correctly in eclipse xml editor

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
         >

        <TextView 
            android:id="@+id/ReminderMessage"
          android:layout_width="fill_parent"  
          android:layout_height="match_parent"
            android:background="@color/Aqua"
        android:textColor="@color/Black"
    android:textSize="10dp"
            />

    </RelativeLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3" 
        android:baselineAligned="false"
        android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"

        >

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#0F0" >

            <Button
                android:id="@+id/yes"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="Yes"
            android:background="@color/Green"
            android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/ReminderMessage"


                >
            </Button>



        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#F00" >


            <Button
                android:id="@+id/no"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="No/Cancel"
            android:background="@color/Red"
            android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/ReminderMessage"
                >
            </Button>

        </RelativeLayout>

    </LinearLayout>

</LinearLayout>
Vyshakh Amarnath
  • 636
  • 6
  • 17
  • my mistake, I forgot that linear layouts use reverse weighting when rendered: http://stackoverflow.com/questions/8381021/why-does-the-linearlayout-attribute-layout-weight-seem-to-do-the-opposite-of-w . Just switch the weights for the top/bottom pieces and it should work as inteneded – munch1324 Apr 07 '13 at 19:48
  • ah it works now...it shows differently in eclipse but it shows the intended way in the device..was this purposefully done by google or wa s it by mistake?..Once again thanks a lot.... – Vyshakh Amarnath Apr 08 '13 at 10:03
  • I'm not sure to be honest. I know it has fooled me a few times as well :-) – munch1324 Apr 08 '13 at 11:26

1 Answers1

1

Edit: revised to show inverse weighting for linear layouts.

If you want a different look for different sizes, the layout/layout-large/layout-xlarge folders will be the easiest way to define the alert to be specific for different size ranges (ex 7").

If you are using Eclipse as your IDE select "All Screens" to give a good overview of a range of devices.

Other than that, if you have specific ratios in mind, linear layouts are an easy way to define in terms of percents. Here is yours in linearlayouts (it will keep proportions across screen sizes):

enter image description here

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3" >

    </RelativeLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#0F0" >

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#F00" >

        </RelativeLayout>

    </LinearLayout>

</LinearLayout>
munch1324
  • 1,148
  • 5
  • 10
  • Edited based your answer but it is not showing correctly on the device although it shows correctly in eclipse IDE. Details are edited above. – Vyshakh Amarnath Apr 07 '13 at 19:25