How to show app version on Preferences Activity?
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.:
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.
I ended up with this.
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>
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.
上一篇: 如何使用libgdx / robovm获取应用程序版本
下一篇: 如何在首选项活动中显示应用程序版本?