Network Requests with Volley

Consider Volley a 2.0 version of Android Asynchronous Http Client.

A major advantage of Volley over ASyncTask is that you can do multiple requests simultaneously without the overhead of thread management.

Gradle

Add

implementation 'com.android.volley:volley:1.2.1'

to your module gradle dependencies.

Basic Anatomy of a Volley Request

val queue = Volley.newRequestQueue(context)
val stringRequest = StringRequest(
    Request.Method.GET, url,
    { response ->
        Log.e(TAG, "success")
    },
    {
        Log.e(TAG, "error")
    })
queue.add(stringRequest)

Localization of Android Apps

If you want to add another language for your app do the following

  • Create a new folder e.g. app/src/main/res/values-de
  • Add strings.xml into this folder
  • Translate the content

The cool thing about translation in android is that you can do it incrementally: when there is no translation for a string resource in a locale Android will automagically use the value from /res/values/strings.xml

Use github with tokens

I got an error accessing github from Android Studio

It seems that github disabled the usage of user/pw authentification.

To fix it you have to

  • Go to https://github.com/settings/tokens
  • Generate new token
  • enable “repo”, “admin:org”, “gist”

Now you can use the token from within Android Studio

Play sound on Android

Resource files shall be placed under app\src\main\res\raw

import android.media.AudioManager
import android.media.SoundPool
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity


class MainActivity : AppCompatActivity() {
    private var mCSoundOne: Int = 0
    private lateinit var mSoundPool: SoundPool
    private var mLoaded = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        mSoundPool = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Log.d("SoundPoolTest", "Building Soundpool")
            SoundPool.Builder().setMaxStreams(1).build()
        } else {
            SoundPool(1, AudioManager.STREAM_MUSIC, 1)
        }
        mSoundPool.setOnLoadCompleteListener(SoundPool.OnLoadCompleteListener { soundPool, sampleId, status ->
            Log.d("SoundPoolTest", "Load Completed")
            mLoaded = true
        })
        Log.d("SoundPoolTest", "Loading wav")
        mCSoundOne = mSoundPool.load(applicationContext, R.raw.eins, 1)

    }

    fun play_one(view: View?) {
        Log.d("SoundPoolTest", "Button Clicked")
        if (mLoaded) {
            Log.d("SoundPoolTest", "Playing wav")
            mSoundPool.play(mCSoundOne, 1.0f, 1.0f, 0, 0, 1.0f)
        }
    }
}