getWidth for a child view within DialogFragment

  1. I have a DialogFragment class named DF.java 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");
    dialog.show(((Activity)context).getFragmentManager(), "DFInstance");

Code within DialogFragment DF.java class

public Dialog onCreateDialog(Bundle savedInstanceState) {

    LayoutInflater inflater = LayoutInflater.from(context);
    View dialog_layout = inflater.inflate(R.layout.dialog_DF,
            null);
    CurrentDialog = new Dialog(context, R.style.DFTheme);
    CurrentDialog.setCanceledOnTouchOutside(true);
    CurrentDialog.setContentView(dialog_layout);
    CurrentDialog.getWindow().setBackgroundDrawable(
            new ColorDrawable(android.graphics.Color.TRANSPARENT));
    CurrentDialog.setOnShowListener(new OnShowListener() {
        @Override
        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);
    CurrentDialog.show();

    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.

Answers


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()
    {
        @Override
        public void onShow(DialogInterface dialog)
        {
            width = ((Dialog)dialog).findViewById(R.id.field_for_width).getMeasuredWidth());

        }
    });

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 CurrentDialog.show() in onCreateDialog() but only used the following lines in the Activity code to show the dialog:

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

Need Your Help

sails.io.js: subscribe to a sub-collection of records

javascript sails.js sails.io.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 ...