Margin attributes not working inside a custom DialogFragment
Asked Answered
P

1

5

I have some EditTexts inside the layout I have defined for the view of a custom DialogFragment. I put margins to the right of them. But its not reflecting when the DialogFragment is rendered on the screen.

login_dialog.xml

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

    <Button
        android:id="@+id/loginDialogCloseButton"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@drawable/btn_dialog_disable"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="10dp"
        android:layout_marginRight="20dp"


        />

    <TextView 
        android:id="@+id/loginNameTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="NAME"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="10dp"
        android:layout_alignBottom="@+id/loginNameEditText"

        android:layout_marginBottom="10dp"
        android:textStyle="bold"
        android:textColor="@android:color/darker_gray"        
        />

    <EditText
        android:id="@+id/loginNameEditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/loginPasswordEditText"
        android:layout_alignParentRight="true"
        android:layout_marginRight="20dp"
        android:layout_below="@+id/loginDialogCloseButton"

        android:layout_marginTop="20dp"
        android:layout_toRightOf="@+id/loginNameTextView" />

     <TextView 
        android:id="@+id/loginPasswordTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="PASSWORD"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="10dp"
        android:layout_alignBottom="@+id/loginPasswordEditText"        
        android:layout_marginBottom="10dp"
        android:textStyle="bold"
        android:textColor="@android:color/darker_gray"        
        />
    <EditText
        android:id="@+id/loginPasswordEditText"
        android:inputType="textPassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/loginNameEditText"
        android:layout_toRightOf="@+id/loginPasswordTextView"
        android:layout_alignParentRight="true"
        android:layout_marginRight="20dp"
        android:layout_marginTop="20dp"

        android:layout_marginLeft="10dp"
        />

    <Button
        android:id="@+id/loginButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/loginPasswordEditText"
        android:layout_below="@+id/loginPasswordEditText"
        android:layout_marginTop="20dp"
        android:text="LOGIN"
        android:gravity="center"
        android:textColor="@color/lightBlack"
        android:textStyle="bold" />

</RelativeLayout>

LoginDialog.java

@SuppressLint("ValidFragment")
public class LoginDialog extends DialogFragment {

    //*******interface for communicating with the host********//
    public interface LoginCommunicator{
        public void getLoginCredentials(String name,String password);

    }

    LoginCommunicator listener;

    //******default constructor*********************//
    public LoginDialog() {
        // TODO Auto-generated constructor stub
    }

    public LoginDialog(FragmentSettings fragmentSettings) {
        // TODO Auto-generated constructor stub
        listener=fragmentSettings;
    }

    /*//********from stackOverflow: it's safer to add the request for
    a title-less window in the onCreateDialog() method instead of
    onCreateView(). This way you can prevent ennoying NPE when
    you're not using your fragment as a Dialog
*/  
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        Dialog dialog= super.onCreateDialog(savedInstanceState);

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

        return dialog;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view=inflater.inflate(R.layout.login_dialog, container);
    //  getDialog().setTitle("LOGIN");

    //  getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        final EditText loginNameEditText=(EditText)view.findViewById(R.id.loginNameEditText);
        final EditText loginPasswordEditText=(EditText)view.findViewById(R.id.loginPasswordEditText);

        Button loginButton=(Button) view.findViewById(R.id.loginButton);
        loginButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                String name=loginNameEditText.getText().toString();
                String password=loginPasswordEditText.getText().toString();

                listener.getLoginCredentials(name, password);
            }
        });
        return view;
    }
}

View rendered:

enter image description here

so android:layout_marginRight="20dp" is making no change I guess. How to get this change achieved ?

Praline answered 26/11, 2013 at 6:24 Comment(0)
H
17

In some cases, the very outer margin just does not work or gets cut to zero on some Android API versions. It mostly happens in AdapterViews and Dialogs. If you use padding instead, it'll work on all Android version.

Herzl answered 26/11, 2013 at 6:27 Comment(2)
sorry it required me 6 minutes to accept. Meanwhile got busy with other stuffs. Accepted now :)Praline
I had similar problem with buttons at one moment, it appeared I put backgroundTint instead background attribute on.Wiese

© 2022 - 2024 — McMap. All rights reserved.