Android dialog is compressed

684 views Asked by At

I am using a layout to show dialog. In my xml design it is perfect but when i open it in the app, it is scrambled.. Below are the screen shots:

The xml file design:

xml

And the dialog in mobile:

mobile

my xml code is:

<?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"
android:weightSum="11"
android:background="#ffffff">

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

    <TextView
        android:id="@+id/textView_goal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Set You Goal with "
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="4" >
    </RelativeLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="horizontal"
    android:weightSum="2" >

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

        <EditText
               android:id="@+id/goal_value_1"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
               android:layout_centerVertical="true"
              android:ems="10"
              android:layout_marginLeft="40dp"
              android:inputType="phone" >

            <requestFocus />
        </EditText>

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

         <TextView
             android:id="@+id/goal_unit_1"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
            android:layout_centerInParent="true"
             android:text="mm Hg"
             android:textAppearance="?android:attr/textAppearanceMedium" 
     />

    </RelativeLayout>

   </LinearLayout>
    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.5"
    android:orientation="horizontal"
    android:weightSum="2" >
     </LinearLayout>
    <LinearLayout
    android:id="@+id/hide_lay"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="horizontal"
    android:weightSum="2" >
      <RelativeLayout
        android:layout_width="0dp"
        android:layout_weight="1.5"
        android:layout_height="match_parent" >

          <EditText
              android:id="@+id/goal_value_2"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:inputType="phone"
              android:layout_marginLeft="40dp"
              android:layout_centerHorizontal="true"
              android:layout_centerVertical="true"
              android:ems="10" />

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

         <TextView
             android:id="@+id/textView2"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_centerInParent="true"
             android:text="mm Hg"
             android:textAppearance="?android:attr/textAppearanceMedium"
         />

       </RelativeLayout>
     </LinearLayout>
    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.5"
    android:orientation="horizontal"
    android:weightSum="2" >
     </LinearLayout>

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

   <Button
     android:id="@+id/button_goal_set"
     android:layout_width="match_parent"
     android:layout_marginLeft="40dp"
     android:layout_height="wrap_content"
     android:layout_centerHorizontal="true"
     android:layout_centerVertical="true"
     android:text="Set"
     android:textColor="#ffffff"
     android:background="#6FD0EA" />

   </RelativeLayout>



  </LinearLayout>

And my java code is :

final Dialog d = new Dialog(AddMeasurements.this);

            //tell the Dialog to use the dialog.xml as it's layout description
            d.setContentView(R.layout.measurement_goal);
            d.setTitle("Set Goal");
            final EditText value = (EditText) d.findViewById(R.id.goal_value_1);
            final EditText value2 = (EditText) d.findViewById(R.id.goal_value_2);
            goal=(TextView)d.findViewById(R.id.textView_goal);
            goal.setText("Set your goal for "+getIntent().getStringExtra("mname"));
            LinearLayout l=(LinearLayout)d.findViewById(R.id.hide_lay);
            l.setVisibility(View.GONE);
            final TextView unit=(TextView)d.findViewById(R.id.goal_unit_1);
            unit.setText("mg/dl");
            Button send = (Button) d.findViewById(R.id.button_goal_set);

              send.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {

                    sp.edit().putString("Blood Glucose", value.getText().toString()+" "+unit.getText().toString()).commit();
                    d.dismiss();
                }

            });
              d.show();

Ive tried every thing, but could not fix this dialog, please help Thanx

2

There are 2 answers

0
Pankaj On BEST ANSWER

Change your dialog box code with below code and it would would work for sure

final Dialog d = new Dialog(MainActivity.this);

        //tell the Dialog to use the dialog.xml as it's layout description
        d.setContentView(R.layout.dialog);
        d.setTitle("Set Goal");

       //Added this lines
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.copyFrom(d.getWindow().getAttributes());
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;


        final EditText value = (EditText) d.findViewById(R.id.goal_value_1);
        final EditText value2 = (EditText) d.findViewById(R.id.goal_value_2);
        TextView goal=(TextView)d.findViewById(R.id.textView_goal);
        goal.setText("Set your goal for "+getIntent().getStringExtra("mname"));
        LinearLayout l=(LinearLayout)d.findViewById(R.id.hide_lay);
        l.setVisibility(View.GONE);
        final TextView unit=(TextView)d.findViewById(R.id.goal_unit_1);
        unit.setText("mg/dl");
        Button send = (Button) d.findViewById(R.id.button_goal_set);

          send.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {

//                sp.edit().putString("Blood Glucose", value.getText().toString()+" "+unit.getText().toString()).commit();
                d.dismiss();
            }

        });
          d.show();
          //added this line also
          d.getWindow().setAttributes(lp);
4
Preethi Rao On

try edit your linear layout to this

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="11"
android:background="#ffffff">

and try editing all the height and width property of other view elements no where it should be dependent on the parent dialogue layout.

And also add this line in your dialogue code

dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);