0% found this document useful (0 votes)
29 views

1) Adding Two Numbers

1. The document describes code for building a simple calculator app in Android. It includes XML layout code with EditTexts for user input, operation Buttons, and a TextView to display results. The Java code handles button clicks by parsing the user input, performing calculations, and updating the result TextView. 2. It also includes code to log the activity lifecycle methods as the app runs, to demonstrate the activity lifecycle. 3. Additional code shows calculating a factorial of a user-entered number, displaying the result, and including an ImageView to display an image on the screen. 4. The last part describes code for an activity with EditTexts for email and password, and passing these values between

Uploaded by

Nandana Ullas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

1) Adding Two Numbers

1. The document describes code for building a simple calculator app in Android. It includes XML layout code with EditTexts for user input, operation Buttons, and a TextView to display results. The Java code handles button clicks by parsing the user input, performing calculations, and updating the result TextView. 2. It also includes code to log the activity lifecycle methods as the app runs, to demonstrate the activity lifecycle. 3. Additional code shows calculating a factorial of a user-entered number, displaying the result, and including an ImageView to display an image on the screen. 4. The last part describes code for an activity with EditTexts for email and password, and passing these values between

Uploaded by

Nandana Ullas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 74

1) Adding two numbers

activity_main.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical">

<EditText
android:id="@+id/number1"
android:layout_width="321dp"
android:layout_height="67dp"
android:ems="10"
android:hint="no 1"
android:inputType="number"
tools:layout_editor_absoluteX="93dp"
tools:layout_editor_absoluteY="206dp"
tools:text="Enter Number 1" />

<EditText
android:id="@+id/number2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="no2"
android:inputType="number"
tools:text="Enter Number 2" />

<Button
android:id="@+id/add"
android:layout_width="182dp"
android:layout_height="wrap_content"
android:text="+"
tools:layout_editor_absoluteX="143dp"
tools:layout_editor_absoluteY="402dp" />

<Button
android:id="@+id/sub"
android:layout_width="182dp"
android:layout_height="wrap_content"
android:text="-" />

<Button
android:id="@+id/mul"
android:layout_width="181dp"
android:layout_height="wrap_content"
android:text="X" />

<Button
android:id="@+id/div"
android:layout_width="179dp"
android:layout_height="wrap_content"
android:text="/" />

<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="result"
android:textSize="25dp" />
</LinearLayout>

MainAvtivity.java
public class MainActivity extends AppCompatActivity {
EditText e1,e2;
TextView t1;
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1=(EditText)findViewById(R.id.number1);
e2=(EditText)findViewById(R.id.number2);
t1=(TextView)findViewById(R.id.result);
b1=(Button)findViewById(R.id.add);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Int i1= Integer.parseInt(e1.getText().toString());
Int i2= Integer.parseInt(e2.getText().toString());
Int i3=i1+i2;
t1.setText(Integer.toString(i3));
}
});
}
2)Simple calculator

<?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"
tools:context=".MainActivity">

<EditText
android:id="@+id/number1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter first number"
android:inputType="number" />

<EditText
android:id="@+id/number2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter second number"
android:inputType="number" />

<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Result"
android:textSize="25dp" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow>
<Button
android:id="@+id/add"
android:layout_width="64dp"
android:layout_height="wrap_content"
android:text="+" />
<Button
android:id="@+id/sub"
android:layout_width="58dp"
android:layout_height="wrap_content"
android:text="-" />
</TableRow>
<TableRow>
<Button
android:id="@+id/mul"
android:layout_width="66dp"
android:layout_height="wrap_content"
android:text="X" />
<Button
android:id="@+id/div"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:text="/" />
</TableRow>
</TableLayout>
</LinearLayout>

JAVA
package com.example.sathnew;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {


EditText e1,e2;
TextView t1;
Button b1,b2,b3,b4;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1=(EditText)findViewById(R.id.number1);
e2=(EditText)findViewById(R.id.number2);
t1=(TextView)findViewById(R.id.result);
b1=(Button)findViewById(R.id.add);
b2=(Button)findViewById(R.id.sub);
b3=(Button)findViewById(R.id.mul);
b4=(Button)findViewById(R.id.div);

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int i1 = Integer.parseInt(e1.getText().toString());
int i2 = Integer.parseInt(e2.getText().toString());
int i3 = i1 + i2;
t1.setText(Integer.toString(i3));
}
}) ;
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int i1 = Integer.parseInt(e1.getText().toString());
int i2 = Integer.parseInt(e2.getText().toString());
int i3 = i1 - i2;
t1.setText(Integer.toString(i3));
}
}) ;
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int i1 = Integer.parseInt(e1.getText().toString());
int i2 = Integer.parseInt(e2.getText().toString());
int i3 = i1 * i2;
t1.setText(Integer.toString(i3));

}
}) ;

b4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
float i1 = Float.parseFloat(e1.getText().toString());
float i2 = Float.parseFloat(e2.getText().toString());
float i3 = i1 / i2;
t1.setText(Float.toString(i3));

}
}) ;

}
}
3) Familiarization of Activity Life Cycle

package com.example.activity_life;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
String tag="activity life cycle";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(tag,"In the onCreate() event");
}
public void onStart()
{
super.onStart();
Log.d(tag,"In the onStart() event");
}

public void onRestart()


{
super.onRestart();
Log.d(tag,"In the onRestart() event");
}
public void onResume()
{
super.onResume();
Log.d(tag,"In the onResume() event");
}
public void onPause()
{
super.onPause();
Log.d(tag,"In the onPause() event");
}

public void onStop()


{
super.onStop();
Log.d(tag,"In the onStop() event");
}
public void onDestroy()
{
super.onDestroy();
Log.d(tag,"In the onDestroy() event");
}
}

4) Facorial and imageview


<?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"
tools:context=".MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter the number"
android:inputType="textPersonName" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Result"
android:textSize="30dp" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/janapathi">
</ImageView>
</LinearLayout>

java code
package com.example.factorial;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {


Button cal ;
EditText txt;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt=(EditText)findViewById(R.id.editText);
cal=(Button)findViewById(R.id.button);
textView=(TextView)findViewById(R.id.textView);
cal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

int num=Integer.parseInt(txt.getText().toString());
factorial(num);
}
});

}
void factorial(double num)
{
int fact =1,i;
for(i=1;i<=num;i++)
{
fact=fact*i;
}

textView.setText("Factorial is"+fact);
}
}

5) Passing intent with email and password check


Acitivty_1

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


<AbsoluteLayout
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/email"
android:layout_width="357dp"
android:layout_height="wrap_content"
android:layout_x="17dp"
android:layout_y="91dp"
android:ems="10"
android:hint="Enter Email"
android:inputType="textPersonName" />
<EditText
android:id="@+id/password"
android:layout_width="337dp"
android:layout_height="wrap_content"
android:layout_x="24dp"
android:layout_y="159dp"
android:ems="10"
android:hint="Password"
android:inputType="textPersonName" />
<Button
android:id="@+id/registers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="148dp"
android:layout_y="238dp"
android:text="Register" />
</AbsoluteLayout>

Main Activity.java
package com.example.firstactivity;

import androidx.appcompat.app.AppCompatActivity;

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

import java.security.PrivateKey;

public class MainActivity extends AppCompatActivity {


private EditText memail, mpassword;
private Button mregister;

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

memail=(EditText)findViewById(R.id.email);
mpassword=(EditText)findViewById(R.id.password);
mregister=(Button)findViewById(R.id.registers);
mregister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String mail=memail.getText().toString();
String pass=mpassword.getText().toString();

if(mail.isEmpty())
{
memail.setError("Email not entered");
}else
{
memail.setError(null);

}
if(pass.isEmpty())
{
mpassword.setError("please enter password");
}

else
{
memail.setError(null);
}
if(!mail.isEmpty() || !pass.isEmpty())
{
Intent intent=new Intent(getApplicationContext(),Activity_2.class);
startActivity(intent);
}

}
});

}
}

Acitivty_2

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


<AbsoluteLayout
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=".Activity_2">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="4dp"
android:layout_y="239dp"
android:text="You are successfully Registered"
android:textSize="30dp" />
</AbsoluteLayout>

6) Linking Activities using Intent

Activity_main_1

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


<AbsoluteLayout
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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="181dp"
android:layout_y="292dp"
android:text="Activity_1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="172dp"
android:layout_y="335dp"
android:text="Open Activity_2" />
</AbsoluteLayout>

MainActivity.java

package com.example.indent;
import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {


private Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openactivity2();
}
});

public void openactivity2()


{
Intent intent =new Intent(this, Activity_2.class);
startActivity(intent);

7) Passing data between two activities using intent


<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
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/email"
android:layout_width="357dp"
android:layout_height="wrap_content"
android:layout_x="17dp"
android:layout_y="91dp"
android:ems="10"
android:hint="Enter Email"
android:inputType="textPersonName" />
<EditText
android:id="@+id/password"
android:layout_width="337dp"
android:layout_height="wrap_content"
android:layout_x="24dp"
android:layout_y="159dp"
android:ems="10"
android:hint="Password"
android:inputType="textPersonName" />
<Button
android:id="@+id/registers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="148dp"
android:layout_y="238dp"
android:text="Register" />
</AbsoluteLayout>

Main Activity .java

package com.example.firstactivity;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.security.PrivateKey;
public class MainActivity extends AppCompatActivity {
private EditText memail, mpassword;
private Button mregister;

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

memail=(EditText)findViewById(R.id.email);
mpassword=(EditText)findViewById(R.id.password);
mregister=(Button)findViewById(R.id.registers);
mregister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String mail=memail.getText().toString();
String pass=mpassword.getText().toString();
if(mail.isEmpty())
{
memail.setError("Email not entered");
}else
{
memail.setError(null);
}
if(pass.isEmpty())
{
mpassword.setError("please enter password");
}
else
{
memail.setError(null);
}
if(!mail.isEmpty() || !pass.isEmpty())
{
Intent intent=new Intent(getApplicationContext(),Activity_2.class);
intent.putExtra("mail",mail);
intent.putExtra("pass",pass);
startActivity(intent);
}

}
});

}
}

Second Activity.Xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
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=".Activity_2">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="4dp"
android:layout_y="239dp"
android:text="You are successfully Registered"
android:textSize="30dp" />
</AbsoluteLayout>

Activity_2.java

package com.example.firstactivity;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class Activity_2 extends AppCompatActivity {
String email;
String password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);

email=getIntent().getExtras().getString("mail");
password=getIntent().getExtras().getString("pass");
TextView textView=(TextView)findViewById(R.id.textView);
textView.setText("Email :"+" "+email+'\n'+"Password:"+" "+password);
}
}
8) Implicit Intent – Open Browser, Open Map, Dialer
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
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/b1"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_x="116dp"
android:layout_y="78dp"
android:background="#E91E63"
android:onClick="goBtn"
android:text="open browser"
tools:layout_editor_absoluteX="141dp"
tools:layout_editor_absoluteY="129dp" />
<Button
android:id="@+id/b2"
android:layout_width="132dp"
android:layout_height="54dp"
android:layout_x="119dp"
android:layout_y="179dp"
android:background="#2769B0"
android:onClick="mapBtn"
android:text="openMap"
tools:layout_editor_absoluteX="161dp"
tools:layout_editor_absoluteY="316dp" />
<Button
android:id="@+id/b3"
android:layout_width="124dp"
android:layout_height="wrap_content"
android:layout_x="117dp"
android:layout_y="291dp"
android:background="#03A9F4"
android:text="DIAL"
tools:layout_editor_absoluteX="161dp"
tools:layout_editor_absoluteY="225dp" />
</AbsoluteLayout>

Uniform Resource Identifier (URI) is a string of characters used to identify


a resource. A URI identifies a resource either by location, or a name, or
both. Such identification enables interaction with representations of the
resource over a network, typically the World Wide Web, using specific
protocols.
Main_activity.java
package com.example.browsermapdail;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button makecall,browse,map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
browse=(Button)findViewById(R.id.b1);
map=(Button)findViewById(R.id.b2);
makecall=(Button)findViewById(R.id.b3);
makecall.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent(Intent.ACTION_DIAL,
Uri.parse("tel:+9497632086"));
startActivity(i);
}
});
}
public void goBtn(View v)
{
String url="https://google.com";
Intent intent=new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
}
public void mapBtn(View v)
{

Intent intent=new Intent(Intent.ACTION_VIEW);


intent.setData(Uri.parse("geo:10.8505,76.2711"));
startActivity(intent);
}
}

9) Returning Data Using Intent Object


Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.user.getmessage.MainActivity">
<Button
android:id="@+id/getm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="127dp"
android:layout_y="247dp"
android:text="get message"
tools:layout_editor_absoluteX="133dp"
tools:layout_editor_absoluteY="230dp" />
<TextView
android:id="@+id/t1"
android:layout_width="match_parent"
android:layout_height="69dp"
android:layout_x="0dp"
android:layout_y="0dp"
android:textSize="30dp"
tools:layout_editor_absoluteX="132dp"
tools:layout_editor_absoluteY="74dp" />
</AbsoluteLayout>

main_activity.java
package com.example.user.getmessage;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity
{
Button b1;
String msg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.getm);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(MainActivity.this,activity2.class);
msg=getIntent().getExtras().getString("msg");
TextView textView=(TextView)findViewById(R.id.t1);
textView.setText(""+msg+"");
startActivity(intent);
}
});
}
}
Activity2.xml

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


<AbsoluteLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.user.getmessage.activity2">
<Button
android:id="@+id/retm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="137dp"
android:layout_y="216dp"
android:text="return"
tools:layout_editor_absoluteX="130dp"
tools:layout_editor_absoluteY="261dp" />
<EditText
android:id="@+id/e2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="60dp"
android:layout_y="93dp"
android:ems="10"
android:inputType="textPersonName"
tools:layout_editor_absoluteX="64dp"
tools:layout_editor_absoluteY="105dp" />
</AbsoluteLayout>

activity2.java

package com.example.user.getmessage;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class activity2 extends AppCompatActivity
{
Button b2;
EditText e2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity2);
b2=(Button)findViewById(R.id.retm);
e2=(EditText)findViewById(R.id.e2);
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String msg=e2.getText().toString();
Intent intent=new Intent(activity2.this,MainActivity.class);
intent.putExtra("msg",msg);
startActivity(intent);

}
});
}
}
10) DIFFERENT LAYOUTS

Activity main
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
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/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="94dp"
android:layout_y="67dp"
android:ems="10"
android:inputType="textPersonName"
android:text="DIFFERENT LAYOUTS" />
<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="131dp"
android:layout_y="175dp"
android:text="TABLE LAYOUT" />
<Button
android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="133dp"
android:layout_y="232dp"
android:text="FRAME LAYOUT" />
<Button
android:id="@+id/b3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="135dp"
android:layout_y="291dp"
android:text="Relative Layout" />
</AbsoluteLayout>

MAIN_ACTIVITY .JAVA

package com.example.diff_layout;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {


Button b1,b2,b3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.b1);
b2=(Button)findViewById(R.id.b2);
b3=(Button)findViewById(R.id.b3);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openact();
}
});

b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openact2();
}
});
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openact3();
}
});
}
public void openact()
{
Intent intent=new Intent(this,table_layout.class);
startActivity(intent);

}
public void openact2()
{
Intent intent=new Intent(this,Frame_layout.class);
startActivity(intent);
}
public void openact3()
{
Intent intent=new Intent(this,relative_layout.class);
startActivity(intent);

ACTIVITY_TABLE LAYOUT.XML

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


<TableLayout 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=".table_layout">
<TableRow android:layout_marginTop="60dp">
<TextView
android:layout_width="150dp"
android:text="Username"/>
<EditText android:layout_width="200dp"/>

</TableRow>

<TableRow android:layout_marginTop="60dp">
<TextView
android:layout_width="150dp"
android:text="Password"/>
<EditText android:layout_width="200dp"/>

</TableRow>
<TableRow>
<Button android:text="Login"/>

</TableRow>

</TableLayout>

ACIVITY_FRAME LAYOUT.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=".Frame_layout">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/ganapathi"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_margin="100dp"
android:text="Set as Wall paper" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_margin="150dp"
android:text="Home" />
</FrameLayout>
</RelativeLayout>

11) Create a Simpe ListView


Activity_Main.xml

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


<AbsoluteLayout
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/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="144dp"
android:layout_y="2dp"
android:background="@color/colorPrimary"
android:text="LIST OF STUDENTS IN CTS6"
android:textColor="#ffff"
android:textSize="15dp"
android:textStyle="bold" />
<ListView
android:id="@+id/list1"
android:layout_width="wrap_content"
android:layout_height="702dp"
android:layout_below="@id/textView"
android:layout_x="0dp"
android:layout_y="31dp" />
</AbsoluteLayout>

MainActivity.java
package com.example.simplelistview;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


ListView stud;
String[] students= {
"1. Rahul", "2. Abeen", "3. Abhilash", "4.Abijith", "5.Anagha",
"6.Hariprasad", "7.Jubin", "8.Prince", "9. Suhail", "10. Midhun"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
stud=(ListView)findViewById(R.id.list1);
ArrayAdapter<String>adapter=new
ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,students);
stud.setAdapter(adapter);

AdapterView.OnItemClickListener itemClickListener=new
AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int
position, long id) {

Toast.makeText(getBaseContext(),students[position],Toast.LENGTH_SHOR
T).show();
}
};
stud.setOnItemClickListener(itemClickListener);
}
}

12. SIMPLE RADIO BUTTON


Active_main.xml

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


<AbsoluteLayout
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="#00BFA5"
tools:context=".MainActivity">

<RadioGroup
android:id="@+id/rg1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="119dp"
android:layout_y="59dp">

<RadioButton
android:id="@+id/r1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="130dp"
android:layout_y="179dp"
android:text="C++ prog"
android:textSize="25dp"
android:onClick="check_button"
android:checked="true"/>
<RadioButton
android:id="@+id/r2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="166dp"
android:layout_y="163dp"
android:text="JAVA prog"
android:textSize="25dp"
android:onClick="check_button"/>

<RadioButton
android:id="@+id/r3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="168dp"
android:layout_y="193dp"
android:text="PHP"
android:textSize="25dp"
android:onClick="check_button"/>
</RadioGroup>

<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="50dp"
android:layout_y="12dp"
android:background="#1E88E5"
android:ems="10"
android:inputType="textPersonName"
android:text="Course Fee Calculation"
android:textSize="25dp"
android:textStyle="bold" />

<TextView
android:id="@+id/selected"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="113dp"
android:layout_y="172dp"
android:text="Your Selection"
android:textSize="25dp" />

<Button
android:id="@+id/apply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="144dp"
android:layout_y="218dp"
android:text="APPLY" />

</AbsoluteLayout>

MainActivity.java
package com.example.radio_toggle;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity {
RadioGroup rg1;
RadioButton rb1,rb2,rb3,rb;
EditText e1,e2,e3;
TextView tview;
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tview=(TextView)findViewById(R.id.selected);
b1=(Button)findViewById(R.id.apply);
rg1=(RadioGroup)findViewById(R.id.rg1);
rg1.clearCheck();
rb1=(RadioButton)findViewById(R.id.r1);
rb2=(RadioButton)findViewById(R.id.r2);
rb3=(RadioButton)findViewById(R.id.r3);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int radioId=rg1.getCheckedRadioButtonId();
rb1=findViewById(radioId);
tview.setText("Your Choice:"+rb1.getText());

}
});
}
public void check_button(View v)
{
int radioId=rg1.getCheckedRadioButtonId();
rb1=findViewById(radioId);
Toast.makeText(this, "Your Selected Course is "+rb1.getText(),
Toast.LENGTH_SHORT).show();
}

13) Course Fee Calculation using RadioButton and ToggleButton


activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
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="#A7FFEB"
tools:context=".MainActivity">
<RadioGroup
android:id="@+id/rg1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="129dp"
android:layout_y="65dp">
<RadioButton
android:id="@+id/r1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="130dp"
android:layout_y="179dp"
android:text="C++ prog"
android:textSize="25dp" />
<RadioButton
android:id="@+id/r2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="166dp"
android:layout_y="163dp"
android:text="JAVA prog"
android:textSize="25dp" />
<RadioButton
android:id="@+id/r3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="168dp"
android:layout_y="193dp"
android:text="PHP"
android:textSize="25dp" />
</RadioGroup>
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="56dp"
android:layout_y="21dp"
android:background="#1E88E5"
android:ems="10"
android:inputType="textPersonName"
android:text="Course Fee Calculation"
android:textSize="25dp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="33dp"
android:layout_y="272dp"
android:text="Course Fee"
android:textSize="25dp" />
<EditText
android:id="@+id/fees"
android:layout_width="163dp"
android:layout_height="wrap_content"
android:layout_x="193dp"
android:layout_y="269dp"
android:ems="10"
android:hint="in rupees"
android:inputType="number"
android:textSize="25dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="9dp"
android:layout_y="346dp"
android:text="Fee Discount"
android:textSize="25dp" />

<EditText
android:id="@+id/discount"
android:layout_width="175dp"
android:layout_height="wrap_content"
android:layout_x="191dp"
android:layout_y="351dp"
android:ems="10"
android:hint="in rupees"
android:inputType="number"
android:textSize="25dp" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="40dp"
android:layout_y="449dp"
android:text="Net Fee"
android:textSize="25dp" />

<EditText
android:id="@+id/netfee"
android:layout_width="171dp"
android:layout_height="wrap_content"
android:layout_x="183dp"
android:layout_y="438dp"
android:ems="10"
android:hint="in rupees"
android:inputType="number"
android:textSize="25dp" />
<ToggleButton
android:id="@+id/tbdiscount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="43dp"
android:layout_y="387dp"
android:text="tbdiscount" />
</AbsoluteLayout>
MainAcivity.java
package com.example.radioandtoggle;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity {
ToggleButton tb1;
RadioGroup rg1;
RadioButton rb1,rb2,rb3,rb;
EditText e1,e2,e3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tb1=(ToggleButton)findViewById(R.id.tbdiscount);
rg1=(RadioGroup)findViewById(R.id.rg1);
rg1.clearCheck();
rb1=(RadioButton)findViewById(R.id.r1);
rb2=(RadioButton)findViewById(R.id.r2);
rb3=(RadioButton)findViewById(R.id.r3);
e1=(EditText)findViewById(R.id.fees);
e2=(EditText)findViewById(R.id.discount);
e3=(EditText)findViewById(R.id.netfee);

tb1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(tb1.isChecked())
e2.setEnabled(true);
else
{
e2.setEnabled(false);
e2.setText("0");
}
}
});
rg1.setOnCheckedChangeListener(new
RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {

rb=(RadioButton)group.findViewById(checkedId);
String course=rb.getText().toString();
if(course.equals("C++prog"))
e1.setText("5000");
else if (course.equals("JAVA prog"))
e1.setText("6000");
else
e1.setText("4000");
}
});
e2.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int
after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
if (!s.equals("")) calcNetFee(); }
@Override
public void afterTextChanged(Editable s) { } });

e1.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int
after) { }
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
if (!s.equals("")) calcNetFee();
}
@Override
public void afterTextChanged(Editable s) { } }); }
public void calcNetFee()
{
float dp,fee,netFee;
String strDP,strFEE;
strDP=e2.getText().toString();
strFEE=e1.getText().toString();
if (!strDP.equals("")) dp=Float.valueOf(strDP); else dp=0;
if (!strFEE.equals("")) fee=Float.parseFloat(strFEE); else fee=0;
netFee=fee-(fee*dp)/100;
e3.setText(String.valueOf(netFee));

}
}

13.Integrate a website in android application using webview

main activity.java

package com.example.webview;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {


private WebView webView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView=findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("http:/www.amazon.com");
}
}

activity xml

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


<AbsoluteLayout
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/t1"
android:layout_width="match_parent"
android:layout_height="126dp"
android:layout_x="0dp"
android:background="#303852"
android:gravity="center_vertical"
android:paddingLeft="15dp"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:text="Webview example"
android:textColor="#99DDD7"
android:textSize="20dp" />

<WebView
android:id="@+id/webview"
android:layout_width="411dp"
android:layout_height="596dp"
android:layout_alignParentBottom="true"
android:layout_x="5dp"
android:layout_y="126dp" />
</AbsoluteLayout>

android manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.webview">

<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="webview"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

14. LIST VIEW WITH IMAGE AND TEXT

ACTIVITY_MAIN.XML

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


<AbsoluteLayout
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/itemid"
android:layout_width="196dp"
android:layout_height="40dp"
android:layout_x="193dp"
android:layout_y="58dp"
android:hint="name" />

<ImageView
android:id="@+id/itemimage"
android:layout_width="171dp"
android:layout_height="66dp"
android:layout_x="0dp"
android:layout_y="46dp" />

</AbsoluteLayout>
CUSTOMLAYOUT.XML

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


<AbsoluteLayout
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:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_y="0dp"
android:background="#0091EA"
android:gravity="center_horizontal|center_vertical"
android:paddingLeft="15dp"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:text="Country List"
android:textColor="#ffffff"
android:textSize="25dp"
android:textStyle="bold" />

<ListView
android:id="@+id/lvPr"
android:layout_width="match_parent"
android:layout_height="680dp"
android:layout_x="-4dp"
android:layout_y="39dp"></ListView>
</AbsoluteLayout>

STRING.XML

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


<resources>
<string name="app_name">ListViewUsage</string>
<string-array name="images">
<item>India</item>
<item>China</item>
<item>Germany</item>
<item>Argentina</item>
</string-array>
</resources>

MAINACTIVITY.JAVA

package com.example.listview_image_text;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;

public class MainActivity extends AppCompatActivity {

ListView lv;
String images[];
int imageID [] =
{R.drawable.india,R.drawable.china,R.drawable.germany,R.drawable.argen
tina };
ArrayList <HashMap<String,String>> al;
SimpleAdapter ap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.customlayout);
lv=findViewById(R.id.lvPr);
images=getResources().getStringArray(R.array.images);
al=new ArrayList<HashMap<String, String>>();

for (int i=0; i<images.length; i++) {


HashMap<String, String> hm = new HashMap<String, String>();
hm.put("name", images[i]);
hm.put("photo", String.valueOf(imageID[i]));
al.add(hm);
}
String cnames[]={"name","photo"};
int cimages[]={R.id.itemid, R.id.itemimage};
ap=new SimpleAdapter(this,al,R.layout.activity_main,cnames,cimages);
lv.setAdapter(ap);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {

Toast.makeText(getApplicationContext(),images[position],Toast.LENGTH_L
ONG).show();
} }); } }

15. MULTIPLE CHOICE QUESTIONS USING RADIO BUTTON

ACTIVITY_MAIN.XML

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


<AbsoluteLayout
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:id="@+id/m"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#608EA2"
tools:context=".MainActivity">
<TextView
android:id="@+id/t1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_x="-2dp"
android:layout_y="19dp"
android:text="MULTIPLE CHOICE QUESTIONS"
android:textAlignment="center"
android:textSize="24sp"
android:textStyle="bold" />
<TextView
android:id="@+id/t2"
android:layout_width="370dp"
android:layout_height="38dp"
android:layout_x="18dp"
android:layout_y="61dp"
android:text="1) 1TB = ?"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/t3"
android:layout_width="198dp"
android:layout_height="35dp"
android:layout_x="70dp"
android:layout_y="522dp"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="@+id/b1"
android:layout_width="122dp"
android:layout_height="wrap_content"
android:layout_x="280dp"
android:layout_y="516dp"
android:text="submit" />
<RadioGroup
android:id="@+id/rg1"
android:layout_width="332dp"
android:layout_height="wrap_content"
android:layout_x="63dp"
android:layout_y="101dp">
<RadioButton
android:id="@+id/rb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="check_button"
android:text="1024 KB"
android:textSize="18sp" />
<RadioButton
android:id="@+id/rb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="check_button"
android:text="1024 MB"
android:textSize="18sp" />
<RadioButton
android:id="@+id/rb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="check_button"
android:text="1024 GB"
android:textSize="18sp" />
<RadioButton
android:id="@+id/rb4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="check_button"
android:text="1024 TB"
android:textSize="18sp" />
</RadioGroup>
<TextView
android:id="@+id/t4"
android:layout_width="367dp"
android:layout_height="45dp"
android:layout_x="13dp"
android:layout_y="254dp"
android:text="2) URI stands for ?"
android:textSize="18sp"
android:textStyle="bold" />
<RadioGroup
android:id="@+id/rg2"
android:layout_width="331dp"
android:layout_height="188dp"
android:layout_x="51dp"
android:layout_y="303dp">
<RadioButton
android:id="@+id/rb6"
android:layout_width="match_parent"
android:layout_height="46dp"
android:onClick="check_button1"
android:text="Universal Resource Identifier"
android:textSize="18sp" />
<RadioButton
android:id="@+id/rb7"
android:layout_width="match_parent"
android:layout_height="46dp"
android:onClick="check_button1"
android:text="Unique Resource Identifier"
android:textSize="18sp" />
<RadioButton
android:id="@+id/rb8"
android:layout_width="match_parent"
android:layout_height="42dp"
android:onClick="check_button1"
android:text="Uniform Remainder Identifier"
android:textSize="18sp" />
<RadioButton
android:id="@+id/rb5"
android:layout_width="match_parent"
android:layout_height="42dp"
android:onClick="check_button"
android:text="Uniform Resource Identifier"
android:textSize="18sp" />
</RadioGroup>
</AbsoluteLayout>

MAINACTIVITY.JAVA

package com.example.multiplechoice;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {

RadioGroup rg1,rg2;
RadioButton rb,rb1,rb2,rb3,rb4,rb5,rb6,rb7,rb8;
Button b1;
TextView t1,t2,t3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rg1=(RadioGroup)findViewById(R.id.rg1);
rg1.clearCheck();
rg2=(RadioGroup)findViewById(R.id.rg2);
rg2.clearCheck();
rb1=(RadioButton)findViewById(R.id.rb1);
rb2=(RadioButton)findViewById(R.id.rb2);
rb3=(RadioButton)findViewById(R.id.rb3);
rb4=(RadioButton)findViewById(R.id.rb4);
rb5=(RadioButton)findViewById(R.id.rb5);
rb6=(RadioButton)findViewById(R.id.rb6);
rb7=(RadioButton)findViewById(R.id.rb7);
rb8=(RadioButton)findViewById(R.id.rb8);
b1=(Button)findViewById(R.id.b1);
t1=(TextView)findViewById(R.id.t1);
t2=(TextView)findViewById(R.id.t2);
t3=(TextView)findViewById(R.id.t3);

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int radioId=rg1.getCheckedRadioButtonId();
rb=findViewById(radioId);
if(rb3.isChecked() && rb5.isChecked())
{
int count=2;
t3.setText("TOTAL MARKS : "+count);
}
else if(rb5.isChecked()){

int count=1;
t3.setText("TOTAL MARKS : "+count);

}
else if(rb3.isChecked() ){
int count=1;
t3.setText("TOTAL MARKS : "+count);

}
else{
int count=0;
t3.setText("TOTAL MARKS : "+count);
}

} }); }
public void check_button(View v)
{
int radioId=rg1.getCheckedRadioButtonId();
rb=findViewById(radioId);
Toast.makeText(this, "Your choice is" +rb.getText(),
Toast.LENGTH_SHORT).show();
}

public void check_button1(View v)


{
int radioId=rg2.getCheckedRadioButtonId();
rb=findViewById(radioId);
Toast.makeText(this, "Your choice is" +rb.getText(),
Toast.LENGTH_SHORT).show();
} }

16 SIMPLE INTEREST CALCULATION IN FIRST ACTIVITY RESULT IN SECOND


ACTIVTY : PRINCIPAL AMOUNT,NO.OF YRS, RATEOFINTEREST

ACTIVITY_MAIN.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">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="64dp"
android:text="SIMPLE INTEREST CALCULATOR"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.514"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/e1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="56dp"
android:ems="10"
android:hint="Principle amount"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<EditText
android:id="@+id/e2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="44dp"
android:ems="10"
android:hint="No. Of Years"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/e1" />
<EditText
android:id="@+id/e3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:ems="10"
android:hint="Rate of Interest"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/e2" />
<Button
android:id="@+id/butten"
android:layout_width="209dp"
android:layout_height="56dp"
android:layout_marginTop="56dp"
android:text="SUBMIT"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/e3"
app:layout_constraintVertical_bias="0.023" />
</androidx.constraintlayout.widget.ConstraintLayout>

MAINACTIVITY.JAVA
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {


Button b1;
EditText e11,e22,e33;
Intent i;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=findViewById(R.id.butten);
e11=findViewById(R.id.e1);
e33=findViewById(R.id.e3);
e22=findViewById(R.id.e2);

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int amt=Integer.parseInt(e11.getText().toString());
int no=Integer.parseInt(e22.getText().toString());

int rate=Integer.parseInt(e33.getText().toString());
float resul=amt*no*rate;
float result=resul/100;
i = new Intent(MainActivity.this,result.class);
Bundle b = new Bundle();
b.putFloat("res",result);
i.putExtras(b);
startActivity(i);

}
//
}); } }

ACTIVITY_RESULT.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=".result">

<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="260dp"
android:ems="10"
android:hint="result is"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@android:string/VideoView_error_button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.538"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText"
app:layout_constraintVertical_bias="0.195" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="208dp"
android:text="Simple Interest is"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.304"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

RESULT.JAVA

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;
public class result extends AppCompatActivity {
EditText e1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_result);
Button b1=findViewById(R.id.button);
e1=findViewById(R.id.editText);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle a= getIntent().getExtras();
Float text= a.getFloat("res");
e1.setText(""+text);
}
});
}
}
17. Create an activity, First activity contain a button named CLICK , when
we click ,it lead to second activity. In second activity, Write code to calculate
area and perimeter of square ,pass the result to first activity

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"
tools:context="com.mithun.square.MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="AREA OF SQUARE"
android:textSize="35dp" />
<TextView
android:id="@+id/text"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text=""
android:textSize="30dp" />
<TextView
android:id="@+id/text1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text=""
android:textSize="30dp" />
<Button
android:layout_marginTop="50dp"
android:layout_width="100dp"
android:layout_height="50dp"
android:onClick="area"
android:text="CLick"/>
</LinearLayout>
</LinearLayout>

ACTIVITY_AREA.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"
tools:context="com.mithun.square.area">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="45dp"
android:text="Enter length :"
android:textSize="25dp"/>
<EditText
android:id="@+id/num"
android:layout_width="100dp"
android:layout_height="45dp"
android:text=""
android:textSize="24dp"
android:inputType="number"/>
</LinearLayout>
<Button
android:layout_marginTop="10dp"
android:layout_width="100dp"
android:layout_height="50dp"
android:onClick="find"
android:text="Find"/>
</LinearLayout>

MAINACTIVITY.JAVA

package com.mithun.square;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView text,text1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text=findViewById(R.id.text);
text1=findViewById(R.id.text1);
}
public void area(View view){
Intent intent=new Intent(this,area.class);
startActivityForResult(intent,1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==1 && resultCode==RESULT_OK){
String s=data.getStringExtra("area");
String p=data.getStringExtra("per");
text.setText("Area is "+s);
text1.setText("Perimeter is "+p);
} } }

AREA.JAVA
package com.mithun.square;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class area extends AppCompatActivity {
EditText num;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_area);
num=findViewById(R.id.num);
}
public void find(View view){
int a=Integer.parseInt(num.getText().toString());
int area=a*a;
int per=4*a;
Intent intent=new Intent();
intent.putExtra("area",String.valueOf(area));
intent.putExtra("per",String.valueOf(per));
//startActivity(intent);
setResult(Activity.RESULT_OK, intent);
finish();

} }

18. Saving Data Using the SharedPreferences Objects

ACTIVITY_MAIN.XML
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
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/t1"
android:layout_width="201dp"
android:layout_height="43dp"
android:layout_x="41dp"
android:layout_y="27dp"
android:text="STUDENT DETAILS!"
android:textSize="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/rollno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="42dp"
android:layout_y="104dp"
android:ems="10"
android:hint="Enter your roll number"
android:inputType="textPersonName" />
<EditText
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="44dp"
android:layout_y="193dp"
android:ems="10"
android:hint="Enter your name"
android:inputType="textPersonName" />
<EditText
android:id="@+id/branch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="42dp"
android:layout_y="303dp"
android:ems="10"
android:hint="Enter your branch"
android:inputType="textPersonName" />
<Button
android:id="@+id/save"
android:layout_width="230dp"
android:layout_height="wrap_content"
android:layout_x="42dp"
android:layout_y="378dp"
android:text="Save and Continue" />
<Button
android:id="@+id/skip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="109dp"
android:layout_y="450dp"
android:text="Skip" />
</AbsoluteLayout>

MAIN_ACTIVITY.JAVA

package com.example.sharedstudent;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
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 {
EditText rno,name,branch;
Button btnsave,btnskip;
SharedPreferences prefs;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rno=findViewById(R.id.rollno);
name=findViewById(R.id.name);
branch=findViewById(R.id.branch);
btnsave=findViewById(R.id.save);
btnskip=findViewById(R.id.skip);

btnsave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String rlno=rno.getText().toString();
String nme=name.getText().toString();
String br=branch.getText().toString();

prefs=getSharedPreferences("MyPref",MODE_PRIVATE);
SharedPreferences.Editor editor=prefs.edit();
editor.putString("rollno",rlno);
editor.putString("name",nme);
editor.putString("branch",br);
editor.commit();
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
}
});
btnskip.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,Main3Activity.class);
startActivity(intent);

Context context = getApplicationContext();


CharSequence text = "Nothing to do";
int duration = Toast.LENGTH_SHORT;

Toast toast = Toast.makeText(context, text, duration);


toast.show();
} }); } }

ACIVITY_MAIN2.XML

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


<AbsoluteLayout
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=".Main2Activity">

<TextView
android:id="@+id/t2"
android:layout_width="351dp"
android:layout_height="48dp"
android:layout_x="22dp"
android:layout_y="40dp"
android:text="STUDENT DETAILS"
android:textSize="30sp" />

<TextView
android:id="@+id/tv"
android:layout_width="275dp"
android:layout_height="293dp"
android:layout_x="55dp"
android:layout_y="99dp" />
</AbsoluteLayout>

MAIN2ACTIVITY.JAVA

package com.example.sharedstudent;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.TextView;

public class Main2Activity extends AppCompatActivity {


TextView tvobs;
SharedPreferences prefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);

tvobs=findViewById(R.id.tv);
prefs=getSharedPreferences("MyPref",MODE_PRIVATE);
String rolno=prefs.getString("rollno","no values");
String name=prefs.getString("name","no values");
String bran=prefs.getString("branch","no values");

String Observations="RollNo:"+rolno +"\n Name: " +name +"\n


Branch :"+bran;

tvobs.setText(Observations);
}
}

ACTIVITY_MAIN3.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=".Main3Activity">

<ImageView
android:id="@+id/imageView"
android:layout_width="331dp"
android:layout_height="197dp"
app:srcCompat="@android:drawable/ic_delete"
tools:layout_editor_absoluteX="40dp"
tools:layout_editor_absoluteY="178dp" />
</androidx.constraintlayout.widget.ConstraintLayout>

19) INTERNAL STORAGE FILE

<?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/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:padding="5dp"
android:text="Android Read and Write Text from/to a File"
android:textStyle="bold"
android:textSize="28sp" />

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="22dp"
android:minLines="5"
android:layout_margin="5dp">

<requestFocus />
</EditText>

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Write Text into File"
android:onClick="WriteBtn"
android:layout_alignTop="@+id/button2"
android:layout_alignRight="@+id/editText1"
android:layout_alignEnd="@+id/editText1" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Read Text From file"
android:onClick="ReadBtn"
android:layout_centerVertical="true"
android:layout_alignLeft="@+id/editText1"
android:layout_alignStart="@+id/editText1" />

</RelativeLayout>

MAIN_ACTIVITY.JAVA

package com.example.internalstoragefiles;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class MainActivity extends Activity {

EditText textmsg;
static final int READ_BLOCK_SIZE = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

textmsg=(EditText)findViewById(R.id.editText1);
}

// write text to file


public void WriteBtn(View v) {
// add-write text into file
try {
FileOutputStream fileout=openFileOutput("mytextfile1.txt",
MODE_PRIVATE);
OutputStreamWriter outputWriter=new OutputStreamWriter(fileout);
outputWriter.write(textmsg.getText().toString());
outputWriter.close();

//display file saved message


Toast.makeText(getBaseContext(), "File saved successfully!",
Toast.LENGTH_SHORT).show();

} catch (Exception e) {
e.printStackTrace();
}
}

// Read text from file


public void ReadBtn(View v) {
//reading text from file
try {
FileInputStream fileIn=openFileInput("mytextfile1.txt");
InputStreamReader InputRead= new InputStreamReader(fileIn);

char[] inputBuffer= new char[READ_BLOCK_SIZE];


String s="";
int charRead;

while ((charRead=InputRead.read(inputBuffer))>0) {
// char to string conversion
String readstring=String.copyValueOf(inputBuffer,0,charRead);
s +=readstring;
}
InputRead.close();
textmsg.setText(s);

} catch (Exception e) {
e.printStackTrace();
}
}
}

20) Hello world HTML5

ACTIVITY_MAIN.XML
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
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">

<WebView
android:id="@+id/web"
android:layout_width="418dp"
android:layout_height="582dp"
android:layout_x="0dp"
android:layout_y="55dp" />
</AbsoluteLayout>

MAIN_ACTIVITY.JAVA

package com.example.html5;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;

public class MainActivity extends AppCompatActivity {

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

WebView webView=findViewById(R.id.web);
webView.loadUrl("file:///android_asset/helloworld.html");
}
}

HELLOWORLD.HTML

<!DOCTYPE html>
<hmtl>
<head>
</head>
<body>
<b>HELLO WORLD</b>
<br>
<br>
WELCOME TO html5 application development<br>
<button>Click here</button>

</body>
</hmtl>

21) STUDENTS DATABASE USING SQLite

ACTIVITY_MAIN.xlm

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


<AbsoluteLayout
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/editroll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="88dp"
android:layout_y="96dp"
android:ems="10"
android:hint="Enter Roll No. here"
android:inputType="number" />

<EditText
android:id="@+id/editname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="92dp"
android:layout_y="156dp"
android:ems="10"
android:hint="Enter Name Here"
android:inputType="textPersonName" />

<EditText
android:id="@+id/editbranch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="91dp"
android:layout_y="226dp"
android:ems="10"
android:hint="Enter Branch here"
android:inputType="textPersonName" />

<EditText
android:id="@+id/editscore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="100dp"
android:layout_y="286dp"
android:ems="10"
android:hint="Enter Score here"
android:inputType="number" />

<Button
android:id="@+id/btnclear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="202dp"
android:layout_y="396dp"
android:text="CLEAR" />

<Button
android:id="@+id/btnget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="12dp"
android:layout_y="472dp"
android:text="GET STUDENTS" />

<Button
android:id="@+id/btngetall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="144dp"
android:layout_y="471dp"
android:text="GETALL" />

<Button
android:id="@+id/btnupdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="241dp"
android:layout_y="471dp"
android:text="UPDATE SCORE" />

<Button
android:id="@+id/btndelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="120dp"
android:layout_y="535dp"
android:text="DELETE STUDENTS" />

<EditText
android:id="@+id/editText9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="97dp"
android:layout_y="24dp"
android:ems="10"
android:inputType="textPersonName"
android:text="STUDENTS DATABASE" />

<Button
android:id="@+id/btnsaves"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="100dp"
android:layout_y="396dp"
android:text="save" />
</AbsoluteLayout>

MAINACTIVTY.JAVA

package com.example.studdatabasenew;
import androidx.appcompat.app.AppCompatActivity;
import android.database.sqlite.SQLiteDatabase;
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 {


EditText e1,e2,e3,e4;
Button b1,b2,b3,b4,b5,b6;
DBHelper1 dbh;
SQLiteDatabase db;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbh=new DBHelper1(getApplicationContext());
b1=(Button)findViewById(R.id.btnsaves);
b2=(Button)findViewById(R.id.btnclear);
b3=(Button)findViewById(R.id.btnget);
b4=(Button)findViewById(R.id.btngetall);
b5=(Button)findViewById(R.id.btnupdate);
b6=(Button)findViewById(R.id.btndelete);
e1=(EditText)findViewById(R.id.editroll);
e2=(EditText)findViewById(R.id.editname);
e3=(EditText)findViewById(R.id.editbranch);
e4=(EditText)findViewById(R.id.editscore);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String roll=e1.getText().toString();
String studname=e2.getText().toString();
String sbranch=e3.getText().toString();
int sscore= Integer.parseInt(e4.getText().toString());
dbh.addStudent(roll,studname,sbranch,sscore);
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clearData();
}
});
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

String result= dbh.getStudent(e1.getText().toString());


String na=dbh.getStudent(e2.getText().toString());

Toast.makeText(getApplicationContext(),result,Toast.LENGTH_LONG).show();

Toast.makeText(getApplicationContext(),na,Toast.LENGTH_LONG).show();
}
});

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

StringBuffer result= dbh.getAll(e3.getText().toString());

Toast.makeText(getApplicationContext(),result,Toast.LENGTH_LONG).show();
}
});
b5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int sc=Integer.parseInt(e4.getText().toString());
String r=e1.getText().toString();
dbh.updateScore(r,sc);
dbh.getStudent(r);
}
});

b6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String r=e1.getText().toString();
dbh.deleteStud(r);
}
});
}
void clearData()
{
e1.setText("");
e2.setText("");
e3.setText("");
e4.setText("");
}
}

DBHeper1.JAVA

package com.example.studdatabasenew;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;

public class DBHelper1 extends SQLiteOpenHelper {


public static final String DBName="College";
public static final int DBVer=1;
Context appContext;
String createTableQuery="create table student ( id integer primary key
autoincrement,rollno text,name text, branch text, score int)";
public DBHelper1(Context context) {
super(context, DBName, null, DBVer);
appContext=context;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(createTableQuery);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void addStudent(String rno, String sname, String br, int sc)
{
SQLiteDatabase db=getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put("rollno",rno);
cv.put("name",sname);
cv.put("branch",br);
cv.put("score",sc);
long id=db.insert("student",null,cv);
Toast.makeText(appContext,"Total Students
Inserted :"+id,Toast.LENGTH_LONG).show();
}
public String getStudent(String roll){
String result;
SQLiteDatabase db=getReadableDatabase();
Cursor c=db.query("student",new
String[]{"name","branch"},"rollno='"+roll+"'",null,null,null,null);
if (c.getCount()>0)
{c.moveToFirst();
result=c.getString(c.getColumnIndex("name"));}
else
result=roll+" not found";
return result;
}
public StringBuffer getAll(String branch)
{
StringBuffer result=new StringBuffer("");
String str;
SQLiteDatabase db=getReadableDatabase();
Cursor c=db.query("student",new

String[]{"rollno","name","branch","score"},"branch='"+branch+"'",null,null,null,nu
ll);
if (c.getCount()>0)
{
c.moveToFirst();
do{

str=c.getString(c.getColumnIndex("rollno"))+c.getString(c.getColumnIndex("na
me"))+c.getString(c.getColumnIndex("score"));
result.append(str+"\n");
}while(c.moveToNext());
}
else
{
result.append("No Students in " + branch);
}
return result;
}
public void updateScore(String roll,int sc)
{
SQLiteDatabase db=getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put("score",sc);
long id=db.update("student",cv,"rollno='"+roll+"'",null);
Toast.makeText(appContext,"Updated :"+id,Toast.LENGTH_LONG).show();
}
public void deleteStud(String roll) {
SQLiteDatabase db = getWritableDatabase();
long id = db.delete("student", "rollno='" + roll + "'", null);
Toast.makeText(appContext, "Deleted :" + id, Toast.LENGTH_LONG).show();

}}

22)DIGITAL BIODATA
USING HTML5

ACTIVITY-MAIN.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">

<WebView
android:id="@+id/wb"
android:layout_width="409dp"
android:layout_height="729dp"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp" />
</androidx.constraintlayout.widget.ConstraintLayout>

MAIN _ACTIVITY.JAVA

package com.example.digitalbio;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;

public class MainActivity extends AppCompatActivity {

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

WebView webView=findViewById(R.id.wb);
WebSettings webSettings=webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
webView.loadUrl("file:///android_asset/index.html");

}
}

INDEX.HTML

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="script.js"></script>
</head>
<body bgcolor=" #87CEFA" style="text align:center,margin top:50px">
<div id="main_screen" style="margin top:40%">
<b>DIGITAL BIO DATA </b>
</br>
</br>

<button style="width:100px;70px" onclick ="openRegister()">ADD


</br>DETAILS</button>
<button style ="width:100px;70px" onclick ="showHomepage()">VIEW
</br>DETAILS</button>
</div>
<div id ="register_screen" style="display:none">
<b>ADD DETAILS</b>
<br>
<br>First name:<br><input id="fname" type="text" name="fname"><br>
<br>Last name:<br><input id="lname" type="text" name="lname"><br>
<br>Date of Birth:<br><input id="dob" type="date" name="dname"><br>
<br>Email:<br><input id="email" type="email" name="ename"><br>
<br>Blood Group:<br><input id="blood" type="text" name="bname"><br>
<br><input type ="submit" value="cancel" onclick="closeRegistration()">
<input type="submit" value="submit" onclick="register()">
<br>
<br>
</div>
<div id="home_screen"style="display:none">
hi
</div>
</body>
</html>

SCRIPT.JS

function openRegister()
{
var div=document.getElementById('register_screen');
div.style.display='block';
var div=document.getElementById('main_screen');
div.style.display='none';
}
function closeRegistration()
{
var div1=document.getElementById('register_screen');
div1.style.display='none';
var div2=document.getElementById('main_screen');
div2.style.display='block';
}

function register()
{
var fname=document.getElementById("fname").value;
var lname=document.getElementById("lname").value;
var dob=document.getElementById("dob").value;
var blood=document.getElementById("blood").value;
var email=document.getElementById("email").value;

localStorage.setItem("fname",fname);
localStorage.setItem("lname",lname);
localStorage.setItem("email",email);
localStorage.setItem("dob",dob);
localStorage.setItem("blood",blood);

closeRegistration();
alert("Details added successfully");
}
function showHomepage()
{
var div=document.getElementById('main_screen');
div.style.display='none';
var div=document.getElementById('home_screen');
div.style.display='block';

div.innerHTML="<br></br><b>DIGITAL BIO DATA</b>"+"</br></br><b>FIRST


NAME:</b>"+localStorage.getItem("fname")+
"</br><b>LAST NAME:</b>"+localStorage.getItem("lname")+
"</br><b>date of birth</b>"+localStorage.getItem("dob")+
"</br><b>EMAIL</b>"+localStorage.getItem("email")+
"<br><b>BLOOD GROUP:</b>"+localStorage.getItem("blood");
}

SCREENSHOTS

You might also like