Monday, October 16, 2017

Android: Android Intent and start a new Activity


Objective: 
The objective of this Android tutorial is to use Android intent and open a new activity on button click. Refer the screen shots below.
Screenshots:

On-click of “CLICK ME” button, it should display the new activity as shown below.




Instructions to setup:
1. Git clone the project and you should see the project \3_Intent_Start_New_Activity downloaded
2. Now open the project in Android Studio
3. Press the keyboard – Shift + F10 to run the App, select the configured virtual device and click OK.

4. You can now see the output in the Android Emulator.

Source Code Explanation:
1. Activity_main.xml ((under \src\res\layout\):
1. Overall Layout is set as "RelativeLayout"
2. Button component is set with:
id as "button"
background as "holo_blue_dark"
text as "Click Me"




2. MainActivity.java:
1. Instantiate Button bt1 by using findViewById() by passing id name "button", as defined in activity_main.xml
2. Set On-click listener function for the button and onClick invoke the Intent (android.content.Intent) with required parameters – MainActivity.this, SecondActivity.class as shown in the code below.
3. Now invoke startActivity() with intent as input parameter.


3. SecondActivity.java:
Just all the default values in Activity.


4. activity_second.xml (under \src\res\layout\)
1. Overall Layout is set as "RelativeLayout"
2. TextView component is set with:
id as "textView"
background as "holo_blue_dark"
text as "happy_birthday value defined in strings.xml"
textColor as “colorAccent”
textSize as “36sp”
textStyle as “bold”


5. colors.xml (under \src\res\values\):
Default standard colors defined
string.xml (under \src\res\values\)
All string values defined

6.
styles.xml (under \src\res\values\)
All the default styles defined.



No comments:

Post a Comment