In: Computer Science
Exercise 8-2 (work with menus) Exercise 8-3 (work with preferences) android studio
So, let's talk about the menus first:
Menus are basically used to give various options to the user. It comes in the right of toolbar.
Attributes:
id | Used to give ids to the menus so as to conncect it with front end |
title | Used to give title to the menus i.e the text you see |
icon | Used to give icons to the menus. Like setting icon |
showAsAction | Tells that how this item should be visible on the app bar |
visible | Tells whether this item is visible or not |
enable | Tells whether this item is enabled or not. Like if it disable user can't do action on this |
checkable | Tells whether this item can contains checkable box or not |
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/profile"
android:title="My Profile" />
<item
android:id="@+id/logout"
android:title="Logout" />
</menu>
So this is how a xml file of a menu looks like.
This xml file will create a menu with 2 options i.e My Profile and Logout.
MainActivity.java
// This function is used to create or inflate menu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
/*we are taking the default MenuInflator
then we are calling the inflate function by passing the Resource file and the menu which
we are getting from the function onCreateOptionsMenu.
*/
return super.onCreateOptionsMenu(menu);
}
// This function is used to select options in the menu
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// we have to pass the menu item which we are getting through the function parameter
//then by passing it into the switch we can perform function according to out need
switch (item.getItemId()) {
case R.id.logout: {
//logout code here
break;
}
case R.id.profile: {
//my profile code here
}
}
return super.onOptionsItemSelected(item);
}
Now let's talk about the prefrences.
Prefrences or Shared Prefrences are used to store data into key-value pairs.
These are used to store the preffered state of the application selected by the user like dark mode or light mode,
like to store whether the user is logged in or not, passwords and similiar type of data.
To store the prefrences you first have to call a function which is call by using current context i.e current activity.
function is getSharedPreferences
e.g this.getSharedPreferences or in case of fragment you will be usign context.
Now, this function will take 2 parameters i.e name(String) of prefrence and mode(Int) in which you want to call the method.
e.g this.getSharedPreferences("isLoggedIn",Context.MODE_PRIVATE)
Complete Code to store password of user:
//To open an editor for sharedprefrences which will store the values
SharedPreferences.Editor editor = getSharedPreferences("PasswordStore", Context.MODE_PRIVATE).edit();
//you can use a global string variable instead of "PasswordStore"
String pass = "12345"; //you can also take this from user using editText.
editor.putString("Password", pass);//this will create a key-value pair with key = Password and value = 12345
editor.apply();//this will apply changes
/*Note : Instead of putString you can use putInt, putFloat, putStringSet(array of strings), putBoolean
putLong */
/* Then for editor we have clear() method to clear the editor*/
//Now if you want to fetch this password you can do it by:
SharedPreferences passwordPref = getSharedPreferences("PasswordStore", Context.MODE_PRIVATE);
//Note: You have to pass the exact name of preferences which you used while storing it
String password = passwordPref.getString("Password",null);
//Note : You have to pass the exact name of key which you used while storing it.
/* First parameter is key of preferences and other is default value i.e if you want to fetch the value
of that preference which you did not store it will give you the default value.
/*Note : Instead of getString you can use getInt, getFloat, getStringSet(array of strings), getBoolean
getLong according to the value you stored*/
So, this was the explanation for the menus and
preferences.
I hope I am able to make you understand, if yes then do give it a
thumbs up .
It really helps :)