Skip to content

Instantly share code, notes, and snippets.

@codingbychanche
Last active June 2, 2021 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codingbychanche/734fb9839d177550cbc2d194a554bd25 to your computer and use it in GitHub Desktop.
Save codingbychanche/734fb9839d177550cbc2d194a554bd25 to your computer and use it in GitHub Desktop.
Generic info activity for Android app's. Shows how to get your app's version tag- name from the associated- gradle file. Shows how to check if a new version of this app is available at Google Play Store.
Asociated library's (include into yout build- gradle)
// jsoup HTML parser library @ https://jsoup.org/
implementation 'org.jsoup:jsoup:1.13.1'
Layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#ffffff"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
tools:context=".InfoActivity">
<TextView
android:id="@+id/info_new_version_available"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:paddingLeft="16dp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"></TextView>
<WebView
android:id="@+id/browser"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginStart="9dp"
android:layout_marginTop="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="@+id/info_new_version_available"
app:layout_constraintTop_toBottomOf="@+id/info_new_version_available"></WebView>
<ProgressBar
android:id="@+id/html_load_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/browser"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Strings=> Shows how to implement HTML- Strings into string- resources
<!-- Info activity -->
<string name="app_title">Einsatzplan</string>
<string name="version_text">Version:</string>
<string name="version_info_update_available">
<![CDATA[
Es ist eine neuere Version dieser App im Google Play Store verfügbar. Einsatzplan Version:
]]>
</string>
<string name="version_info_ok">
<![CDATA[
Ihre App ist auf dem neustem Stand.
]]>
</string>
Activity source code:
package com.berthold.convertjobscheduletocalendar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.text.HtmlCompat;
import androidx.lifecycle.ViewModelProvider;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.ProgressBar;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class InfoActivity extends AppCompatActivity {
/**
* Shows app info...
*/
// Info
private static String tag;
// UI
private MainActivityViewModel mainActivityViewModel;
// Filesystem
private BufferedReader bufferedReader;
// Html
private StringBuilder htmlSite;
private TextView updateInfoView;
private WebView webView;
private ProgressBar progress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info);
// ViewModel
mainActivityViewModel = new ViewModelProvider(this).get(MainActivityViewModel.class);
// UI
final Handler handler = new Handler();
Context context = getApplicationContext();
updateInfoView=findViewById(R.id.info_new_version_available);
webView = (WebView) findViewById(R.id.browser);
progress = (ProgressBar) findViewById(R.id.html_load_progress);
// @rem:Get current locale (determine language from Androids settings@@
//final Locale current=getResources().getConfiguration().locale;
final String current = getResources().getConfiguration().locale.getLanguage();
//Log.v("LOCALE", "Country:" + current);
// Check if there is an update available
String currentVersion=GetThisAppsVersion.thisVersion(getApplicationContext());
getSupportActionBar().setSubtitle("Version:"+currentVersion);
String latestVersionInGooglePlay=mainActivityViewModel.getAppVersionfromGooglePlay(getApplicationContext());
if (latestVersionInGooglePlay.equals(currentVersion)) {
//updateInfoView.setText(getResources().getText(R.string.version_info_is_latest_version));
updateInfoView.setText(HtmlCompat.fromHtml(getResources().getText(R.string.version_info_ok)+"",0));
} else
updateInfoView.setText(HtmlCompat.fromHtml(getResources().getText(R.string.version_info_update_available) + latestVersionInGooglePlay,0));
// Load html...
progress.setVisibility(View.VISIBLE);
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
htmlSite = new StringBuilder();
// @rem:Shows how to load data from androids 'assests'- folder@@
if (current.equals("de") || current.equals("en")) {
if (current.equals("de"))
bufferedReader = new BufferedReader(new InputStreamReader(getAssets().open("InfoPage-de.html")));
if (current.equals("en"))
bufferedReader = new BufferedReader(new InputStreamReader(getAssets().open("InfoPage-en.html")));
} else
bufferedReader = new BufferedReader(new InputStreamReader(getAssets().open("InfoPage-en.html")));
String line;
while ((line = bufferedReader.readLine()) != null)
htmlSite.append(line);
} catch (IOException io) {
Log.v("Info", io.toString());
}
// Show
handler.post(new Runnable() {
@Override
public void run() {
progress.setVisibility(View.GONE);
webView.loadData(htmlSite.toString(), "text/html", null);
}
});
}
});
t.start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment