In this example, I have a bunch of city names as an array variable in JavaScript. Then I want to make a list of links for all the city names. Here I do it using jQuery: <div id=”kotas”></div> <script> var kotas = [ “Banda Aceh”, “Langsa”, “Lhokseumawe”, “Meulaboh”, “Sabang”, “Subulussalam”, “Denpasar”, “Pangkalpinang”, “Cilegon”, “Serang”, “Tangerang…
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 returns false.
Listing image files in a directory and populate a ListView from it in Android
I have a directory in sdcard called “mydir” and there are lot of .png image files with another files. Then I want to list only that .png files and populate a ListView from it. I do it like this: String path = “/sdcard/mydir”; File directory = new File(path); if (!directory.exists()){ directory.mkdir(); } File[] files =…
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 = new String[3]; items[0] = “First item”; items[1] = “Second item”;…
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 text.
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 File(path); File[] files = directory.listFiles(); for (int i = 0;…
Requesting permission in run time in Android
If we are making an app for Android greater than SDK 23, we need to request some sensitive permissions (for example, to write file in external storage) in run time. Here is a snippet to do so: if (Build.VERSION.SDK_INT >= 23) { if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { } else { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1); }…
If a folder (directory) is not existed, create it – Android Java Snippet
With this snippet, we are able to check if a folder existed in device’s storage or not. If not, then we create it. Here is the snippet: File directory = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + “/afolder”); if (! directory.exists()){ directory.mkdir(); }
Simple PHP read and write to a text file
This code snippet reads a text file that if it is not existed it will create one with some text in it. Next time this code is executed, if the file existed, PHP echoes the content of the file. <?php $settingfile = “settings.txt”; $filelink = fopen($settingfile, “r”); $filecontent = fread($filelink, filesize($settingfile)); echo $filecontent; fclose($filelink); $newContents…
Changing the skybox material programmatically in Unity
I’m using this simple script after attaching it to UI Buttons to change my skybox materials. Let’s say we have two skybox materials with two UI Buttons to switch between them. Here is the script: using System.Collections; using System.Collections.Generic; using UnityEngine; public class ChangeSky : MonoBehaviour { public Material sky1; public Material sky2; public void…
Unity click and drag script to rotate camera
Just googling around to find how to rotate camera with mouse click and drag. Thanks to this link: https://answers.unity.com/questions/1189946/click-and-drag-to-rotate-camera-like-a-pan.html This code is originally taken from that link: using System.Collections; using System.Collections.Generic; using UnityEngine; public class CamRotate : MonoBehaviour { public float speed = 3.5f; private float X; private float Y; void Update() { if(Input.GetMouseButton(0)) { transform.Rotate(new…
Unity iOS ‘GoogleMobileAds/GoogleMobileAds.h’ file not found
Another one of common errors on building Unity games in Mac when we compile it using XCode is this error that states: ‘GoogleMobileAds/GoogleMobileAds.h’ file not found The quick solution is to reimport GoogleMobileAds plugin. That’s it.
Solving mach-o linker error on publishing Unity – XCode
One of common error when we want to build our Unity game from XCode is this mach-o linker error. There are many solutions on internet but in my case, the solution is this simple one: Change target build to 8.0 On Build Settings, set Enable Module to Yes and Enable Bitcode to No.
JavaScript random numbers examples
Generating random numbers is a common thing in programming. Here is some examples in JavaScript: //Basic Random Math.random(); //From 0 to specific number Math.random() * 10; //Removing decimals Math.floor(Math.random() * 10); //Random between 5 and 10 Math.floor(Math.random() * 5) + 5; //Random between 5 and 10 (included 10) Math.floor(Math.random() * 5) + 6;
Creating Depth of Field Effect in Blender
Depth of Field effect in 3D rendering adds realism to it. It’s easy how to create this effect in Blender. To create DOF effect in Blender first we need to adjust our camera’s focus distance, then add defocus effect on render node as I’ll show you in this tutorial.
Showing Poly and Vert Statistics in 3Ds Max
If you sell 3D models online, sometimes marketplaces require poly count or triangles count of your object. In 3Ds Max, it’s easy to show that statistics. Press 7 and it will appear in your viewport. If you need more stat details, go to Views > Viewport Configuration > Statistics.
PHP & MySQL useful snippets
For us, working with database in web development is a mandatory task. Here in this post I’m going to list, as much as I know, PHP & MySQL snippets that I frequently use.
Inserting Arabic text with PHP into MySQL Database
We need to set the encoding of texts we store into database to UTF-8 if we work with Arabic texts. Otherwise, Arabic texts appear like ?????????? in our database.
Using Command Prompt to render Blender animations
We don’t need to open Blender to render our animation to make rendering progress faster. After you set everything, close your Blender and run Command Prompt (in Windows), then navigate to your Blender program directory:
Blender quick tips and shortcuts
Working with blender is much faster and easier if you know most of it’s keyboard shortcuts. Here in this post I’m going to list them as much as I can: