Search code examples
androidandroid-layoutandroid-dialogdialog

Set height and width in an Dialog Box?


I am using a XML-layout which I am prompting as the dialog box. Designing of XML-layout is well formatted with enough required height and width.. But when I open it as the dialog box its width is getting disturbed so how to set height and width of dialog box through coding.

I even had referred this previous STACK OVERFLOW QUESTION

Here is the code:

// Layout Inflater Code..
    editDialog = new Dialog(this);
        layoutEdit = LayoutInflater.from(this).inflate(R.layout.createlayout, null);
    
    //layoutEdit.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
    editDialog.setContentView(layoutEdit);

// Called the Dialogbox to inflate

updateButton.setOnClickListener(new View.OnClickListener() {
                
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                editDialog.show();  
                }
            });

// XML File Code:

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



<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/bd"
    android:orientation="vertical" >


    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:freezesText="false"
        android:text="Enter Name"
        
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/whtie"
        android:typeface="monospace" />


    <EditText
        android:id="@+id/txtname"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName" >

    </EditText>


</LinearLayout>    
</ScrollView>

Solution

  • You better use an activity that looks like a dialog (I feel it will be better in your case). Here is an example code:

        public class DialogActivity extends Activity {
    /**
     * Initialization of the Activity after it is first created.  Must at least
     * call {@link android.app.Activity#setContentView setContentView()} to
     * describe what is to be displayed in the screen.
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // Be sure to call the super class.
        super.onCreate(savedInstanceState);
        
        requestWindowFeature(Window.FEATURE_LEFT_ICON);
        
        // See assets/res/any/layout/dialog_activity.xml for this
        // view layout definition, which is being set here as
        // the content of our screen.
        setContentView(R.layout.dialog_activity);
        
        getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, 
                android.R.drawable.ic_dialog_alert);
    }
    }
    

    This code is from api demos