ANDROID PROGRAMS DAY 3 //Android project to navigate from first page to second page using Intent Step 1: Create a new project Step 2: Enter necessary details while creating project. Step 3: Drag and drop a button in activity_main.xml file. Step 4: Add android:onclick= visitsecondpage in activity_main.xml file. Step 5: Right click on the folder which consists of MainActivity.java file, add new Activity class. New Activity Empty Activity
Step 6: Give appropriate Activity name for the new class. Step 7: Open activity_second.xml file and add a TextView for a Welcome message. Step 8: Open MainActivity.java class and in the visitsecondpage function, create Intent object. Intent obj = new Intent(this, SecondActivity.class); startactivity(obj); Step 9: Run the Application and test the working. //activity_main.xml <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.constraintlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.pramod.vc_inentsimple.mainactivity"> <TextView android:text="hello World!" app:layout_constraintbottom_tobottomof="parent" app:layout_constraintleft_toleftof="parent" app:layout_constraintright_torightof="parent" app:layout_constrainttop_totopof="parent" app:layout_constraintvertical_bias="0.228" /> <Button android:id="@+id/button" android:text="button" android:onclick="visitsecondpage"
app:layout_constraintleft_toleftof="parent" app:layout_constraintright_torightof="parent" tools:layout_constrainttop_creator="1" tools:layout_constraintbottom_creator="1" app:layout_constraintbottom_tobottomof="parent" app:layout_constrainttop_totopof="parent" /> </android.support.constraint.constraintlayout> //MainActivity.java import android.content.intent; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.view; public class MainActivity extends AppCompatActivity @Override protected void oncreate(bundle savedinstancestate) super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); public void visitsecondpage (View view) Intent obj = new Intent(this, SecondActivity.class); startactivity(obj);
//activity_second.xml <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.constraintlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.pramod.vc_inentsimple.secondactivity"> <TextView android:id="@+id/textview" android:text="welcome to Second Page" android:textappearance="@android:style/textappearance.large" tools:layout_constrainttop_creator="1" app:layout_constraintleft_toleftof="parent" app:layout_constraintright_torightof="parent" android:layout_margintop="117dp" app:layout_constrainttop_totopof="parent" /> </android.support.constraint.constraintlayout> // SecondActivity.java import android.support.v7.app.appcompatactivity; import android.os.bundle; public class SecondActivity extends AppCompatActivity @Override protected void oncreate(bundle savedinstancestate) super.oncreate(savedinstancestate); setcontentview(r.layout.activity_second);
//Android project to pass data from first page to second page using Intent object. Step 1: Create a new project Step 2: Enter necessary details while creating project. Step 3: Drag and drop a EditText and a button in activity_main.xml file. Step 4: Add android:onclick= visitsecondpage in activity_main.xml file. Step 5: Add android:hint= Name in the activity_main.xml file. Step 6: Right click on the folder which consists of MainActivity.java file, add new Activity class. New Activity Empty Activity Step 7: Give appropriate Activity name for the new class.
Step 8: Open activity_second.xml file and add a TextView for a Welcome message. Step 9: Create an EditText object in MainActivity.java file. Step 10: In the visitsecondpage function, extract string entered in the EditText widget and store it in a string object. EditText txt = (EditText)findViewById(R.id.editText); String str = txt.gettext().tostring(); Step 11: Create Intent object and pass the string value to second page using putextra() function. Intent obj = new Intent(this, SecondActivity.class); obj.putextra("name", str); startactivity(obj); Step 12: Open SecondActivity.java file extract the data passed from MainActivity using Bundle object. Store the value in a string object. Bundle obj = getintent().getextras();
String str1 = obj.getstring("name"); Step 13: Set the welcome message in a TextView using settext() function in SecondActivity.java file TextView tv = (TextView)findViewById(R.id.textView); tv.settext("welcome to Second Page " + str1); Step 14: Run the Application and test the working. //activity_main.xml <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.constraintlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.pramod.vc_intentdatapassing.mainactivity">
<Button android:id="@+id/button" android:text="button" android:onclick="visitsecondpage" tools:layout_constrainttop_creator="1" android:layout_marginend="61dp" app:layout_constraintright_torightof="@+id/edittext" android:layout_margintop="95dp" app:layout_constrainttop_tobottomof="@+id/edittext" /> <EditText android:id="@+id/edittext" android:ems="10" android:inputtype="textpersonname" android:hint="name" tools:layout_constrainttop_creator="1" android:layout_marginstart="67dp" android:layout_margintop="71dp" app:layout_constraintleft_toleftof="parent" app:layout_constrainttop_totopof="parent" /> </android.support.constraint.constraintlayout> //MainActivity.java import android.content.intent; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.view; public class MainActivity extends AppCompatActivity EditText txt; @Override protected void oncreate(bundle savedinstancestate)
super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); public void visitsecondpage (View view) txt = (EditText)findViewById(R.id.editText); String str = txt.gettext().tostring(); Intent obj = new Intent(this, SecondActivity.class); obj.putextra("name", str); startactivity(obj); //activity_second.xml <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.constraintlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.pramod.vc_intentdatapassing.secondactivity" > <TextView android:id="@+id/textview" android:text="welcome to Second Page" android:textappearance="@android:style/textappearance.large" tools:layout_constrainttop_creator="1" app:layout_constraintleft_toleftof="parent"
app:layout_constraintright_torightof="parent" android:layout_margintop="117dp" app:layout_constrainttop_totopof="parent" /> </android.support.constraint.constraintlayout> // SecondActivity.java import android.support.v7.app.appcompatactivity; import android.os.bundle; public class SecondActivity extends AppCompatActivity @Override protected void oncreate(bundle savedinstancestate) super.oncreate(savedinstancestate); setcontentview(r.layout.activity_second); Bundle obj = getintent().getextras(); String str1 = obj.getstring("name"); TextView tv = (TextView)findViewById(R.id.textView); tv.settext("welcome to Second Page " + str1); //Android project to Create a Registration Page and use Intent to send data to next page Step 1: Create a new project Step 2: Enter necessary details while creating project. Step 3: Add necessary widgets in activity_main.xml file. Step 4: Add android:onclick= visitsecondpage in activity_main.xml file.
Step 5: Right click on the folder which consists of MainActivity.java file, add new Activity class. New Activity Empty Activity //activity_main.xml: <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.constraintlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.ibmsap.test_registration_new.mainactivity"> <TextView android:text="welcome to registration page" android:textappearance="@android:style/textappearance.large" app:layout_constraintbottom_tobottomof="parent" app:layout_constraintleft_toleftof="parent" app:layout_constraintright_torightof="parent" app:layout_constrainttop_totopof="parent" app:layout_constraintvertical_bias="0.031" android:id="@+id/textview" /> <EditText android:id="@+id/edittext" android:layout_marginend="36dp" android:layout_margintop="19dp" android:ems="10" android:hint="enter Your Name" android:inputtype="textpersonname" app:layout_constraintright_torightof="@+id/textview" app:layout_constrainttop_tobottomof="@+id/textview" tools:layout_constrainttop_creator="1" />
<EditText android:id="@+id/edittext2" android:layout_margintop="23dp" android:ems="10" android:hint="enter Your USN" android:inputtype="textpersonname" app:layout_constraintleft_toleftof="@+id/edittext" app:layout_constraintright_torightof="@+id/edittext" app:layout_constrainttop_tobottomof="@+id/edittext" tools:layout_constrainttop_creator="1" /> <EditText android:id="@+id/edittext3" android:layout_marginend="1dp" android:layout_margintop="14dp" android:ems="10" android:hint="enter Your EMail ID" android:inputtype="textemailaddress" app:layout_constraintright_torightof="@+id/edittext2" app:layout_constrainttop_tobottomof="@+id/edittext2" tools:layout_constrainttop_creator="1" /> <CheckBox android:id="@+id/checkbox" android:layout_marginstart="16dp" android:layout_margintop="23dp" android:text="python" app:layout_constraintleft_toleftof="parent" app:layout_constrainttop_tobottomof="@+id/edittext3" tools:layout_constrainttop_creator="1" /> <CheckBox android:id="@+id/checkbox2"
android:text="c++" tools:layout_constrainttop_creator="1" android:layout_marginstart="50dp" android:layout_margintop="22dp" app:layout_constrainttop_tobottomof="@+id/edittext3" app:layout_constraintleft_toleftof="@+id/edittext3" /> <CheckBox android:id="@+id/checkbox3" android:layout_marginend="49dp" android:layout_margintop="22dp" android:text="java" app:layout_constraintright_torightof="parent" app:layout_constrainttop_tobottomof="@+id/edittext3" tools:layout_constrainttop_creator="1" /> <CheckBox android:id="@+id/checkbox4" android:layout_marginstart="16dp" android:layout_margintop="13dp" android:text="android" app:layout_constraintleft_toleftof="parent" app:layout_constrainttop_tobottomof="@+id/checkbox" tools:layout_constrainttop_creator="1" /> <RadioGroup android:layout_width="0dp" android:id="@+id/radiogroup" app:layout_constraintright_toleftof="@+id/checkbox2" tools:layout_constrainttop_creator="1" android:layout_marginstart="8dp" android:layout_marginend="6dp"
android:layout_margintop="20dp" app:layout_constrainttop_tobottomof="@+id/checkbox4" app:layout_constraintleft_toleftof="parent"> <RadioButton android:id="@+id/radiobutton2" android:layout_weight="1" android:text="second year" /> <RadioButton android:id="@+id/radiobutton3" android:layout_weight="1" android:text="third Year" /> <RadioButton android:id="@+id/radiobutton" android:layout_weight="1" android:text="fourth Year" tools:layout_editor_absolutex="16dp" tools:layout_editor_absolutey="317dp" /> </RadioGroup> <Button android:id="@+id/button" android:layout_marginbottom="30dp" android:layout_marginstart="25dp" android:onclick="registerdetails" android:text="submit" app:layout_constraintbottom_tobottomof="parent" app:layout_constraintleft_torightof="@+id/radiogroup" tools:layout_constraintbottom_creator="1" /> </android.support.constraint.constraintlayout>
//MainActivity.java import android.content.intent; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.view; import android.widget.checkbox; import android.widget.edittext; import android.widget.radiobutton; import android.widget.radiogroup; import android.widget.toast; public class MainActivity extends AppCompatActivity EditText name, usn, email; CheckBox chk1, chk2, chk3, chk4; RadioGroup rgp; RadioButton rb1; @Override protected void oncreate(bundle savedinstancestate) super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); name = (EditText)findViewById(R.id.editText); usn = (EditText)findViewById(R.id.editText2); email = (EditText)findViewById(R.id.editText3); chk1 = (CheckBox)findViewById(R.id.checkBox); chk2 = (CheckBox)findViewById(R.id.checkBox2); chk3 = (CheckBox)findViewById(R.id.checkBox3); chk4 = (CheckBox)findViewById(R.id.checkBox4); rgp = (RadioGroup)findViewById(R.id.radioGroup); //rb1 = (RadioButton)findViewById(R.id.radioButton2); //rb2 = (RadioButton)findViewById(R.id.radioButton3); //rb3 = (RadioButton)findViewById(R.id.radioButton); public void registerdetails(view view) String str1, str2, str3; String courses = ""; str1 = name.gettext().tostring();
str2 = usn.gettext().tostring(); str3 = email.gettext().tostring(); if(str1.isempty() str2.isempty() str3.isempty() ) Toast.makeText(getApplicationContext(), "Please Enter Your Details", Toast.LENGTH_LONG).show(); else if(chk1.ischecked()) courses = courses + chk1.gettext().tostring(); //courses = courses + "Python"; if(chk2.ischecked()) courses = courses + chk2.gettext().tostring(); if(chk3.ischecked()) courses = courses + chk3.gettext().tostring(); if(chk4.ischecked()) courses = courses + chk4.gettext().tostring(); int id = rgp.getcheckedradiobuttonid(); rb1 = (RadioButton)findViewById(id); String year = rb1.gettext().tostring(); Intent obj = new Intent(getApplicationContext(), Main2Activity.class); obj.putextra("name", str1); obj.putextra("usn", str2); obj.putextra("email", str3); obj.putextra("courses", courses); obj.putextra("year", year); startactivity(obj); //activity_main2.xml <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.constraintlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.ibmsap.test_registration_new.main2activity"> </android.support.constraint.constraintlayout> //Main2Activity.java import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.widget.toast; public class Main2Activity extends AppCompatActivity @Override protected void oncreate(bundle savedinstancestate) super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main2); Bundle obj = getintent().getextras(); String name = obj.getstring("name"); String usn = obj.getstring("usn"); String email = obj.getstring("email"); String courses = obj.getstring("courses"); String year = obj.getstring("year"); String str = "Name : " + name + "\nusn : " + usn + "\nemail ID : " + email + "\ncourses : " + courses + "\nyear : " + year; Toast.makeText(getApplicationContext(), str, Toast.LENGTH_LONG).show();