Saturday, August 15, 2015

Profile Page With First Time Login Check


.
Profile Page With First Time Login Check
Using Android Studio 1.4

1) Start Up

Follow previous tutorial or download start up project MyProfile2-profile-fields.zip.

2) Edit MainActivity

package com.notarazi.myprofile;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.EditText;
public class MyProfile extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_my_profile);
       checkFirstLogin();
       prepareForm();
   }
   private void prepareForm() {
       SharedPreferences preferences = getSharedPreferences("myprefs",MODE_PRIVATE);
       EditText etUserName=(EditText)findViewById(R.id.username);
       EditText etUserPhone=(EditText)findViewById(R.id.userphone);
       EditText etUserEmail=(EditText)findViewById(R.id.useremail);
       // If value for key not exist then return second param value - In this case "..."
       etUserName.setText(preferences.getString("username""..."));
       etUserPhone.setText(preferences.getString("userphone""..."));
       etUserEmail.setText(preferences.getString("useremail""..."));
   }
   private void checkFirstLogin() {
       SharedPreferences preferences = getSharedPreferences("myprefs",MODE_PRIVATE);
       // If value for key not exist then return second param value - In this case true
       if (preferences.getBoolean("firstLogin"true)) {
           initProfile();
           SharedPreferences.Editor editor = preferences.edit();
           editor.putBoolean("firstLogin"false);
           editor.commit();
       }
   }
   private void initProfile() {
       SharedPreferences preferences = getSharedPreferences("myprefs",MODE_PRIVATE);
       SharedPreferences.Editor editor = preferences.edit();
       editor.putString("username","Demo TryTest");
       editor.putString("userphone","01234567890");
       editor.putString("useremail","demotrytest@gmail.com");
       editor.commit();
   }
}
OUTCOME.

DOWNLOAD

REFERENCES


.

No comments:

Post a Comment