Android Example Program
Android Example Program
File: activity_main.xml
<RelativeLayout>
1. <EditText
2. android:id="@+id/editText1"
3. android:layout_width="wrap_content"
4. android:layout_height="wrap_content"
5. android:layout_alignParentTop="true"
6. android:layout_centerHorizontal="true"
7. android:layout_marginTop="61dp"
8. android:ems="10"
9. android:inputType="number"
10. tools:layout_editor_absoluteX="84dp"
11. tools:layout_editor_absoluteY="53dp" />
12.
13. <EditText
14. android:id="@+id/editText2"
15. android:layout_width="wrap_content"
16. android:layout_height="wrap_content"
17. android:layout_below="@+id/editText1"
18. android:layout_centerHorizontal="true"
19. android:layout_marginTop="32dp"
20. android:ems="10"
21. android:inputType="number"
22. tools:layout_editor_absoluteX="84dp"
23. tools:layout_editor_absoluteY="127dp" />
24.
25. <Button
26. android:id="@+id/button"
27. android:layout_width="wrap_content"
28. android:layout_height="wrap_content"
29. android:layout_below="@+id/editText2"
30. android:layout_centerHorizontal="true"
31. android:layout_marginTop="109dp"
32. android:text="ADD"
33. tools:layout_editor_absoluteX="148dp"
34. tools:layout_editor_absoluteY="266dp" />
</RelativeLayout>
File: [Link]
1. import [Link];
2. import [Link];
3. import [Link];
4. import [Link];
5. import [Link];
6.
7. public class MainActivity extends AppCompatActivity
{
8. private EditText edittext1, edittext2;
9. private Button buttonSum;
10.
11. @Override
12. protected void onCreate(Bundle savedInstance
State) {
13. [Link](savedInstanceState);
14. setContentView([Link].activity_main);
15.
16. addListenerOnButton();
17. }
18.
19. public void addListenerOnButton() {
20. edittext1 = (EditText) findViewById([Link]
Text1);
21. edittext2 = (EditText) findViewById([Link]
Text2);
22. buttonSum = (Button) findViewById([Link]
on);
23.
24. [Link](new [Link]
ClickListener() {
25. @Override
26. public void onClick(View view) {
27. String value1=[Link]().toStri
ng();
28. String value2=[Link]().toStri
ng();
29. int a=[Link](value1);
30. int b=[Link](value2);
31. int sum=a+b;
32. [Link](getApplicationContext()
,[Link](sum), Toast.LENGTH_LONG).show();
33. }
34. });
35. }
36. }
Android ToggleButton Example
File: activity_main.xml
1. <?xml version="1.0" encoding="utf-8"?>
2. <[Link] xm
lns:android="[Link]
android"
3. xmlns:app="[Link]
auto"
4. xmlns:tools="[Link]
5. android:layout_width="match_parent"
6. android:layout_height="match_parent"
7. tools:context=".MainActivity">
8.
9. <ToggleButton
10. android:id="@+id/toggleButton"
11. android:layout_width="wrap_content"
12. android:layout_height="wrap_content"
13. android:layout_marginLeft="8dp"
14. android:layout_marginTop="80dp"
15. android:text="ToggleButton"
16. android:textOff="Off"
17. android:textOn="On"
18. app:layout_constraintEnd_toStartOf="@+id/
toggleButton2"
19. app:layout_constraintStart_toStartOf="paren
t"
20. app:layout_constraintTop_toTopOf="parent" /
>
21.
22. <ToggleButton
23. android:id="@+id/toggleButton2"
24. android:layout_width="wrap_content"
25. android:layout_height="wrap_content"
26. android:layout_marginRight="60dp"
27. android:layout_marginTop="80dp"
28. android:text="ToggleButton"
29. android:textOff="Off"
30. android:textOn="On"
31. app:layout_constraintEnd_toEndOf="parent"
32. app:layout_constraintTop_toTopOf="parent" /
>
33.
34. <Button
35. android:id="@+id/button"
36. android:layout_width="wrap_content"
37. android:layout_height="wrap_content"
38. android:layout_marginBottom="144dp"
39. android:layout_marginLeft="148dp"
40. android:text="Submit"
41. app:layout_constraintBottom_toBottomOf="p
arent"
42. app:layout_constraintStart_toStartOf="paren
t" />
43. </
[Link]>
File: [Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
private ToggleButton toggleButton1,
toggleButton2;
private Button buttonSubmit;
@Override
protected void onCreate(Bundle
savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
toggleButton1=(ToggleButton)findViewById([Link]
eButton);
toggleButton2=(ToggleButton)findViewById([Link]
eButton2);
buttonSubmit=(Button)findViewById([Link]);
[Link](new
[Link]() {
@Override
public void onClick(View v) {
StringBuilder result = new StringBuilder();
[Link]("ToggleButton1 :
").append([Link]());
[Link]("\nToggleButton2 :
").append([Link]());
//Displaying the message in toast
[Link](getApplicationContext(),
[Link](),Toast.LENGTH_LONG).show();
}
});
}
}
Output
File: activity_main.xml
1. import [Link];
2. import [Link];
3. import [Link];
4. import [Link];
5. import [Link];
6. import [Link];
7.
8. public class MainActivity extends AppCompatActivity
{
9. CheckBox pizza,coffe,burger;
10. Button buttonOrder;
11. @Override
12. protected void onCreate(Bundle savedInstance
State) {
13. [Link](savedInstanceState);
14. setContentView([Link].activity_main);
15. addListenerOnButtonClick();
16. }
17. public void addListenerOnButtonClick(){
18. //Getting instance of CheckBoxes and Button
from the activty_main.xml file
19. pizza=(CheckBox)findViewById([Link]
x);
20. coffe=(CheckBox)findViewById([Link]
x2);
21. burger=(CheckBox)findViewById([Link]
ox3);
22. buttonOrder=(Button)findViewById([Link]
on);
23.
24. //Applying the Listener on the Button click
25. [Link](new View.O
nClickListener(){
26.
27. @Override
28. public void onClick(View view) {
29. int totalamount=0;
30. StringBuilder result=new StringBuilder(
);
31. [Link]("Selected Items:");
32. if([Link]()){
33. [Link]("\nPizza 100Rs");
34. totalamount+=100;
35. }
36. if([Link]()){
37. [Link]("\nCoffe 50Rs");
38. totalamount+=50;
39. }
40. if([Link]()){
41. [Link]("\nBurger 120Rs");
42. totalamount+=120;
43. }
44. [Link]("\nTotal:
"+totalamount+"Rs");
45. //Displaying the message on the toast
46. [Link](getApplicationContext()
, [Link](), Toast.LENGTH_LONG).show();
47. }
48.
49. });
50. }
51. }
Android RadioButton Example
File: activity_main.xml
xmlns:android="[Link]
res/android"
xmlns:app="[Link]
auto"
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select your Subject ?"
android:textStyle="bold"
android:layout_marginLeft="10dp"
android:textSize="20dp"/>
android:layout_height="wrap_content">
android:layout_height="wrap_content"
android:id="@+id/radia_id1"
android:text="DBMS"
android:textSize="20dp"/>
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/radia_id2"
android:text="C/C++ Programing"
android:textSize="20dp"/>
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/radia_id3"
android:text="Data Structure"
android:textSize="20dp"/>
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/radia_id4"
android:text="Algorithms"
android:textSize="20dp"/>
</RadioGroup>
</RelativeLayout>
[Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Override
protected void onCreate(Bundle
savedInstanceState)
{
[Link](savedInstanceState);
setContentView([Link].activity_main);
submit =
(Button)findViewById([Link]);
clear = (Button)findViewById([Link]);
radioGroup =
(RadioGroup)findViewById([Link]);
RadioButton radioButton =
(RadioButton)group .findViewById(checkedId);
}
});
@Override
public void onClick(View v)
{
int selectedId =
[Link]();
if (selectedId == -1)
Toast.LENGTH_SHORT) .show();
}
else
RadioButton radioButton =
(RadioButton)[Link](selectedId);
[Link]([Link],
[Link](),Toast.LENGTH_SHORT)
.show();
}
}
});
@Override
public void onClick(View v)
{
// Clear RadioGroup
// i.e. reset all the Radio Buttons
[Link]();
}
});
}
}
AutoCompleteTextView Example
In this example, we are displaying the programming
languages in the autocompletetextview. All the
programming languages are stored in string array. We
are using the ArrayAdapter class to display the array
content.
Let's see the simple example of autocompletetextview
in android.
1. activity_main.xml
Drag the AutoCompleteTextView and TextView from
the pallete, now the activity_main.xml file will like this:
File: activity_main.xml
35. <?xml version="1.0" encoding="utf-8"?>
36. <[Link]
xmlns:android="[Link]
res/android"
37. xmlns:app="[Link]
apk/res-auto"
38. xmlns:tools="[Link]
tools"
39. android:layout_width="match_parent"
40. android:layout_height="match_parent"
41.
42.
43. <TextView
44. android:layout_width="wrap_content"
45. android:layout_height="wrap_content"
46. android:text="What is your favourite pro
gramming language?"
47. app:layout_constraintBottom_toBottomOf
="parent"
48. app:layout_constraintLeft_toLeftOf="par
ent"
49. app:layout_constraintRight_toRightOf="p
arent"
50. app:layout_constraintTop_toTopOf="pare
nt"
51. app:layout_constraintVertical_bias="0.03
2" />
52.
53. <AutoCompleteTextView
54. android:id="@+id/
autoCompleteTextView"
55. android:layout_width="200dp"
56. android:layout_height="wrap_content"
57. android:layout_marginLeft="92dp"
58. android:layout_marginTop="144dp"
59. android:text=""
60. app:layout_constraintStart_toStartOf="p
arent"
61. app:layout_constraintTop_toTopOf="pare
nt" />
62.
63. </
[Link]>
Activity class
Let's write the code of AutoCompleteTextView.
File: [Link]
5. android:layout_width="match_parent"
6. android:layout_height="match_parent"
7.
8.
9. <Spinner
10. android:id="@+id/spinner"
11. android:layout_width="149dp"
12. android:layout_height="40dp"
13. android:layout_marginBottom="8dp"
14. android:layout_marginEnd="8dp"
15. android:layout_marginStart="8dp"
16. android:layout_marginTop="8dp"
17. app:layout_constraintBottom_toBottomOf
="parent"
18. app:layout_constraintEnd_toEndOf="par
ent"
19. app:layout_constraintHorizontal_bias="0
.502"
20. app:layout_constraintStart_toStartOf="p
arent"
21. app:layout_constraintTop_toTopOf="pare
nt"
22. app:layout_constraintVertical_bias="0.49
8" />
23.
24. </
[Link]>
Activity class
Let's write the code to display item on the spinner and
perform event handling.
File: [Link]
1.
2.
3. import [Link]
;
4. import [Link];
5. import [Link];
6. import [Link];
7. import [Link];
8. import [Link];
9. import [Link];
10.
11. public class MainActivity extends AppCompat
Activity implements
12. [Link] {
13. String[] country = { "India", "USA", "China
", "Japan", "Other"};
14.
15. @Override
16. protected void onCreate(Bundle savedInsta
nceState) {
17. [Link](savedInstanceState);
18. setContentView([Link].activity_main);
19. //Getting the instance of Spinner and appl
ying OnItemSelectedListener on it
20. Spinner spin = (Spinner) findViewById(R
.[Link]);
21. [Link](this);
22.
23. //Creating the ArrayAdapter instance hav
ing the country list
24. ArrayAdapter aa = new ArrayAdapter(thi
s,[Link].simple_spinner_item,country);
25. [Link](android.R
.layout.simple_spinner_dropdown_item);
26. //Setting the ArrayAdapter data on the S
pinner
27. [Link](aa);
28.
29. }
30.
31. //Performing action onItemSelected and on
Nothing selected
32. @Override
33. public void onItemSelected(AdapterView<?
> arg0, View arg1, int position, long id) {
34. [Link](getApplicationContext(),
country[position] , Toast.LENGTH_LONG).show();
35. }
36. @Override
37. public void onNothingSelected(AdapterVie
w<?> arg0) {
38. // TODO Auto-generated method stub
39. }
40. }
41.
42.
Example of ListView
activity_main.xml
First we need to drag and drop ListView component
from palette to activity_main.xml file.
File: activity_main.xml
1. <?xml version="1.0" encoding="utf-8"?>
2. <[Link] xml
ns:android="[Link]
android"
3. xmlns:app="[Link]
res-auto"
4. xmlns:tools="[Link]
5. android:layout_width="match_parent"
6. android:layout_height="match_parent"
7. tools:context="[Link].M
ainActivity">
8.
9. <ListView
10. android:id="@+id/listView"
11. android:layout_width="match_parent"
12. android:layout_height="fill_parent"
13. />
14. </
[Link]>
Create an additional [Link] file in layout folder
which contains view components displayed in listview.
[Link]
File: [Link]
1. <?xml version="1.0" encoding="utf-8"?>
2.
3. <TextView xmlns:android="http://
[Link]/apk/res/android"
4. android:id="@+id/textView"
5. android:layout_width="wrap_content"
6. android:layout_height="wrap_content"
7. android:text="Medium Text"
8. android:textStyle="bold"
9. android:textAppearance="?android:attr/
textAppearanceMedium"
10. android:layout_marginLeft="10dp"
11. android:layout_marginTop="5dp"
12. android:padding="2dp"
13. android:textColor="#4d4d4d"
14. />
Now place the list of data in [Link] file by
creating string-array.
[Link]
File:[Link]
1. <resources>
2. <string name="app_name">ListView</string>
3. <string-array name="array_technology">
4. <item>Android</item>
5. <item>Java</item>
6. <item>Php</item>
7. <item>Hadoop</item>
8. <item>Sap</item>
9. <item>Python</item>
10. <item>Ajax</item>
11. <item>C++</item>
12. <item>Ruby</item>
13. <item>Rails</item>
14. <item>.Net</item>
15. <item>Perl</item>
16. </string-array>
17. </resources>
Activity class
In java class we need to add adapter to listview using
setAdapter() method of listview.
File: [Link]
1. package [Link];
2.
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 AppCompat
Activity {
13. ListView listView;
14. TextView textView;
15. String[] listItem;
16. @Override
17. protected void onCreate(Bundle savedInsta
nceState) {
18. [Link](savedInstanceState);
19. setContentView([Link].activity_main);
20.
21. listView=(ListView)findViewById([Link]
View);
22. textView=(TextView)findViewById([Link].t
extView);
23. listItem = getResources().getStringArray
([Link].array_technology);
24. final ArrayAdapter<String> adapter = n
ew ArrayAdapter<String>(this,
25. [Link].simple_list_item_1,
[Link].text1, listItem);
26. [Link](adapter);
27.
28. [Link](new Ad
[Link]() {
29. @Override
30. public void onItemClick(AdapterView<
?> adapterView, View view, int position, long l) {
31. // TODO Auto-generated method stu
b
32. String value=[Link](positi
on);
33. [Link](getApplicationCont
ext(),value,Toast.LENGTH_SHORT).show();
34.
35. }
36. });
37. }
38. }
Android Gridview Example
File: activity_main.xml
1. <GridView xmlns:android="http://
[Link]/apk/res/android"
2. android:id="@+id/gridView1"
3. android:numColumns="auto_fit"
4. android:gravity="center"
5. android:columnWidth="50dp"
6. android:stretchMode="columnWidth"
7. android:layout_width="fill_parent"
8. android:layout_height="fill_parent" >
9.
10. </GridView>
1. Activity class
File: [Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link]
stener;
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends Activity {
GridView gridView;
static final String[] numbers = new String[] {
"1", "2", "3", "4", "5",
"6", "7", "8", "9", "10",};
@Override
protected void onCreate(Bundle savedInstanceS
tate) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
gridView = (GridView) findViewById([Link]
dView1);
ArrayAdapter<String> adapter = new Array
Adapter<String>(this,
[Link].simple_list_item_1, nu
mbers);
[Link](adapter);
[Link](new OnIte
mClickListener() {
@Override
public void onItemClick(AdapterView<?>
parent, View view, int position,long id) {
[Link](getApplicationContext()
,((TextView) view).getText(), Toast.LENGTH_LON
G).show();
}
public boolean onCreateOptionsMenu(Menu men
u) {
// Inflate the menu; this adds items to the acti
on bar if it is present.
getMenuInflater().inflate([Link].activity_mai
n, menu);
return true;
}
}
});
}
ImageView Example
<RelativeLayout
xmlns:android="[Link]
res/android"
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_ma
rgin"
android:paddingLeft="@dimen/activity_horizontal_mar
gin"
android:paddingRight="@dimen/activity_horizontal_m
argin"
android:paddingTop="@dimen/activity_vertical_margi
n"
tools:context=".MainActivity">
<ImageView
android:id="@+id/simpleImageViewLion"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:scaleType="fitXY"
android:src="@drawable/lion" />
<ImageView
android:id="@+id/simpleImageViewMonkey"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_below="@+id/simpleImageViewLion"
android:layout_marginTop="10dp"
android:scaleType="fitXY"
android:src="@drawable/monkey" />
</RelativeLayout>
File;[Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Override
protected void onCreate(Bundle
savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
ImageView simpleImageViewLion = (ImageView)
findViewById([Link]);//get the id
of first image view
ImageView simpleImageViewMonkey =
(ImageView)
findViewById([Link]);//get the
id of second image view
[Link](new
[Link]() {
@Override
public void onClick(View view) {
[Link](getApplicationContext(),
"Lion", Toast.LENGTH_LONG).show();//display the text
on image click event
}
});
[Link](new
[Link]() {
@Override
public void onClick(View view) {
[Link](getApplicationContext(),
"Monkey", Toast.LENGTH_LONG).show();//display the
text on image click event
}
});
}
Output:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/
activity_vertical_margin"
android:paddingLeft="@dimen/
activity_horizontal_margin"
android:paddingRight="@dimen/
activity_horizontal_margin"
android:paddingTop="@dimen/
activity_vertical_margin"
tools:context="[Link]
inActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/
textAppearanceMedium"
android:text="Vertical ScrollView example"
android:id="@+id/textView"
android:layout_gravity="center_horizontal"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true" />
<ScrollView android:layout_marginTop="30dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/scrollView">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 1" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 2" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 3" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 4" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 5" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 6" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 7" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 8" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 9" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 10" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 11" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 12" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 13" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 14" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 15" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 16" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 17" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 18" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 19" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 20" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
File: [Link]
import [Link]
ivity;
import [Link];
public class MainActivity extends AppCompatActiv
ity {
@Override
protected void onCreate(Bundle savedInstanceS
tate) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
}
}
Output
Android HorizontalScrollView
android:layout_height="wrap_content
"
android:text="New Button1"
android:id="@+id/button1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content
"
android:text="New Button2"
android:id="@+id/button2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content
"
android:text="New Button3"
android:id="@+id/button3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content
"
android:text="New Button4"
android:id="@+id/button4" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content
"
android:text="New Button5"
android:id="@+id/button5" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content
"
android:text="New Button6"
android:id="@+id/button6" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content
"
android:text="New Button7"
android:id="@+id/button7" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content
"
android:text="New Button8"
android:id="@+id/button8"/>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</RelativeLayout>
File: [Link]
import [Link]
ivity;
import [Link];
public class MainActivity extends AppCompatActiv
ity {
@Override
protected void onCreate(Bundle savedInstanceS
tate) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
}
}
Android Custom Toast Alert Example
Note:
File:Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="[Link]
apk/res/android"
xmlns:app="[Link]
res-auto"
xmlns:tools="[Link]
ls"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/customToast"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_below="@+id/simpleToast"
android:layout_centerHorizontal="true"
android:layout_margin="50dp"
android:background="#0f0"
android:text="Custom Toast"
android:textColor="#fff"
android:textSize="20sp" />
</RelativeLayout>
File:custom_toast_layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="[Link]
apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/toast_layout_root">
<!-- ImageVView and TextView for custom
Toast -->
<ImageView
android:id="@+id/toastImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp" />
<TextView
android:id="@+id/toastTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFF" />
</LinearLayout>
File :[Link]
package [Link];
import
[Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import static
[Link].custom_toast_l
ayout;
@Override
protected void onCreate(Bundle
savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
// get the reference of Button's
simpleToast = (Button)
findViewById([Link]);
customToast = (Button)
findViewById([Link]);
// perform setOnClickListener event on
simple Toast Button
[Link](new
[Link]() {
@Override
public void onClick(View v) {
// initiate a Toast with message and
duration
Toast toast =
[Link](getApplicationContext(), "Simple
Toast In Android", Toast.LENGTH_LONG); //
initiate the Toast with context, message and
duration for the Toast
[Link]([Link] |
Gravity.CENTER_HORIZONTAL, 0, 0); // set
gravity for the Toast.
[Link](); // display the Toast
}
});
// perform setOnClickListener event on
custom Toast Button
[Link](new
[Link]() {
@Override
public void onClick(View v) {
// Retrieve the Layout Inflater and
inflate the layout from xml
LayoutInflater inflater =
getLayoutInflater();
View layout =
[Link]([Link].custom_toast_layout,
(ViewGroup)
findViewById([Link].toast_layout_root));
// get the reference of TextView and
ImageVIew from inflated layout
TextView toastTextView = (TextView)
[Link]([Link]);
ImageView toastImageView =
(ImageView)
[Link]([Link]);
// set the text in the TextView
[Link]("Custom Toast
In Android");
// set the Image in the ImageView
[Link]([Link].ic_
launcher);
// create a new Toast using context
Toast toast = new
Toast(getApplicationContext());
[Link](Toast.LENGTH_LONG); // set
the duration for the Toast
[Link](layout); // set the inflated
layout
[Link](); // display the custom Toast
}
});
}
}
Output
Android TimePicker Example
Android TimePicker widget is used to select date.
It allows you to select time by hour and minute.
You cannot select time by seconds.
The [Link] is the subclass of
FrameLayout class.
File: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
1. <RelativeLayout xmlns:android="http://
[Link]/apk/res/android"
2. xmlns:app="[Link]
res-auto"
3. xmlns:tools="[Link]
4. android:layout_width="match_parent"
5. android:layout_height="match_parent"
6.
7.
8. <TextView
9. android:id="@+id/textView1"
10. android:layout_width="wrap_content"
11. android:layout_height="wrap_content"
12. android:layout_above="@+id/button1"
13. android:layout_alignParentLeft="true"
14. android:layout_alignParentStart="true"
15. android:layout_marginBottom="102dp"
16. android:layout_marginLeft="30dp"
17. android:layout_marginStart="30dp"
18. android:text="" />
19.
20. <Button
21. android:id="@+id/button1"
22. android:layout_width="wrap_content"
23. android:layout_height="wrap_content"
24. android:layout_alignParentBottom="true
"
25. android:layout_centerHorizontal="true"
26. android:layout_marginBottom="20dp"
27. android:text="Change Time" />
28.
29. <TimePicker
30. android:id="@+id/timePicker"
31. android:layout_width="wrap_content"
32. android:layout_height="wrap_content"
33. android:layout_above="@+id/textView1"
34. android:layout_centerHorizontal="true"
35. android:layout_marginBottom="36dp" />
36. </RelativeLayout>
File: [Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActiv
ity {
TextView textview1;
TimePicker timepicker;
Button changetime;
@Override
protected void onCreate(Bundle savedInstanceS
tate) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
textview1=(TextView)findViewById([Link]
View1);
timepicker=(TimePicker)findViewById([Link]
mePicker);
//Uncomment the below line of code for 24 ho
ur view
timepicker.setIs24HourView(true);
changetime=(Button)findViewById([Link]
n1);
[Link](getCurrentTime());
[Link](new [Link]
ClickListener(){
@Override
public void onClick(View view) {
[Link](getCurrentTime());
}
});
}
public String getCurrentTime(){
String currentTime="Current Time: "+timepi
[Link]()
+":"+[Link]();
return currentTime;
}
}
Android Implicit Intent Example
Let's see the simple example of implicit intent that displays a web
page.
activity_main.xml
File: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
1. <[Link] xmlns:android="
[Link]
2. xmlns:app="[Link]
3. xmlns:tools="[Link]
4. android:layout_width="match_parent"
5. android:layout_height="match_parent"
6. tools:context="[Link]
ctivity">
7.
8. <EditText
9. android:id="@+id/editText"
10. android:layout_width="wrap_content"
11. android:layout_height="wrap_content"
12. android:layout_marginEnd="8dp"
13. android:layout_marginStart="8dp"
14. android:layout_marginTop="60dp"
15. android:ems="10"
16. app:layout_constraintEnd_toEndOf="parent"
17. app:layout_constraintHorizontal_bias="0.575"
18. app:layout_constraintStart_toStartOf="parent"
19. app:layout_constraintTop_toTopOf="parent" />
20.
21. <Button
22. android:id="@+id/button"
23. android:layout_width="wrap_content"
24. android:layout_height="wrap_content"
25. android:layout_marginRight="8dp"
26. android:layout_marginLeft="156dp"
27. android:layout_marginTop="172dp"
28. android:text="Visit"
29. app:layout_constraintEnd_toEndOf="parent"
30. app:layout_constraintHorizontal_bias="0.0"
31. app:layout_constraintStart_toStartOf="parent"
32. app:layout_constraintTop_toBottomOf="@+id/
editText" />
33. </[Link]>
Activity class
File: [Link]
1. package [Link];
2.
3. import [Link];
4. import [Link];
5. import [Link];
6. import [Link];
7. import [Link];
8. import [Link];
9. import [Link];
10.
11. public class MainActivity extends AppCompatActivity {
12.
13. Button button;
14. EditText editText;
15.
16. @Override
17. protected void onCreate(Bundle savedInstanceState) {
18. [Link](savedInstanceState);
19. setContentView([Link].activity_main);
20.
21. button = findViewById([Link]);
22. editText = findViewById([Link]);
23.
24. [Link](new [Link](
){
25. @Override
26. public void onClick(View view) {
27. String url=[Link]().toString();
28. Intent intent=new Intent(Intent.ACTION_VIEW, U
[Link](url));
29. startActivity(intent);
30. }
31. });
32. }
33. }
Output:
Android Explicit Intent Example
Android Explicit intent specifies the component to be invoked from
activity. In other words, we can call another activity in android by
explicit intent.
We can also pass the information from one activity to another using
explicit intent.
Here, we are going to see an example to call one activity from
another and vice-versa.
Android calling one activity from another activity example
Let's see the simple example of android explicit example that calls
one activity from another and vice versa.
activity_main.xml
File: activity_main.xml
1. <?xml version="1.0" encoding="utf-8"?>
2. <[Link] xmlns:android="
[Link]
3. xmlns:app="[Link]
4. xmlns:tools="[Link]
5. android:layout_width="match_parent"
6. android:layout_height="match_parent"
7. tools:context="[Link]
tivity">
8.
9. <TextView
10. android:layout_width="wrap_content"
11. android:layout_height="wrap_content"
12. android:layout_marginEnd="8dp"
13. android:layout_marginStart="8dp"
14. android:layout_marginTop="8dp"
15. android:text="First Activity"
16. app:layout_constraintBottom_toBottomOf="parent"
17. app:layout_constraintEnd_toEndOf="parent"
18. app:layout_constraintHorizontal_bias="0.454"
19. app:layout_constraintLeft_toLeftOf="parent"
20. app:layout_constraintRight_toRightOf="parent"
21. app:layout_constraintStart_toStartOf="parent"
22. app:layout_constraintTop_toTopOf="parent"
23. app:layout_constraintVertical_bias="0.06" />
24.
25. <Button
26. android:id="@+id/button"
27. android:layout_width="wrap_content"
28. android:layout_height="wrap_content"
29. android:layout_marginEnd="8dp"
30. android:layout_marginStart="8dp"
31. android:layout_marginTop="392dp"
32. android:onClick="callSecondActivity"
33. android:text="Call second activity"
34. app:layout_constraintEnd_toEndOf="parent"
35. app:layout_constraintStart_toStartOf="parent"
36. app:layout_constraintTop_toTopOf="parent" />
37.
38. </[Link]>
ActivityOne class
File: [Link]
1. package [Link];
2.
3. import [Link];
4. import [Link];
5. import [Link];
6. import [Link];
7.
8. public class FirstActivity extends AppCompatActivity {
9.
10. @Override
11. protected void onCreate(Bundle savedInstanceState) {
12. [Link](savedInstanceState);
13. setContentView([Link].activity_first);
14. }
15. public void callSecondActivity(View view){
16. Intent i = new Intent(getApplicationContext(), Second
[Link]);
17. [Link]("Value1", "Android By Javatpoint");
18. [Link]("Value2", "Simple Tutorial");
19. // Set the request code to any code you like, you can i
dentify the
20. // callback via this code
21. startActivity(i);
22. }
23.
24. }
activitytwo_main.xml
File: activitytwo_main.xml
1. <?xml version="1.0" encoding="utf-8"?>
2. <[Link] xmlns:android="
[Link]
3. xmlns:app="[Link]
4. xmlns:tools="[Link]
5. android:layout_width="match_parent"
6. android:layout_height="match_parent"
7. tools:context="[Link]
Activity">
8.
9. <TextView
10. android:layout_width="wrap_content"
11. android:layout_height="wrap_content"
12. android:layout_marginEnd="8dp"
13. android:layout_marginStart="8dp"
14. android:layout_marginTop="8dp"
15. android:text="Second Activity"
16. app:layout_constraintBottom_toBottomOf="parent"
17. app:layout_constraintEnd_toEndOf="parent"
18. app:layout_constraintHorizontal_bias="0.454"
19. app:layout_constraintLeft_toLeftOf="parent"
20. app:layout_constraintRight_toRightOf="parent"
21. app:layout_constraintStart_toStartOf="parent"
22. app:layout_constraintTop_toTopOf="parent"
23. app:layout_constraintVertical_bias="0.06" />
24.
25. <Button
26. android:id="@+id/button"
27. android:layout_width="wrap_content"
28. android:layout_height="wrap_content"
29. android:layout_marginEnd="8dp"
30. android:layout_marginStart="8dp"
31. android:layout_marginTop="392dp"
32. android:onClick="callFirstActivity"
33. android:text="Call first activity"
34. app:layout_constraintEnd_toEndOf="parent"
35. app:layout_constraintStart_toStartOf="parent"
36. app:layout_constraintTop_toTopOf="parent" />
37. </[Link]>
ActivityTwo class
File: [Link]
1. package [Link];
2.
3. import [Link];
4. import [Link];
5. import [Link];
6. import [Link];
7. import [Link];
8.
9. public class SecondActivity extends AppCompatActivity {
10.
11. @Override
12. protected void onCreate(Bundle savedInstanceState) {
13. [Link](savedInstanceState);
14. setContentView([Link].activity_second);
15. Bundle extras = getIntent().getExtras();
16. String value1 = [Link]("Value1");
17. String value2 = [Link]("Value2");
18. [Link](getApplicationContext(),"Values are:
\n First value: "+value1+
19. "\n Second Value: "+value2, Toast.LENGTH_LON
G).show();
20. }
21. public void callFirstActivity(View view){
22. Intent i = new Intent(getApplicationContext(), FirstAc
[Link]);
23. startActivity(i);
24. }
25.
26. }
Output:
Android Intent Filters with Examples
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="[Link]
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/sendMail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="150dp"
android:layout_marginTop="230dp"
android:text="Send Mail" />
</LinearLayout>
Note:
For example, let's assume that you have an Activity that needs to
launch an email client and sends an email using your Android device.
For this purpose, your Activity would send an ACTION_SEND along
with appropriate chooser, to the Android Intent Resolver. The
specified chooser gives the proper interface for the user to pick how
to send your email data.
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
Button btnSend = (Button)findViewById([Link]);
[Link](new [Link]() {
@Override
public void onClick(View v) {
Intent si = new Intent(Intent.ACTION_SEND);
[Link]("message/rfc822");
[Link](Intent.EXTRA_EMAIL, new String[]
{"support@[Link]"});
[Link](Intent.EXTRA_SUBJECT, "Welcome ");
[Link](Intent.EXTRA_TEXT, "Hi Guest, Welcome ");
startActivity([Link](si,"Choose Mail App"));
}
});
}
}
If you observe above code we used multiple components to send
email, those are
setType - We use this property to set the MIME type of data that we
want to send. Here we used “message/rfc822” and other MIME
types are “text/plain” and “image/jpg”
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="[Link]" />
<category
android:name="[Link]" />
<action android:name="[Link]"/>
<category
android:name="[Link]"/>
<data android:mimeType="message/rfc822"/>
</intent-filter>
</activity>
</application>
</manifest>
If you observe above [Link] file we added following
extra fields of Intent filters.
action - we use this property to define that the activity can perform
SEND action.
category - we included the DEFAULT category for this activity to be
able to receive implicit intents.
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="30dp"
tools:context=".MainActivity">
</LinearLayout>
File: [Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
EditText Text;
Button btnText;
TextToSpeech textToSpeech;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
Text = findViewById([Link]);
btnText = findViewById([Link]);
[Link]([Link]);
}
}
});
// Adding OnClickListener
[Link](new [Link]()
{
@Override
public void onClick(View view) {
[Link]([Link]().toString(),TextToSpeech.QUEUE_
FLUSH,null);
}
});
}
}
Android TelephonyManager
The [Link] class provides
information about the telephony services such as subscriber id, sim
serial number, phone network type etc. Moreover, you can
determine the phone state etc.
Android TelephonyManager Example
Let's see the simple example of TelephonyManager that prints
information of the telephony services.
activity_main.xml
Drag one textview from the pallete, now the xml file will look like
this.
File: activity_main.xml
1. <RelativeLayout xmlns:androclass="http://
[Link]/apk/res/android"
2. xmlns:tools="[Link]
3. android:layout_width="match_parent"
4. android:layout_height="match_parent"
5. android:paddingBottom="@dimen/activity_vertical_margin"
6. android:paddingLeft="@dimen/activity_horizontal_margin"
7. android:paddingRight="@dimen/activity_horizontal_margin"
8. android:paddingTop="@dimen/activity_vertical_margin"
9. tools:context=".MainActivity" >
10.
11. <TextView
12. android:id="@+id/textView1"
13. android:layout_width="wrap_content"
14. android:layout_height="wrap_content"
15. android:layout_alignParentLeft="true"
16. android:layout_alignParentTop="true"
17. android:layout_marginLeft="38dp"
18. android:layout_marginTop="30dp"
19. android:text="Phone Details:" />
20.
21. </RelativeLayout>
Activity class
Now, write the code to display the information about the telephony
services.
File: [Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends Activity {
TextView textView1;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
textView1=(TextView)findViewById([Link].textView1);
//Get the instance of TelephonyManager
TelephonyManager tm=(TelephonyManager)getSystemSer
vice(Context.TELEPHONY_SERVICE);
//Calling the methods of TelephonyManager the returns th
e information
String IMEINumber=[Link]();
String subscriberID=[Link]();
String SIMSerialNumber=[Link]();
String networkCountryISO=[Link]();
String SIMCountryISO=[Link]();
String softwareVersion=[Link]();
String voiceMailNumber=[Link]();
//Get the phone type
String strphoneType="";
int phoneType=[Link]();
switch (phoneType)
{
case (TelephonyManager.PHONE_TYPE_CDMA):
strphoneType="CDMA";
break;
case (TelephonyManager.PHONE_TYPE_GSM):
strphoneType="GSM";
break;
case (TelephonyManager.PHONE_TYPE_NONE):
strphoneType="NONE";
break;
}
//getting information if phone is in roaming
boolean isRoaming=[Link]();
String info="Phone Details:\n";
info+="\n IMEI Number:"+IMEINumber;
info+="\n SubscriberID:"+subscriberID;
info+="\n Sim Serial Number:"+SIMSerialNumber;
info+="\n Network Country ISO:"+networkCountryISO;
info+="\n SIM Country ISO:"+SIMCountryISO;
info+="\n Software Version:"+softwareVersion;
info+="\n Voice Mail Number:"+voiceMailNumber;
info+="\n Phone Network Type:"+strphoneType;
info+="\n In Roaming? :"+isRoaming;
[Link](info);//displaying the information in the
textView
}
}
1. [Link]
You need to provide READ_PHONE_STATE permission in the
[Link] file.
File: [Link]
1. <?xml version="1.0" encoding="utf-8"?>
2. <manifest xmlns:androclass="[Link]
apk/res/android"
3. package="[Link]"
4. android:versionCode="1"
5. android:versionName="1.0" >
6.
7. <uses-sdk
8. android:minSdkVersion="8"
9. android:targetSdkVersion="17" />
10.
11. <uses-permission android:name="[Link].R
EAD_PHONE_STATE"/>
12.
13. <application
14. android:allowBackup="true"
15. android:icon="@drawable/ic_launcher"
16. android:label="@string/app_name"
17. android:theme="@style/AppTheme" >
18. <activity
19. android:name="[Link].
MainActivity"
20. android:label="@string/app_name" >
21. <intent-filter>
22. <action android:name="[Link].M
AIN" />
23.
24. <category android:name="[Link]
[Link]" />
25. </intent-filter>
26. </activity>
27. </application>
28.
29. </manifest>
Activity class
Let's write the code to know the call state.
File: [Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
TelephonyManager telephonyManager =
(TelephonyManager)getSystemService([Link]
EPHONY_SERVICE);
PhoneStateListener callStateListener = new PhoneStateList
ener() {
public void onCallStateChanged(int state, String incomingN
umber)
{
if(state==TelephonyManager.CALL_STATE_RINGING){
[Link](getApplicationContext(),"Phone I
s Riging",
Toast.LENGTH_LONG).
show();
}
if(state==TelephonyManager.CALL_STATE_OFFHOOK){
[Link](getApplicationContext(),"Phone is C
urrently in A call",
Toast.LENGTH_LO
NG).show();
}
if(state==TelephonyManager.CALL_STATE_IDLE){
[Link](getApplicationContext(),"phone is n
either ringing nor in a call",
[Link]
GTH_LONG).show();
}
}
};
[Link](callStateListener,PhoneStateListe
ner.LISTEN_CALL_STATE);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is
present.
getMenuInflater().inflate([Link], menu);
return true;
}
}
1. [Link]
You need to provide READ_PHONE_STATE permission in the
[Link] file.
File: [Link]
1. <?xml version="1.0" encoding="utf-8"?>
2. <manifest xmlns:androclass="[Link]
apk/res/android"
3. package="[Link]"
4. android:versionCode="1"
5. android:versionName="1.0" >
6.
7. <uses-sdk
8. android:minSdkVersion="8"
9. android:targetSdkVersion="17" />
10.
11.
12. <uses-permission android:name="[Link].
READ_PHONE_STATE" />
13.
14. <application
15. android:allowBackup="true"
16. android:icon="@drawable/ic_launcher"
17. android:label="@string/app_name"
18. android:theme="@style/AppTheme" >
19. <activity
20. android:name="[Link]
vity"
21. android:label="@string/app_name" >
22. <intent-filter>
23. <action android:name="[Link].M
AIN" />
24.
25. <category android:name="[Link]
[Link]" />
26. </intent-filter>
27. </activity>
28. </application>
29.
30. </manifest>
How to make a phone call in android
We are able to make a phone call in android via intent. You need to
write only three lines of code to make a phone call.
1. Intent callIntent = new Intent(Intent.ACTION_CALL);
2. [Link]([Link]("[Link] the
number
3. startActivity(callIntent);
1. Example of phone call in android
activity_main.xml
Drag the EditText and Button from the pallete, now the
activity_main.xml file will like this:
File: activity_main.xml
1. <RelativeLayout xmlns:androclass="http://
[Link]/apk/res/android"
2. xmlns:tools="[Link]
3. android:layout_width="match_parent"
4. android:layout_height="match_parent"
5. tools:context=".MainActivity" >
6.
7. <Button
8. android:id="@+id/button1"
9. android:layout_width="wrap_content"
10. android:layout_height="wrap_content"
11. android:layout_alignParentTop="true"
12. android:layout_centerHorizontal="true"
13. android:layout_marginTop="118dp"
14. android:text="Call" />
15.
16. <EditText
17. android:id="@+id/editText1"
18. android:layout_width="wrap_content"
19. android:layout_height="wrap_content"
20. android:layout_alignParentTop="true"
21. android:layout_centerHorizontal="true"
22. android:layout_marginTop="25dp"
23. android:ems="10" />
24.
25. </RelativeLayout>
1. Write the permission code in [Link]
file
You need to write CALL_PHONE permission as given below:
<uses-permission android:name="[Link].CALL_PH
ONE" />
File: [Link]
1. <?xml version="1.0" encoding="utf-8"?>
2.
3. <manifest xmlns:androclass="[Link]
apk/res/android"
4. package="[Link]"
5. android:versionCode="1"
6. android:versionName="1.0" >
7.
8. <uses-sdk
9. android:minSdkVersion="8"
10. android:targetSdkVersion="16" />
11.
12. <uses-permission android:name="[Link].C
ALL_PHONE" />
13. <application
14. android:allowBackup="true"
15. android:icon="@drawable/ic_launcher"
16. android:label="@string/app_name"
17. android:theme="@style/AppTheme" >
18. <activity
19. android:name="[Link]
ty"
20. android:label="@string/app_name" >
21. <intent-filter>
22. <action android:name="[Link].M
AIN" />
23.
24. <category android:name="[Link]
[Link]" />
25. </intent-filter>
26. </activity>
27. </application>
28.
29. </manifest>
Activity class
Let's write the code to make the phone call via intent.
File: [Link]
1. package [Link];
2.
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. import [Link];
12.
13. public class MainActivity extends Activity {
14. EditText edittext1;
15. Button button1;
16. @Override
17. protected void onCreate(Bundle savedInstanceState) {
18. [Link](savedInstanceState);
19. setContentView([Link].activity_main);
20.
21. //Getting the edittext and button instance
22. edittext1=(EditText)findViewById([Link].editText1);
23. button1=(Button)findViewById([Link].button1);
24.
25. //Performing action on button click
26. [Link](new OnClickListener(){
27.
28. @Override
29. public void onClick(View arg0) {
30. String number=[Link]().toString();
31. Intent callIntent = new Intent(Intent.ACTION_CA
LL);
32. [Link]([Link]("[Link]
33. startActivity(callIntent);
34. }
35.
36. });
37. }
38.
39. @Override
40. public boolean onCreateOptionsMenu(Menu menu) {
41. // Inflate the menu; this adds items to the action bar i
f it is present.
42. getMenuInflater().inflate([Link].activity_main, men
u);
43. return true;
}
44.
45. }
We can send sms in android via intent. You need to write only 4 lines
of code the send sms in android.
1. //Getting intent and PendingIntent instance
2. Intent intent=new Intent(getApplicationContext(),MainActivity.
class);
3. PendingIntent pi=[Link](getApplicationCont
ext(), 0, intent,0);
4.
5. //Get the SmsManager instance and call the sendTextMessage
method to send message
6. SmsManager sms=[Link]();
7. [Link]("8802177690", null, "hello welcome", pi,
null);
Example of sending sms in android
activity_main.xml
Drag the 2 edittexts, 2 textviews and 1 button from the pallete, now
the activity_main.xml file will like this:
File: activity_main.xml
1. <RelativeLayout xmlns:androclass="http://
[Link]/apk/res/android"
2. xmlns:tools="[Link]
3. android:layout_width="match_parent"
4. android:layout_height="match_parent"
5. tools:context=".MainActivity" >
6.
7. <EditText
8. android:id="@+id/editText1"
9. android:layout_width="wrap_content"
10. android:layout_height="wrap_content"
11. android:layout_alignParentRight="true"
12. android:layout_alignParentTop="true"
13. android:layout_marginRight="20dp"
14. android:ems="10" />
15.
16. <EditText
17. android:id="@+id/editText2"
18. android:layout_width="wrap_content"
19. android:layout_height="wrap_content"
20. android:layout_alignLeft="@+id/editText1"
21. android:layout_below="@+id/editText1"
22. android:layout_marginTop="26dp"
23. android:ems="10"
24. android:inputType="textMultiLine" />
25.
26. <TextView
27. android:id="@+id/textView1"
28. android:layout_width="wrap_content"
29. android:layout_height="wrap_content"
30. android:layout_alignBaseline="@+id/editText1"
31. android:layout_alignBottom="@+id/editText1"
32. android:layout_toLeftOf="@+id/editText1"
33. android:text="Mobile No:" />
34.
35. <TextView
36. android:id="@+id/textView2"
37. android:layout_width="wrap_content"
38. android:layout_height="wrap_content"
39. android:layout_alignBaseline="@+id/editText2"
40. android:layout_alignBottom="@+id/editText2"
41. android:layout_alignLeft="@+id/textView1"
42. android:text="Message:" />
43.
44. <Button
45. android:id="@+id/button1"
46. android:layout_width="wrap_content"
47. android:layout_height="wrap_content"
48. android:layout_alignLeft="@+id/editText2"
49. android:layout_below="@+id/editText2"
50. android:layout_marginLeft="34dp"
51. android:layout_marginTop="48dp"
52. android:text="Send SMS" />
53.
54. </RelativeLayout>
Write the permission code in [Link] file
You need to write SEND_SMS permission as given below:
1. <uses-permission android:name="[Link].SEND_S
MS"/>
2.
File: [Link]
1. <?xml version="1.0" encoding="utf-8"?>
2. <manifest
3. xmlns:androclass="[Link]
android"
4. package="[Link]"
5. android:versionCode="1"
6. android:versionName="1.0" >
7.
8. <uses-sdk
9. android:minSdkVersion="8"
10. android:targetSdkVersion="16" />
11.
12. <uses-permission android:name="[Link].S
END_SMS"/>
13.
14. <uses-permission android:name="[Link].R
ECEIVE_SMS"/>
15.
16. <application
17. android:allowBackup="true"
18. android:icon="@drawable/ic_launcher"
19. android:label="@string/app_name"
20. android:theme="@style/AppTheme" >
21. <activity
22. android:name="[Link]
y"
23. android:label="@string/app_name" >
24. <intent-filter>
25. <action android:name="[Link].M
AIN" />
26.
27. <category android:name="[Link]
[Link]" />
28. </intent-filter>
29. </activity>
30. </application>
31.
32. </manifest>
Activity class
Let's write the code to make the phone call via intent.
File: [Link]
1. package [Link];
2.
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. import [Link];
12. import [Link];
13. import [Link];
14.
15. public class MainActivity extends Activity {
16.
17. EditText mobileno,message;
18. Button sendsms;
19. @Override
20. protected void onCreate(Bundle savedInstanceState) {
21. [Link](savedInstanceState);
22. setContentView([Link].activity_main);
23.
24. mobileno=(EditText)findViewById([Link].editText1);
25. message=(EditText)findViewById([Link].editText2);
26. sendsms=(Button)findViewById([Link].button1);
27.
28. //Performing action on button click
29. [Link](new OnClickListener() {
30.
31. @Override
32. public void onClick(View arg0) {
33. String no=[Link]().toString();
34. String msg=[Link]().toString();
35.
36. //Getting intent and PendingIntent instance
37. Intent intent=new Intent(getApplicationContext()
,[Link]);
38. PendingIntent pi=[Link](getA
pplicationContext(), 0, intent,0);
39.
40. //Get the SmsManager instance and call the send
TextMessage method to send message
41. SmsManager sms=[Link]();
42. [Link](no, null, msg, pi,null);
43.
44. [Link](getApplicationContext(), "Messa
ge Sent successfully!",
45. Toast.LENGTH_LONG).show();
46. }
47. });
48. }
49.
50. @Override
51. public boolean onCreateOptionsMenu(Menu menu) {
52. // Inflate the menu; this adds items to the action bar i
f it is present.
53. getMenuInflater().inflate([Link].activity_main, men
u);
54. return true;
55. }
56.
57. }
We can easily send email in android via intent. You need to write few
lines of code only as given below
1. Intent email = new Intent(Intent.ACTION_SEND);
2. [Link](Intent.EXTRA_EMAIL, new String[]{ to});
3. [Link](Intent.EXTRA_SUBJECT, subject);
4. [Link](Intent.EXTRA_TEXT, message);
5.
6. //need this to prompts email client only
7. [Link]("message/rfc822");
8.
9. startActivity([Link](email, "Choose an Email clie
nt :"));
File: activity_main.xml
<RelativeLayout xmlns:androclass="http://
[Link]/apk/res/android"
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="22dp"
android:layout_marginTop="16dp"
android:ems="10" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="18dp"
android:ems="10" >
</EditText>
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_below="@+id/editText2"
android:layout_marginTop="28dp"
android:ems="10"
android:inputType="textMultiLine" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText1"
android:layout_alignBottom="@+id/editText1"
android:layout_alignParentLeft="true"
android:text="To:" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText2"
android:layout_alignBottom="@+id/editText2"
android:layout_alignParentLeft="true"
android:text="Subject:" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText3"
android:layout_alignBottom="@+id/editText3"
android:layout_alignParentLeft="true"
android:text="Message:" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText3"
android:layout_below="@+id/editText3"
android:layout_marginLeft="76dp"
android:layout_marginTop="20dp"
android:text="Send" />
</RelativeLayout>
1. Activity class
Let's write the code to send email via intent.
File: [Link]
1. package [Link];
2.
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 Activity {
13. EditText editTextTo,editTextSubject,editTextMessage;
14. Button send;
15. @Override
16. protected void onCreate(Bundle savedInstanceState) {
17. [Link](savedInstanceState);
18. setContentView([Link].activity_main);
19.
20. editTextTo=(EditText)findViewById([Link].editText1);
21. editTextSubject=(EditText)findViewById([Link]
2);
22. editTextMessage=(EditText)findViewById([Link]
t3);
23.
24. send=(Button)findViewById([Link].button1);
25.
26. [Link](new OnClickListener(){
27.
28. @Override
29. public void onClick(View arg0) {
30. String to=[Link]().toString();
31. String subject=[Link]().toString
();
32. String message=[Link]().toStr
ing();
33.
34.
35. Intent email = new Intent(Intent.ACTION_SEND);
<RelativeLayout
xmlns:android="[Link]
pk/res/android"
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="[Link]" >
<TextView
android:id="@+id/txtview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="5"
android:singleLine="false"
android:textSize="20sp"
/>
</RelativeLayout>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<!-- Permissions to receive and read SMS -->
<uses-permission android:name="[Link].RECEIVE_SMS"/>
<uses-permission android:name="[Link].READ_SMS"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="[Link]" />
<category android:name="[Link]" />
</intent-filter>
</activity>
<!-- Declare SMS Broadcast receiver -->
<receiver android:name=".SMSBReceiver" >
<intent-filter android:priority="999" >
<action android:name="[Link].SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
In the onCreate() method of the MainActivity class, you receive the data
(sender number and message) sent from the broadcast and display it on the
TextView.
Bundle in Android
It is known that Intents are used in Android to pass to the data
from one activity to another. But there is one another way, that
can be used to pass the data from one activity to another in a
better way and less code space ie by using Bundles in Android.
Android Bundles are generally used for passing data from one
activity to another. Basically here concept of key-value pair is
used where the data that one wants to pass is the value of the
map, which can be later retrieved by using the key. Bundles are
used with intent and values are sent and retrieved in the same
fashion, as it is done in the case of Intent. It depends on the
user what type of values the user wants to pass, but bundles
can hold all types of values (int, String, boolean, char) and pass
them to the new activity.
Step 2: Open res -> layout -> activity_main.xml (or) [Link] and add following code:
In this step we create two TextView‘s for displaying employee name and salary.
Step 3 : Now open app -> java -> package -> [Link] and add the below code.
In this step firstly we get the reference of both TextView’s then we parse the JSON using JSONObject
methods and finally we set the data in Textview’s.