How to check is the device connected to internet or not in Android

With this simple snippet, we can check is the device is connected to internet or not: private boolean isConnected(){ ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); return activeNetworkInfo != null && activeNetworkInfo.isConnected(); } So this “isConnected()” returns true if it’s connected, otherwise it...

Simple clickable Android ListView example

Follow this short tutorial to create a ListView in Android. First let’s create the list view element in our layout file: <ListView android:id=”@+id/lblistview” android:layout_width=”match_parent” android:layout_height=”match_parent” android:layout_marginBottom=”8dp” android:layout_marginEnd=”8dp” android:layout_marginStart=”8dp” android:layout_marginTop=”8dp” app:layout_constraintBottom_toBottomOf=”parent” app:layout_constraintEnd_toEndOf=”parent” app:layout_constraintStart_toStartOf=”parent” app:layout_constraintTop_toTopOf=”parent” /> Then in your Java file: lblist = (ListView)findViewById(R.id.lblistview); String[] items =...

JSON to Object and Array in Java

Try this snippet to convert JSON text into object and/or array in Java: JSONObject object = new JSONObject(jsontext); JSONArray array = jsonObj.getJSONArray(“somearray”); for (int i = 0; i < somearray.length(); i++) { JSONObject item = somearray.getJSONObject(i); String somevalue = item.getString(“somekey”); } jsontext is your JSON...

Listing all files in a directory in Android

Let’s say we have a directory in device’s storage called “mydir” and there are files inside it. By using this snippet we can list all names of that files and folders: String path = Environment.getExternalStorageDirectory().getAbsolutePath() + “/maktabamajaziya”; String downloadedbooks = “”; File directory = new...