Skip to content
This repository was archived by the owner on Jun 22, 2022. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ apply plugin: 'com.android.application'

android {
compileSdkVersion Integer.parseInt(project.ANDROID_COMPILE_SDK_VERSION)
buildToolsVersion project.ANDROID_BUILD_TOOLS_VERSION

defaultConfig {
applicationId "com.akexorcist.localizationapp"
Expand All @@ -22,6 +21,7 @@ android {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
buildToolsVersion = '27.0.3'
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
import android.view.View;

import com.akexorcist.localizationactivity.ui.LocalizationActivity;
import com.akexorcist.localizationapp.customactivity.CustomActivity;
import com.akexorcist.localizationapp.customactivity.SimpleCustomActivity;
import com.akexorcist.localizationapp.nestedfragment.NestedFragmentActivity;
import com.akexorcist.localizationapp.simpleactivity.SimpleActivity;
import com.akexorcist.localizationapp.simplefragment.SimpleFragmentActivity;
import com.akexorcist.localizationapp.stackedactivity.Stack1Activity;
import com.akexorcist.localizationapp.viewpager.ViewPagerActivity;

public class MainActivity extends LocalizationActivity {
public class MainActivity extends CustomActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
Expand All @@ -30,30 +31,30 @@ public void onCreate(Bundle savedInstanceState) {
}

public View.OnClickListener onSimpleActivityClick() {
return view -> goToActivity(SimpleActivity.class);
return view -> openActivity(SimpleActivity.class);
}

public View.OnClickListener onCustomActivityClick() {
return view -> goToActivity(SimpleCustomActivity.class);
return view -> openActivity(SimpleCustomActivity.class);
}

public View.OnClickListener onStackedActivityClick() {
return view -> goToActivity(Stack1Activity.class);
return view -> openActivity(Stack1Activity.class);
}

public View.OnClickListener onSimpleFragmentClick() {
return view -> goToActivity(SimpleFragmentActivity.class);
return view -> openActivity(SimpleFragmentActivity.class);
}

private View.OnClickListener onNestedFragmentClick() {
return view -> goToActivity(NestedFragmentActivity.class);
return view -> openActivity(NestedFragmentActivity.class);
}

public View.OnClickListener onViewPagerClick() {
return view -> goToActivity(ViewPagerActivity.class);
return view -> openActivity(ViewPagerActivity.class);
}

public void goToActivity(Class<?> activity) {
public void openActivity(Class<?> activity) {
Intent intent = new Intent(this, activity);
startActivity(intent);
}
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'
classpath 'com.android.tools.build:gradle:3.2.0-alpha08'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ POM_LICENCE_DIST=repo
POM_DEVELOPER_ID=Akexorcist
POM_DEVELOPER_NAME=Somkiat Khitwongwattana

ANDROID_BUILD_TOOLS_VERSION=26.0.2
ANDROID_COMPILE_SDK_VERSION=26
ANDROID_TARGET_SDK_VERSION=26
ANDROID_BUILD_TOOLS_VERSION=27.0.3
ANDROID_COMPILE_SDK_VERSION=27
ANDROID_TARGET_SDK_VERSION=27
ANDROID_MIN_SDK=17
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
2 changes: 1 addition & 1 deletion localizationActivity/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ apply plugin: 'com.android.library'

android {
compileSdkVersion Integer.parseInt(project.ANDROID_COMPILE_SDK_VERSION)
buildToolsVersion project.ANDROID_BUILD_TOOLS_VERSION

defaultConfig {
minSdkVersion Integer.parseInt(project.ANDROID_MIN_SDK)
Expand All @@ -18,6 +17,7 @@ android {
res.srcDirs = ['src/main/res']
}
}
buildToolsVersion = '27.0.3'
}

dependencies {
Expand Down
9 changes: 1 addition & 8 deletions localizationActivity/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.akexorcist.localizationactivity">

<application>
<activity
android:name=".ui.BlankDummyActivity"
android:theme="@android:style/Theme.Black.NoTitleBar" />
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
import android.os.Bundle;
import android.os.Handler;
import android.os.LocaleList;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.util.DisplayMetrics;

import com.akexorcist.localizationactivity.ui.BlankDummyActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -83,14 +83,11 @@ public Context getApplicationContext(Context applicationContext) {
}

public Resources getResources(Resources resources) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
Configuration config = resources.getConfiguration();
config.locale = LanguageSetting.getLanguage(activity);
DisplayMetrics metrics = resources.getDisplayMetrics();
return new Resources(activity.getAssets(), metrics, config);
} else {
return resources;
}
Configuration config = resources.getConfiguration();
Locale locale = LanguageSetting.getLanguage(activity);
config.setLocale(locale);
DisplayMetrics metrics = resources.getDisplayMetrics();
return new Resources(activity.getAssets(), metrics, config);
}

// Provide method to set application language by country name.
Expand Down Expand Up @@ -156,12 +153,14 @@ private void setupLocale(Locale locale) {


private void updateLocaleConfiguration(Context context, Locale locale) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
Configuration config = context.getResources().getConfiguration();
Configuration config = context.getResources().getConfiguration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
config.setLocale(locale);
} else {
config.locale = locale;
DisplayMetrics dm = context.getResources().getDisplayMetrics();
context.getResources().updateConfiguration(config, dm);
}
DisplayMetrics dm = context.getResources().getDisplayMetrics();
context.getResources().updateConfiguration(config, dm);
}

// Avoid duplicated setup
Expand All @@ -173,17 +172,15 @@ private boolean isCurrentLanguageSetting(Context context, Locale locale) {
private void notifyLanguageChanged() {
sendOnBeforeLocaleChangedEvent();
activity.getIntent().putExtra(KEY_ACTIVITY_LOCALE_CHANGED, true);
callDummyActivity();
activity.recreate();
recreate(activity, true);
}

// Check if locale has change while this activity was run to back stack.
private void checkLocaleChange(Context context) {
if (!isCurrentLanguageSetting(context, currentLanguage)) {
sendOnBeforeLocaleChangedEvent();
isLocalizationChanged = true;
callDummyActivity();
activity.recreate();
recreate(activity, true);
}
}

Expand All @@ -207,7 +204,33 @@ private void sendOnAfterLocaleChangedEvent() {
}
}

private void callDummyActivity() {
activity.startActivity(new Intent(activity, BlankDummyActivity.class));
/**
* Helper method to recreate the Activity. This method should be called after a Locale change.
* @param activity the Activity that will be recreated
* @param animate a flag indicating if the recreation will be animated or not
*/
public static void recreate(Activity activity, boolean animate) {
Intent restartIntent = new Intent(activity, activity.getClass());

Bundle extras = activity.getIntent().getExtras();
if (extras != null) {
restartIntent.putExtras(extras);
}

if (animate) {
ActivityCompat.startActivity(
activity,
restartIntent,
ActivityOptionsCompat
.makeCustomAnimation(activity, android.R.anim.fade_in, android.R.anim.fade_out)
.toBundle()
);
} else {
activity.startActivity(restartIntent);
activity.overridePendingTransition(0, 0);
}

activity.finish();

}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.