Aim:
To develop a Simple Android Application that uses Layout Managers and Event Listeners.
Procedure:
Creating a New project:
Open Android Stdio and then click on File -> New -> New project.
Then type the Application name as “[Link].2″ and click Next.
Then select the Minimum SDK as shown below and click Next.
Then select the Empty Activity and click Next.
Finally click Finish.
It will take some time to build and load the project.
After completion it will look as given below.
Creating Second Activity for the Android Application:
Click on File -> New -> Activity -> Empty Activity.
Type the Activity Name as SecondActivity and click Finish button.
Thus Second Activity For the application is created.
Designing layout for the Android Application:
Designing Layout for Main Activity:
Click on app -> res -> layout -> activity_main.xml.
Now click on Text as shown below.
Then delete the code which is there and type the code as given below.
Code for Activity_main.xml:
1 <?xml version="1.0" encoding="utf-8"?>
2 <RelativeLayout xmlns:android="[Link]
3 xmlns:tools="[Link]
4 android:layout_width="match_parent"
5 android:layout_height="match_parent"
6 tools:context=".MainActivity">
8 <LinearLayout
9 android:layout_width="match_parent"
10 android:layout_height="100dp">
11 <TextView
12 android:id="@+id/textView"
13 android:layout_width="match_parent"
14 android:layout_height="wrap_content"
15 android:layout_margin="30dp"
16 android:text="Details Form"
17 android:textSize="25sp"
18 android:gravity="center"/>
19 </LinearLayout>
20
21 <GridLayout
22 android:id="@+id/gridLayout"
23 android:layout_width="match_parent"
24 android:layout_height="match_parent"
25 android:layout_marginTop="100dp"
26 android:layout_marginBottom="200dp"
27 android:columnCount="2"
28 android:rowCount="3">
29 <TextView
30 android:id="@+id/textView1"
31 android:layout_width="wrap_content"
32 android:layout_height="wrap_content"
33 android:layout_margin="10dp"
34 android:layout_row="0"
35 android:layout_column="0"
36 android:text="Name"
37 android:textSize="20sp"
38 android:gravity="center"/>
39
40 <EditText
41 android:id="@+id/editText"
42 android:layout_width="wrap_content"
43 android:layout_height="wrap_content"
44 android:layout_margin="10dp"
45 android:layout_row="0"
46 android:layout_column="1"
47 android:ems="10"/>
48
49 <TextView
50 android:id="@+id/textView2"
51 android:layout_width="wrap_content"
52 android:layout_height="wrap_content"
53 android:layout_margin="10dp"
54 android:layout_row="1"
55 android:layout_column="0"
56 android:text="[Link]"
57 android:textSize="20sp"
58 android:gravity="center"/>
59
60 <EditText
61 android:id="@+id/editText2"
62 android:layout_width="wrap_content"
63 android:layout_height="wrap_content"
64 android:layout_margin="10dp"
65 android:layout_row="1"
66 android:layout_column="1"
67 android:inputType="number"
68 android:ems="10"/>
69
70 <TextView
71 android:id="@+id/textView3"
72 android:layout_width="wrap_content"
73 android:layout_height="wrap_content"
74 android:layout_margin="10dp"
75 android:layout_row="2"
76 android:layout_column="0"
77 android:text="Dept"
78 android:textSize="20sp"
79 android:gravity="center"/>
80
81 <Spinner
82 android:id="@+id/spinner"
83 android:layout_width="wrap_content"
84 android:layout_height="wrap_content"
85 android:layout_margin="10dp"
86 android:layout_row="2"
87 android:layout_column="1"
88 android:spinnerMode="dropdown"/>
89
90 </GridLayout>
91
92 <Button
93 android:id="@+id/button"
94 android:layout_width="wrap_content"
95 android:layout_height="wrap_content"
96 android:layout_alignParentBottom="true"
97 android:layout_centerInParent="true"
98 android:layout_marginBottom="150dp"
99 android:text="Submit"/>
100
101 </RelativeLayout>
Now click on Design and your activity will look as given below.
So now the designing part of Main Activity is completed.
Designing Layout for Second Activity:
Click on app -> res -> layout -> activity_second.xml.
Now click on Text as shown below.
Then delete the code which is there and type the code as given below.
Code for Activity_second.xml:
?
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="[Link]
3 xmlns:tools="[Link]
4 android:layout_width="match_parent"
5 android:layout_height="match_parent"
6 tools:context="[Link]"
7 android:orientation="vertical"
8 android:gravity="center">
10 <TextView
11 android:id="@+id/textView1"
12 android:layout_width="wrap_content"
13 android:layout_height="wrap_content"
14 android:layout_margin="20dp"
15 android:text="New Text"
16 android:textSize="30sp"/>
17
18 <TextView
19 android:id="@+id/textView2"
20 android:layout_width="wrap_content"
21 android:layout_height="wrap_content"
22 android:layout_margin="20dp"
23 android:text="New Text"
24 android:textSize="30sp"/>
25
26 <TextView
27 android:id="@+id/textView3"
28 android:layout_width="wrap_content"
29 android:layout_height="wrap_content"
30 android:layout_margin="20dp"
31 android:text="New Text"
32 android:textSize="30sp"/>
33
34 </LinearLayout>
Now click on Design and your activity will look as given below.
So now the designing part of Second Activity is also completed.
Java Coding for the Android Application:
Java Coidng for Main Activity:
Click on app -> java -> [Link].exno2 -> MainActivity.
Then delete the code which is there and type the code as given below.
Code for [Link]:
1 package [Link].exno2;
3 import [Link];
4 import [Link];
5 import [Link];
6 import [Link];
7 import [Link];
8 import [Link];
9 import [Link];
10 import [Link];
11
12 public class MainActivity extends AppCompatActivity {
13
14 //Defining the Views
15 EditText e1,e2;
16 Button bt;
17 Spinner s;
18
19 //Data for populating in Spinner
20 String [] dept_array={"CSE","ECE","IT","Mech","Civil"};
21
22 String name,reg,dept;
23
24 @Override
25 protected void onCreate(Bundle savedInstanceState) {
26 [Link](savedInstanceState);
27 setContentView([Link].activity_main);
28
29 //Referring the Views
30 e1= (EditText) findViewById([Link]);
31 e2= (EditText) findViewById([Link].editText2);
32
33 bt= (Button) findViewById([Link]);
34
35 s= (Spinner) findViewById([Link]);
36
37 //Creating Adapter for Spinner for adapting the data from array to Spinner
38 ArrayAdapter adapter= new ArrayAdapter([Link],[Link].simple_spinner_item,dept_array)
39 [Link](adapter);
40
41 //Creating Listener for Button
42 [Link](new [Link]() {
43 @Override
44 public void onClick(View v) {
45
46 //Getting the Values from Views(Edittext & Spinner)
47 name=[Link]().toString();
48 reg=[Link]().toString();
49 dept=[Link]().toString();
50
51 //Intent For Navigating to Second Activity
52 Intent i = new Intent([Link],[Link]);
53
54 //For Passing the Values to Second Activity
55 [Link]("name_key", name);
56 [Link]("reg_key",reg);
57 [Link]("dept_key", dept);
58
59 startActivity(i);
60
61 }
62 });
63 }
64 }
So now the Coding part of Main Activity is completed.
Java Coding for Second Activity:
Click on app -> java -> [Link].exno2 -> SecondActivity.
Then delete the code which is there and type the code as given below.
Code for [Link]:
1 package [Link].exno2;
3 import [Link];
4 import [Link];
5 import [Link];
6 import [Link];
8 public class SecondActivity extends AppCompatActivity {
10 TextView t1,t2,t3;
11
12 String name,reg,dept;
13
14 @Override
15 protected void onCreate(Bundle savedInstanceState) {
16 [Link](savedInstanceState);
17 setContentView([Link].activity_second);
18
19 t1= (TextView) findViewById([Link].textView1);
20 t2= (TextView) findViewById([Link].textView2);
21 t3= (TextView) findViewById([Link].textView3);
22
23 //Getting the Intent
24 Intent i = getIntent();
25
26 //Getting the Values from First Activity using the Intent received
27 name=[Link]("name_key");
28 reg=[Link]("reg_key");
29 dept=[Link]("dept_key");
30
31 //Setting the Values to Intent
32 [Link](name);
33 [Link](reg);
34 [Link](dept);
35
36 }
37 }
So now the Coding part of Second Activity is also completed.
Now run the application to see the output.
Output:
Result:
Thus a Simple Android Application that uses Layout Managers and Event Listeners is
developed and executed successfully.
Next story Simple Android Application for