3
\$\begingroup\$

I have an Activity called AuthActivity and I would like all of my authentication Fragments to be in this Activity. I will have 4 Fragments: LoginFragment, RegisterFragment, ForgotPasswordFragment and RememberedLoginFragment. I currently only have the LoginFragment and I would like some feedback before I continue making the others. I would like some feedback on all of the code, Java and XML.

fragment_login.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_login"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <View
        android:id="@+id/loginTopView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/colorPrimary" />

    <RelativeLayout
        android:id="@+id/loginCenterView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="5"
        android:background="@color/colorAccent">

        <ImageView
            android:id="@+id/loginLogo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:src="@drawable/ic_logo"
            android:contentDescription="@string/logo" />

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/loginBottomView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="10"
        android:padding="15dp"
        android:background="@color/colorPrimary">

        <EditText
            android:id="@+id/loginEmail"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:hint="@string/email"
            android:inputType="textEmailAddress"
            android:textColor="@android:color/white"
            android:textColorHint="#AAFFFFFF" />

        <EditText
            android:id="@+id/loginPassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/loginEmail"
            android:hint="@string/password"
            android:inputType="textPassword"
            android:textColor="@android:color/white"
            android:textColorHint="#AAFFFFFF" />

        <CheckBox
            android:id="@+id/loginRememberMe"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/loginPassword"
            android:text="@string/remember_me" />

        <CheckBox
            android:id="@+id/loginStayLoggedIn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/loginRememberMe"
            android:text="@string/stay_logged_in" />

        <Button
            android:id="@+id/loginButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/loginStayLoggedIn"
            android:text="@string/login"
            android:textAllCaps="false" />

        <TextView
            android:id="@+id/loginForgotPassword"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/loginButton"
            android:layout_centerHorizontal="true"
            android:textColor="@android:color/white"
            android:text="@string/forgot_password" />

        <TextView
            android:id="@+id/loginRegisterLink"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:textColor="@android:color/white"
            android:text="@string/register_link_text" />

    </RelativeLayout>

</LinearLayout>

LoginFragment.java:

public class LoginFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_login, container, false);
    }

}

activity_auth.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_auth"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

AuthActivity.java:

public class AuthActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_auth);

        if (savedInstanceState != null) {
            return;
        }

        LoginFragment loginFragment = new LoginFragment();
        loginFragment.setArguments(getIntent().getExtras());

        getSupportFragmentManager().beginTransaction().add(R.id.activity_auth, loginFragment).commit();

Here is an image of the result: enter image description here

\$\endgroup\$
2
\$\begingroup\$

Well, since there is hardly any Java code to review I'll review your xml code.

It is good practice to combine attributes that your layouts/views have in common in your /res/values/styles.xml file. F.e. :

<style name="LayoutStyleMatchParent">        
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">match_parent</item>
</style> 

<style name="LayoutStyleWrapContent">        
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
 </style>

<style name="LayoutStyleWide">        
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
 </style>

<style name="EditTextStyle" parent="LayoutStyleWide">
    <item name="android:textColor">@android:color/white</item>
    <item name="android:textColorHint">#AAFFFFFF</item>
</style>

<style name="EditTextLogin" parent="EditTextStyle">
    <item name="android:hint">@string/email</item>
    <item name="android:inputType">textEmailAddress</item>
    <item name="android:layout_alignParentTop">true</item>
</style>

<style name="EditTextPassword" parent="EditTextStyle">
    <item name="android:hint">@string/password</item>
    <item name="android:inputType">textPassword</item>
    <item name="android:layout_below">@id/loginEmail</item>
</style>

<style name="CheckBoxRememberMe" parent="LayoutStyleWrapContent">
    <item name="android:layout_below">@id/loginPassword</item>
    <item name="android:text="@string/remember_me"</item>
</style>

<style name="CheckBoxStayLoggedIn" parent="LayoutStyleWrapContent">
    <item name="android:layout_below="@id/loginRememberMe"</item>
    <item name="android:text="@string/stay_logged_in"</item>
</style>

etc...

This minimizes code duplication and you can reuse/extend these styles in your other Fragments

So this part of your xml code :

    <EditText
        android:id="@+id/loginEmail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:hint="@string/email"
        android:inputType="textEmailAddress"
        android:textColor="@android:color/white"
        android:textColorHint="#AAFFFFFF" />

    <EditText
        android:id="@+id/loginPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/loginEmail"
        android:hint="@string/password"
        android:inputType="textPassword"
        android:textColor="@android:color/white"
        android:textColorHint="#AAFFFFFF" />

    <CheckBox
        android:id="@+id/loginRememberMe"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/loginPassword"
        android:text="@string/remember_me" />

    <CheckBox
        android:id="@+id/loginStayLoggedIn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/loginRememberMe"
        android:text="@string/stay_logged_in" />

then could look like this :

    <EditText
        android:id="@+id/loginEmail"
        style="@style/EditTextLogin" />

    <EditText
        android:id="@+id/loginPassword"
        style="@style/EditTextPassword" />

    <CheckBox
        android:id="@+id/loginRememberMe"
        style="@style/CheckBoxRememberMe" />

    <CheckBox
        android:id="@+id/loginStayLoggedIn"
        style="@style/CheckBoxStayLoggedIn" />
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.