Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Margin attributes not working inside a custom DialogFragment

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 ?

like image 318
Md. Arafat Al Mahmud Avatar asked Nov 26 '13 06:11

Md. Arafat Al Mahmud


People also ask

Is DialogFragment deprecated?

This class was deprecated in API level 28. Use the Support Library DialogFragment for consistent behavior across all devices and access to Lifecycle.

What is the difference between dialog & DialogFragment?

Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.


1 Answers

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.

like image 54
flx Avatar answered Oct 02 '22 09:10

flx