To solve the reported problem I had to make small adjustments to the LoginDialog class. The main change occurred in the onCreateDialog()
method, 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 onCreateView()
method. In this I do 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 fine. But the layout appears small.
Toworkaroundtheproblem,thefollowingcodemustbewrittenintheonStart()
method:
@OverridepublicvoidonStart(){super.onStart();Dialogdialog=getDialog();if(dialog!=null){dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);}}
Ready!Everythingworkingperfectly.
LoginDialogclasscompletecode
publicclassLoginDialogextendsDialogFragment{@OverridepublicDialogonCreateDialog(BundlesavedInstanceState){Dialogdialog=super.onCreateDialog(savedInstanceState);dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);returndialog;}@OverridepublicvoidonStart(){super.onStart();Dialogdialog=getDialog();if(dialog!=null){dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);}}@Nullable@OverridepublicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,BundlesavedInstanceState){Viewview=inflater.inflate(R.layout.login,container,false);ButtonbotaoCancelar=(Button)view.findViewById(R.id.botao_cancelar);ButtonbotaoLogin=(Button)view.findViewById(R.id.botao_login);botaoCancelar.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewv){//TODOrealizandoocancelamentododialog}});botaoLogin.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewv){//TODOrealizarologin}});returnview;}}
LoginLayout
<?xmlversion="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 height and width declared with the maximum value ( match_parent
), however these values are neglected and need to be overwritten during the DialogFragment
lifecycle, as can be seen in the onStart()
of class LoginDialog
.