Mad Codesss

Download as pdf or txt
Download as pdf or txt
You are on page 1of 46

1.

Develop a program to implement Auto Complete Text View

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout 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=".MainActivity">

<TextView
android:id="@+id/textView2"
android:layout_width="292dp"
android:layout_height="29dp"
android:layout_alignParentTop="true"
android:layout_marginTop="124dp"
android:text="example_autocompletetextview"
tools:layout_editor_absoluteX="7dp"
tools:layout_editor_absoluteY="73dp" />

<AutoCompleteTextView
android:id="@+id/autoCompleteTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:layout_alignLeft="@+id/textView2"
android:layout_marginLeft="107dp"
android:layout_marginTop="32dp"
android:ems="10"
tools:layout_editor_absoluteX="82dp"
tools:layout_editor_absoluteY="126dp" />

</RelativeLayout>
package com.example.myapplication;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class MainActivity extends Activity {


AutoCompleteTextView autocomplete;

String[] arr = { "Paries,France", "PA,United States","Parana,Brazil",


"Padua,Italy", "Pasadena,CA,United States"};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

autocomplete = (AutoCompleteTextView)
findViewById(R.id.autoCompleteTextView1);

ArrayAdapter<String> adapter = new ArrayAdapter<String>


(this,android.R.layout.select_dialog_item, arr);

autocomplete.setThreshold(2);
autocomplete.setAdapter(adapter);
}
}
2. Write a program to create a toggle button to display ON/OFF Bluetooth on
the display screen.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
android:padding="16dp"
android:background="@color/white"
tools:context=".MainActivity">

<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="onToggleClick"
/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="100dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_above="@+id/toggleButton"
android:textStyle="bold"
android:textColor="@color/black"/>
</RelativeLayout>
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.ToggleButton;

public class MainActivity


extends AppCompatActivity {

ToggleButton togglebutton;
TextView textview;
@Override
protected void onCreate(
Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

togglebutton
= (ToggleButton)findViewById(
R.id.toggleButton);

textview
= (TextView)findViewById(
R.id.textView);
}

public void onToggleClick(View view)


{
if (togglebutton.isChecked()) {
textview.setText("Bluetooth is OFF");
}
else {
textview.setText("Bluetooth is ON");
}
}
}
3. Write a program to create a login form for a social networking site.

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout 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"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/textViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="Login Form"
android:layout_gravity="center"/>
<EditText
android:id="@+id/editTextUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:inputType="text"
android:padding="8dp"
android:layout_marginTop="16dp"/>
<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"
android:padding="8dp"
android:layout_marginTop="16dp"/>
<Button
android:id="@+id/buttonLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:textSize="18sp"
android:layout_marginTop="16dp"/>
</LinearLayout>
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText editTextUsername = findViewById(R.id.editTextUsername);
EditText editTextPassword = findViewById(R.id.editTextPassword);
Button buttonLogin = findViewById(R.id.buttonLogin);
buttonLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = editTextUsername.getText().toString();
String password = editTextPassword.getText().toString();
// Perform authentication
if (username.equals("Ajinkya Patil") &&
password.equals("Social Password")) {
Toast.makeText(getApplicationContext(), "Login
successful", Toast.LENGTH_SHORT).show(); }
else {
Toast.makeText(getApplicationContext(), "Invalid username
or password", Toast.LENGTH_SHORT).show();
}

}
});
}
}
4. Write a program to show five checkboxes and toast selected checkboxes

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout 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=".MainActivity">
<CheckBox
android:id="@+id/checkBox5"
android:layout_width="123dp"
android:layout_height="45dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="145dp"
android:layout_marginTop="257dp"
android:layout_marginEnd="143dp"
android:layout_marginBottom="301dp"
android:text="MAD" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="123dp"
android:layout_height="45dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="235dp"
android:layout_marginTop="73dp"
android:layout_marginEnd="53dp"
android:layout_marginBottom="485dp"
android:text="MGT" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="123dp"
android:layout_height="45dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="237dp"
android:layout_marginTop="174dp"
android:layout_marginEnd="51dp"
android:layout_marginBottom="384dp"
android:text="ETI" />
<CheckBox
android:id="@+id/checkBox4"
android:layout_width="123dp"
android:layout_height="45dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="52dp"
android:layout_marginTop="73dp"
android:layout_marginEnd="236dp"
android:layout_marginBottom="485dp"
android:text="PWP" />
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="123dp"
android:layout_height="45dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="53dp"
android:layout_marginTop="174dp"
android:layout_marginEnd="235dp"
android:layout_marginBottom="384dp"
android:text="WBP" />
</RelativeLayout>
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
CheckBox checkBox1, checkBox2, checkBox3, checkBox4, checkBox5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkBox1=findViewById(R.id.checkBox1);
checkBox2=findViewById(R.id.checkBox2);
checkBox3=findViewById(R.id.checkBox3);
checkBox4=findViewById(R.id.checkBox4);
checkBox5=findViewById(R.id.checkBox5);
checkBox1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(checkBox1.isChecked()){
String getCBData=checkBox1.getText().toString();
Toast.makeText(MainActivity.this, "you clicked
:"+getCBData, Toast.LENGTH_SHORT).show();
}else if(!checkBox1.isChecked())
{
String uncheckdata=checkBox1.getText().toString();
Toast.makeText(MainActivity.this, "you unclicked
:"+uncheckdata, Toast.LENGTH_SHORT).show();
}
}
});
checkBox2.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view) {
if(checkBox2.isChecked()){
String getCBData=checkBox2.getText().toString();
Toast.makeText(MainActivity.this, "you clicked
:"+getCBData, Toast.LENGTH_SHORT).show();
}else if(!checkBox2.isChecked()){
String uncheckdata=checkBox2.getText().toString();
Toast.makeText(MainActivity.this, "you unclicked
:"+uncheckdata, Toast.LENGTH_SHORT).show();
}
}
});
checkBox3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(checkBox3.isChecked()){
String getCBData=checkBox3.getText().toString();
Toast.makeText(MainActivity.this, "you clicked
:"+getCBData, Toast.LENGTH_SHORT).show();
}
else if(!checkBox3.isChecked()){
String uncheckdata=checkBox3.getText().toString();
Toast.makeText(MainActivity.this, "you unclicked
:"+uncheckdata, Toast.LENGTH_SHORT).show();
}
}
});
checkBox4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(checkBox4.isChecked()){
String getCBData=checkBox4.getText().toString();
Toast.makeText(MainActivity.this, "you clicked
:"+getCBData, Toast.LENGTH_SHORT).show(); }
else if(!checkBox4.isChecked()){
String uncheckdata=checkBox4.getText().toString();
Toast.makeText(MainActivity.this, "you unclicked
:"+uncheckdata, Toast.LENGTH_SHORT).show(); } } });
checkBox5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(checkBox5.isChecked()){
String getCBData=checkBox5.getText().toString();
Toast.makeText(MainActivity.this, "you clicked
:"+getCBData,
Toast.LENGTH_SHORT).show(); }
else if(!checkBox5.isChecked()){
String uncheckdata=checkBox5.getText().toString();
Toast.makeText(MainActivity.this, "you unclicked
:"+uncheckdata, Toast.LENGTH_SHORT).show(); }}});}}
5. Develop a program to implement Radio Button and Radio Group

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout 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=".MainActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#e0e0e0"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select your favourite Food Culture :: "
android:textColor="#000"
android:textSize="20sp"
android:textStyle="bold" />

<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<RadioButton
android:id="@+id/johnCena"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:checked="true"
android:text="Indian"
android:textColor="#154"
android:textSize="20sp"
android:textStyle="bold" />

<RadioButton
android:id="@+id/romanReigns"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:checked="false"
android:text="Chinese"
android:textColor="#154"
android:textSize="20sp"
android:textStyle="bold" />

<RadioButton
android:id="@+id/goldBerg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:checked="false"
android:text="Continental"
android:textColor="#154"
android:textSize="20sp"
android:textStyle="bold" />

<RadioButton
android:id="@+id/sheamus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:checked="false"
android:text="Spanish"
android:textColor="#154"
android:textSize="20sp"
android:textStyle="bold" />

<RadioButton
android:id="@+id/randyOrton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:checked="false"
android:text="Italian"
android:textColor="#154"
android:textSize="20sp"
android:textStyle="bold" />

</RadioGroup>

<Button
android:id="@+id/submitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dp"
android:background="#0f0"
android:padding="10dp"
android:text="Submit"
android:textColor="#fff"
android:textSize="20sp"
android:textStyle="bold" />

</LinearLayout>
</LinearLayout>
package com.example.myapplication;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.RadioButton;
import android.widget.Button;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {

RadioButton johnCena, randyOrton, goldBerg, romanReigns, sheamus;


String selectedSuperStar;
Button submit;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
johnCena = (RadioButton) findViewById(R.id.johnCena);
randyOrton = (RadioButton) findViewById(R.id.randyOrton);
goldBerg = (RadioButton) findViewById(R.id.goldBerg);
romanReigns = (RadioButton) findViewById(R.id.romanReigns);
sheamus = (RadioButton) findViewById(R.id.sheamus);
submit = (Button) findViewById(R.id.submitButton);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (randyOrton.isChecked()) {
selectedSuperStar = randyOrton.getText().toString();
} else if (sheamus.isChecked()) {
selectedSuperStar = sheamus.getText().toString();
} else if (johnCena.isChecked()) {
selectedSuperStar = johnCena.getText().toString();
} else if (romanReigns.isChecked()) {
selectedSuperStar = romanReigns.getText().toString();
} else if (goldBerg.isChecked()) {
selectedSuperStar = goldBerg.getText().toString();
}
Toast.makeText(getApplicationContext(),"You
selected"+selectedSuperStar, Toast.LENGTH_LONG).show(); // print the value of
selected super star
}
});
}
}
6. Develop a program to implement Progress Bar

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/idRLContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<!--on below line we are creating


a text for our app-->
<TextView
android:id="@+id/idTVHeading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@id/idPBLoading"
android:layout_centerInParent="true"
android:layout_margin="20dp"
android:gravity="center"
android:padding="10dp"
android:text="Progress Bar in Android"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold" />

<!--on below line we are creating a progress bar-->


<ProgressBar
android:id="@+id/idPBLoading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/idBtnDisplayProgress"
android:layout_centerHorizontal="true"
android:layout_margin="20dp"
android:visibility="gone" />

<!--on below line we are creating a button-->


<Button
android:id="@+id/idBtnDisplayProgress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="20dp"
android:text="Show Progress Bar"
android:textAllCaps="false" />

</RelativeLayout>
package com.example.myapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

// on below line we are creating a variable.


private Button showProgressBtn;
private ProgressBar loadingPB;
boolean isProgressVisible = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showProgressBtn = findViewById(R.id.idBtnDisplayProgress);
loadingPB = findViewById(R.id.idPBLoading);

showProgressBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

if (isProgressVisible) {

showProgressBtn.setText("Show Progress Bar");

loadingPB.setVisibility(View.GONE);

isProgressVisible = false;
} else {
isProgressVisible = true;

showProgressBtn.setText("Hide Progress Bar");

loadingPB.setVisibility(View.VISIBLE);
}
}
});
}
}
Develop a program to implement List View and Grid View.

List View:
8. Write a program to display an image using Image View and a button named
as “Change Image”. Once you click on button another image should get
displayed

Take two images and add in res>drawable

<?xml version="1.0" encoding="utf-8"?>


<androidx.constraintlayout.widget.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=".MainActivity">

<AbsoluteLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:id="@+id/imageView"
android:layout_width="91dp"
android:layout_height="108dp"
android:layout_x="154dp"
android:layout_y="249dp"
tools:srcCompat="@tools:sample/avatars" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="152dp"
android:layout_y="465dp"
android:text="Change" />
</AbsoluteLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {


Button b1;
ImageView ig;
public int count=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=findViewById(R.id.button);
ig=findViewById(R.id.imageView);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(count==0){
ig.setImageResource(R.drawable.sad);
count=1;
}
else if (count==1) {
ig.setImageResource(R.drawable.happy);
count=0;
}
}
});
}
}
9. Write a program to display three checkboxes and one button ’order’ , Once
you Clicked on button it should toast different selected checkboxes along
with items individual and total price.

<?xml version="1.0" encoding="utf-8"?>


<androidx.constraintlayout.widget.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=".MainActivity">

<AbsoluteLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="144dp"
android:layout_marginTop="68dp"
android:layout_x="160dp"
android:layout_y="143dp"
android:text="Pizza"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="144dp"
android:layout_marginTop="28dp"
android:layout_x="160dp"
android:layout_y="230dp"
android:text="Coffee"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/checkBox" />

<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="144dp"
android:layout_marginTop="28dp"
android:layout_x="160dp"
android:layout_y="312dp"
android:text="Burger"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/checkBox2" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="144dp"
android:layout_marginTop="184dp"
android:layout_x="158dp"
android:layout_y="419dp"
android:text="Order"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/checkBox3" />

</AbsoluteLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.myapplication;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
CheckBox pizza,coffe,burger;
Button buttonOrder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButtonClick();
}
public void addListenerOnButtonClick(){
//Getting instance of CheckBoxes and Button from the activty_main.xml
file
pizza=(CheckBox)findViewById(R.id.checkBox);
coffe=(CheckBox)findViewById(R.id.checkBox2);
burger=(CheckBox)findViewById(R.id.checkBox3);
buttonOrder=(Button)findViewById(R.id.button);

//Applying the Listener on the Button click


buttonOrder.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
int totalamount=0;
StringBuilder result=new StringBuilder();
result.append("Selected Items:");
if(pizza.isChecked()){
result.append("\nPizza 100Rs");
totalamount+=100;
}
if(coffe.isChecked()){
result.append("\nCoffe 50Rs");
totalamount+=50;
}
if(burger.isChecked()){
result.append("\nBurger 120Rs");
totalamount+=120;
}
result.append("\nTotal: "+totalamount+"Rs");
//Displaying the message on the toast
Toast.makeText(getApplicationContext(), result.toString(),
Toast.LENGTH_LONG).show();
}

});
} }
10.Develop a program to implement date and Time Picker
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">

<AbsoluteLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<DatePicker
android:id="@+id/simpleDatePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#150"
android:datePickerMode="spinner" />

<Button
android:id="@+id/submitButton"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_below="@+id/simpleDatePicker"
android:layout_x="116dp"
android:layout_y="311dp"
android:text="submit"/>

<TimePicker
android:id="@+id/timePicker"
android:layout_width="316dp"
android:layout_height="305dp"
android:layout_marginTop="360dp"
android:layout_x="38dp"
android:layout_y="396dp" />

</AbsoluteLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

DatePicker simpleDatePicker;
Button submit;
TimePicker t1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initiate the date picker and a button
simpleDatePicker = (DatePicker) findViewById(R.id.simpleDatePicker);
submit = (Button) findViewById(R.id.submitButton);
// perform click event on submit button
t1=(TimePicker)findViewById(R.id.timePicker);
t1.setIs24HourView(true);

submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String day = "Day = " + simpleDatePicker.getDayOfMonth();
String month = "Month = " + (simpleDatePicker.getMonth() +
1);
String year = "Year = " + simpleDatePicker.getYear();
Toast.makeText(getApplicationContext(), day + "\n" + month +
"\n" + year, Toast.LENGTH_LONG).show();
}
});

}
11.Write a program to create a Hello World Activity using all lifecycles method
to display messages using log.d.
Now press the back button on the Emulator and exit the App:

Go to Logcat again and scroll down to bottom. You will see 3 more methods were called:
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
public String tag="application is:";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(this,"on create",Toast.LENGTH_SHORT).show();
Log.d(tag,"Activity is created");
}
@Override
protected void onStart() {
super.onStart();
Toast.makeText(this,"on start",Toast.LENGTH_SHORT).show();
Log.d(tag,"Started");
}
@Override
protected void onResume() {
super.onResume();
Log.d(tag,"Resumed");
}
@Override
protected void onPause() {
super.onPause();
Log.d(tag,"Paused");
}
@Override
protected void onStop() {
super.onStop();
Log.d(tag,"Stoped");
}
@Override
protected void onDestroy() {

super.onDestroy();
Log.d(tag,"Destroyed");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d(tag,"Restarted");
}
}
13. Write a program to create a text field and a button ‘Navigate’. When you enter
www.google.com and press navigate button it should open google home page.

Xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">

<AbsoluteLayout
android:layout_width="409dp"
android:layout_height="729dp"
android:orientation="horizontal"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp">

<EditText
android:id="@+id/editTextText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="91dp"
android:layout_y="110dp"
android:ems="10"
android:inputType="text"
android:hint="Enter url" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="152dp"
android:layout_y="201dp"
android:text="Navigate" />
</AbsoluteLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Java
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {


Button b1;
EditText e1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

b1 = findViewById(R.id.button);
e1 = findViewById(R.id.editTextText);

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String url = e1.getText().toString();
if (!url.isEmpty()) {
// Check if the URL is not empty
if (!url.startsWith("http://") && !url.startsWith("https://")) {
// If the URL doesn't start with http:// or https://, add http:// prefix
url = "http://" + url;
}
Intent implicitIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(implicitIntent);
}
}
});
}
}
12. Develop a program to implement new activity using explicit intent and implicit
intent.

Activity_main.Xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity">

<Button
android:id="@+id/explicitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Launch Second Activity (Explicit)" />

<Button
android:id="@+id/implicitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/explicitButton"
android:layout_marginTop="16dp"
android:text="Open URL (Implicit)" />

</RelativeLayout>

ActivityMain.Java:

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button explicitButton = findViewById(R.id.explicitButton);


explicitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Explicit Intent
Intent explicitIntent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(explicitIntent);
}
});

Button implicitButton = findViewById(R.id.implicitButton);


implicitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Implicit Intent
Intent implicitIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("https://www.google.com"));
startActivity(implicitIntent);
}
});
}
}

Manifest:
Add the below tag on the <Application> tag in the manifest file:
<activity android:name=".SecondActivity" />

Create a another xml file with name activity_second.xml

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second Activity"
android:textSize="24sp"
android:layout_centerInParent="true"/>

</RelativeLayout>

Create a new java file with name SecondActivity.java

package com.example.myapplication;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

public class SecondActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
14. Write a program to create a button “Start Dialer”. When you click on this button it
should open phone dialer.

Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity">

<Button
android:id="@+id/startDialerButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Start Dialer" />

</RelativeLayout>

MainActivity.java
package com.example.myapplication
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {


Button b1;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

b1=findViewById(R.id.startDialerButton);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_DIAL);
startActivity(intent);
}
});
}
}
15. Write a program to demonstrate any system broadcast message.

Mainactivity.java
package com.example.myapplication;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

AirplaneModeChangeReceiver airplaneModeChangeReceiver = new AirplaneModeChangeReceiver();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@Override
protected void onStart() {
super.onStart();
IntentFilter filter = new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);
registerReceiver(airplaneModeChangeReceiver, filter);
}

@Override
protected void onStop() {
super.onStop();
unregisterReceiver(airplaneModeChangeReceiver);
}
}

Create a new java file with name AirplaneModeChangeReceiver.java


package com.example.myapplication;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.widget.Toast;

public class AirplaneModeChangeReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
if (isAirplaneModeOn(context.getApplicationContext())) {
Toast.makeText(context, "AirPlane mode is on", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "AirPlane mode is off", Toast.LENGTH_SHORT).show();
}
}

private static boolean isAirplaneModeOn(Context context) {


return Settings.System.getInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
}
}
16. Develop a program to implement to build camera

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout 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=".MainActivity">

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Take a Photo" >
</Button>

<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/button1"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher_background" />

</RelativeLayout>

Mainactivity.java
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {


private static final int CAMERA_REQUEST = 1888;
ImageView imageView;
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

imageView = (ImageView) this.findViewById(R.id.imageView1);


Button photoButton = (Button) this.findViewById(R.id.button1);

photoButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {


if (requestCode == CAMERA_REQUEST) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}

}
18.Create a sample application with login module (Check user name and password) on

successful login, Change Text view “Login Successful” and on fail, alert user using toast

“Login Fail”.

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout 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=".MainActivity">

<EditText
android:id="@+id/usernameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="50dp"
android:layout_marginEnd="20dp"
android:hint="Username"
android:inputType="text" />

<EditText
android:id="@+id/passwordEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/usernameEditText"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:hint="Password"
android:inputType="textPassword" />

<Button
android:id="@+id/loginButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/passwordEditText"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:text="Login" />

<TextView
android:id="@+id/loginResultTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/loginButton"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text=""
android:textSize="18sp" />

</RelativeLayout>

MainActivity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private EditText usernameEditText, passwordEditText;


private Button loginButton;
private TextView loginResultTextView;

// Predefined username and password


private final String USERNAME = "user";
private final String PASSWORD = "password";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

usernameEditText = findViewById(R.id.usernameEditText);
passwordEditText = findViewById(R.id.passwordEditText);
loginButton = findViewById(R.id.loginButton);
loginResultTextView = findViewById(R.id.loginResultTextView);

loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
login();
}
});
}

private void login() {


String username = usernameEditText.getText().toString().trim();
String password = passwordEditText.getText().toString().trim();

if (username.equals(USERNAME) && password.equals(PASSWORD)) {


// Login successful
loginResultTextView.setText("Login Successful");
} else {
// Login failed
Toast.makeText(this, "Login Fail", Toast.LENGTH_SHORT).show();
}
}
}
19.Develop a program to send and receive SMS

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<EditText
android:id="@+id/phoneNumberEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Phone Number"
android:inputType="phone" />

<EditText
android:id="@+id/messageEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/phoneNumberEditText"
android:hint="Message" />

<Button
android:id="@+id/sendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/messageEditText"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text="Send" />

</RelativeLayout>

MainACtivity.java
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

public class MainActivity extends AppCompatActivity {

private static final int REQUEST_SEND_SMS = 1;

private EditText phoneNumberEditText;


private EditText messageEditText;
private Button sendButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

phoneNumberEditText = findViewById(R.id.phoneNumberEditText);
messageEditText = findViewById(R.id.messageEditText);
sendButton = findViewById(R.id.sendButton);

sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendSMS();
}
});

// Check for SMS permission


if (ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) !=
PackageManager.PERMISSION_GRANTED) {
// Permission not granted, request it
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS},
REQUEST_SEND_SMS);
}
}

private void sendSMS() {


String phoneNumber = phoneNumberEditText.getText().toString();
String message = messageEditText.getText().toString();

if (phoneNumber.isEmpty() || message.isEmpty()) {
Toast.makeText(this, "Phone number and message cannot be empty", Toast.LENGTH_SHORT).show();
return;
}

try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, message, null, null);
Toast.makeText(this, "SMS sent", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(this, "Failed to send SMS", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}

AndroidManifest.XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-feature
android:name="android.hardware.telephony"
android:required="false" />

<uses-permission android:name="android.permission.SEND_SMS" />


<uses-permission android:name="android.permission.RECEIVE_SMS" />

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
<receiver android:name=".SmsReceiver"
android:exported="true">>

<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>

Create a new file with name SmsReciever.java


package com.example.myapplication;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class SmsReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
if (pdus != null) {
for (Object pdu : pdus) {
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdu);
String senderPhoneNumber = smsMessage.getOriginatingAddress();
String messageBody = smsMessage.getMessageBody();

// Do something with the received message


Toast.makeText(context, "SMS from: " + senderPhoneNumber + "\nMessage: " + messageBody,
Toast.LENGTH_SHORT).show();
}
}
}
}
}
20.Develop a program to send email.

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout 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=".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" >

<requestFocus />
</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>

MainActivity.java
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {


EditText editTextTo,editTextSubject,editTextMessage;
Button send;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

editTextTo=(EditText)findViewById(R.id.editText1);
editTextSubject=(EditText)findViewById(R.id.editText2);
editTextMessage=(EditText)findViewById(R.id.editText3);

send=(Button)findViewById(R.id.button1);

send.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View arg0) {
String to=editTextTo.getText().toString();
String subject=editTextSubject.getText().toString();
String message=editTextMessage.getText().toString();

Intent email = new Intent(Intent.ACTION_SEND);


email.putExtra(Intent.EXTRA_EMAIL, new String[]{ to});
email.putExtra(Intent.EXTRA_SUBJECT, subject);
email.putExtra(Intent.EXTRA_TEXT, message);

//need this to prompts email client only


email.setType("message/rfc822");

startActivity(Intent.createChooser(email, "Choose an Email client :"));

});
}

You might also like