battle programming a forum for elite programmers with extreme will power to sharpen theire skills
 
HomeSearchRegisterLog in
Share
 

 kotlin AS PL grimoire

Go down 
Go to page : Previous  1, 2
AuthorMessage
kurosen
codding
codding
kurosen

Posts : 105
Join date : 2012-04-17

kotlin AS PL grimoire - Page 2 Empty
PostSubject: Kotlin get url source code as text   kotlin AS PL grimoire - Page 2 EmptyFri Jun 28, 2019 11:31 pm


Code:
package com.example.json3

import android.os.AsyncTask
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
import org.json.JSONObject
import java.io.IOException
import java.io.InputStreamReader
import java.lang.Exception
import java.net.HttpURLConnection
import java.net.URL
import android.widget.Toast
import java.io.BufferedReader


class MainActivity : AppCompatActivity() {


    private var xmlString = ""
    private val XML_URL = "https://www.yotamarker.com"
    var XML_UR= "https://reqres.in/api/users/2"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        getDataJSON()
        try {
            Thread.sleep(6000)
        } catch (e: InterruptedException) {
            e.printStackTrace()
        }

        txt1.text = xmlString


    }

    fun getDataJSON() {
        //we never use void in AsyncTask, we need to use Void
        object : AsyncTask<Void, Void, String>() {
            override fun doInBackground(vararg voids: Void): String {
                //we need to open HTTP URL Connection to our desired URL (www.boi.org.il)
                var connection: HttpURLConnection? = null
                try {
                    connection = URL(XML_URL).openConnection() as HttpURLConnection
                    val buf = BufferedReader(InputStreamReader(connection.inputStream))
                    var line: String?
                    line = buf.readLine()
                    while (line != null) {
                        line = buf.readLine()
                        xmlString += line
                    }
                } catch (e: IOException) {
                    e.printStackTrace()
                } finally {
                    connection!!.disconnect()
                }

                return xmlString
            }

            override fun onPostExecute(jsonString: String) {
                super.onPostExecute(jsonString)
                http@ http://Log.e("XML", "onPostExecute: " + jsonString);
                // display json string
                Toast.makeText(applicationContext, jsonString, Toast.LENGTH_LONG).show()

            }
        }.execute()
    }

}

in the manifest :
<uses-permission android:name="android.permission.INTERNET"></uses-permission>

chobit
Back to top Go down
kurosen
codding
codding
kurosen

Posts : 105
Join date : 2012-04-17

kotlin AS PL grimoire - Page 2 Empty
PostSubject: kotlin getting JSON info   kotlin AS PL grimoire - Page 2 EmptySat Jun 29, 2019 12:51 am

here I used the swap api to get Luke Skywalker :

here is the swap if you want to play : https://swapi.co/

txt1 is the default textbook of the kotlin empty main activity
add internet permission to the manifest

Code:
package com.example.json3

import android.os.AsyncTask
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
import org.json.JSONObject
import java.io.IOException
import java.io.InputStreamReader
import java.lang.Exception
import java.net.HttpURLConnection
import java.net.URL
import android.widget.Toast
import java.io.BufferedReader
import org.json.JSONException




class MainActivity : AppCompatActivity() {


    private var jsonString = ""
    private val jsonURL = "https://swapi.co/api/people/1/"
    //var XML_UR= "https://reqres.in/api/users/2"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        getDataJSON()
        try {
            Thread.sleep(6000)
        } catch (e: InterruptedException) {
            e.printStackTrace()
        }

        http://txt1.text = jsonString
        parseJson(jsonString)

    }

    fun getDataJSON() {
        //we never use void in AsyncTask, we need to use Void
        object : AsyncTask<Void, Void, String>() {
            override fun doInBackground(vararg voids: Void): String {
                //we need to open HTTP URL Connection to our desired URL (www.boi.org.il)
                var connection: HttpURLConnection? = null
                try {
                    connection = URL(jsonURL).openConnection() as HttpURLConnection
                    val buf = BufferedReader(InputStreamReader(connection.inputStream))
                    var line: String? = buf.readLine()
                    while (line != null) {
                        jsonString += line
                    }
                } catch (e: IOException) {
                    e.printStackTrace()
                } finally {
                    connection!!.disconnect()
                }

                return jsonString
            }

            override fun onPostExecute(jsonString: String) {
                super.onPostExecute(jsonString)
                http://Log.e("XML", "onPostExecute: $jsonString")
                parseJson(jsonString)
            }
        }.execute()
    }

    private fun parseJson(jsonString: String) {
        val jsonObj = JSONObject(jsonString)
        val namee = jsonObj.getString("name")

        txt1.text= namee

    }

}
Back to top Go down
Moti Barski
super
super
Moti Barski

Posts : 394
Join date : 2011-08-02

kotlin AS PL grimoire - Page 2 Empty
PostSubject: kotlin get battery data   kotlin AS PL grimoire - Page 2 EmptyWed Jul 10, 2019 7:15 pm

it shows number of times displayed + battery level
also notice in the onReceive interface there are vals that can show
if charging, and by AC or usb charging

Code:
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.BatteryManager
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    var b8TRcounter = 0
    private val mBatInfoReceiver = object : BroadcastReceiver() {
        override fun onReceive(ctxt: Context, intent: Intent) {
            val level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0)
            b8TRcounter++;
            txtBox.setText("$level% $b8TRcounter")
            val status: Int = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1)
            val isCharging: Boolean = status == BatteryManager.BATTERY_STATUS_CHARGING
                    || status == BatteryManager.BATTERY_STATUS_FULL

            val chargePlug: Int = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1)
            val usbCharge: Boolean = chargePlug == BatteryManager.BATTERY_PLUGGED_USB
            val acCharge: Boolean = chargePlug == BatteryManager.BATTERY_PLUGGED_AC
            //txtBox.setText("$usbCharge")
        }
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        this.registerReceiver(this.mBatInfoReceiver, IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    }

}



nice cheers

_________________
MB over and out emp it up
Back to top Go down
Moti Barski
super
super
Moti Barski

Posts : 394
Join date : 2011-08-02

kotlin AS PL grimoire - Page 2 Empty
PostSubject: kotlin play audio files one after another   kotlin AS PL grimoire - Page 2 EmptyWed Jul 31, 2019 8:26 pm


assumong you put the files in a raw dir that you create in the res dir


Code:
 mediaPlayer = MediaPlayer.create(this, R.raw.hadouken)
                mediaPlayer.setNextMediaPlayer(MediaPlayer.create(this, R.raw.shouryuken))
                mediaPlayer.start()

battle programming

_________________
MB over and out emp it up
Back to top Go down
Moti Barski
super
super
Moti Barski

Posts : 394
Join date : 2011-08-02

kotlin AS PL grimoire - Page 2 Empty
PostSubject: kotlin hide action bar (top blue strip with app name)   kotlin AS PL grimoire - Page 2 EmptyWed Jul 31, 2019 8:34 pm

Code:
supportActionBar?.hide()

put it in the on create like this :

Code:
override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        supportActionBar?.hide()
        this.registerReceiver(this.mBatInfoReceiver, IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    }

hadouken hadouken hadouken alert

_________________
MB over and out emp it up
Back to top Go down
Moti Barski
super
super
Moti Barski

Posts : 394
Join date : 2011-08-02

kotlin AS PL grimoire - Page 2 Empty
PostSubject: kotloin tts   kotlin AS PL grimoire - Page 2 EmptyFri Aug 16, 2019 9:25 pm

activity_main.xml

Code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                            xmlns:tools="http://schemas.android.com/tools"
                                            android:layout_width="match_parent"
                                            android:layout_height="match_parent"
                                            tools:context="com.tutorialkart.texttospeechapp.MainActivity">

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:gravity="center">
        <EditText
                android:id="@+id/edittext_input"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10" >
            <requestFocus />
        </EditText>

        <Button
                android:id="@+id/button_speak"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Speak" />
    </LinearLayout>
</android.support.constraint.ConstraintLayout>

Main/activity.kt

Code:

package com.yotamarker.kotlintts

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.speech.tts.TextToSpeech
import android.util.Log
import android.widget.Button
import android.widget.EditText
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*

class MainActivity : AppCompatActivity(), TextToSpeech.OnInitListener {

    private var tts: TextToSpeech? = null
    private var buttonSpeak: Button? = null
    private var editText: EditText? = null

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

        buttonSpeak = this.button_speak
        editText = this.edittext_input

        buttonSpeak!!.isEnabled = false;
        tts = TextToSpeech(this, this)

        buttonSpeak!!.setOnClickListener { speakOut() }
    }

    override fun onInit(status: Int) {

        if (status == TextToSpeech.SUCCESS) {
            // set US English as language for tts
            val result = tts!!.setLanguage(Locale.US)

            if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TTS","The Language specified is not supported!")
            } else {
                buttonSpeak!!.isEnabled = true
            }

        } else {
            Log.e("TTS", "Initilization Failed!")
        }

    }

    private fun speakOut() {
        val text = editText!!.text.toString()
        tts!!.speak(text, TextToSpeech.QUEUE_FLUSH, null,"")
    }

    public override fun onDestroy() {
        // Shutdown TTS
        if (tts != null) {
            tts!!.stop()
            tts!!.shutdown()
        }
        super.onDestroy()
    }

}

not going to lie humiliation

_________________
MB over and out emp it up
Back to top Go down
Moti Barski
super
super
Moti Barski

Posts : 394
Join date : 2011-08-02

kotlin AS PL grimoire - Page 2 Empty
PostSubject: kotlin onLongClick listener   kotlin AS PL grimoire - Page 2 EmptySun Nov 03, 2019 9:50 am

place this code in the MainActivity onCreate function

Code:
imageView2.setOnLongClickListener {
            mbTTS.voiceIt("hadouken")
            true
        }

1 replace imageView2 with the view yto have long click
2 replace mbTTS.voiceIt("hadouken") with the code to run when said view has been long clicked geek

in the xml I enable the onLongOnClick attribute of the view.

_________________
MB over and out emp it up
Back to top Go down
 
kotlin AS PL grimoire
Back to top 
Page 2 of 2Go to page : Previous  1, 2

Permissions in this forum:You cannot reply to topics in this forum
battle programmers alliance :: battle programming alliance :: battle programming-
Jump to: