HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Android Authentication Activity with Fragments

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
androidwithfragmentsauthenticationactivity

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:


    

    

        

    

    

        

        

        

        

        

        

        

    


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:



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:

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. :

        
    match_parent
    match_parent
 

        
    wrap_content
    wrap_content
 

        
    match_parent
    wrap_content
 

    @android:color/white
    #AAFFFFFF

    @string/email
    textEmailAddress
    true

    @string/password
    textPassword
    @id/loginEmail

    @id/loginPassword
    

    
    


etc...

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

So this part of your xml code :


    

    

    


then could look like this :


    

    

    

Code Snippets

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

Context

StackExchange Code Review Q#150253, answer score: 2

Revisions (0)

No revisions yet.