Created
September 29, 2012 12:41
-
-
Save teppeihomma/3803888 to your computer and use it in GitHub Desktop.
バージョンを表示するView Widget
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<string name="version">Version: %s</string> | |
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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