I'm trying to make a Login/Registration activity by using sharedpreference. The app will only hold one login/password information.
So on original start up the app will check if the user has already made an account. This will be kept under a boolean called Registered. If that is true then it will enable the login button and disable the register button. If it is false then it will enable the register button and disable the login button.
When the types in his wanted username and password they can then hit the register button which will set the username and password into strings while changing the boolean to true and then put them in the sharedpreferences. Then the activity will refresh itself.
Currently i cannot get the buttons to change. I think i may be doing sharedpreferences wrong as this is my first time working with them. (Im new to android development.)
Here is my current code.
I have updated the code to include editor.apply()
I have added a few bits and pieces, mainly getting the value at start.
FINAL UPDATE: I have fixed the problem. It seemed to lie within the .setenabled i was using for the buttons. Once i switched them to .setvisibility, they switched perfectly. Thank you all for your help!
public class MainActivity extends AppCompatActivity {
EditText Username_input, Password_input;
String Username, Password;
//if false then no, if true then yes
boolean Registered;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
Registered = sharedPref.getBoolean("Registered", false);
Button RegisterButton = (Button) findViewById(R.id.Register_btn);
Button Loginbutton = (Button) findViewById(R.id.Login_btn);
// If the user is registered.
if (Registered == false) {
Loginbutton.setEnabled(false);
RegisterButton.setEnabled(true);
// If the user is registered already.
} else {
Loginbutton.setEnabled(true);
RegisterButton.setEnabled(false);
}
RegisterButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Username_input = (EditText) findViewById(R.id.Username_input);
Password_input = (EditText) findViewById(R.id.Password_input);
Username = Username_input.getText().toString();
Password = Password_input.getText().toString();
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean("Registered", true);
editor.putString("Username", Username);
editor.putString("Password", Password);
editor.apply();
finish();
startActivity(getIntent());
}
});
}
}
XML Code
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/Username_txtview"
android:id="@+id/Username_txtview"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="32dp"
android:layout_marginStart="32dp"
android:layout_marginTop="32dp"
android:editable="false" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/Username_input"
android:enabled="true"
android:layout_marginLeft="32dp"
android:layout_marginRight="32dp"
android:layout_below="@+id/Username_txtview"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/Password_txtview"
android:id="@+id/Password_txtview"
android:layout_below="@+id/Username_input"
android:layout_alignLeft="@+id/Username_input"
android:layout_alignStart="@+id/Username_input" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/Password_input"
android:enabled="true"
android:layout_marginLeft="32dp"
android:layout_marginRight="32dp"
android:layout_below="@+id/Password_txtview"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:layout_marginRight="80dp"
android:text="@string/Login_btn"
android:id="@+id/Login_btn"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Register_btn"
android:id="@+id/Register_btn"
android:layout_below="@+id/Login_btn"
android:layout_marginLeft="80dp"
android:layout_marginRight="80dp"
android:layout_marginTop="26dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
/>
</RelativeLayout>