OFFICIAL HUSSAIN SOFTWARES WEBSITE 2018
WELCOME TO THE OFFICIAL HUSSAIN SOFTWARES WEBSITE PLEASE REGISTER FIRST AFTER YOU ARE ABLE TO DOWNLOAD ALL FREE SOFTWARES FOR MORE FREE SOFTWARES FOR COMPUTER AND MOBILES Visit

www.hussain-softwares.forumarabia.com
Like us on facebook
Latest topics
Search
 
 

Display results as :
 

 


Rechercher Advanced Search

online counter
tools webmaster


Please do not change this code for a perfect fonctionality of your counter

my info viewer
tools webmaster


Please do not change this code for a perfect fonctionality of your counter

intelligent team flag counter
Flag Counter
Keywords

Mail  480p  Cracker  china  Hindi  SMTP  NIMBUZZ  maker  movie  


Lesson 5, learn how to do coding on your Android mobile

Go down

Lesson 5, learn how to do coding on your Android mobile Empty Lesson 5, learn how to do coding on your Android mobile

Post by hussain on Sat Dec 15, 2018 5:31 pm


  • Android Developers
     

  •  Docs
     

  •  Guides


Start another activity


  • Contents
  • Respond to the send button
  • Build an Intent
  • Create the second activity
  • Add a text view
  • Display the message
  • Add up navigation
  • Run the app


After completing the previous lesson, you have an app that shows an activity (a single screen) with a text field and a button. In this lesson, you’ll add some code to 
Code:
MainActivity
[size]
 that starts a new activity to display the message when the user taps Send.
Note: This lesson expects that you are using Android Studio 3.0 or higher.

Respond to the send button

Add a method to the 
[/size]
Code:
MainActivity
[size]
 class that's called by the button as follows:
[/size][list="box-sizing: inherit; margin-right: 0px; margin-left: 0px; padding-left: 40px; list-style-position: outside; list-style-image: initial;"]
[*]In the file app > java > com.example.myfirstapp > MainActivity, add the 
Code:
sendMessage()
method stub as shown below:

KOTLIN

JAVA




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) {
        // Do something in response to button
    }

}

You may see an error because Android Studio cannot resolve the 
Code:
View
 class used as the method argument. So click to place your cursor on the 
Code:
View
declaration, and then perform a Quick Fix by pressing Alt + Enter (or Option + Enter on Mac). (If a menu appears, select Import class.)
[*]Now return to the activity_main.xml file to call this method from the button:
[list="box-sizing: inherit; margin-right: 0px; margin-left: 0px; padding-left: 40px; list-style: lower-alpha outside;"]
[*]Click to select the button in the Layout Editor.
[*]In the Attributes window, locate the onClick property and selectsendMessage [MainActivity] from the drop-down list.
[/list]

[/list]
[size]
Now when the button is tapped, the system calls the 
[/size]
Code:
sendMessage()
[size]
 method.
Take note of the details in this method that are required in order for the system to recognize it as compatible with the 
[/size]
Code:
android:onClick
[size]
 attribute. Specifically, the method has the following characteristics:
[/size]

  • Public access
  • A void or, in Kotlin, an implicit unit return value

  • Code:
    View
     as the only parameter (it is the
    Code:
    View
     object that was clicked)

[size]
Next, you’ll fill in this method to read the contents of the text field and deliver that text to another activity.

Build an Intent

An 
[/size]
Code:
Intent
[size]
 is an object that provides runtime binding between separate components, such as two activities. The 
[/size]
Code:
Intent
[size]
 represents an app’s "intent to do something." You can use intents for a wide variety of tasks, but in this lesson, your intent starts another activity.
In 
[/size]
Code:
MainActivity
[size]
, add the 
[/size]
Code:
EXTRA_MESSAGE
[size]
constant and the 
[/size]
Code:
sendMessage()
[size]
 code, as shown here:

KOTLIN

JAVA




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(R.id.editText)
        val message = editText.text.toString()
        val intent = Intent(this, DisplayMessageActivity::class.java).apply {
            putExtra(EXTRA_MESSAGE, message)
        }
        startActivity(intent)

    }
}

Android Studio again encounters Cannot resolve symbol errors, so press Alt + Enter (or Option + Return on Mac). Your imports should end up as the following:

KOTLIN

JAVA




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

An error remains for 
[/size]
Code:
DisplayMessageActivity
[size]
, but that's okay; you'll fix that in the next section.
Here's what's going on in 
[/size]
Code:
sendMessage()
[size]
:
[/size]

  • The 
    Code:
    Intent
     constructor takes two parameters:


    • Code:
      Context
       as its first parameter (
      Code:
      this
       is used because the 
      Code:
      Activity
       class is a subclass of
      Code:
      Context
      )
    • The 
      Code:
      Class
       of the app component to which the system should deliver the 
      Code:
      Intent
       (in this case, the activity that should be started).


  • The 
    Code:
    putExtra()
     method adds the 
    Code:
    EditText
    's value to the intent. An 
    Code:
    Intent
     can carry data types as key-value pairs called extras. Your key is a public constant 
    Code:
    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 using your app's package name as a prefix. This ensures the keys are unique, in case your app interacts with other apps.
  • The 
    Code:
    startActivity()
     method starts an instance of the 
    Code:
    DisplayMessageActivity
     specified by the 
    Code:
    Intent
    . Now you need to create that class.

[size]
The Navigation Architecture Component, currently in alpha, allows you to use the Navigation Editor to associate one activity with another. Once the relationship is made, you can use the API to start the second activity when the user triggers the associated action (i.e. clicking a button). To learn more, see The Navigation Architecture Component.

Create the second activity

[/size][list="box-sizing: inherit; margin-right: 0px; margin-left: 0px; padding-left: 40px; list-style-position: outside; list-style-image: initial;"]
[*]In the Project window, right-click the appfolder and select New > Activity > Empty Activity.
[*]In the Configure Activity window, enter "DisplayMessageActivity" for Activity Name and click Finish (leave all other properties set to the defaults).
[/list]
[size]
Android Studio automatically does three things:
[/size]

  • Creates the 
    Code:
    DisplayMessageActivity
    file.
  • Creates the corresponding 
    Code:
    activity_display_message.xml
    layout file.
  • Adds the required 
    Code:
    <activity>
     element in 
    Code:
    AndroidManifest.xml
    .

[size]
If you run the app and tap the button on the first activity, the second activity starts but is empty. This is because the second activity uses the empty layout provided by the template.

Add a text view

Lesson 5, learn how to do coding on your Android mobile Constraint-textview_2x
Figure 1. The text view centered at the top of the layout
The new activity includes a blank layout file, so now you'll add a text view where the message will appear.
[/size][list="box-sizing: inherit; margin-right: 0px; margin-left: 0px; padding-left: 40px; list-style-position: outside; list-style-image: initial;"]
[*]Open the file app > res > layout > activity_display_message.xml.
[*]Click Turn On Autoconnect Lesson 5, learn how to do coding on your Android mobile Layout-editor-autoconnect-on in the toolbar (it should then be enabled, as shown in figure 1).
[*]In the Palette window, click Text and then drag a TextView into the layout—drop it near the top-center of the layout so that it snaps to the vertical line that appears. Autoconnect adds left and right constraints to place the view in the horizontal center.
[*]Create one more constraint from the top of the text view to the top of the layout, so it appears as shown in figure 1.
[/list]
[size]
Optionally, make some adjustments to the text style by expanding textAppearance in theAttributes window and change attributes such as textSize and textColor.

Display the message

Now you will modify the second activity to display the message that was passed by the first activity.
[/size][list="box-sizing: inherit; margin-right: 0px; margin-left: 0px; padding-left: 40px; list-style-position: outside; list-style-image: initial;"]
[*]In 
Code:
DisplayMessageActivity
, add the following code to the 
Code:
onCreate()
method:

KOTLIN

JAVA




override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_display_message)
   
    // Get the Intent that started this activity and extract the string
    val message = intent.getStringExtra(EXTRA_MESSAGE)

    // Capture the layout's TextView and set the string as its text
    val textView = findViewById(R.id.textView).apply {
        text = message
    }

}

[*]Press Alt + Enter (or Option + Return on Mac) to import missing classes. Your imports should end up as the following:

KOTLIN

JAVA




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

[/list]
[size]

Add up navigation

Each screen in your app that is not the main entry point (all screens that are not the "home" screen) should provide navigation so the user can return to the logical parent screen in the app hierarchy by tapping the Up button in the app bar.
All you need to do is declare which activity is the logical parent in the
[/size]
Code:
AndroidManifest.xml
[size]
 file. So open the file at app > manifests > AndroidManifest.xml, locate the 
[/size]
Code:
<activity>
[size]
 tag for 
[/size]
Code:
DisplayMessageActivity
[size]
 and replace it with the following:


          android:parentActivityName=".MainActivity">
   
            android:name="android.support.PARENT_ACTIVITY"
        android:value=".MainActivity" />

The Android system now automatically adds the Up button in the app bar.

Run the app

Now run the app again by clicking Apply Changes Lesson 5, learn how to do coding on your Android mobile Toolbar-apply-changes in the toolbar. When it opens, type a message in the text field, and tap Sendto see the message appear in the second activity.
Lesson 5, learn how to do coding on your Android mobile Screenshot-activity2
Figure 2. Screenshots of both activities
That's it, you've built your first Android app!
To continue learning the basics about Android app development, follow the other links provided on this tutorial's front page.[/size]

www.hussain-software.officialforum.net




how do i change my setting
how do i change my rank
how do i post a topic in a forum
how do i add a signature to my post
how can i get back my forgot password
how do i show an image below my username
if you have any doubt like this then send me email

email id:       [You must be registered and logged in to see this link.]


                      Lesson 5, learn how to do coding on your Android mobile HLesson 5, learn how to do coding on your Android mobile ULesson 5, learn how to do coding on your Android mobile SLesson 5, learn how to do coding on your Android mobile SLesson 5, learn how to do coding on your Android mobile ALesson 5, learn how to do coding on your Android mobile ILesson 5, learn how to do coding on your Android mobile N
hussain
hussain
ADMINISTRATOR
ADMINISTRATOR

Join date : 2015-01-29
Real name Real name : hussain
Age : 26
country country : United Arab Emirates
facebook id facebook id : https://www.facebook.com/hussainneyaz7
twitter id twitter id : https://www.twitter.com/hussainneyaz7
nimbuzz id nimbuzz id : huss4in.@nimbuzz.com
whatsapp whatsapp : +91**********
Posts Posts : 1125
h.s.points h.s.points : 41834
H.S.Reputation H.S.Reputation : 0

View user profile http://hussain-software.forumotion.com

Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum