Basic Unity and Client-Server Request-Response with PHP

In order to make our Unity game connected to the online world, it must be able to send requests to and get responses from our server.

In this tutorial you will see the basic way to make our game able to communicate to PHP server side script.

Creating a Menu System that Hides and Shows Divs and Sections

Hi,

This is a short tutorial about creating simple menu system that if you click a menu item, it will show a div and hide another divs.

With using this system you can make a single page web app nicely.

Watch the video here:

How to Export HTML Table to Excel File with JavaScript

Let’s say we have a huge data in an HTML table. Now we need to export it as an Excel file. What to do?

Basically, we can select entire table, copy and paste it into a blank Excel document.

Or, if we code it in JavaScript, here how we can achieve that. Watch this video:

Array of cities in JS and show it on a div

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 Selatan", "Tangerang", "Bengkulu", "Gorontalo", "Kota Administrasi Jakarta Barat", "Kota Administrasi Jakarta Pusat", "Kota Administrasi Jakarta Selatan", "Kota Administrasi Jakarta Timur", "Kota Administrasi Jakarta Utara", "Sungai Penuh", "Jambi", "Bandung", "Bekasi", "Bogor", "Cimahi", "Cirebon", "Depok", "Sukabumi", "Tasikmalaya", "Banjar", "Magelang", "Pekalongan", "Purwokerto", "Salatiga", "Semarang", "Surakarta", "Tegal", "Batu", "Blitar", "Kediri", "Madiun", "Malang", "Mojokerto", "Pasuruan", "Probolinggo", "Surabaya", "Pontianak", "Singkawang", "Banjarbaru", "Banjarmasin", "Palangkaraya", "Balikpapan", "Bontang", "Samarinda", "Tarakan", "Batam", "Tanjungpinang", "Bandar Lampung", "Metro", "Ternate", "Tidore Kepulauan", "Ambon", "Tual", "Bima", "Mataram", "Kupang", "Sorong", "Jayapura", "Dumai", "Pekanbaru", "Makassar", "Palopo", "Parepare", "Palu", "Bau-Bau", "Kendari", "Bitung", "Kotamobagu", "Manado", "Tomohon", "Bukittinggi", "Padang", "Padangpanjang", "Pariaman", "Payakumbuh", "Sawahlunto", "Solok", "Lubuklinggau", "Pagaralam", "Palembang", "Prabumulih", "Binjai", "Medan", "Padang Sidempuan", "Pematangsiantar", "Sibolga", "Tanjungbalai", "Tebingtinggi", "Yogyakarta" ];
kotas.sort();
$("#kotas").html(function(){
	var c = "";
	for(var i = 0; i < kotas.length; i++){
		if(i < kotas.length-1)
			c += "<a href='#'>" + kotas[i] + "</a>, ";
		else
			c += "<a href='#'>" + kotas[i] + "</a>";
	}
	return c;
});
</script>

 

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 = directory.listFiles();

String[] localfiles = new String[files.length];
for (int i = 0; i < files.length; i++)
{
	localfiles[i] = files[i].getName();
}
ArrayAdapter<String> lbadapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, localfiles);


List<String> localfiles = new ArrayList<String>();
for (int i = 0; i < files.length; i++)
{
	if(files[i].getName().contains(".png"))
		localfiles.add(files[i].getName());
}
ArrayAdapter<String> lbadapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, localfiles);

lblist.setAdapter(lbadapter);

lblist.setOnItemClickListener(new AdapterView.OnItemClickListener() {

	@Override
	public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
		final String itemname = (String) parent.getItemAtPosition(position);
		Toast.makeText(getApplicationContext(), itemname, Toast.LENGTH_LONG).show();
	}

});

 

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";
items[2] = "Third item";

ArrayAdapter<String> lbadapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, items);

lblist.setAdapter(lbadapter);

lblist.setOnItemClickListener(new AdapterView.OnItemClickListener() {

	@Override
	public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
		final String item = (String) parent.getItemAtPosition(position);
		Toast.makeText(getApplicationContext(),item, Toast.LENGTH_LONG).show();
	}

});

By clicking on each item a toast message will be displayed containing item text.