How can I disable landscape mode in Android?

Add android:screenOrientation=”portrait” to the activity in the AndroidManifest.xml. For example: <activity android:name=”.SomeActivity” android:label=”@string/app_name” android:screenOrientation=”portrait” /> Since this has become a super-popular answer, I feel very guilty as forcing portrait is rarely the right solution to the problems it’s frequently applied to. The major caveats with forced portrait: This does not absolve you of having to … Read more

How can you get the build/version number of your Android application?

If you’re using the Gradle plugin/Android Studio, as of version 0.7.0, version code and version name are available statically in BuildConfig. Make sure you import your app’s package, and not another BuildConfig: import com.yourpackage.BuildConfig; … int versionCode = BuildConfig.VERSION_CODE; String versionName = BuildConfig.VERSION_NAME; No Context object needed! Also make sure to specify them in your … Read more

Android intent filter for a particular file extension?

Here is how I defined my activity in my AndroidManifest.xml to get this to work. <activity android:name=”com.keepassdroid.PasswordActivity”> <intent-filter> <action android:name=”android.intent.action.VIEW” /> <category android:name=”android.intent.category.DEFAULT” /> <category android:name=”android.intent.category.BROWSABLE” /> <data android:scheme=”file” /> <data android:mimeType=”*/*” /> <data android:pathPattern=”.*\\.kdb” /> <data android:host=”*” /> </intent-filter> </activity> The scheme of file indicates that this should happen when a local file is … Read more

“Protected Apps” setting on Huawei phones, and how to handle it

There isn’t a setting in the manifest, and Huawei has enabled Tinder because it’s a popular app. There isn’t a way to know if apps are protected. Anyway I used ifHuaweiAlert() in onCreate() to show an AlertDialog: private void ifHuaweiAlert() { final SharedPreferences settings = getSharedPreferences(“ProtectedApps”, MODE_PRIVATE); final String saveIfSkip = “skipProtectedAppsMessage”; boolean skipMessage = … Read more

Android intent filter: associate app with file extension

You need multiple intent filters to address different situation you want to handle. Example 1, handle http requests without mimetypes: <intent-filter> <action android:name=”android.intent.action.VIEW” /> <category android:name=”android.intent.category.BROWSABLE” /> <category android:name=”android.intent.category.DEFAULT” /> <data android:scheme=”http” /> <data android:host=”*” /> <data android:pathPattern=”.*\\.pdf” /> </intent-filter> Handle with mimetypes, where the suffix is irrelevant: <intent-filter> <action android:name=”android.intent.action.VIEW” /> <category android:name=”android.intent.category.BROWSABLE” /> … Read more

Android studio adds unwanted permission after running application on real device

compile ‘com.google.android.gms:play-services:+’ This library will request location permissions, as several pieces of Play Services need it. First, never use +. If you want to allow free-floating patchlevels (e.g., 22.0.+), that’s not completely insane, but using + for the version is insane. Next, consider using one (or more) of the more focused dependencies, rather than the … Read more