Kotlin Introduction : Day 2

2 min read

Dear Friends

 

Kotlin is the future of android development. Google is pushing kotlin language forward for android development. There are very good reasons what benefits it provides over java language. To know more about the differences please visit link.  Its very important for Senior Java developers for android devs  to learn Kotlin for android development.

For Junior Java devs and freshers I would also recommend Java.  In our forthcoming tutorials we will be explaining kotlin theory more.  But right now lets dive into a simple android app written in kotlin lang.

class MainActivity : AppCompatActivity() {

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

 

 

So as expected above code will  produce an Activity. Further , Lets create a Fragment and link it with the activity.

For that lets open activity_main.xml file in layout folder and add id to root layout if not previously added.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/root"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

</android.support.constraint.ConstraintLayout>

 

Now create a fragment like below for eg having a web view in which we are going to launch Google Search Page ,

 

class WebFragment : Fragment() {

    var mywebView: WebView? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        var view = inflater.inflate(R.layout.fragment_web, container, false)

        mywebView = view.findViewById(R.id.webview)

        return view;
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        mywebView!!.webViewClient = object : WebViewClient() {

            override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
                view?.loadUrl(request.toString())
                return true
            }
        }

        mywebView!!.loadUrl("https://google.co.in")


    }
}

Now come back to main activity and add this fragment to root layout.  So below code of activity will become like this

 

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.Toast

class MainActivity : AppCompatActivity(),ItemFragment.OnListFragmentInteractionListener {
    override fun onListFragmentInteraction(item: DummyContent.DummyItem?) {
        Toast.makeText(this,"This is "+item?.content,Toast.LENGTH_LONG).show();
    }

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

        // below is the added code
        var i = WebFragment()
        supportFragmentManager.beginTransaction().replace(R.id.root,i).commit()
    }
}

 

I hope you have already declared , activity inside AndroidManifest.xml file and relevant internet permission. You xml file will look like this

<?xml version="1.0" encoding="utf-8"?>
<manifest package="in.relsellglobal.logweb"
          xmlns:android="http://schemas.android.com/apk/res/android">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

 

Run your application and you will see Google search page opens up. !

 

Stay tuned for more tutorials.

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *