How to show app version on Preferences Activity?

I ended up with this.

enter image description here

Instead of extending classes and whatnot I went with this

SettingsActivity.java

public class SettingsActivity extends PreferenceActivity {

public static final String TAG = "caveman";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    addPreferencesFromResource(R.xml.pref_general);

    Preference pref = findPreference( "developer" );
    pref.setSummary("Marathon Apps");

    Preference pref1 = findPreference( "version" );
    try {
        pref1.setSummary(appVersion());
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);

    LinearLayout root = (LinearLayout)findViewById(android.R.id.list).getParent().getParent().getParent();
    Toolbar bar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.tool_bar, root, false);
    root.addView(bar, 0); // insert at top
    bar.setTitle("Prism");
    bar.setSubtitle("Settings");
    bar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });
}

public String appVersion() throws PackageManager.NameNotFoundException {
    PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
    String version = pInfo.versionName;
    return version;
}

}

pref_general.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="About">
<PreferenceScreen
    android:title="Developer"
    android:key="developer"
    android:summary="Marathon Apps">
</PreferenceScreen>

<PreferenceScreen
    android:title="Version"
    android:key="version"
    android:summary="1.0">
</PreferenceScreen>
</PreferenceCategory>


</PreferenceScreen>

To get version name directly to preference xml without reading from java code

add this to gradle file

 applicationVariants.all { variant ->
    variant.resValue "string", "versionName", variant.versionName }

That will inject string xml value with versionName

and in preference xml you can just get value

<Preference android:title="Version"
    android:summary="@string/versionName">

</Preference>

Complete gradle file for reference

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.app"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true
    }

    applicationVariants.all { variant ->
        variant.resValue "string", "versionName", variant.versionName
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    dataBinding {
        enabled = true
    }
    buildToolsVersion '28.0.3'
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
}