Key turm in android

chain is a bidirectional constraint between two or more views that allows you to lay out the chained views in unison.

Match constraints means that the width expands to meet the definition of the horizontal constraints and margins. Therefore, the text box stretches to fill the horizontal space that remains after the button and all the margins are accounted for.

If your layout didn't turn out as expected, click See the final layout XML below to see what your XML should look like. Compare it to what you see in the Code tab. If your attributes appear in a different order, that's okay.

const val EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE"

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

   
/** Called when the user taps the Send button */
   
fun sendMessage(view: View) {
       
val editText = findViewById<EditText>(R.id.editText)
       
val message = editText.text.toString()
       
val intent = Intent(this, DisplayMessageActivity::class.java).apply {
            putExtra
(EXTRA_MESSAGE, message)
       
}
        startActivity
(intent)

   
}
}

Expect Android Studio to encounter Cannot resolve symbol errors again. To clear the errors, press Alt+Enter, or Option+Return on a Mac. Your should end up with the following imports:

import androidx.appcompat.app.AppCompatActivity
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.EditText

An error still remains for DisplayMessageActivity, but that's okay. You fix it in the next section.

Here's what's going on in sendMessage():

  • The Intent constructor takes two parameters, a Context and a Class.

    The Context parameter is used first because the Activity class is a subclass of Context.

    The Class parameter of the app component, to which the system delivers the Intent, is, in this case, the activity to start.

  • The putExtra() method adds the value of EditText to the intent. An Intent can carry data types as key-value pairs called extras.

    Your key is a public constant EXTRA_MESSAGE because the next activity uses the key to retrieve the text value. It's a good practice to define keys for intent extras with your app's package name as a prefix. This ensures that the keys are unique, in case your app interacts with other apps.

  • The startActivity() method starts an instance of the DisplayMessageActivity that's specified by the Intent. Next, you need to create that class.

0 Comments

Brand creation, trend analysis & style consulting

Lorem Ipsum has been the industry's standard dummy text ever since. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since.