Genral of Android Practical

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

SURYADATTA COLLEGE OF MANAGEMENT

INFORMATION RESEARCH & TECHNOLOGY


BAVDHAN, PUNE – 411021

CS-555-MJP : Lab Course on CS-552-MJ (Mobile App


Development Technologies)

Submitted by
● NAME OF STUDENT ROLL NO
1.Mayank Parihar 2023100312

Under the Guidance of


Poonam Surve

SUBMITTED IN PARTIAL FULFILLMENT


OF MASTER OF SCIENCE (COMPUTER
SCIENCE)
SEM-II
SAVITRIBAI PHULE PUNE UNIVERSITY
For Academic Year 2023-2024
1.Java Android Program to demonstrate login form with validation.
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/loginscrn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:text="Login"
android:textSize="25dp"
android:textStyle="bold"
android:layout_gravity="center"/>
<TextView
android:id="@+id/fstTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginTop="20dp"
android:text="Email"/>
<EditText
android:id="@+id/txtEmail1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:ems="10"/>
<TextView
android:id="@+id/secTxt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password"
android:layout_marginLeft="100dp" />
<EditText
android:id="@+id/txtPwd1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:inputType="textPassword"
android:ems="10" />
<Button
android:id="@+id/btnLogin1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:text="Login" />
<TextView android:id="@+id/lnkRegister"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="New to Tutlane? Register here"
android:gravity="center"
android:textSize="20dp"
android:textColor="#3F51B5"/>
</LinearLayout>

MainActivity.java
package com.example.myapplication1;
import static com.example.myapplication1.R.id.txtPwd1;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
EditText edtuser, edtpass;
Button btnLogin;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtuser = (EditText) findViewById(R.id.txtEmail1);
edtpass = (EditText) findViewById(R.id.txtPwd1);
btnLogin = (Button) findViewById(R.id.btnLogin1);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (edtuser.getText().toString().trim().length() == 0) {
edtuser.setError("Username is not entered");
edtuser.requestFocus();
}
if (edtpass.getText().toString().trim().length() == 0) {
edtpass.setError("Password is not entered");
edtpass.requestFocus();
} else {
Intent it = new Intent(getApplicationContext(),
MainActivity.class);
startActivity(it);
}
}
});
}
}
Output:

2 Java Android Program to demonstrate Registration form with validation.


Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:orientation="vertical"
tools:context=".MainActivity"
tools:ignore="HardcodedText">
<EditText
android:id="@+id/firstName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:hint="First Name"
android:inputType="text" />
<EditText
android:id="@+id/lastName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:hint="Last Name"
android:inputType="text" />
<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:hint="Email"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:hint="Password"
android:inputType="textPassword" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:gravity="end"
android:orientation="horizontal">
<Button
android:id="@+id/cancelButton"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="4dp"
android:text="Register"
/>
</LinearLayout>
</LinearLayout>

MainActivity.java
package com.example.registation;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
EditText edtuser, edtpass;
Button btnLogin;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtuser = (EditText) findViewById(R.id.firstName);
edtpass = (EditText) findViewById(R.id.lastName);
btnLogin = (Button) findViewById(R.id.cancelButton);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (edtuser.getText().toString().trim().length() == 0) {
edtuser.setError("Username is not entered");
edtuser.requestFocus();
}
if (edtpass.getText().toString().trim().length() == 0) {
edtpass.setError("Password is not entered");
edtpass.requestFocus();
} else {
Intent it = new Intent(getApplicationContext(),
MainActivity.class);
startActivity(it);
}
}
});
}
}
Output:

3 Create the simple calculator and perform appropriate operation


<?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/relative1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/edt1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button1"style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/button4"
android:layout_alignRight="@+id/button4"
android:layout_below="@+id/edt1"
android:layout_marginTop="94dp"
android:text="1" />
<Button
android:id="@+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button1"
android:layout_toLeftOf="@+id/button3"
android:layout_toStartOf="@+id/button3"
android:text="2" />
<Button
android:id="@+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button2"
android:layout_centerHorizontal="true"
android:text="3" />
<Button
android:id="@+id/button4"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_toLeftOf="@+id/button2"
android:text="4" />
<Button
android:id="@+id/button5"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button4"
android:layout_alignLeft="@+id/button2"
android:layout_alignStart="@+id/button2"
android:text="5" />
<Button
android:id="@+id/button6"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button3"
android:layout_alignStart="@+id/button3"
android:layout_below="@+id/button3"
android:text="6" />
<Button
android:id="@+id/button7"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button4"
android:layout_toLeftOf="@+id/button2"
android:text="7" />
<Button
android:id="@+id/button8"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button5"
android:layout_alignStart="@+id/button5"
android:layout_below="@+id/button5"
android:text="8" />
<Button
android:id="@+id/button9"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button6"
android:layout_alignStart="@+id/button6"
android:layout_below="@+id/button6"
android:text="9" />
<Button
android:id="@+id/buttonadd"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/edt1"
android:layout_alignRight="@+id/edt1"
android:layout_alignTop="@+id/button3"
android:layout_marginLeft="46dp"
android:layout_marginStart="46dp"
android:layout_toRightOf="@+id/button3"
android:text="+" />
<Button
android:id="@+id/buttonsub"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/buttonadd"
android:layout_alignLeft="@+id/buttonadd"
android:layout_alignRight="@+id/buttonadd"
android:layout_alignStart="@+id/buttonadd"
android:layout_below="@+id/buttonadd"
android:text="-" />
<Button
android:id="@+id/buttonmul"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/buttonsub"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignStart="@+id/buttonsub"
android:layout_below="@+id/buttonsub"
android:text="*" />
<Button
android:id="@+id/button10"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button7"
android:layout_toLeftOf="@+id/button2"
android:text="." />
<Button
android:id="@+id/button0"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button8"
android:layout_alignStart="@+id/button8"
android:layout_below="@+id/button8"
android:text="0" />
<Button
android:id="@+id/buttonC"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button9"
android:layout_alignStart="@+id/button9"
android:layout_below="@+id/button9"
android:text="C" />
<Button
android:id="@+id/buttondiv"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/buttonmul"
android:layout_alignLeft="@+id/buttonmul"
android:layout_alignRight="@+id/buttonmul"
android:layout_alignStart="@+id/buttonmul"
android:layout_below="@+id/buttonmul"
android:text="/" />
<Button
android:id="@+id/buttoneql"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/buttondiv"
android:layout_alignLeft="@+id/button10"
android:layout_alignRight="@+id/buttondiv"
android:layout_alignStart="@+id/button10"
android:layout_below="@+id/button0"
android:layout_marginTop="37dp"
android:text="=" />
</RelativeLayout>

MainActivity.java
package com.example.calculator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
Button button0, button1, button2, button3, button4, button5, button6,
button7, button8, button9, buttonAdd, buttonSub,
buttonDivision,
buttonMul, button10, buttonC, buttonEqual;
EditText crunchifyEditText;
float mValueOne, mValueTwo;
boolean crunchifyAddition, mSubtract, crunchifyMultiplication,
crunchifyDivision;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button0 = (Button) findViewById(R.id.button0);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button5 = (Button) findViewById(R.id.button5);
button6 = (Button) findViewById(R.id.button6);
button7 = (Button) findViewById(R.id.button7);
button8 = (Button) findViewById(R.id.button8);
button9 = (Button) findViewById(R.id.button9);
button10 = (Button) findViewById(R.id.button10);
buttonAdd = (Button) findViewById(R.id.buttonadd);
buttonSub = (Button) findViewById(R.id.buttonsub);
buttonMul = (Button) findViewById(R.id.buttonmul);
buttonDivision = (Button) findViewById(R.id.buttondiv);
buttonC = (Button) findViewById(R.id.buttonC);
buttonEqual = (Button) findViewById(R.id.buttoneql);
crunchifyEditText = (EditText) findViewById(R.id.edt1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
crunchifyEditText.setText(crunchifyEditText.getText() +
"1");
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
crunchifyEditText.setText(crunchifyEditText.getText() +
"2");
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
crunchifyEditText.setText(crunchifyEditText.getText() +
"3");
}
});
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
crunchifyEditText.setText(crunchifyEditText.getText() +
"4");
}
});
button5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
crunchifyEditText.setText(crunchifyEditText.getText() +
"5");
}
});
button6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
crunchifyEditText.setText(crunchifyEditText.getText() +
"6");
}
});
button7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
crunchifyEditText.setText(crunchifyEditText.getText() +
"7");
}
});
button8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
crunchifyEditText.setText(crunchifyEditText.getText() +
"8");
}
});
button9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
crunchifyEditText.setText(crunchifyEditText.getText() +
"9");
}
});
button0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
crunchifyEditText.setText(crunchifyEditText.getText() +
"0");
}
});
buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (crunchifyEditText == null) {
crunchifyEditText.setText("");
} else {
mValueOne =
Float.parseFloat(crunchifyEditText.getText() + "");
crunchifyAddition = true;
crunchifyEditText.setText(null);
}
}
});
buttonSub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mValueOne = Float.parseFloat(crunchifyEditText.getText() +
"");
mSubtract = true;
crunchifyEditText.setText(null);
}
});
buttonMul.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mValueOne = Float.parseFloat(crunchifyEditText.getText() +
"");
crunchifyMultiplication = true;
crunchifyEditText.setText(null);
}
});
buttonDivision.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mValueOne = Float.parseFloat(crunchifyEditText.getText() +
"");
crunchifyDivision = true;
crunchifyEditText.setText(null);
}
});
buttonEqual.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mValueTwo = Float.parseFloat(crunchifyEditText.getText() +
"");
if (crunchifyAddition == true) {
crunchifyEditText.setText(mValueOne + mValueTwo + "");
crunchifyAddition = false;
}
if (mSubtract == true) {
crunchifyEditText.setText(mValueOne - mValueTwo + "");
mSubtract = false;
}
if (crunchifyMultiplication == true) {
crunchifyEditText.setText(mValueOne * mValueTwo + "");
crunchifyMultiplication = false;
}
if (crunchifyDivision == true) {
crunchifyEditText.setText(mValueOne / mValueTwo + "");
crunchifyDivision = false;
}
}
});
buttonC.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
crunchifyEditText.setText("");
}
});
button10.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
crunchifyEditText.setText(crunchifyEditText.getText() +
".");
}});}}
Output:
4 Create an Android application which examine, that a phone number, which a user has
entered is in the given format. * Area code should be one of the following: 040, 041, SPPU
M.Sc. Computer Science Syllabus 2023-24 54 050, 0400, 044 * There should 6- 8 numbers in
telephone number (+ area code).
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical" >
<TextView
android:layout_marginTop="200px"
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="90px"
android:text="Enter Telephone Number " />
<Spinner
android:layout_marginTop="50px"
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:tooltipText="d"
android:scrollbarSize="10dp"
tools:ignore="ExtraText" />
<EditText
android:id="@+id/Phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="phone" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Valid" />
</LinearLayout>

MainActivity.java
package com.example.pratical4;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements
AdapterView.OnItemSelectedListener {
EditText edtuser;
Button btnLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(this);
List<String> categories = new ArrayList<String>();
categories.add("+40");
categories.add("+41");
categories.add("+050");
categories.add("+0400");
categories.add("+044");
ArrayAdapter<String> dataAdapter = new
ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,
categories);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdow
n_item);
spinner.setAdapter(dataAdapter);
edtuser=(EditText)findViewById(R.id.Phone);
btnLogin=(Button)findViewById(R.id.button);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if (edtuser.getText().toString().trim().length()
== 0) {
edtuser.setError("Enter Mobile number ");
edtuser.requestFocus();
} if (edtuser.length() != 8) {
edtuser.setError("Enter correct Number ");
edtuser.requestFocus();
}
else {
Intent it = new
Intent(getApplicationContext(),
MainActivity.class);
startActivity(it);
}
}
});
}
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String item =
parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(), "Selected: " + item,
Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
}
}
Output:
5 .By using Spinner, Buttons. Write a program to draw GUI.
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical" >
<TextView
android:layout_marginTop="100px"
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="60px"
android:text="Enter Item :- " />
<EditText
android:id="@+id/editTextTextPersonName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
/>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow>
<Button
android:layout_marginTop="100px"
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:width="500px"
android:height="150px"
android:text="Add To Spinner" />
<Button
android:layout_marginTop="100px"
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:width="500px"
android:height="150px"
android:text="Remove To Spinner" />
</TableRow>
</TableLayout>
<TextView
android:layout_marginTop="100px"
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="80px"
android:text="See Response Below :-" />
<Spinner
android:layout_marginTop="200px"
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:tooltipText="d"
android:scrollbarSize="10dp"
tools:ignore="ExtraText" />
</LinearLayout>

MainActivity.java
package com.example.pratical5;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements
AdapterView.OnItemSelectedListener {
EditText edtuser;
Button btnLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(this);
List<String> categories = new ArrayList<String>();
categories.add("Apple");
categories.add("Banana");
categories.add("Grape");
categories.add("Mango");
categories.add("Pineapple");
categories.add("Watermelon");
ArrayAdapter<String> dataAdapter = new
ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,
categories);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdow
n_item);
spinner.setAdapter(dataAdapter);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String item =
parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(), "Selected: " + item,
Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
}
}
Output:

6 Create an Android application, which show to the user 5-10 quiz questions. All questions
have 4 possible options and one right option exactly. Application counts and shows to the
user how many answers were right and shows the result to user.
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"
android:background="@color/teal_200"
android:padding="24dp"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/total_question"
android:text="Total Questions "
android:layout_centerHorizontal="true"
android:textSize="20dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/question"
android:textStyle="bold"
android:text="This will be the question"
android:textColor="@color/white"
android:textSize="24dp"
android:textAlignment="center"
android:layout_margin="20dp"
android:layout_above="@id/choices_layout"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/choices_layout"
android:layout_centerInParent="true"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ans_A"
android:layout_margin="5dp"
android:backgroundTint="@color/white"
android:text="Ans A"
android:textColor="@color/black"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ans_B"
android:layout_margin="5dp"
android:backgroundTint="@color/white"
android:text="Ans B"
android:textColor="@color/black"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ans_C"
android:layout_margin="5dp"
android:backgroundTint="@color/white"
android:text="Ans C"
android:textColor="@color/black"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ans_D"
android:layout_margin="5dp"
android:backgroundTint="@color/white"
android:text="Ans D"
android:textColor="@color/black"/>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/submit_btn"
android:text="Submit"
android:layout_below="@id/choices_layout"
android:layout_marginTop="40dp"/>
</RelativeLayout>

QuestionAnswer.java
package com.example.quiz;
public class QuestionAnswer {
public static String question[] ={
"full form of ipl",
"which blower take highest wicket in one session?",
"which team highest qualify in semi-final in ipl? ",
"which player get most golden duck in ipl history ?",
"which team win most final in ipl history ?"
};
public static String choices[][] = {
{"indian premier league","india premier","itt","italy
premier league"},
{"deepak sharma","DJ bravo","umesh yadav","kuldeep yadav"},
{"DC","MI","GT","CSK"},
{"MS DHONI","VIRAT KHOLI","ROHIT SHARMA","MAYANK SHARMA"},
{"LSG","MI","KKR","CSK"},
};
public static String correctAnswers[] = {
"indian premier league",
"DJ bravo",
"CSK",
"ROHIT SHARMA",
"MI"
};
}

MainActivity.java
package com.example.quiz;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements
View.OnClickListener {
TextView totalQuestionsTextView;
TextView questionTextView;
Button ansA, ansB, ansC, ansD;
Button submitBtn;
int score = 0;
int totalQuestion = QuestionAnswer.question.length;
int currentQuestionIndex = 0;
String selectedAnswer = "";
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
totalQuestionsTextView = findViewById(R.id.total_question);
questionTextView = findViewById(R.id.question);
ansA = findViewById(R.id.ans_A);
ansB = findViewById(R.id.ans_B);
ansC = findViewById(R.id.ans_C);
ansD = findViewById(R.id.ans_D);
submitBtn = findViewById(R.id.submit_btn);
ansA.setOnClickListener(this);
ansB.setOnClickListener(this);
ansC.setOnClickListener(this);
ansD.setOnClickListener(this);
submitBtn.setOnClickListener(this);
totalQuestionsTextView.setText("Total questions : " +
totalQuestion);
loadNewQuestion();
}
@Override
public void onClick(View view) {
ansA.setBackgroundColor(Color.WHITE);
ansB.setBackgroundColor(Color.WHITE);
ansC.setBackgroundColor(Color.WHITE);
ansD.setBackgroundColor(Color.WHITE);
Button clickedButton = (Button) view;
if (clickedButton.getId() == R.id.submit_btn) {
if
(selectedAnswer.equals(QuestionAnswer.correctAnswers[currentQuestionIndex])
){
score++;
}
currentQuestionIndex++;
loadNewQuestion();
} else {
//choices button clicked
selectedAnswer = clickedButton.getText().toString();
clickedButton.setBackgroundColor(Color.MAGENTA);
}
}
void loadNewQuestion() {
if (currentQuestionIndex == totalQuestion) {
finishQuiz();
return;
}
questionTextView.setText(QuestionAnswer.question[currentQuestionIndex]);
ansA.setText(QuestionAnswer.choices[currentQuestionIndex][0]);
ansB.setText(QuestionAnswer.choices[currentQuestionIndex][1]);
ansC.setText(QuestionAnswer.choices[currentQuestionIndex][2]);
ansD.setText(QuestionAnswer.choices[currentQuestionIndex][3]);
}
void finishQuiz() {
String passStatus = "";
if (score > totalQuestion * 0.60) {
passStatus = "Passed";
} else {
passStatus = "Failed";
}
new AlertDialog.Builder(this)
.setTitle(passStatus)
.setMessage("Score is " + score + " out of " +totalQuestion)
.setPositiveButton("Restart", (dialogInterface, i) ->
restartQuiz())
.setCancelable(false)
.show();
}
void restartQuiz() {
score = 0;
currentQuestionIndex = 0;
loadNewQuestion();
}
}

7 Construct an app to display the image on date wise.

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:id="@+id/activity_main"

android:layout_width="match_parent"

android:layout_height="match_parent">

<ImageView

android:id="@+id/img1"

android:layout_width="200dp"

android:layout_height="200dp"

android:layout_centerHorizontal="true" />

<Button

android:id="@+id/button"

android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:layout_below="@+id/img1"

android:layout_centerHorizontal="true"

android:text="Change Image" />

</RelativeLayout>

MainActivity.java

package com.example.pratical7_1;

import android.support.v7.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 iv;

boolean flag;

int images[]={R.drawable.ic1,R.drawable.ic2,R.drawable.ic3 };

int i=0;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

iv=(ImageView) findViewById(R.id.img1);

b1=(Button) findViewById(R.id.button);

flag=true;

b1.setOnClickListener(new View.OnClickListener(){

@Override

public void onClick(View v) {

iv.setImageResource(images[i]);

i++;

if(i==1)

i=0;
}

});

Output:

Image added
8 .Construct image switcher using setFactory().
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageSwitcher
android:id="@+id/simpleImageSwitcher"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp" />
<Button
android:id="@+id/buttonNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:layout_gravity="center"
android:background="#050"
android:textColor="#fff"
android:textStyle="bold"
android:text="NEXT" />
</LinearLayout>

MainActivity.java
package com.example.pratical8;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ViewSwitcher;
public class MainActivity extends AppCompatActivity {
private ImageSwitcher simpleImageSwitcher;
Button btnNext;
// Array of Image IDs to Show In ImageSwitcher
int imageIds[] = {R.drawable.image1, R.drawable.images2,
R.drawable.image3, R.drawable.images4, R.drawable.images5};
int count = imageIds.length;
// to keep current Index of ImageID array
int currentIndex = -1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get The references of Button and ImageSwitcher
btnNext = (Button) findViewById(R.id.buttonNext);
simpleImageSwitcher = (ImageSwitcher)
findViewById(R.id.simpleImageSwitcher);
// Set the ViewFactory of the ImageSwitcher that will create
ImageView object when asked
simpleImageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
public View makeView() {
// TODO Auto-generated method stub
// Create a new ImageView and set it's properties
ImageView imageView = new
ImageView(getApplicationContext());
// set Scale type of ImageView to Fit Center
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
// set the Height And Width of ImageView To FIll PARENT
imageView.setLayoutParams(new
ImageSwitcher.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT));
return imageView;
}
});
// Declare in and out animations and load them using AnimationUtils
class
Animation in = AnimationUtils.loadAnimation(this,
android.R.anim.slide_in_left);
Animation out = AnimationUtils.loadAnimation(this,
android.R.anim.slide_out_right);
// set the animation type to ImageSwitcher
simpleImageSwitcher.setInAnimation(in);
simpleImageSwitcher.setOutAnimation(out);
// ClickListener for NEXT button
// When clicked on Button ImageSwitcher will switch between Images
// The current Image will go OUT and next Image will come in with
specified animation
btnNext.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
currentIndex++;
// Check If index reaches maximum then reset it
if (currentIndex == count)
currentIndex = 0;
simpleImageSwitcher.setImageResource(imageIds[currentIndex]); // set the
image in ImageSwitcher
}
});
}
}
Output:
9 Construct a bank app to display different menu like windrow, deposite etc.

Activity_main.xml

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

<LinearLayout

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"

android:orientation="vertical"

tools:context=".MainActivity">

<!--heading text view-->

<TextView

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_margin="20dp"

android:text="IFSC Code Validator"

android:textAlignment="center"

android:textColor="@color/purple_500"

android:textSize="30sp" />

<!-- edit text for entering our IFSC code

we are specifying input type as number

and we are also mentioning our input type

as textcapCharacters because IFSC code is

having all capital characters-->

<EditText

android:id="@+id/idedtIfscCode"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_margin="20dp"

android:hint="Enter IFSC code"


android:importantForAutofill="no"

android:inputType="textCapCharacters"

android:maxLines="1"

android:singleLine="true"

android:textAllCaps="true" />

<Button

android:id="@+id/idBtnGetBankDetails"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_horizontal"

android:layout_marginTop="50dp"

android:text="Get Bank Details"

android:textAllCaps="false" />

<EditText

android:id="@+id/idedtIfscCode1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_margin="20dp"

android:hint="windrow"

android:importantForAutofill="no"

android:inputType="textCapCharacters"

android:maxLines="1"

android:singleLine="true"

android:textAllCaps="true" />

<!--button to get the data from IFSC code-->

<Button

android:id="@+id/idBtnGetBankDetails1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_horizontal"

android:layout_marginTop="50dp"

android:text="Get Bank windrow"

android:textAllCaps="false" />

<EditText
android:id="@+id/idedtIfscCode2"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_margin="20dp"

android:hint="deposite"

android:importantForAutofill="no"

android:inputType="textCapCharacters"

android:maxLines="1"

android:singleLine="true"

android:textAllCaps="true" />

<!--button to get the data from IFSC code-->

<Button

android:id="@+id/idBtnGetBankDetails2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_horizontal"

android:layout_marginTop="50dp"

android:text="Get Money Deposite Bank "

android:textAllCaps="false" />

<!--text view to display the

data received from IFSC code-->

<TextView

android:id="@+id/idTVBankDetails"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_margin="20dp"

android:padding="10dp"

android:textAlignment="center"

android:textAllCaps="false"

android:textColor="@color/purple_500"

android:textSize="15sp" />

</LinearLayout>
MainActivity.java

package com.example.bank;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.text.TextUtils;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

import android.widget.Toast;

import org.json.JSONException;

import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {

// creating variables for edit text

// and our text views.

private EditText ifscCodeEdt;

private TextView bankDetailsTV;

// creating a variable for

// our ifsc code string.

String ifscCode;

// creating a variable for request queue.

private RequestQueue mRequestQueue;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

// initializing our variables.

ifscCodeEdt = findViewById(R.id.idedtIfscCode);

Button getBankDetailsBtn = findViewById(R.id.idBtnGetBankDetails);

bankDetailsTV = findViewById(R.id.idTVBankDetails);

// initializing our request queue variable with request queue

// and passing our context to it.

mRequestQueue = Volley.newRequestQueue(MainActivity.this);

// initializing on click listener for our button.


getBankDetailsBtn.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

// getting string from edittext.

ifscCode = ifscCodeEdt.getText().toString();

// validating if the edit text

// is empty or not.

if (TextUtils.isEmpty(ifscCode)) {

// displaying a toast message if the text field is

empty

Toast.makeText(MainActivity.this, "Please enter valid

IFSC code", Toast.LENGTH_SHORT).show();

} else {

// calling a method to display

// our ifsc code details.

getDataFromIFSCCode(ifscCode);

});

private void getDataFromIFSCCode(String ifscCode) {

// clearing our cache of request queue.

mRequestQueue.getCache().clear();

// below is the url from where we will be getting

// our response in the json format.

String url = "http://api.techm.co.in/api/v1/ifsc/" + ifscCode;

// below line is use to initialize our request queue.

RequestQueue queue = Volley.newRequestQueue(MainActivity.this);

// creating a json object request for our API.

JsonObjectRequest objectRequest = new

JsonObjectRequest(Request.Method.GET, url, null, new

Response.Listener<JSONObject>() {

@Override

public void onResponse(JSONObject response) {


// this method is used to get

// the response from the API.

try {

if (response.getString("status").equals("failed")) {

// checking if the response is not loaded and

// status for the repose is fail.

// if response status is failure we are displaying

// an invalid IFSC code in our text view.

bankDetailsTV.setText("Invalid IFSC Code");

} else {

// if the status is successful we are

// extracting data from JSON file

JSONObject dataObj =

response.getJSONObject("data");

String state = dataObj.optString("STATE");

String bankName = dataObj.optString("BANK");

String branch = dataObj.optString("BRANCH");

String address = dataObj.optString("ADDRESS");

String contact = dataObj.optString("CONTACT");

String micrcode = dataObj.optString("MICRCODE");

String city = dataObj.optString("CITY");

// after extracting this data we are displaying

// that data in our text view.

bankDetailsTV.setText("Bank Name : " + bankName +

"\nBranch : " + branch + "\nAddress : " + address + "\nMICR Code : " +

micrcode + "\nCity : " + city + "\nState : " + state + "\nContact : " +

contact);

} catch (JSONException e) {

// if we get any error while loading data

// we are setting our text as invalid IFSC code.

e.printStackTrace();

bankDetailsTV.setText("Invalid IFSC Code");

}
}

}, new Response.ErrorListener() {

@Override

public void onErrorResponse(VolleyError error) {

// if we get any error while loading json

// data we are setting our text to invalid IFSC code.

bankDetailsTV.setText("Invalid IFSC Code");

});

// below line is use for adding object

// request to our request queue.

queue.add(objectRequest);

Output:
10 Create an Android application, where the user can enter player name and points in one
view and display it in another view.
Activity_main.xml
<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"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="player name"
android:id="@+id/message_text"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="player score"
android:id="@+id/message_text1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="send"
android:onClick="sendmessage"
/>
</LinearLayout>
MainActivity.java
1package com.example.pratical10;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
EditText message_text;
EditText message_text1;
public final static String MESSAGE_KEY = ".message_key";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendmessage(View view) {
message_text = (EditText) findViewById(R.id.message_text);
String message = message_text.getText().toString();
Intent intent = new Intent(this, secondActivity.class);
intent.putExtra(MESSAGE_KEY, message);
startActivity(intent);
}
}
2<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".secondActivity">
</android.support.constraint.ConstraintLayout>
2
package com.example.pratical10;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class secondActivity extends Activity {
public final static String MESSAGE_KEY =".message_key";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String message = intent.getStringExtra(MESSAGE_KEY);
TextView textView = new TextView(this);
textView.setTextSize(45);
textView.setText(message);
}
}
Output:
12 Write an application to accept two numbers from the user, and displays them, but
reject input if both numbers are greater than 10 and asks for two new numbers.
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical" >
<TextView
android:layout_marginTop="200px"
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="No1" />
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
/>
<TextView
android:layout_marginTop="100px"
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="No2" />
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
/>
<Button
android:layout_marginTop="100px"
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
MainActivity.java
package com.example.practical12;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText edtuser, edtpass;
Button btnLogin;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtuser = (EditText) findViewById(R.id.editText);
edtpass = (EditText) findViewById(R.id.editText2);
btnLogin = (Button) findViewById(R.id.button);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (edtuser.length() != 1) {
edtuser.setError("greter than 10 ");
edtuser.requestFocus();
Toast.makeText(getApplicationContext(),"Text 1 is
greater than 10",Toast.LENGTH_SHORT).show();
}
else if (edtpass.length() != 1) {
edtpass.setError("greter than 10 ");
edtpass.requestFocus();
Toast.makeText(getApplicationContext(),"Text 2 is
greater than 10",Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(),"Rejected4",Toast.LENGTH_SHORT).show
();
Intent it = new Intent(getApplicationContext(),
MainActivity.class);
startActivity(it);
}
}
});
}
}
Output:
13 Create table Customer (id, name, address, phno). Create Application for Performing the following
operation on the table. (using sqlite database) i) Insert New Customer Details. ii) Show All the
Customer Details

Activity_main.xml

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

<AbsoluteLayout 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:layout_x="50dp"

android:layout_y="20dp"

android:text="Student Details"

android:textSize="30sp" />

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_x="20dp"

android:layout_y="110dp"

android:text="Enter Rollno:"

android:textSize="20sp" />

<EditText

android:id="@+id/Rollno"

android:layout_width="150dp"

android:layout_height="wrap_content"

android:layout_x="175dp"
android:layout_y="100dp"

android:inputType="number"

android:textSize="20sp" />

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_x="20dp"

android:layout_y="160dp"

android:text="Enter Name:"

android:textSize="20sp" />

<EditText

android:id="@+id/Name"

android:layout_width="150dp"

android:layout_height="wrap_content"

android:layout_x="175dp"

android:layout_y="150dp"

android:inputType="text"

android:textSize="20sp" />

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_x="20dp"

android:layout_y="210dp"

android:text="Enter Marks:"

android:textSize="20sp" />

<EditText

android:id="@+id/Marks"

android:layout_width="150dp"

android:layout_height="wrap_content"

android:layout_x="175dp"

android:layout_y="200dp"

android:inputType="number"

android:textSize="20sp" />

<Button
android:id="@+id/Insert"

android:layout_width="150dp"

android:layout_height="wrap_content"

android:layout_x="25dp"

android:layout_y="300dp"

android:text="Insert"

android:textSize="30dp" />

<Button

android:id="@+id/Delete"

android:layout_width="150dp"

android:layout_height="wrap_content"

android:layout_x="200dp"

android:layout_y="300dp"

android:text="Delete"

android:textSize="30dp" />

<Button

android:id="@+id/Update"

android:layout_width="150dp"

android:layout_height="wrap_content"

android:layout_x="25dp"

android:layout_y="400dp"

android:text="Update"

android:textSize="30dp" />

<Button

android:id="@+id/View"

android:layout_width="150dp"

android:layout_height="wrap_content"

android:layout_x="200dp"

android:layout_y="400dp"

android:text="View"

android:textSize="30dp" />

<Button

android:id="@+id/ViewAll"

android:layout_width="200dp"
android:layout_height="wrap_content"

android:layout_x="100dp"

android:layout_y="500dp"

android:text="View All"

android:textSize="30dp" />

</AbsoluteLayout>

MainActivity.java

package com.example.practical13;

import android.app.Activity;

import android.app.AlertDialog.Builder;

import android.content.Context;

import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

public class MainActivity extends Activity implements OnClickListener

EditText Rollno,Name,Marks;

Button Insert,Delete,Update,View,ViewAll;

SQLiteDatabase db;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState)

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Rollno=(EditText)findViewById(R.id.Rollno);

Name=(EditText)findViewById(R.id.Name);

Marks=(EditText)findViewById(R.id.Marks);

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

Delete=(Button)findViewById(R.id.Delete);
Update=(Button)findViewById(R.id.Update);

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

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

Insert.setOnClickListener(this);

Delete.setOnClickListener(this);

Update.setOnClickListener(this);

View.setOnClickListener(this);

ViewAll.setOnClickListener(this);

// Creating database and table

db=openOrCreateDatabase("StudentDB", Context.MODE_PRIVATE, null);

db.execSQL("CREATE TABLE IF NOT EXISTS student(rollno VARCHAR,name

VARCHAR,marks VARCHAR);");

public void onClick(View view)

// Inserting a record to the Student table

if(view==Insert)

// Checking for empty fields

if(Rollno.getText().toString().trim().length()==0||

Name.getText().toString().trim().length()==0||

Marks.getText().toString().trim().length()==0)

showMessage("Error", "Please enter all values");

return;

db.execSQL("INSERT INTO student

VALUES('"+Rollno.getText()+"','"+Name.getText()+

"','"+Marks.getText()+"');");

showMessage("Success", "Record added");

clearText();

// Deleting a record from the Student table

if(view==Delete)
{

// Checking for empty roll number

if(Rollno.getText().toString().trim().length()==0)

showMessage("Error", "Please enter Rollno");

return;

Cursor c=db.rawQuery("SELECT * FROM student WHERE

rollno='"+Rollno.getText()+"'", null);

if(c.moveToFirst())

db.execSQL("DELETE FROM student WHERE

rollno='"+Rollno.getText()+"'");

showMessage("Success", "Record Deleted");

else

showMessage("Error", "Invalid Rollno");

clearText();

// Updating a record in the Student table

if(view==Update)

// Checking for empty roll number

if(Rollno.getText().toString().trim().length()==0)

showMessage("Error", "Please enter Rollno");

return;

Cursor c=db.rawQuery("SELECT * FROM student WHERE

rollno='"+Rollno.getText()+"'", null);

if(c.moveToFirst()) {

db.execSQL("UPDATE student SET name='" + Name.getText() +


"',marks='" + Marks.getText() +

"' WHERE rollno='"+Rollno.getText()+"'");

showMessage("Success", "Record Modified");

else {

showMessage("Error", "Invalid Rollno");

clearText();

// Display a record from the Student table

if(view==View)

// Checking for empty roll number

if(Rollno.getText().toString().trim().length()==0)

showMessage("Error", "Please enter Rollno");

return;

Cursor c=db.rawQuery("SELECT * FROM student WHERE

rollno='"+Rollno.getText()+"'", null);

if(c.moveToFirst())

Name.setText(c.getString(1));

Marks.setText(c.getString(2));

else

showMessage("Error", "Invalid Rollno");

clearText();

// Displaying all the records

if(view==ViewAll)

{
Cursor c=db.rawQuery("SELECT * FROM student", null);

if(c.getCount()==0)

showMessage("Error", "No records found");

return;

StringBuffer buffer=new StringBuffer();

while(c.moveToNext())

buffer.append("Rollno: "+c.getString(0)+"\n");

buffer.append("Name: "+c.getString(1)+"\n");

buffer.append("Marks: "+c.getString(2)+"\n\n");

showMessage("Student Details", buffer.toString());

public void showMessage(String title,String message)

Builder builder=new Builder(this);

builder.setCancelable(true);

builder.setTitle(title);

builder.setMessage(message);

builder.show();

public void clearText()

Rollno.setText("");

Name.setText("");

Marks.setText("");

Rollno.requestFocus();

Output:
14 Create an application that allows the user to enter a number in the textbox named „getnum‟. Check
whether the number in the textbox „getnum‟ is palindrome or not. Print the message accordingly in
the label control named lbldisplay when the user clicks on the button „check‟.

Activity_main.xml

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

<android.support.constraint.ConstraintLayout

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity">

<EditText

android:id="@+id/string"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:ems="10"
android:inputType="textPersonName"

android:minHeight="48dp"

android:hint="Enter the String"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintHorizontal_bias="0.497"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toTopOf="parent"

app:layout_constraintVertical_bias="0.192" />

<Button

android:id="@+id/button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Check Palindrome"

app:layout_constraintStart_toStartOf="@+id/string"

app:layout_constraintEnd_toEndOf="@+id/string"

app:layout_constraintTop_toBottomOf="@+id/string"

android:layout_marginTop="30dp"/>

<TextView

android:id="@+id/textView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textSize="14sp"

app:layout_constraintStart_toStartOf="@+id/button"

app:layout_constraintEnd_toEndOf="@+id/button"

app:layout_constraintTop_toBottomOf="@+id/button"

android:layout_marginTop="30dp"/>

</android.support.constraint.ConstraintLayout>

MainActivity.java

package com.example.pratical14;

import android.annotation.SuppressLint;

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

import android.widget.Button;
import android.widget.EditText;

import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

EditText string;

TextView result;

Button button;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

string = findViewById(R.id.string);

result = findViewById(R.id.textView);

button = findViewById(R.id.button);

button.setOnClickListener(view -> {

String stringValue = string.getText().toString();

checkPalindrome(stringValue);

});

@SuppressLint("SetTextI18n")

private void checkPalindrome(String stringValue) {

String reversed = new

StringBuilder(stringValue).reverse().toString();

if(stringValue.equals(reversed))

result.setText("Palindrome");

else

result.setText("Not Palindrome");

Output:
15 Create Following Table: Emp (emp_no,emp_name,address,phone,salary) Dept
(dept_no,dept_name,location) Emp-Dept is related with one-many relationship. Create application for
performing the following Operation on the table 1) Add Records into Emp and Dept table. 2) Accept
Department name from User and delete employee information which belongs to that department.

Activity_main.xml

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

<AbsoluteLayout 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:layout_x="50dp"

android:layout_y="20dp"

android:text="emp Details"

android:textSize="30sp" />

<TextView

android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:layout_x="20dp"

android:layout_y="110dp"

android:text="emp_no:"

android:textSize="20sp" />

<EditText

android:id="@+id/Rollno"

android:layout_width="150dp"

android:layout_height="wrap_content"

android:layout_x="175dp"

android:layout_y="100dp"

android:inputType="number"

android:textSize="20sp" />

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_x="20dp"

android:layout_y="160dp"

android:text="emp_name:"

android:textSize="20sp" />

<EditText

android:id="@+id/Name"

android:layout_width="150dp"

android:layout_height="wrap_content"

android:layout_x="175dp"

android:layout_y="150dp"

android:inputType="text"

android:textSize="20sp" />

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_x="20dp"

android:layout_y="210dp"

android:text="e_location:"
android:textSize="20sp" />

<EditText

android:id="@+id/e_location"

android:layout_width="150dp"

android:layout_height="wrap_content"

android:layout_x="175dp"

android:layout_y="200dp"

android:inputType="text"

android:textSize="20sp" />

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_x="50dp"

android:layout_y="20dp"

android:text="emp Details"

android:textSize="30sp" />

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_x="20dp"

android:layout_y="110dp"

android:text="emp_no:"

android:textSize="20sp" />

<EditText

android:id="@+id/emp_no"

android:layout_width="150dp"

android:layout_height="wrap_content"

android:layout_x="175dp"

android:layout_y="100dp"

android:inputType="number"

android:textSize="20sp" />

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"
android:layout_x="20dp"

android:layout_y="160dp"

android:text="emp_name:"

android:textSize="20sp" />

<EditText

android:id="@+id/emp_name"

android:layout_width="150dp"

android:layout_height="wrap_content"

android:layout_x="175dp"

android:layout_y="150dp"

android:inputType="text"

android:textSize="20sp" />

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_x="28dp"

android:layout_y="424dp"

android:text="location:"

android:textSize="20sp" />

<EditText

android:id="@+id/location"

android:layout_width="150dp"

android:layout_height="wrap_content"

android:layout_x="162dp"

android:layout_y="417dp"

android:inputType="text"

android:textSize="20sp" />

<Button

android:id="@+id/Insert"

android:layout_width="150dp"

android:layout_height="wrap_content"

android:layout_x="45dp"

android:layout_y="477dp"

android:text="Insert"
android:textSize="30dp" />

<Button

android:id="@+id/Delete"

android:layout_width="150dp"

android:layout_height="wrap_content"

android:layout_x="216dp"

android:layout_y="475dp"

android:text="Delete"

android:textSize="30dp" />

<Button

android:id="@+id/ViewAll"

android:layout_width="200dp"

android:layout_height="wrap_content"

android:layout_x="114dp"

android:layout_y="552dp"

android:text="View All"

android:textSize="30dp" />

<TextView

android:id="@+id/textView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_x="50dp"

android:layout_y="265dp"

android:text="dept Detail"

android:textSize="30sp"/>

<TextView

android:id="@+id/textView3"

android:layout_width="103dp"

android:layout_height="wrap_content"

android:layout_x="24dp"

android:layout_y="321dp"

android:text="dept_no"

android:textSize="20sp" />

<EditText
android:id="@+id/dept_no"

android:layout_width="150dp"

android:layout_height="wrap_content"

android:layout_x="158dp"

android:layout_y="312dp"

android:inputType="number"

android:textSize="20sp" />

<TextView

android:id="@+id/textView31"

android:layout_width="105dp"

android:layout_height="wrap_content"

android:layout_x="14dp"

android:layout_y="371dp"

android:text="dept_name"

android:textSize="20sp" />

<EditText

android:id="@+id/dept_name"

android:layout_width="150dp"

android:layout_height="wrap_content"

android:layout_x="159dp"

android:layout_y="366dp"

android:inputType="text"

android:textSize="20sp" />

</AbsoluteLayout>

MainActivity.java

package com.example.practical15;

import android.app.Activity;

import android.app.AlertDialog.Builder;

import android.content.Context;

import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;
import android.widget.Button;

import android.widget.EditText;

public class MainActivity extends Activity implements OnClickListener {

EditText emp_no, emp_name, e_location,dept_no,dept_name,location;

Button Insert, Delete, ViewAll;

SQLiteDatabase db;

/**

* Called when the activity is first created.

*/

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

emp_no = (EditText) findViewById(R.id.emp_no);

emp_name = (EditText) findViewById(R.id.emp_name);

e_location = (EditText) findViewById(R.id.e_location);

dept_no = (EditText) findViewById(R.id.dept_no);

dept_name = (EditText) findViewById(R.id.dept_name);

location = (EditText) findViewById(R.id.location);

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

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

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

Insert.setOnClickListener(this);

Delete.setOnClickListener(this);

ViewAll.setOnClickListener(this);

// Creating database and table

db = openOrCreateDatabase("empDB", Context.MODE_PRIVATE, null);

db.execSQL("CREATE TABLE IF NOT EXISTS emp(emp_no VARCHAR,emp_name

VARCHAR,e_location VARCHAR,dept_no VARCHAR,dept_name VARCHAR,location

VARCHAR);");

public void onClick(View view) {

// Inserting a record to the Student table

if (view == Insert) {
// Checking for empty fields

if (emp_no.getText().toString().trim().length() == 0 ||

emp_name.getText().toString().trim().length() == 0 ||

e_location.getText().toString().trim().length() == 0 ||

dept_no.getText().toString().trim().length() == 0 ||

dept_name.getText().toString().trim().length() == 0

||

location.getText().toString().trim().length() ==

0 ){

showMessage("Error", "Please enter all values");

return;

db.execSQL("INSERT INTO emp VALUES('" + emp_no.getText() +

"','" + emp_name.getText() +

"','" + e_location.getText() + "');");

showMessage("Success", "Record added");

clearText();

// Deleting a record from the Student table

if (view == Delete) {

// Checking for empty roll number

if (emp_no.getText().toString().trim().length() == 0) {

showMessage("Error", "Please enter emp_no");

return;

Cursor c = db.rawQuery("SELECT * FROM emp WHERE emp_no='" +

emp_no.getText() + "'", null);

if (c.moveToFirst()) {

db.execSQL("DELETE FROM emp WHERE emp_no='" +

emp_no.getText() + "'");

showMessage("Success", "Record Deleted");

} else {

showMessage("Error", "Invalid emp_no");

}
clearText();

// Checking for empty roll number

if (emp_no.getText().toString().trim().length() == 0) {

showMessage("Error", "Please enter emp_no");

return;

Cursor c = db.rawQuery("SELECT * FROM emp WHERE rollno='" +

emp_no.getText() + "'", null);

if (c.moveToFirst()) {

db.execSQL("UPDATE emp SET name='" + emp_name.getText() +

"',marks='" + e_location.getText() +

"' WHERE emp_no='" + emp_no.getText() + "'");

showMessage("Success", "Record Modified");

} else {

showMessage("Error", "Invalid emp_no");

clearText();

// Display a record from the Student table

// Checking for empty roll number

if (emp_no.getText().toString().trim().length() == 0) {

showMessage("Error", "Please enter Rollno");

return;

Cursor c = db.rawQuery("SELECT * FROM emp WHERE emp_no='" +

emp_no.getText() + "'", null);

if (c.moveToFirst()) {

emp_name.setText(c.getString(1));

e_location.setText(c.getString(2));

} else {

showMessage("Error", "Invalid emp_no");


clearText();

// Displaying all the records

if (view == ViewAll) {

Cursor c = db.rawQuery("SELECT * FROM emp", null);

if (c.getCount() == 0) {

showMessage("Error", "No records found");

return;

StringBuffer buffer = new StringBuffer();

while (c.moveToNext()) {

buffer.append("emp_no: " + c.getString(0) + "\n");

buffer.append("emp_name: " + c.getString(1) + "\n");

buffer.append("e_location: " + c.getString(2) + "\n\n");

showMessage("emp Details", buffer.toString());

public void showMessage(String title, String message) {

Builder builder = new Builder(this);

builder.setCancelable(true);

builder.setTitle(title);

builder.setMessage(message);

builder.show();

public void clearText() {

emp_no.setText("");

emp_name.setText("");

e_location.setText("");

emp_no.requestFocus();

dept_no.setText("");

dept_name.setText("");

location.setText("");
dept_no.requestFocus();

Output:

16 Java Andorid Program to Perform all arithmetic Operations using Calculators .

Activity_main.xml

<?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"

android:padding="20dp"

android:orientation="vertical">

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="arithmetic Operations"

android:textSize="25sp"

android:layout_marginBottom="16dp"
android:textColor="@android:color/black" />

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal"

android:layout_marginBottom="20dp">

<EditText

android:id="@+id/first_no"

android:layout_width="102dp"

android:layout_height="59dp"

android:ems="10"

android:layout_marginHorizontal="50dp"

android:hint="Enter" />

<EditText

android:id="@+id/second_no"

android:layout_width="102dp"

android:layout_height="59dp"

android:ems="10"

android:hint="Enter" />

</LinearLayout>

<LinearLayout

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:orientation="horizontal"

android:layout_marginBottom="20dp">

<TextView

android:textSize="35sp"

android:id="@+id/answer"

android:layout_width="102dp"

android:layout_height="59dp"

android:layout_marginHorizontal="50dp"

android:hint="ans" />

</LinearLayout>

<LinearLayout
android:orientation="vertical"

android:layout_marginLeft="250dp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginBottom="30dp">

<Button

android:id="@+id/sub"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="sub -"

android:textSize="25sp"

android:layout_marginBottom="16dp" />

<Button

android:id="@+id/add"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginBottom="16dp"

android:text="add +"

android:textSize="25sp"

tools:ignore="OnClick" />

<Button

android:id="@+id/div"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="divide /"

android:textSize="25sp"

android:layout_marginBottom="16dp" />

<Button

android:id="@+id/mul"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginBottom="16dp"

android:text="mul X"

android:textSize="25sp"/>
<Button

android:id="@+id/equals"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginBottom="16dp"

android:text="="

android:textSize="35sp"/>

</LinearLayout>

</LinearLayout>

MainActivity.java

package com.example.pratical18;

//import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

EditText no1 , no2;

Button add ,mul ,div , sub,equal;

TextView answer;

double ans = 0;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

// for text views

no1 = findViewById(R.id.first_no);

no2 = findViewById(R.id.second_no);

// for button with operations

add = findViewById(R.id.add);

mul = findViewById(R.id.mul);
div = findViewById(R.id.div);

sub = findViewById(R.id.sub);

// for equal to button

equal = findViewById(R.id.equals);

// for answer field

answer = findViewById(R.id.answer);

add.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

String num1 = no1.getText().toString();

String num2 = no2.getText().toString();

if (num1.isEmpty() || num2.isEmpty()) {

Toast.makeText(getApplicationContext(),"Enter

Numbers",Toast.LENGTH_SHORT).show();

else {

double a =

Double.parseDouble(no1.getText().toString());

double b =

Double.parseDouble(no2.getText().toString());

ans = a + b;

});

sub.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

String num1 = no1.getText().toString();

String num2 = no2.getText().toString();

if (num1.isEmpty() || num2.isEmpty()) {

Toast.makeText(getApplicationContext(),"Enter

Numbers",Toast.LENGTH_SHORT).show();

else {
double a =

Double.parseDouble(no1.getText().toString());

double b =

Double.parseDouble(no2.getText().toString());

ans = a - b;

});

mul.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

String num1 = no1.getText().toString();

String num2 = no2.getText().toString();

if (num1.isEmpty() || num2.isEmpty()) {

Toast.makeText(getApplicationContext(),"Enter

Numbers",Toast.LENGTH_SHORT).show();

else {

double a =

Double.parseDouble(no1.getText().toString());

double b =

Double.parseDouble(no2.getText().toString());

ans = a * b;

});

div.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

String num1 = no1.getText().toString();

String num2 = no2.getText().toString();

if (num1.isEmpty() || num2.isEmpty()) {

Toast.makeText(getApplicationContext(), "Enter

Numbers", Toast.LENGTH_SHORT).show();
} else {

double a =

Double.parseDouble(no1.getText().toString());

double b =

Double.parseDouble(no2.getText().toString());

if (b != 0)

ans = a / b;

else

Toast.makeText(getApplicationContext(), "Enter

Valid Numbers", Toast.LENGTH_SHORT).show();

});

equal.setOnClickListener(new

View.OnClickListener() {

@Override

public void onClick(View v)

String ans1 =

String.valueOf(ans);

answer.setText(ans1);

ans= 0;

});

Output:
17 Java Android Program to Change the Image Displayed on the Screen

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" >

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal"

android:paddingBottom="40px"

android:weightSum="2" >

<RadioGroup

android:id="@+id/rg1"

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:layout_weight="1"
android:orientation="vertical" >

<RadioButton

android:id="@+id/radioButton1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentRight="true"

android:layout_alignParentTop="true"

android:layout_marginTop="20dp"

android:text="Image2" />

<RadioButton

android:id="@+id/radioButton2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignBaseline="@+id/radioButton1"

android:layout_alignBottom="@+id/radioButton1"

android:layout_alignParentLeft="true"

android:text="Image1" />

</RadioGroup>

<RadioGroup

android:id="@+id/rg2"

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:layout_weight="1"

android:orientation="vertical" >

<RadioButton

android:id="@+id/radioButton3"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="30dp"

android:text="Image3" />

<RadioButton

android:id="@+id/radioButton4"

android:layout_width="wrap_content"

android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/radioButton3"

android:layout_alignBottom="@+id/radioButton3"

android:layout_alignParentRight="true"

android:text="Image4" />

</RadioGroup>

</LinearLayout>

<Button

android:id="@+id/button1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:layout_alignParentRight="true"

android:layout_centerVertical="true"

android:text="Change Image" />

<ImageView

android:id="@+id/imageView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/button1"

android:layout_alignParentBottom="true"

android:layout_alignParentRight="true"

android:layout_below="@+id/button1"

android:layout_marginTop="50dp"/>

</RelativeLayout>

MainActivity.java

package com.example.pra18;

import android.app.Activity;

import android.graphics.Typeface;

import android.os.Bundle;

import android.renderscript.Type;

import android.view.Gravity;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;
import android.widget.ImageView;

import android.widget.RadioGroup;

import android.widget.RadioGroup.OnCheckedChangeListener;

import android.widget.TextView;

public class MainActivity extends Activity implements

OnCheckedChangeListener {

RadioGroup group1, group2;

Button gen;

ImageView img;

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

group1 = (RadioGroup) findViewById(R.id.rg1);

group1.setOnCheckedChangeListener((OnCheckedChangeListener) this);

group2 = (RadioGroup) findViewById(R.id.rg2);

group2.setOnCheckedChangeListener((OnCheckedChangeListener) this);

img = (ImageView) findViewById(R.id.imageView1);

// oncheckedChanged function

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

gen.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

});

@Override

public void onCheckedChanged(RadioGroup group, int checkedId) {

// TODO Auto-generated method stub

switch (checkedId) {

case R.id.radioButton1:

img.setImageResource(R.drawable.image4);
break;

case R.id.radioButton2:

img.setImageResource(R.drawable.image5);

break;

case R.id.radioButton3:

img.setImageResource(R.drawable.image6);

break;

case R.id.radioButton4:

img.setImageResource(R.drawable.image7);

break;

default:

break;

}}

Output:

18 Java Android Program to Demonstrate Alert Dialog Box

Activity_main.xml

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

<android.support.constraint.ConstraintLayout

xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity">

<Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/button"

android:text="Close app"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintLeft_toLeftOf="parent"

app:layout_constraintRight_toRightOf="parent"

app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

Values

Strings.xml

<resources>

<string name="app_name">AlertDialog</string>

<string name="dialog_message">Welcome to Alert Dialog</string>

<string name="dialog_title">Javatpoint Alert Dialog</string>

</resources>

MainActivity.java

package com.example.pratical20;

import android.content.DialogInterface;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.app.AlertDialog;

import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

Button closeButton;

AlertDialog.Builder builder;
@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

closeButton = (Button) findViewById(R.id.button);

builder = new AlertDialog.Builder(this);

closeButton.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

//Uncomment the below code to Set the message and title

from the strings.xml file

builder.setMessage(R.string.dial

og_message) .setTitle(R.string.dialog_title);

//Setting message manually and performing action on button

click

builder.setMessage("Do you want to close this

application ?")

.setCancelable(false)

.setPositiveButton("Yes", new

DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int

id) {

finish();

Toast.makeText(getApplicationContext(),"you

choose yes action for alertbox",

Toast.LENGTH_SHORT).show();

})

.setNegativeButton("No", new

DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int

id) {

// Action for 'NO' Button

dialog.cancel();
Toast.makeText(getApplicationContext(),"you

choose no action for alertbox",

Toast.LENGTH_SHORT).show();

});

//Creating dialog box

AlertDialog alert = builder.create();

//Setting the title manually

alert.setTitle("AlertDialogExample");

alert.show();

});

Output:

19 Java Android Program to Demonstrate the Menu Application

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


<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item android:id="@+id/search_item"

android:title="Search" />

<item android:id="@+id/upload_item"

android:title="Upload" />

<item android:id="@+id/copy_item"

android:title="Copy" />

<item android:id="@+id/print_item"

android:title="Print" />

<item android:id="@+id/share_item"

android:title="Share" />

<item android:id="@+id/bookmark_item"

android:title="BookMark" />

</menu>

package.com.example.practical19;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

@Override

public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.options_menu, menu);

return true;

@Override
public boolean onOptionsItemSelected(MenuItem item) {

Toast.makeText(this, "Selected Item: " +item.getTitle(), Toast.LENGTH_SHORT).show();

switch (item.getItemId()) {

case R.id.search_item:

// do your code

return true;

case R.id.upload_item:

// do your code

return true;

case R.id.copy_item:

// do your code

return true;

case R.id.print_item:

// do your code

return true;

case R.id.share_item:

// do your code

return true;

case R.id.bookmark_item:

// do your code

return true;

default:

return super.onOptionsItemSelected(item);

Output:
Q22. Java Android Prgram to send email with attachment

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

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="vertical"

android:padding="4dp"

tools:context=".MainActivity">

<EditText

android:id="@+id/etTo"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_margin="5dp"

android:hint="Receiver's Email Address!"

android:inputType="textEmailAddress"

android:singleLine="true" />

<EditText

android:id="@+id/etSubject"

android:layout_width="match_parent"

android:layout_height="wrap_content"
android:layout_margin="5dp"

android:hint="Enter Subject"

android:singleLine="true" />

<EditText

android:id="@+id/etMessage"

android:layout_width="match_parent"

android:layout_height="200dp"

android:layout_margin="5dp"

android:gravity="top|start"

android:hint="Compose Email"

android:inputType="textMultiLine" />

<RelativeLayout

android:layout_width="match_parent"

android:layout_height="wrap_content">

<Button

android:id="@+id/btSend"

android:layout_width="80dp"

android:layout_height="50dp"

android:layout_margin="5dp"

android:text="Send" />

<Button

android:id="@+id/btAttachment"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentEnd="true"

android:text="attachment" />

</RelativeLayout>

<TextView

android:id="@+id/tvAttachment"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:drawableStart="@drawable/ic_attach"

android:visibility="gone" />

</LinearLayout>
Package.com.example.practical22;

import android.app.Activity;

import android.content.Intent;

import android.net.Uri;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

import android.widget.Toast;

public class MainActivity extends Activity {

EditText etEmail;

EditText etSubject;

EditText etMessage;

Button Send;

Button attachment;

TextView tvAttachment;

String email;

String subject;

String message;

Uri URI = null;

private static final int PICK_FROM_GALLERY = 101;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

etEmail = findViewById(R.id.etTo);

etSubject = findViewById(R.id.etSubject);

etMessage = findViewById(R.id.etMessage);

attachment = findViewById(R.id.btAttachment);

tvAttachment = findViewById(R.id.tvAttachment);

Send = findViewById(R.id.btSend);

Send.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

sendEmail();

});

//attachment button listener

attachment.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

openFolder();

});

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

if (requestCode == PICK_FROM_GALLERY && resultCode == RESULT_OK) {

URI = data.getData();

tvAttachment.setText(URI.getLastPathSegment());

tvAttachment.setVisibility(View.VISIBLE);

public void sendEmail() {

try {

email = etEmail.getText().toString();

subject = etSubject.getText().toString();

message = etMessage.getText().toString();

final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);

emailIntent.setType("plain/text");

emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{email});

emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);

if (URI != null) {

emailIntent.putExtra(Intent.EXTRA_STREAM, URI);

emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);

this.startActivity(Intent.createChooser(emailIntent, "Sending email..."));

} catch (Throwable t) {
Toast.makeText(this, "Request failed try again: "+ t.toString(), Toast.LENGTH_LONG).show();

public void openFolder() {

Intent intent = new Intent();

intent.setType("image/*");

intent.setAction(Intent.ACTION_GET_CONTENT);

intent.putExtra("return-data", true);

startActivityForResult(Intent.createChooser(intent, "Complete action using"),


PICK_FROM_GALLERY);

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

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

package="com.example.practical24">

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

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

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

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

<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.Practical24"

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>

</application>

</manifest>

Ouput:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy