Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Create New Android Activity

100 views

Published on

Online tutorial created for android developer - beginners. Step by step instructions with real-time development video. Lesson plan is divided into three part to help create new android project, add new layout, activity and apply styles to widgets.
In this tutorial, we will learn how to create new activity to an existing android project, send user input from one activity to another and add navigation button to the app bar for the user to return to parent screen.

For online video presentation, watch our YouTube video:
https://youtu.be/YYkt7S2i4DI

Published in: Education
  • Be the first to comment

Create New Android Activity

  1. 1. Create New Activity ANDROID MOBILE APPLICATION TRAINING VIDEO - 3 Copyright © 2020 Transpose Solutions. All Rights Reserved.
  2. 2. OVERVIEW In this tutorial, we will learn how to create new activity to an exisiting android project, send user input from one activity to another and add navigation button to the app bar for the user to return to parent screen. Copyright © 2020 Transpose Solutions. All Rights Reserved.
  3. 3. OPEN AN EXISTING PROJECT From your task bar/start menu, click open “Android Studio” In the Welcome to Android Studio window:  Click Open an existing Android Studio project. In the Open File or Project window:  Go to Android Studio Projects Folder and Select My Mobile App – the project we created in our earlier tutorial and click OK. Note: If you have not viewed our earlier tutorial – create new layout, please view that before starting this tutorial. Copyright © 2020 Transpose Solutions. All Rights Reserved.
  4. 4. OPEN MAIN ACTIVITY CLASS Android Studio main window appears after some process. We need to open MainActivity class (Jave file) – code behind the main_activity.xml: Click Open app > java > com.transposesolutions.mymobileapp > MainActivity. Add a Method “displayMessage” Note: This training material uses Android Studio v3.6.1. Package name “com.transposesolutions.mymobileapp” is our domain and app name, you should have your own package name. Copyright © 2020 Transpose Solutions. All Rights Reserved.
  5. 5. ADDING A METHOD We need to create the displayMessage() in the MainActivity class, this method will be used to start a new activity when app user clicks the submit button. Click open – app > java > com.transposesolutions.mymobileapp > MainActivity Add the following displayMessage() method stub inside AppCompactActivity: Note: You might see an error because Android Studio cannot resolve the View class used as the method argument. To clear the error, click the View declaration, place your cursor on it, and then press Alt+Enter, or Option+Enter on a Mac, to perform a Quick Fix. If a menu appears, select Import class. Copyright © 2020 Transpose Solutions. All Rights Reserved.
  6. 6. DEFINE - CLICK EVENT HANDLER Now, let us define the click event handler for the “Submit” button in the file activity_main.xml by adding the android:onClick: Open app > res > layout > activity_main.xml. Select the button in the Layout Editor. In the Attributes window, locate the onClick property Select “displayMessage” from its drop-down list. Now when the “Submit” button is tapped, the system calls the displayMessage() method. Note: We need to add few lines of code to the displayMessage(), so that the system can read the contents of the EditText (User input) and deliver that text to another activity which we will create next by adding a new activity to display the user input from main_activity_xml. Copyright © 2020 Transpose Solutions. All Rights Reserved.
  7. 7. ADD NEW ACTIVITY Let us add new activity to the project to display the user input from main_activity.xml. Please follow the following steps to add a new activity: In the Project window, right-click the app folder and select New > Activity > Empty Activity. In the Configure Activity window, enter "DisplayActivity" for Activity Name. Leave all other properties set to their defaults and click Finish. Now, main window appears after some processes with activity_display.xml opened with the layout editor. Copyright © 2020 Transpose Solutions. All Rights Reserved.
  8. 8. OPEN – LAYOUT EDITOR Let us open the layout editor, set the margin to the display_activity.xml: If your editor shows the XML source, click the Design tab. Click Select Design Surface and select Blueprint. Click Show in the Layout Editor toolbar and make sure that Show All Constraints is checked. Make sure Autoconnect is off. Click Default Margins in the toolbar and select your margin to “24”. If needed, you can adjust the margins for each view later. Click Device for Preview in the toolbar and select 5.5, 1440 × 2560, 560 dpi (Pixel XL). Copyright © 2020 Transpose Solutions. All Rights Reserved.
  9. 9. ADD WIDGETS Let us add new UI string to app “app_message”, add the following widgets to the display_activity.xml and apply styles: TextView with ID: “welcome_message” – To display a welcome message and set the following: Set layout width: 0dp; layout margin top: 20dp Set widget property to “Center - Horizontally” Locate the text property, which is currently set to “Name” and delete the value. Then, click “Pick a Resource” and select “app_message” from the list. Set text gravity to “Center”; Set text size: 30 sp and text color: @color/colorAccent TextView with ID: “user_input” – To display the text entered by the app user in the main activity Set layout width: 0dp; layout margin top: 20dp Set widget property to “Center - Horizontally” and layout_constraint top to bottom Set text gravity to “Center”; Set text size: 30 sp and text color: @color/colorPrimary Copyright © 2020 Transpose Solutions. All Rights Reserved.
  10. 10. ADD NAVIGATION Let us add a navigation button to app bar for the app user to return to main_activity.xml, to do that we need to declare “main_activity.xml is the parent screen in the AndroidManifest.xml:  Open the file at app > manifests > AndroidManifest.xml  locate the <activity> tag for DisplayMessageActivity replace <activity> tag with below code: Copyright © 2020 Transpose Solutions. All Rights Reserved. Next, we need to go back to MainActivity class file to add few lines of code to the displayMessage(), so that the system can read the contents of the field and deliver that text to display_activity.xml.
  11. 11. USING - INTENT OBJECT Let us open MainActivity Class and add few lines of code to the displayMessage(), for this purpose, we use “Intent” object to start display_activity.xml and “putExtra()” method to add the user entered values from the “EditText” to Intent. Fill in the following code to displaymessage(), add SEND_MESSAGE constant and import required class: Note: For more technical documents about “Intent” can be accessed from Google developer guide: https://developer.android.com/reference/android/content/Intent Copyright © 2020 Transpose Solutions. All Rights Reserved.
  12. 12. DISPLAYMESSAGE() Let us examine, what does the code we just added to the displayMessage(): 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 SEND_MESSAGE because the next activity uses the key to retrieve the text value. The startActivity() method starts an instance of the DisplayActivity that's specified by the Intent. Copyright © 2020 Transpose Solutions. All Rights Reserved.
  13. 13. DISPLAY USER INPUT To display the user input from main_activity.xml to display_activity.xml. For this purpose, we use Intent object and “getIntent()” method. Let us open DisplayActivity Class and fill in the following code to onCreate() Method and import required class : Now, we can run the app to view the changes we have done so far in this tutorial. Copyright © 2020 Transpose Solutions. All Rights Reserved.
  14. 14. RUN YOUR APP Use Android studio emulator to run your app: In the toolbar, select your app from the run/debug configurations drop-down menu.  From the target device drop-down menu, select the AVD that you want to run your app on.  Click Run Android Studio installs the app on the AVD and starts the emulator. Copyright © 2020 Transpose Solutions. All Rights Reserved.
  15. 15. Conclusion We hope you were able to follow the instruction mentioned this tutorial, please find the final output of screen we developed so far: Copyright © 2020 Transpose Solutions. All Rights Reserved. Main_Activity.xml Display_Activity.xml
  16. 16. Thank you for watching the video! We hope the training video 1, 2 and 3 was helpful. Please read the description of this video and external weblink provided to learn more… Copyright © 2020 Transpose Solutions. All Rights Reserved.

×