UNIT V: Localization, Menus and Shared Preference
UNIT V: Localization, Menus and Shared Preference
5.1 Localization
An android application can run on many devices in many different regions. In
order to make your application more interactive, your application should handle
text, numbers, files etc. in ways appropriate to the locales where your application
will be used.
Localizing Strings
In order to localize the strings used in your application, make a new folder
under res with name of values-local where local would be the replaced with the
region.
For example, in the case of Marathi, the values-mr folder would be made under
res
Once that folder is made, copy the strings.xml from default folder to the folder
you have created. And change its contents as in Marathi.
By using Options Menu, we can combine multiple actions and other options
that are relevant to our current activity. We can define items for the options
menu from either our Activity or Fragment class.
<item android:id="@+id/mail"
android:icon="@drawable/ic_mail"
android:title="@string/mail" />
</menu>
@Override
public void onCreateOptionsMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_example, menu);
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.mail:
// do something
return true;
default:
return super.onContextItemSelected(item);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btnShow);
registerForContextMenu(btn);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Context Menu");
menu.add(0, v.getId(), 0, "Upload");
menu.add(0, v.getId(), 0, "Search");
}
getPreferences()
This method is for activity level preferences and each activity will have its own
preference file and by default
getSharedPreferences()
This method is useful to get the values from multiple shared preference files by
passing the name as a parameter to identify the file. We can call this from any
Context in our app.
This is only applicable to apps that target Android 11 (API level 30) and declare
the MANAGE_EXTERNAL_STORAGE permission, which is added in Android 11.
If your app meets the policy requirements for acceptable use or is eligible for an
exception, you will be required to declare this and any other high-risk
permissions using the Permissions
File management
App’s core purpose involves the access, editing, and management (including
maintenance) of files and folders outside of its app-specific storage space.
App must have a need to automatically access multiple directories outside of its
app-specific storage space for the purpose of backup and restore
Anti-virus apps
App’s core purpose is to scan the device and provide anti-virus security features
to the device user
Apps that must locate, access, and edit compatible file types outside of its app-
specific or shared storage
Search
App’s core purpose is to search through files and folders across the device’s
external storage
You must know Email functionality with intent, Intent is carrying data from one
component to another component with-in the application or outside the
application.
To send an email from your application, you don’t have to implement an email
client from the beginning, but you can use an existing one like the default Email
app provided from Android, Gmail, Outlook, K-9 Mail etc.
we need to write an Activity that launches an email client, using an implicit Intent
with the right action and data.
Android has built-in support to add TO, SUBJECT, CC, TEXT etc.
Ex.
String[] TO = {""};
String[] CC = {""};
SmsManager API
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("phoneNo", null, "sms message", null, null);
Apart from the above method, there are few other important functions available
in SmsManager class.
This method divides a message text into several fragments, none bigger than the
maximum SMS message size.
void sendDataMessage()
This method is used to send a data based SMS to a specific application port.
void sendMultipartTextMessage()
void sendTextMessage()
Thank You
Prepared by: Mr. G. P. Shinde, COCSIT Latur Page 9