How to add Preferences (Settings) in Sketchware project




Hello friends, in this tutorial i will show you, how to add preferences in place of custom settings layout in any of your sketchware projects, this will help you to add default settings layout provided by Android, you will not need checkboxes, switches, etc. as settings method, this layout will contain all. Whether you enable AppCompatActivity or not, it will not brought any changes to the Preferences. Below is a sample video of how it looks when the app is made.


                 DEMO: 



Lets start the tutorial:

Step 1 - Add a vertical linear layout with id linear1, width match_parent, height match_parent, and background color white (for better UI).



Step 2 - In the onCreate block/event of the activity where you have added linear layout, add the following codes.

}  

//A class which extends preference fragment, it will add a fragment in linear1
public static class PrefsFragment extends android.preference.PreferenceFragment {          
 @Override         
 public void onCreate(Bundle savedInstanceState) {             
  super.onCreate(savedInstanceState);  
  android.preference.PreferenceScreen root = getPreferenceManager().createPreferenceScreen(getContext());         
  setPreferenceScreen(root);         
  populatePreferenceHierarchy(root);     
 }      
//It defines the preferences so that you will be able to access then anywhere from your particular activity
private android.preference.CheckBoxPreference cbp;  
private android.preference.SwitchPreference sp;  
private android.preference.EditTextPreference etp;  
private android.preference.ListPreference lvp;

//Contains all the preference categories and preferences  
private void populatePreferenceHierarchy(android.preference.PreferenceScreen root) {


Step 3 - Now lets add preference categories and preferences in them which will contain specific settings. 
For example: if you want to enable dark theme for your app then the checkbox preference will go under general category and it will be titled General and there is a webview in your app and you want some other settings for it then another category will be titled Webview and it will contain all the settings related to webview only.
In this tutorial i m adding two categories (i) checkable category, which will contain CheckBoxPreference and SwitchPreference which both are checkable and (ii) dialog category, which will contain EditTextPreference and ListPreference which both are dialog based preferences.

The codes are:

//A special preference category where u can add your selected preferences
android.preference.PreferenceCategory inlinePrefCat = new android.preference.PreferenceCategory(getContext());         
inlinePrefCat.setTitle("Check Category");         
root.addPreference(inlinePrefCat);  

  // Checkbox preference         
android.preference.CheckBoxPreference cbp = new android.preference.CheckBoxPreference(getContext());         
cbp.setKey("checkbox_preference");         
cbp.setTitle("Checkbox Preference");  
cbp.setSummary("Summary for checkbox");         
inlinePrefCat.addPreference(cbp);

 // Switch preference         
sp = new android.preference.SwitchPreference(getContext());  
sp.setKey("switch_preference");         
sp.setTitle("Switch Preference");  
sp.setSummary("Summary for switch");         
inlinePrefCat.addPreference(sp);

//A special preference category where u can add your selected preferences

android.preference.PreferenceCategory dialogPrefCat = new android.preference.PreferenceCategory(getContext());         
dialogPrefCat.setTitle("Dialog Category");         
root.addPreference(dialogPrefCat);

// Edittext preference         
etp = new android.preference.EditTextPreference(getContext());         
etp.setKey("edittext_preference");         
etp.setTitle("Edittext Preference");         
etp.setSummary("Summary for edittext"); 
etp.setDialogTitle("EditText Dialog Title"); 
etp.setDialogMessage("EditText Dialog Message");         
dialogPrefCat.addPreference(etp); 

// List Preference 
CharSequence[] itemNames = {"item1","item2","item3","item4","item5"};  
CharSequence[] itemValues = {"item1value","item2value","item3value","item4value","item5value"};           
lvp = new android.preference.ListPreference(getContext());        
lvp.setDialogTitle("List Dialiog Title");
lvp.setKey("list_preference");         
lvp.setTitle("List Preference"); 
lvp.setSummary("Summary for list");          
lvp.setEntries(itemNames);         
lvp.setEntryValues(itemValues);         
dialogPrefCat.addPreference(lvp);     


Step 4 - Now you have to add codes which will give you the data of the particular preference, like when checkbox and switch preferences are true or false and the value of edittext preference and list preference.


//When checkbox preference is changed  
cbp.setOnPreferenceChangeListener(new android.preference.Preference.OnPreferenceChangeListener(){      
 @Override     
 public boolean onPreferenceChange(android.preference.Preference p1, Object p2) {      
 // TODO: Implement this method 
  Toast.makeText(getContext(), p2.toString(), 0).show();           
  return true;     
 }    
});    

//When switch preference is changed  
sp.setOnPreferenceChangeListener(new android.preference.Preference.OnPreferenceChangeListener(){      
 @Override     
 public boolean onPreferenceChange(android.preference.Preference p1, Object p2){      
 // TODO: Implement this method 
  Toast.makeText(getContext(), p2.toString(), 0).show();           
  return true;     
 }    
});    

//When edittext preference is changed  
etp.setOnPreferenceChangeListener(new android.preference.Preference.OnPreferenceChangeListener(){      
 @Override     
 public boolean onPreferenceChange(android.preference.Preference p1, Object p2){      
 // TODO: Implement this method 
  Toast.makeText(getContext(), p2.toString(), 0).show();           
  return true;     
 }    
});    

//When list preference is changed  
lvp.setOnPreferenceChangeListener(new android.preference.Preference.OnPreferenceChangeListener(){      
 @Override     
 public boolean onPreferenceChange(android.preference.Preference p1, Object p2){      
 // TODO: Implement this method 
  Toast.makeText(getContext(), p2.toString(), 0).show();
  return true;     
 }    
}); 

}   



NOTE: If you want to send the data of any of the above preferences to another activity, then the SharedPreference component will be helpfull but for it you have to add it through codes.

You have to add this code at the top of all the codes in onCreate event/block

oncreate():
}

private static SharedPreferences yourSharedPreferenceName;

private void oncreate(){

yourSharedPreferenceName = getSharedPreferences("yourSharedPreferenceKey", Activity.MODE_PRIVATE);
//here yourSharedPrefereceName = the name of shared preference you want to give and with the help of this name you will be able to access shared preference from anywhere of your activity AND yourSharedPreferenceKey = the key of the shared preference which should be same as the key entered in the activity where you want to share the data.

//Using this shared preference is added in the source project.


Download the source zip file (which is sh-zip). To add this source project in your sketchware you will need to download SH Recovery app.


Steps to add this project in sketchware:
i) Download the source file from the link.
ii) After downloading, open SH Recovery app and go in the restore section.
iii) Now click on the floating action button to add source file, it will ask you to add file from backged up project or from storage, choose storage.
iv) Select the downloaded source file destination and run it.

If you are not able to add this project, contact me.

PLEASE FOLLOW THE STEPS, OTHER WISE IT WILL GIVE YOU ERROR.
AND NO NEED TO ADD AN EXTRA BRACKETS.

Happy Coding.
#StayHomeStaySafe

No comments:

Note: Only a member of this blog may post a comment.

Powered by Blogger.