Lesson 12: Launch a second “Activity” with Values (parameters)

Hello viewer, You can now have our Tutorial Lessons in your android mobile device and read it offline.
Download kotlin Programming APP on PlayStore
Download Website SEO Lessons APP on PlayStore

Contents

We saw in the previous concept that a program can have more than one window representing each window with a class that inherits from AppCompatActivity.
A very common situation is that the first window needs to send data to the second so that from these proceed to perform an action.

Issue:

Make a program that requests the input of an address of a website and then open a second window that shows that page.

To solve this problem we will use the visual control WebView that allows us to display the content of a website.

We will create a new project called project012.

1 – Our first Activity will have the following visual interface (see controls):

Lesson 12: Launch a second

We have a type control EditText (ID = et1, hint = “Enter a website”) and another of type Button (initialize the id property with the with the Value “button1”).

The source code for this Activity is:

package com.coding180.project012


import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText

class MainActivity: AppCompatActivity () {
    override fun onCreate (savedInstanceState: Bundle?) {
        super.onCreate (savedInstanceState)
        setContentView (R.layout.activity_main)
// coding180.com
        val et1 = findViewById (R.id.et1) as EditText
        val button1 = findViewById (R.id.button1) as Button
        button1.setOnClickListener {
            val intent1 = Intent (this, WebviewActivity :: class.java)
            intent1.putExtra ("address", et1.text.toString())
            startActivity (intent1)
        }
    }
}

As we can see the difference with the previous concept is that we call the putExtra method of the Intent class. It has two parameters of type String, in the first we indicate the name of the data and in the second the value of the data:

        Boton1.setOnClickListener {
            Val attempt1 = Intent (this, Activity2 :: class.java)
            Attempt1.putExtra ("address", et1.text.toString ())
            StartActivity (intent1)
        }

The second visual interface (remember that we must press the right button in the Project window and select New -> Activity -> Emplty Activity):

 

Lesson 12: Launch a second

In the dialog that appears we specify the name of our second Activity and we will call it WebviewActivity.

 

We have an object of the WebView class that is in the “Containers” tab and a Button:

 

Lesson 12: Launch a second

We encode the functionality of the second activity

package com.coding180.project001


import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.webkit.WebView
import android.widget.Button
import android.widget.EditText
import android.widget.TextView

class WebviewActivity: AppCompatActivity () {

    override fun onCreate (savedInstanceState: Bundle?) {
        super.onCreate (savedInstanceState)
        setContentView (R.layout.webview)
// coding180.com
        val bundle = intent.extras
                val data = bundle.getString ("address")
        val webView1 = findViewById (R.id.webView1) as WebView
        webView1.loadUrl("http://${data}")
        val button1 = findViewById (R.id.button1) as Button
                button1.setOnClickListener {
            finish()
        }
    }
}

In this class we define a Bundle variable and initialize it with the value of the extra property of the Activity property of the Activity (this is done to retrieve the parameter (s) that the other Activity sent):

        Val bundle = intent.extras
        Val data = bundle.getString ("address")
        Val webView1 = findViewById (R.id.webView1) as WebView
        WebView1.loadUrl ("http://${data)")

The loadUrl method of the WebView class allows you to view the content of a website.

Important

As our application must access the internet we must make a configuration in the file “AndroidManifest.xml”, we can locate this file.

We added the permission by typing the following in this file:

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

Then the AndroidManifest.xml file is left with the added permission:

Lesson 12: Launch a second

Now our program should be fully functional.

The first window should look something like this:

 

Lesson 12: Launch a second

….

About The Author

Related posts

Leave a Reply