Search code examples
javaandroidandroid-dialogfragment

Dialogfragment positioning


Having a problem with positioning dialogfragment in Android 4.1, on other versions of Android OS everything is okay, problem appears only in 4.1. So what I'm trying to do is position fragment in bottom of the screen, but in Android 4.1 it looks like this with such whitespace: White space at bottom of the screen

And my code:

public class ContactsDialog extends DialogFragment {

String contactNumber = null;
String skypeContact = null;
String userID = null;
int flatsCount = 0;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    TextView contactTV, skypeTV;

    View layout = inflater.inflate(R.layout.contacts_dialog, container);

    contactTV = (TextView) layout.findViewById(R.id.contactNumber);
    skypeTV = (TextView) layout.findViewById(R.id.skypeName);
    RelativeLayout skypeContactsLayout = (RelativeLayout) layout.findViewById(R.id.skypeContactsLayout);
    RelativeLayout contactsLayout = (RelativeLayout) layout.findViewById(R.id.contactsLayout);
    RelativeLayout allUserFlats = (RelativeLayout) layout.findViewById(R.id.allUserFlats);

    contactNumber = getArguments().getString("contactNumber", null);
    skypeContact = getArguments().getString("skypeName", null);
    userID = getArguments().getString("userID", null);
    flatsCount = getArguments().getInt("flatsCount");

    if(TextUtils.isEmpty(skypeContact)) {
        skypeContactsLayout.setVisibility(View.GONE);
    }
    if (TextUtils.isEmpty(userID)) {
        allUserFlats.setVisibility(View.GONE);
    }
    if (flatsCount == 1) {
        allUserFlats.setVisibility(View.GONE);
    }

    String convertedNumber = convertNumber(contactNumber);

    contactTV.setText(convertedNumber);
    skypeTV.setText(skypeContact);

    WindowManager.LayoutParams params = getDialog().getWindow()
            .getAttributes();
    params.gravity = Gravity.BOTTOM | Gravity.CENTER;
    getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    getDialog().getWindow().setAttributes(params);

    contactsLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_CALL);
            intent.setData(Uri.parse("tel:" + contactNumber));
            startActivity(intent);
        }
    });

    allUserFlats.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getActivity(), MainActivity.class);
            intent.putExtra("userID", userID);
            getActivity().finish();
            startActivity(intent);
        }
    });
    skypeContactsLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if(isSkypeClientInstalled(getActivity())){
                Intent sky = new Intent("android.intent.action.VIEW");
                sky.setData(Uri.parse("skype:" + skypeContact));
                startActivity(sky);
            } else {
                Toast.makeText(getActivity(), "Skype не установлен", Toast.LENGTH_LONG).show();
            }


        }
    });

    return layout;
}

public boolean isSkypeClientInstalled(Context myContext) {
    PackageManager myPackageMgr = myContext.getPackageManager();
    try {
        myPackageMgr.getPackageInfo("com.skype.raider", PackageManager.GET_ACTIVITIES);
    }
    catch (PackageManager.NameNotFoundException e) {
        return (false);
    }
    return (true);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(DialogFragment.STYLE_NO_FRAME, R.style.CustomDialog);
}

@Override
public void onStart() {
    super.onStart();
    getDialog().getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
    getDialog().setCanceledOnTouchOutside(true);

}

private String convertNumber(String number) {
    StringBuffer sb = new StringBuffer(number);
    sb.insert(2," (");
    sb.insert(7,") ");
    sb.insert(12,"-");
    sb.insert(15,"-");
    return String.valueOf(sb);
}
}

And also I have the next Layout file

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent" android:layout_height="wrap_content">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingTop="14dp"
        android:background="@color/whiteTextView"
        android:id="@+id/contactsLayout">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="6dp"
            android:layout_marginTop="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginStart="16dp"
            android:layout_marginBottom="16dp"
            android:background="@drawable/phone_grey1"
            android:id="@+id/phoneImage" />

        <TextView
            android:id="@+id/contactNumber"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/blackTextView"
            android:text="Все"
            android:textSize="18sp"
            android:layout_above="@+id/contactTV"
            android:layout_alignLeft="@+id/contactTV"
            android:layout_alignStart="@+id/contactTV" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/contactsString"
            android:textSize="12sp"
            android:textColor="@color/greyTextView"
            android:layout_marginLeft="25dp"
            android:layout_marginStart="25dp"
            android:id="@+id/contactTV"
            android:layout_alignBottom="@+id/phoneImage"
            android:layout_toRightOf="@+id/phoneImage"
            android:layout_toEndOf="@+id/phoneImage" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/skypeContactsLayout"
        android:background="@color/whiteTextView"
        android:layout_below="@+id/contactsLayout"
        android:layout_centerHorizontal="true">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="6dp"
            android:layout_marginTop="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginStart="16dp"
            android:layout_marginBottom="16dp"
            android:background="@drawable/skype1"
            android:id="@+id/skypeImage" />

        <TextView
            android:id="@+id/skypeName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/blackTextView"
            android:text="Все"
            android:textSize="18sp"
            android:layout_above="@+id/skypeTV"
            android:layout_alignLeft="@+id/skypeTV"
            android:layout_alignStart="@+id/skypeTV"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/skypeString"
            android:textSize="12sp"
            android:textColor="@color/greyTextView"
            android:layout_marginLeft="25dp"
            android:layout_marginStart="25dp"
            android:id="@+id/skypeTV"
            android:layout_alignBottom="@+id/skypeImage"
            android:layout_toRightOf="@+id/skypeImage"
            android:layout_toEndOf="@+id/skypeImage" />

    </RelativeLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/whiteTextView"
        android:paddingBottom="14dp"
        android:id="@+id/allUserFlats"
        android:layout_below="@+id/skypeContactsLayout"
        android:layout_centerHorizontal="true">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="6dp"
            android:layout_centerVertical="true"
            android:layout_marginTop="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginStart="16dp"
            android:layout_marginBottom="16dp"
            android:background="@drawable/hamburger1"
            android:id="@+id/otherOffersImage" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/allFlats"
            android:layout_marginLeft="25dp"
            android:layout_marginStart="25dp"
            android:textSize="18sp"
            android:textColor="@color/blackTextView"
            android:id="@+id/otherOffersTV"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/otherOffersImage"
            android:layout_toEndOf="@+id/otherOffersImage" />

    </RelativeLayout>


</LinearLayout>

And my style file

<style name="CustomDialog" parent="@android:style/Theme.Holo.Light" >
    <item name="android:windowIsFloating">true</item>
</style>

Solution

  • First of all try setting:

    params.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
    

    instead of

    params.gravity = Gravity.BOTTOM | Gravity.CENTER;
    

    Also, I think this is not really necessary:

    getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    

    since your style DialogFragment.STYLE_NO_FRAME already disables TITLE area.

    If this doesn't help, pls paste your CustomDialog style.

    EDIT:

    try changing your style to:

    <style name="CustomDialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowBackground">@android:color/white</item>
        <item name="android:background">@android:color/transparent</item>
    </style>