How to show app version on Preferences Activity?
Asked Answered
A

3

8

Id like to add an my app version and possibly build on the my SettingsActivity.

Im trying to show an "About Phone" style activity on my app. But I dont know how to go about it.

I am currently using EditTextPreference in my preference.xml which calls a dialog on Click, but I want it to be like this, where the onClick event doesnt produce a dialog.:

Nexus 5 screenshot

SettingsActivity.java

public class SettingsActivity extends PreferenceActivity {

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

    addPreferencesFromResource(R.xml.pref_general);
}

@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("Settings");
    bar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });
}

}

pref_general.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:title="Settings">

<PreferenceCategory android:title="About" >
    <EditTextPreference
        android:title="Version"
        android:summary="@string/app_version"
        android:key="prefUsername"/>
</PreferenceCategory>

And one more thing, is there a way to get the app and build number from gradle via xml.

I know I code like a caveman and I usually manually update them in the strings.xml, but in my defense I am new to Android.

Anaxagoras answered 7/8, 2015 at 9:58 Comment(2)
I code like a caveman -- don't beat yourself up, we've seen a lot worse here on SO ;) Have a look at this question to retrieve the build number of your app.Bate
Thanks for the encouragement broAnaxagoras
A
17

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>
Anaxagoras answered 8/8, 2015 at 8:32 Comment(0)
U
16

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
    }
}
Unwritten answered 30/3, 2019 at 15:8 Comment(2)
@ValOkafor even its show as error, after compile and run it without issue. actually "applicationVariants.all { variant -> variant.resValue "string", "versionName", variant.versionName }" this will inject varible versionName in build time. so don't add versionName in Strings xml eitherUnwritten
Lint will show a complaint when viewing preferences.xml: Cannot resolve symbol '@string/versionName' :( Not a big deal, but Google should fix that.Headrick
B
1

You can create your own custom Preference, overriding the onClick method so that it does nothing. You can even extend the EditTextPreference if you don't want to re do everything from scratch.

Remember to include the fully qualified name for the class in the pref_general.xml file.

Bilk answered 7/8, 2015 at 10:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.