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 8: Cleartext HTTP traffic not permitted

According to Network security configuration – Starting with Android 9 (API level 28), cleartext support is disabled by default. Also have a look at Android M and the war on cleartext traffic Codelabs explanation from Google Option 1 – First try hitting the URL with https:// instead of http:// Option 2 – Create file res/xml/network_security_config.xml … Read more

How can I open a URL in Android’s web browser from my application?

Try this: Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(“http://www.google.com”)); startActivity(browserIntent); That works fine for me. As for the missing “http://” I’d just do something like this: if (!url.startsWith(“http://”) && !url.startsWith(“https://”)) url = “http://” + url; I would also probably pre-populate your EditText that the user is typing a URL in with “http://”.

How to get screen dimensions as pixels in Android

If you want the display dimensions in pixels you can use getSize: Display display = getWindowManager().getDefaultDisplay(); Point size = new Point(); display.getSize(size); int width = size.x; int height = size.y; If you’re not in an Activity you can get the default Display via WINDOW_SERVICE: WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); Display display = wm.getDefaultDisplay(); If you … Read more

How to lazy load images in ListView in Android

Here’s what I created to hold the images that my app is currently displaying. Please note that the “Log” object in use here is my custom wrapper around the final Log class inside Android. package com.wilson.android.library; /* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file … Read more

What is ‘Context’ on Android?

Putting it simply: As the name suggests, it’s the context of the current state of the application/object. It lets newly-created objects understand what has been going on. Typically you call it to get information regarding another part of your program (activity and package/application). You can get the context by invoking getApplicationContext(), getContext(), getBaseContext() or this … Read more

How do I center text horizontally and vertically in a TextView?

I’m assuming you’re using XML layout. <TextView android:layout_width=”match_parent” android:layout_height=”match_parent” android:gravity=”center” android:text=”@string/**yourtextstring**” /> You can also use gravity center_vertical or center_horizontal according to your need. As @stealthcopter commented, in java: .setGravity(Gravity.CENTER);. And for Kotlin users, .gravity = Gravity.CENTER

How can I fix ‘android.os.NetworkOnMainThreadException’?

NOTE : AsyncTask was deprecated in API level 30. AsyncTask | Android Developers This exception is thrown when an application attempts to perform a networking operation on its main thread. Run your code in AsyncTask: class RetrieveFeedTask extends AsyncTask<String, Void, RSSFeed> { private Exception exception; protected RSSFeed doInBackground(String… urls) { try { URL url = … Read more

error code: 521