getWidth for a child view within DialogFragment

  1. I have a DialogFragment class named which uses a linearlayout based layout named DF.xml.
  2. The child elements (again linearlayouts) in DF.xml have their widths set to 0dp and are calculated using weights.
  3. I plan to show the DialogFragment DF from an activity on a button click, which works fine and the DialogFragment loads fine.

Now, when the DialogFragment DF shows up, I need to get the width of one of its child elements (a linearlayout).

I have tried getWidth, getMeaseuredWidth, ViewTreeObserver etc in onCreateDialog but result is always zero.

Code to show DialogFragment

    DF dialog = DF.newInstance(context, "DFInstance");, "DFInstance");

Code within DialogFragment class

public Dialog onCreateDialog(Bundle savedInstanceState) {

    LayoutInflater inflater = LayoutInflater.from(context);
    View dialog_layout = inflater.inflate(R.layout.dialog_DF,
    CurrentDialog = new Dialog(context,;
            new ColorDrawable(;
    CurrentDialog.setOnShowListener(new OnShowListener() {
        public void onShow(DialogInterface dialog) {
            //Tried getting width here using many approaches, Ex:
            int Width1 = (((Dialog)dialog).findViewById(R.Id.fieldForWidth)).getWidth();
            // Also tried using MeasureSpec here and then getMeasuresWidth
            // Also tried adding above code in ViewTreeObserver here.

    Window window = CurrentDialog.getWindow();
    window.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);;

    return CurrentDialog;

But strangely, if I choose against DialogFragment, and I show the Dialog directly from my main activity, then same code returns width value perfectly within onShow, using getWidth().

But I really need to do this via DialogFragment for the sake of code organization.

Any assistance or pointers as to what I am doing wrong here will be really helpful.


To get the width of the child LinearLayout in the DialogFragment, you can use an OnShowListener together with getMeasuredWidth():

final int width;

myDialog.setOnShowListener(new DialogInterface.OnShowListener()
        public void onShow(DialogInterface dialog)
            width = ((Dialog)dialog).findViewById(;


This works for a child View (just a plain View as well as a LinearLayout) with width set to match_parent or wrap_content.

Please note that in my test I did not call in onCreateDialog() but only used the following lines in the Activity code to show the dialog:

DialogFragment mDialog = DF.newInstance("x", "yz");, "myCurrentDialog");

Need Your Help subscribe to a sub-collection of records

javascript sails.js

I have the following code that queries and listens to comments. Each comment has a page property which stores the comment's page identifier. This identifier is the connection between the comment an...

Embed Windows media player in flex

windows apache-flex flex4 windows-media-player

I have a mp4 file which is to be used in an application. Currently i am in the stage of figuring out the technology to be used for this job. I am familiar with flex and am hoping that i be able to ...