article

Sunday, December 19, 2021

Android Java Dialog Box App Version Name and Version Code

Android Java Dialog Box App Version Name and Version Code

app/src/main/res/layout/activity_main.xml
 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"
    tools:context=".MainActivity">

    <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/dialogbox"
        android:textSize="24sp" tools:text="Dialog Box" tools:textAlignment="center" />

    <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="bottom"
        android:orientation="vertical">

        <Button android:id="@+id/btnabout" android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal" android:text="@string/aboutapp" />
    </LinearLayout>
</LinearLayout>
app/src/main/java/com/myappdev/MainActivity.Java
 
package com.myappdev;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button buttonAboutMe = (Button)findViewById(R.id.btnabout);

        buttonAboutMe.setOnClickListener(new Button.OnClickListener(){

            @Override
            public void onClick(View v) {

                String strVersion;

                PackageInfo packageInfo;
                try {
                    packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
                    strVersion = "Version Name: " + packageInfo.versionName +"\n"
                            + "Version Code: " + String.valueOf(packageInfo.versionCode);
                } catch (NameNotFoundException e) {
                    e.printStackTrace();
                    strVersion = "Cannot load Version!";
                }

                new AlertDialog.Builder(MainActivity.this)
                        .setTitle("About The App!").setMessage(strVersion)
                        .setPositiveButton("OK",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {}
                                }).show();
            }
        });
    }
}
app/src/main/res/value/strings.xml
 
<resources>
    <string name="app_name">MyappDev</string>
    <string name="app_first_started">App first started</string>
    <string name="aboutapp">- About -</string>
    <string name="dialogbox">Dialog Box</string>
</resources>

Related Post