Skip to content

Instantly share code, notes, and snippets.

@teppeihomma
Created September 29, 2012 12:41
Show Gist options
  • Save teppeihomma/3803888 to your computer and use it in GitHub Desktop.
Save teppeihomma/3803888 to your computer and use it in GitHub Desktop.
バージョンを表示するView Widget
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="version">Version: %s</string>
</resources>
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.util.AttributeSet;
import android.widget.TextView;
public class VersionView extends TextView {
public VersionView(Context context) {
this(context, null);
}
public VersionView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public VersionView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
PackageManager packageManager = context.getPackageManager();
String version = null;
try {
PackageInfo packageInfo = packageManager.getPackageInfo(
context.getPackageName(), PackageManager.GET_ACTIVITIES);
version = packageInfo.versionName;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
this.setText(context.getString(R.string.version, version));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment