0% found this document useful (0 votes)
18 views17 pages

Android Internationalization Guide

The document outlines the implementation of internationalization and localization in an Android application, allowing users to change the app's language. It includes XML layout files for the user interface and Java code for handling language changes and Firebase authentication. Additionally, it provides string resources for multiple languages, enhancing user accessibility.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views17 pages

Android Internationalization Guide

The document outlines the implementation of internationalization and localization in an Android application, allowing users to change the app's language. It includes XML layout files for the user interface and Java code for handling language changes and Firebase authentication. Additionally, it provides string resources for multiple languages, enhancing user accessibility.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Internationalization and Localization

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginBottom="632dp"
android:fontFamily="@font/aref_ruqaa"
android:text="@string/heading"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />

<EditText
android:id="@+id/email"
android:layout_width="204dp"
android:layout_height="60dp"
android:layout_marginEnd="100dp"
android:layout_marginBottom="524dp"
android:ems="10"
android:fontFamily="@font/amaranth"
android:hint="@string/email"
android:inputType="textEmailAddress"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />

<EditText
android:id="@+id/password"
android:layout_width="198dp"
android:layout_height="55dp"
android:layout_marginEnd="104dp"
android:layout_marginBottom="428dp"
android:ems="10"
android:fontFamily="@font/amaranth"
android:hint="@string/password"
android:inputType="textPassword"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />

<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="132dp"
android:layout_marginBottom="328dp"
android:fontFamily="@font/amaranth"
android:text="@string/login"
android:textAllCaps="false"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />

<TextView
android:id="@+id/changeLang"
android:layout_width="273dp"
android:layout_height="65dp"
android:layout_marginEnd="60dp"
android:layout_marginBottom="216dp"
android:fontFamily="@font/amaranth"
android:text="@string/changeLang"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
tools:ignore="HardcodedText,TextSizeCheck" />
</[Link]>

-----------------------------------------------------------------------------------
-------------------------------

[Link]

package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

import [Link];
import [Link];

import [Link];

public class MainActivity extends AppCompatActivity {


// textview, alertdialogbuilder, sharedpreferences
TextView changeLang;
[Link] alertDialog;

SharedPreferences sharedPreferences;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
loadLocale(); //function call
setContentView([Link].activity_main);

//initalize part
changeLang = findViewById([Link]);
alertDialog = new [Link](this);

[Link](new [Link]() {
@Override
public void onClick(View v) {
final String[] languages = {"English","ગુજરાતી","हिंदी","मराठी"};
[Link](languages, -1, new
[Link]() {
@Override
public void onClick(DialogInterface dialog, int langId) {
if(langId==0){
setLocale("en");
recreate();
}
else if(langId==1){
setLocale("gu");
recreate();
}
else if(langId==2){
setLocale("hi");
recreate();
}
else if(langId==3){
setLocale("mr");
recreate();
}
}
});
[Link]();
[Link]();
}
});
}
private void setLocale(String languages){
Locale locale = new Locale(languages);
[Link](locale);

Configuration config = new Configuration();


[Link](locale);

getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());

[Link] editor =
getSharedPreferences("Settings",MODE_PRIVATE).edit();

[Link]("My_Lang",languages);
[Link]();
}

private void loadLocale(){


SharedPreferences sharedPreferences1 =
getSharedPreferences("Settings",MODE_PRIVATE);
String language = [Link]("My_Lang","");
setLocale(language);
}
}

[Link]
<resources>
<string name="app_name">AndroidPractice</string>
<string name="heading">Internationalization and Localization</string>
<string name="email">Enter Email</string>
<string name="password">Enter password</string>
<string name="login">Click to Login</string>
<string name="changeLang">Click here to change your app language</string>
</resources>

[Link] (gu)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">AndroidPractice</string>
<string name="heading">આંતરરાષ્ટ્રીયકરણ અને સ્થાનિકીકરણ</string>
<string name="email">ઈમેલ દાખલ કરો</string>
<string name="password">પાસવર્ડ દાખલ કરો</string>
<string name="login">લોગિન કરવા માટે ક્લિક કરો</string>
<string name="changeLang">તમારી એપ્લિકેશન ભાષા બદલવા માટે અહીં ક્લિક
કરો</string>
</resources>

[Link](hi)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">AndroidPractice</string>
<string name="heading">" अंतर्राष्ट्रीयकरण और स्थानीयकरण"</string>
<string name="email">ईमेल दर्ज करें</string>
<string name="password">पास वर्ड दर्ज करें</string>
<string name="login">लॉग इन करने के लिए क्लिक करें</string>
<string name="changeLang">अपने ऐप की भाषा बदलने के लिए यहां क्लिक करें</string>
</resources>

[Link](mr)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">AndroidPractice</string>
<string name="heading">आंतरराष्ट्रीयीकरण आणि स्थानिकीकरण</string>
<string name="email">ईमेल प्रविष्ट करा</string>
<string name="password">पासवर्ड टाका</string>
<string name="login">लॉगिन करण्यासाठी क्लिक करा</string>
<string name="changeLang">तुमची ॲप भाषा बदलण्यासाठी येथे क्लिक करा</string>
</resources>

-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------------------------

Firebase Database

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="72dp"
android:layout_marginBottom="624dp"
android:fontFamily="@font/amarante"
android:text="Firebase Database"
android:textSize="34sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />

<EditText
android:id="@+id/email"
android:layout_width="204dp"
android:layout_height="60dp"
android:layout_marginEnd="100dp"
android:layout_marginBottom="524dp"
android:ems="10"
android:fontFamily="@font/amaranth"
android:hint="Enter Email"
android:inputType="textEmailAddress"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />

<EditText
android:id="@+id/password"
android:layout_width="198dp"
android:layout_height="55dp"
android:layout_marginEnd="104dp"
android:layout_marginBottom="428dp"
android:ems="10"
android:fontFamily="@font/amaranth"
android:hint="Enter Password"
android:inputType="textPassword"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />

<Button
android:id="@+id/register"
android:layout_width="147dp"
android:layout_height="45dp"
android:layout_marginEnd="132dp"
android:layout_marginBottom="340dp"
android:fontFamily="@font/amaranth"
android:text="Register"
android:textAllCaps="false"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />

<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="160dp"
android:layout_marginBottom="260dp"
android:fontFamily="@font/amaranth"
android:text="Login"
android:textAllCaps="false"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />

</[Link]>

[Link]
package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

import [Link];
import [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {

EditText email, password;


Button register, login;
FirebaseAuth firebaseAuth;
DatabaseReference databaseReference;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

email = findViewById([Link]);
password = findViewById([Link]);
register = findViewById([Link]);
login = findViewById([Link]);

firebaseAuth = [Link]();
databaseReference = [Link]().getReference("users");

[Link](new [Link]() {
@Override
public void onClick(View v) {
String emailStr = [Link]().toString();
String passwordStr = [Link]().toString();

[Link](emailStr,
passwordStr).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if ([Link]()) {
String userId = [Link]().getUid();
User user = new User(emailStr, passwordStr); //
Assuming you have a User class
[Link](userId).setValue(user);
[Link]([Link], "Registration
Successful", Toast.LENGTH_SHORT).show();
} else {
if ([Link]() instanceof
FirebaseAuthUserCollisionException) {
[Link]([Link], "Email already in
use.", Toast.LENGTH_SHORT).show();
} else {
[Link]([Link], "Registration
Failed", Toast.LENGTH_SHORT).show();
}
}
}
});
}
});

[Link](new [Link]() {
@Override
public void onClick(View v) {
String emailStr = [Link]().toString();
String passwordStr = [Link]().toString();

[Link](emailStr,
passwordStr).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if ([Link]()) {
[Link]([Link], "Login Successful",
Toast.LENGTH_SHORT).show();
} else {
[Link]([Link], "Login Failed",
Toast.LENGTH_SHORT).show();
}
}
});
}
});
}

// Assuming you have a User class to represent the user data


public static class User {
public String email;
public String password;

public User() {
// Default constructor required for calls to
[Link]([Link])
}

public User(String email, String password) {


[Link] = email;
[Link] = password;
}
}
}

[Link](:app)

plugins {
alias([Link])
alias([Link])
}

android {
namespace = "[Link]"
compileSdk = 34

defaultConfig {
applicationId = "[Link]"
minSdk = 24
targetSdk = 34
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "[Link]"
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("[Link]"),
"[Link]"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
}

dependencies {
implementation([Link])
implementation([Link])
implementation([Link])
implementation([Link])
implementation([Link])
implementation([Link])
testImplementation([Link])
androidTestImplementation([Link])
androidTestImplementation([Link])
}

[Link](ProjectName)
// Top-level build file where you can add configuration options common to all sub-
projects/modules.
plugins {
alias([Link]) apply false
alias([Link]) apply false
}

------FOR FIREBASE AUTHENTICATION-------


[Link]
package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

import [Link];
import [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {

EditText email, password;


Button register, login;
FirebaseAuth firebaseAuth;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

email = findViewById([Link]);
password = findViewById([Link]);
register = findViewById([Link]);
login = findViewById([Link]);

firebaseAuth = [Link]();

[Link](new [Link]() {
@Override
public void onClick(View v) {
String emailStr = [Link]().toString();
String passwordStr = [Link]().toString();

[Link](emailStr,
passwordStr).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if ([Link]()) {
[Link]([Link], "Registration
Successful", Toast.LENGTH_SHORT).show();
} else {
if ([Link]() instanceof
FirebaseAuthUserCollisionException) {
[Link]([Link], "Email already in
use.", Toast.LENGTH_SHORT).show();
} else {
[Link]([Link], "Registration
Failed", Toast.LENGTH_SHORT).show();
}
}
}
});
}
});

[Link](new [Link]() {
@Override
public void onClick(View v) {
String emailStr = [Link]().toString();
String passwordStr = [Link]().toString();

[Link](emailStr,
passwordStr).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if ([Link]()) {
[Link]([Link], "Login Successful",
Toast.LENGTH_SHORT).show();
} else {
[Link]([Link], "Login Failed",
Toast.LENGTH_SHORT).show();
}
}
});
}
});
}
}

-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
--------
FirebaseEmployeeDbs

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="315dp"
android:layout_height="48dp"
android:layout_marginEnd="48dp"
android:layout_marginBottom="644dp"
android:fontFamily="@font/arima_madurai_extrabold"
android:text="Firebase Employee Database"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />

<EditText
android:id="@+id/empemail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="80dp"
android:layout_marginBottom="492dp"
android:ems="10"
android:fontFamily="@font/amaranth"
android:hint="Enter Employee Email"
android:inputType="textEmailAddress"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />

<EditText
android:id="@+id/empname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="80dp"
android:layout_marginBottom="540dp"
android:ems="10"
android:fontFamily="@font/amaranth"
android:hint="Enter Employee Name"
android:inputType="text"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />

<EditText
android:id="@+id/id"
android:layout_width="224dp"
android:layout_height="44dp"
android:layout_marginEnd="92dp"
android:layout_marginBottom="588dp"
android:ems="10"
android:fontFamily="@font/amaranth"
android:hint="Enter Employee ID"
android:inputType="number"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />

<EditText
android:id="@+id/dept"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="80dp"
android:layout_marginBottom="444dp"
android:ems="10"
android:fontFamily="@font/amaranth"
android:hint="Enter Employee Department"
android:inputType="text"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />

<EditText
android:id="@+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="80dp"
android:layout_marginBottom="396dp"
android:ems="10"
android:fontFamily="@font/amaranth"
android:hint="Enter Employee Phone No"
android:inputType="phone"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />

<Button
android:id="@+id/create"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="200dp"
android:layout_marginBottom="344dp"
android:fontFamily="@font/amaranth"
android:text="Create Records"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />

<Button
android:id="@+id/show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="48dp"
android:layout_marginBottom="344dp"
android:fontFamily="@font/amaranth"
android:text="Show Records"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />

</[Link]>

[Link]
package [Link];

public class Employee {


private int empid;
private String empname;
private String email;
private String dept;
private long phone;

public Employee(){

}
public int getEmpid() {
return empid;
}

public void setEmpid(int empid) {


[Link] = empid;
}

public String getEmpname() {


return empname;
}
public void setEmpname(String empname) {
[Link] = empname;
}

public String getEmail() {


return email;
}

public void setEmail(String email) {


[Link] = email;
}

public String getDept() {


return dept;
}

public void setDept(String dept) {


[Link] = dept;
}

public long getPhone() {


return phone;
}

public void setPhone(long phone) {


[Link] = phone;
}
}

[Link]
package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

import [Link];

public class MainActivity extends AppCompatActivity {

EditText id, empname, empemail, dept, phone;


Button create, show;

DatabaseReference dbsreference;

Employee employee;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

id = findViewById([Link]);
empname = findViewById([Link]);
empemail = findViewById([Link]);
dept = findViewById([Link]);
phone = findViewById([Link]);
create = findViewById([Link]);
employee = new Employee();

// FirebaseDatabase -> instanceget -> referenceget -> child - employee


dbsreference =
[Link]().getReference().child("Employee");
[Link](new [Link]() {
@Override
public void onClick(View v) {
[Link]([Link]([Link]().toString()));
[Link]([Link]().toString());
[Link]([Link]().toString());
[Link]([Link]().toString());
[Link]([Link]([Link]().toString()));

[Link]().setValue(employee).addOnCompleteListener([Link], new
OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if([Link]()){
[Link]([Link], "Registered
Successful!!",Toast.LENGTH_SHORT).show();
}
else{
[Link]([Link], "Registration
Failed!!",Toast.LENGTH_SHORT).show();
}
}
});
}
});

show = findViewById([Link]);
[Link](new [Link]() {
@Override
public void onClick(View v) {
[Link](new
ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
ArrayList<String> employeeList = new ArrayList<>();
for (DataSnapshot snapshot : [Link]()) {
Employee emp = [Link]([Link]);
if (emp != null) {
String employeeInfo = "ID: " + [Link]() + "\
nName: " + [Link]() +
"\nEmail: " + [Link]() + "\nDept: " +
[Link]() +
"\nPhone: " + [Link]() + "\n";
[Link](employeeInfo);
}
}
if (![Link]()) {
[Link]([Link], [Link]("\n",
employeeList), Toast.LENGTH_LONG).show();
} else {
[Link]([Link], "No employee data
found", Toast.LENGTH_SHORT).show();
}
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
[Link]([Link], "Error retrieving data",
Toast.LENGTH_SHORT).show();
}
});
}
});
}
}

--MenuProject--
option_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="[Link]
xmlns:app="[Link]
<item
android:id="@+id/open"
android:title="open"
app:showAsAction="never"/>
<item
android:id="@+id/save"
android:title="save"
app:showAsAction="never"/>
<item
android:id="@+id/edit"
android:title="edit"
app:showAsAction="never"/>
</menu>

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<[Link]
android:id="@+id/toolbar"
android:layout_width="500dp"
android:layout_height="69dp"
android:layout_marginEnd="4dp"
android:layout_marginBottom="664dp"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />

</[Link]>

[Link]
package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
toolbar = findViewById([Link]);
setSupportActionBar(toolbar);
}
//click control + 0 => type methodname to generate below
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
[Link]([Link].option_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int id = [Link]();
if(id==[Link]){
[Link](this,"you clicked: " +
[Link](),Toast.LENGTH_SHORT).show();
return true;
}
else if(id==[Link]){

[Link](this,"you clicked: " +


[Link](),Toast.LENGTH_SHORT).show();
return true;
}
else if(id==[Link]){
[Link](this,"you clicked: " +
[Link](),Toast.LENGTH_SHORT).show();
return true;
}
else{
return true;
}
}
}

You might also like