Android Authentication Activity with Fragments

Posted on

Problem

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

Solution

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" />

Leave a Reply

Your email address will not be published. Required fields are marked *