Localizing Strings: Italy, Res/values-It/strings - XML
Localizing Strings: Italy, Res/values-It/strings - XML
Localizing Strings: Italy, Res/values-It/strings - XML
5.1 Localization
In this chapter we will explain , how you can localize your application
according to different regions e.t.c. We will localize the strings used in the
application, and in the same way other things can be localized.
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 italy, the values-it folder would be made under
res. It is shown in the image below −
Once that folder is made, copy the strings.xmlfrom default folder to the
folder you have created. And change its contents. For example, i have
changed the value of hello_world string.
Italy, res/values-it/strings.xml
<resources>
Spanish, res/values-it/strings.xml
<resources>
</resources>
French, res/values-it/strings.xml
<resources>
</resources>
Apart from these languages, the region code of other languages have been
given in the table below −
Afrikanns
1
Code: af. Folder name: values-af
Arabic
2
Code: ar. Folder name: values-ar
Bengali
3
Code: bn. Folder name: values-bn
Czech
4
Code: cs. Folder name: values-cs
Chinese
5
Code: zh. Folder name: values-zh
German
6
Code: de. Folder name: values-de
7 French
Code: fr. Folder name: values-fr
Japanese
8
Code: ja. Folder name: values-ja
The first step in hooking up your menus to an Activity is to create them (which is already shown
above). The 2nd step is to hook into the Activity’s menu creation methods to call your code,
which creates these menu items:
populateMenu(menu);
return super.onCreateOptionsMenu(menu);
1. The menu is created by Android and passed to you; you have to add whatever menu items
you want to this object, you don’t actually create the Menu. This allows Android to populate
the Menu with system defined menu items (in case you call
super.onCreateOptionsMenu(menu) for the Option menu).
2. If you add more than 5 menu items to the given menu, then the 6th option won’t not be
displayed in the grid (like they are in the screenshot above). A “More” button will be shown
and when you select this a list view will be shown with all your menu items. In the grid
view, the accelerators are not shown. The grid view only takes up the bottom portion of the
Activity/screen. The list view takes up the full screen and shows the keyboard accelerators.
3. The code to respond to menu item selection is shown below (this is common to the Option
and Context menu).
Here’s the code to bind the menu items to a ListView component, so when you press-and-hold
this component, it will pop up a context menu:
listview.setOnPopulateContextMenuListener(this);
View view,
Object o) {
populateMenu(menu);
1. Just like with the Option menu, the Menu object is pre-created for you and passed to you.
2. Unlike the Context menu example, you don’t override a method in your Activity class, you
have to call setOnPopulateContextMenuListener(…) on the component you want to bind the
context menu with.
3. This onPopulateContextMenu(…) method is called every time the user performs a press-
and-hold operation on the ListView. This isn’t just created once (like the Option menu).
https://developerlife.com/2008/07/16/option-and-context-menu/#option-menu---menu-button
Some times in your application, if you wanted to ask the user about taking a
decision between yes or no in response of any particular action taken by the
user, by remaining in the same activity and without changing the screen, you
can use Alert Dialog.
Now you have to set the positive (yes) or negative (no) button using the
object of the AlertDialogBuilder class. Its syntax is
alertDialogBuilder.setPositiveButton(CharSequence text,
DialogInterface.OnClickListener listener)
alertDialogBuilder.setNegativeButton(CharSequence text,
DialogInterface.OnClickListener listener)
Apart from this , you can use other functions provided by the builder class to
customize the alert dialog. These are listed below
setIcon(Drawable icon)
1
This method set the icon of the alert dialog box.
setMessage(CharSequence message)
3
This method sets the message to be displayed in the alert dialog
setOnCancelListener(DialogInterface.OnCancelListener
5 onCancelListener)
This method Sets the callback that will be called if the dialog is cancelled.
setTitle(CharSequence title)
6
This method set the title to be appear in the dialog
After creating and setting the dialog builder , you will create an alert dialog
by calling the create() method of the builder class. Its syntax is
alertDialog.show();
This will create the alert dialog and will show it on the screen.
Dialog fragment
Before enter into an example we should need to know dialog fragment.Dialog
fragment is a fragment which can show fragment in dialog box
@Override
})
finish();
});
return builder.create();
}
}
List dialog
It has used to show list of items in a dialog box.For suppose, user need to
select a list of items or else need to click a item from multiple list of items.At
this situation we can use list dialog.
builder.setTitle(Pick a Color)
});
return builder.create();
.setMultiChoiceItems(R.array.toppings, null,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
if (isChecked) {
// If the user checked the item, add it to the selected items
mSelectedItems.add(which);
else if (mSelectedItems.contains(which)) {
mSelectedItems.remove(Integer.valueOf(which));
})
@Override
...
})
@Override
...
});
return builder.create();
https://www.tutorialspoint.com/android/android_alert_dialoges.htm
5.4 Custom dialog
A string resource provides text strings for your application with optional text styling and
formatting. There are three types of resources that can provide your application with strings:
String
String Array
All strings are capable of applying some styling markup and formatting arguments. For
information about styling and formatting strings, see the section about Formatting and Styling.
String
A single string that can be referenced from the application or from other resource files (such as
an XML layout).
Note: A string is a simple resource that is referenced using the value provided in the name attribute (not the
name of the XML file). So, you can combine string resources with other simple resources in the one XML file,
under one <resources> element.
file location:
res/values/filename.xml
The filename is arbitrary. The <string> element's name is used as the resource ID.
resource reference:
In Java: R.string.string_name
In XML:@string/string_name
syntax:
elements:
<resources>
No attributes.
<string>
A string, which can include styling tags. Beware that you must escape apostrophes and
quotation marks. For more information about how to properly style and format your strings
see Formatting and Styling, below.
attributes:
name
String. A name for the string. This name is used as the resource ID.
String array
An array of strings that can be referenced from the application.
Note: A string array is a simple resource that is referenced using the value provided in the nameattribute (not
the name of the XML file). As such, you can combine string array resources with other simple resources in the
one XML file, under one <resources> element.
file location:
res/values/filename.xml
The filename is arbitrary. The <string-array> element's name is used as the resource ID.
resource reference:
In Java: R.array.string_array_name
syntax:
elements:
<resources>
Required. This must be the root node.
No attributes.
<string-array>
attributes:
name
String. A name for the array. This name is used as the resource ID to reference the array.
<item>
A string, which can include styling tags. The value can be a reference to another string resource.
Must be a child of a <string-array> element. Beware that you must escape apostrophes and
quotation marks. See Formatting and Styling, below, for information about to properly style and
format your strings.
No attributes.
https://developer.android.com/guide/topics/resources/string-resource
1. Introduction
Earlier, getting location is very easy with couple of API calls. But to provide more accurate locations
and optimizing the battery usage, Android introduced set APIs that should be combined to get the
best results from the location API. We will be using Fused Location API that combines signals
from GPS, Wi-Fi, and cell networks, as well as accelerometer, gyroscope, magnetometer and
other sensors to provide more accurate results.
● Update Interval: This interval defines the rate in milliseconds at which your app prefers the
location updates. Your app can receive updates lesser or higher than this rate if other apps
requested location updates higher than your value. Let’s say your app requests updates every
10secs, if other app is requesting updates at 5secs, your app might receives the same updates
ignoring the 10sec value.
● Fastest Update Interval: This is the rate at which your app can handle the location updates.
Without this value, you can see inconsistent user experience if your app can’t handle frequent
location updates.
● Priority: The accuracy of the location depends on the source of the hardware used. To define
this, Priority has to be mentioned while requesting the location. The priority can
be BALANCED, HIGH, LOWOR NO_POWER.
●
5. Open the layout file of main activity activity_main.xml and add the below code. In this layout,
few Buttons and TextViews are defined to toggle the location updates and display location
information.
6. Open MainActivity.java and add the below code. Initially the code might look heavy but with
couple of observations you can understand it easily.
● First we initialize all the location related clients such
as FusedLocationProviderClient, LocationRequest, LocationSettingsRequest, LocationCa
llback and SettingsClient in onCreate() method.
● While initializing, we define the interval setInterval(), fastest interval setFastestInterval() and
priority setPriority() on location request.
● Dexter is used to request the location permission before performing any location related
operations.
● startLocationUpdates() requests for location updates. First, it checks whether the location
settings are eanbled and once satisfied, the updates will be requested. Here SettingsClient is
used to check for settings configuration.
● The location updates will be received in LocationCallback and proper UI action is taken place.
If you really want to the location only once, you can call stopLocationUpdates() method
immediately after receiving the first location update.
● The location updates are paused and resume in onPause() and onResume() method to save the
batter power.
Run the app in emulator or on a real device to see it working
https://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/