To solve the reported problem I had to make small adjustments in the Logindialog class. The main change occurred in the method onCreateDialog()
which now returns its value from super.onCreateDialog()
.
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
Another important change was the implementation of the method onCreateView()
. In this I make the Binding of my buttons and return a view containing my layout.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.login, container, false);
Button botaoCancelar = (Button) view.findViewById(R.id.botao_cancelar);
Button botaoLogin = (Button) view.findViewById(R.id.botao_login);
botaoCancelar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//TODO realizando o cancelamento do dialog
}
});
botaoLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//TODO realizar o login
}
});
return view;
}
So far everything works well. Only that the layout appears with reduced dimensions.
To get around the problem the following code should be written in the method onStart()
:
@Override
public void onStart() {
super.onStart();
Dialog dialog = getDialog();
if (dialog != null) {
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
}
Ready! Everything working perfectly.
Full code of the Logindialog class
public class LoginDialog extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
@Override
public void onStart() {
super.onStart();
Dialog dialog = getDialog();
if (dialog != null) {
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.login, container, false);
Button botaoCancelar = (Button) view.findViewById(R.id.botao_cancelar);
Button botaoLogin = (Button) view.findViewById(R.id.botao_login);
botaoCancelar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//TODO realizando o cancelamento do dialog
}
});
botaoLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//TODO realizar o login
}
});
return view;
}
}
Login layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginTop="12dp"
android:layout_marginBottom="12dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:text="@string/fazer_login"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="@string/email"
android:layout_marginLeft="18dp"
android:layout_marginRight="18dp"
android:layout_marginBottom="12dp"
android:id="@+id/email"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="@string/senha"
android:layout_marginLeft="18dp"
android:layout_marginRight="18dp"
android:layout_marginBottom="12dp"
android:id="@+id/senha"/>
<LinearLayout
style="?android:attr/buttonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="40dp"
android:layout_marginLeft="18dp"
android:layout_marginRight="18dp"
android:gravity="right">
<Button
style="?android:attr/buttonBarButtonStyle"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="@color/colorPrimary"
android:text="@string/cancelar"
android:id="@+id/botao_cancelar"/>
<Button
style="?android:attr/buttonBarButtonStyle"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="@color/colorPrimary"
android:text="@string/acessar"
android:id="@+id/botao_login"/>
</LinearLayout>
</LinearLayout>
In the layout you can notice the declared height and width with the maximum value (match_parent
), however these values are disregarded and need to be overwritten during the life cycle of DialogFragment
, as can be seen in the method onStart()
class LoginDialog
.
See if this reply helping.
– ramaral