Vous êtes sur la page 1sur 6

Designing an App to understand Activity and Intent (Part 1)!

This purpose of this application is send information from the screen1 to screen2.! The information would be based on the users input" The application would require the user to key in their Name and ID and the keyed in words or number would be send to screen2 and displayed !

Name" ID

Screen1" (MainActivity.java)

Screen 2! (Display.java)

! Layout 1 (activity_main.xml)! ! ! !

Delete activity_main.xml(RelativeLayout)! Create new layout named activity_main ! Select LinearLayout! In activity_main.xml(LinearLayout) add in the following object in the following order:! 1. TextView! - textSize = 18sp! - android:text = Name: (Extract String)! 2. EditText! - android:id = @+id/editTextName! - layout width = ll_parent! - layout margin Left = 14dp! - layout margin Top = 15dp! - android:hint = Enter Name (Extract String)! - android:inputType = textCapWords! 3. TextView! - layout margin Top = 20dp! - textSize = 18sp! - android:text = Student ID: !

! ! !

4. EditText! - android:id = @+id/editTextID! - layout width = ll_parent! - layout margin Left = 14dp! - layout margin Top = 15dp! - android:hint = Enter ID (Extract String)! - android:inputType = textCapCharacters! 5. Button! - android:id=@+id/buttonNext! - layout margin Top = 20dp! - layout_gravity = center! - textSize = 20sp! - android:text = Next Screen (Extract String)!

! Layout 2 (display.xml)! !

! ! ! !

Create new layout named display.xml! Select LinearLayout! In display.xml add in the following object in the following order:! 1. TextView! - layout width = ll_parent! - android:gravity = center! - layout margin = 15dp! - textSize = 25sp! - android:text = You Entered: (Extract String)! 2. TextView! - android:id = @+id/txtName! - layout width = ll_parent! - layout margin = 15dp! - textSize = 18sp! 3. TextView! - android:id = @+id/txtID! - layout width = ll_parent! - layout margin = 15dp! - textSize = 18sp! 4. Button! - android:id = @+id/btnDone! - layout width = ll_parent! - layout margin Top = 15dp! - android:text = Done (Extract String)!

MainActivity.java!

Create a new class le named Display.java! In MainActiity,java we will assign our editText objects as global variable, this is done by declaring the variable right after the curly bracket before the rst @Override!

Next we will assign the variable with their id from our layout.! Declare the Button (Next) in the layout.!

Create an OnClickListener for the Button (Next)! Similar to our previous app screenS, to jump from 1 activity to another the Intent function is use.! To send information from one activity to next activity, additional functions of Intent will be use:!

intent.putExtra - adding data to be send over with the created "

Information to be sent" .getText() - obtain the text that has been keyed in!

Assign a reference name

.toString() - convert the text to a string

StartActivity(intent)!

Note: Don't forget to import the Library les#

Display.java! Extends Display.java to Activity ! Set contentView to (R.layout.display)! Declare and assign the objects in the layout (2 TextView, 1 Button)! Retrieve information from intent:!

Intent intent = getIntent()!

! !

-require to use the same intent identier from previous activity! .getIntent() - declaring that the intent will be used ! ! to retrieve information Strings are created to store the retrieved information.!

.getStringExtra(reference name) ! - declare that the retrieve information are strings with the reference name created in the previous activity After the information is retrieved, we will now assign it to our textView by using the .setText function to the respective string.!

! Note: identier for intent should be different from the 1 used for getIntent().! !

Create OnClickListener for Button (Done)! Button is to switch back to screen1 (MainActivity.java)!

A ag is added to the intent to prevent the user from returning to it when the back button is pressed. #

Screen 1" A

Next Screen

Screen 1" B

Back button is pressed

Done

Screen 1" C

Flag has clear the previous activity used

StartActivity(i)"

"

! Hint: AndroidManifest.xml! !

Run Application though AVD!

For All the Notes and Projects , ti can now be downloaded at:" https://www.dropbox.com/sh/ggeaw7uxhexh2ko/psIxUm-4lc

Vous aimerez peut-être aussi