Uploaded by Hiren Jha

AndroidPractical

advertisement
3361602 – Android App Development
Government Polytechnic, Gandhinagar
PRACTICAL-1
AIM : Create “Hello World” application to “Hello World” in the middle of
the screen in the red color with white background.
XML for Practical-1
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/andro
id"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin
"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.tonystark.pract1aditya.MainA
ctivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="64dp"
android:textColor="#ff0000"
android:textSize="24sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLar
ge"
android:text="Practical 1 by Aditya Gurav"
1|Page
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
android:id="@+id/tvPract1"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:textColor="@android:color/holo_green_dark"
android:textSize="24sp"
android:textStyle="bold" />
</RelativeLayout>
2|Page
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
Java Code for Practical-1
package com.example.tonystark.pract1aditya;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
3|Page
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
Outputs for Practicals-1
Start Date
4|Page
End/Sign Date
Faculty Sign
Remarks/Grade
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
PRACTICAL-2
AIM : Create sample application with login module.(Check username and
password), validate it for login screen or alert the user with a Toast.
XML for Practical-2
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/andro
id"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin
"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.tonystark.practicals3toe.Pra
ctical3">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Aditya Gurav Practicals"
android:id="@+id/tvTitle"
android:textSize="24sp"
android:textColor="@color/accent_material_light" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/etUsername"
android:layout_below="@+id/tvTitle"
android:layout_alignEnd="@+id/tvTitle"
android:layout_marginTop="73dp" />
5|Page
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/etPassword"
android:layout_below="@+id/etUsername"
android:layout_alignStart="@+id/etUsername" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:id="@+id/button"
android:layout_centerVertical="true"
android:layout_alignStart="@+id/etPassword" />
</RelativeLayout>
6|Page
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
Java Code for Practical-2
package com.example.tonystark.practicals3toe;
import
import
import
import
import
import
import
import
import
android.support.v7.app.AppCompatActivity;
android.os.Bundle;
android.text.Editable;
android.text.TextWatcher;
android.util.Log;
android.view.View;
android.widget.Button;
android.widget.EditText;
android.widget.Toast;
public class Practical3 extends AppCompatActivity
implements TextWatcher {
Button btn;
EditText et1,et2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_practical3);
et1=(EditText)findViewById(R.id.etUsername);
et2=(EditText)findViewById(R.id.etPassword);
btn=(Button)findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(et1.getText().toString().equals("adi") &&
et2.getText().toString().equals("gurav"))
{
Toast.makeText(Practical3.this, "Welcome to the future!
:)", Toast.LENGTH_SHORT).show();
}
else
7|Page
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
{
Toast.makeText(Practical3.this, "Incorrect
username/password! :(", Toast.LENGTH_SHORT).show();
}
}
}
);
}
}
Outputs for Practicals-2
8|Page
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Start Date
9|Page
End/Sign Date
Government Polytechnic, Gandhinagar
Faculty Sign
Remarks/Grade
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
PRACTICAL-3
AIM : Create and validate a login application using username as Email ID
else login button must remain disabled.
XML for Practical-3
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/andro
id"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin
"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.tonystark.practicals3toe.Pra
ctical3">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Aditya Gurav Practicals"
android:id="@+id/tvTitle"
android:textSize="24sp"
android:textColor="@color/accent_material_light" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/etUsername"
android:layout_below="@+id/tvTitle"
android:layout_alignEnd="@+id/tvTitle"
android:layout_marginTop="73dp" />
10 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/etPassword"
android:layout_below="@+id/etUsername"
android:layout_alignStart="@+id/etUsername" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:id="@+id/button"
android:layout_centerVertical="true"
android:layout_alignStart="@+id/etPassword" />
</RelativeLayout>
11 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
Java Code for Practical-3
package com.example.tonystark.practicals3toe;
import
import
import
import
import
import
import
android.support.v7.app.AppCompatActivity;
android.os.Bundle;
android.text.Editable;
android.text.TextWatcher;
android.util.Log;
android.widget.Button;
android.widget.EditText;
public class Practical3 extends AppCompatActivity
implements TextWatcher {
Button btn;
EditText et1,et2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_practical3);
et1=(EditText)findViewById(R.id.etUsername);
et2=(EditText)findViewById(R.id.etPassword);
btn=(Button)findViewById(R.id.button);
et1.addTextChangedListener(this);
}
@Override
public void beforeTextChanged(CharSequence s, int
start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
12 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
}
@Override
public void afterTextChanged(Editable s) {
btn.setEnabled(false);
if (s.toString().contains("@"))//there is at least one
@
{
if (s.toString().contains("."))//there is at least one
.
{
int atIndex, dotIndex;
atIndex = s.toString().indexOf('@');
dotIndex =
s.toString().lastIndexOf('.');
if (atIndex < dotIndex)//@ is prior to .
{
if ((dotIndex - atIndex) >1)//there's something between
@ and .
{
btn.setEnabled(true);
}
}
}
}
}
}
13 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
Outputs for Practicals-3
Start Date
14 | P a g e
End/Sign Date
Faculty Sign
Remarks/Grade
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
PRACTICAL-4
AIM : Create and Login application and open a browser with any one
search engine.
XML for Practical-4
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/andro
id"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin
"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.tonystark.pract4aditya.MainA
ctivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/textView2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMed
ium"
android:text="Practical 4 by Aditya Gurav"
android:id="@+id/textView"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:textColor="#0026ff"
android:textSize="24sp" />
15 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/etUsername"
android:layout_below="@+id/textView2"
android:layout_alignStart="@+id/textView"
android:layout_marginTop="40dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/etPassword"
android:layout_below="@+id/etUsername"
android:layout_alignStart="@+id/etUsername" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:id="@+id/btnLogin"
android:layout_below="@+id/etPassword"
android:layout_alignStart="@+id/etPassword"
android:layout_alignEnd="@+id/etPassword" />
</RelativeLayout>
16 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
Java Code for Practical-4
package com.example.tonystark.pract4aditya;
import
import
import
import
import
import
import
import
android.content.Intent;
android.net.Uri;
android.support.v7.app.AppCompatActivity;
android.os.Bundle;
android.view.View;
android.widget.Button;
android.widget.EditText;
android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText et1,et2;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1=(EditText)findViewById(R.id.etUsername);
et2=(EditText)findViewById(R.id.etPassword);
btn=(Button) findViewById(R.id.btnLogin);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if((et1.getText().toString().equals("adi")) &&
(et2.getText().toString().equals("gurav")))
{
Intent i=new
Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.google.com?q=aditya+gurav"));
startActivity(i);
}
else
17 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
{
Toast.makeText(MainActivity.this, "Username/Password is
incorrect!", Toast.LENGTH_SHORT).show();
}
}
}
);
}
}
18 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
Outputs for Practicals-4
Start Date
19 | P a g e
End/Sign Date
Faculty Sign
Remarks/Grade
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
PRACTICAL-5
AIM : Create an application to display “Hello World” string the number of
times user inputs a numeric value. (Example. If user enters 5, the next
screen should print “Hello World” five times.)
XML for Practical-5
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/andro
id"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin
"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.tonystark.pract5aditya.MainA
ctivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/textView" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/etCount"
android:layout_below="@+id/textView"
android:layout_toEndOf="@+id/textView" />
20 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Display"
android:id="@+id/btnDisplay"
android:layout_below="@+id/etCount"
android:layout_alignStart="@+id/etCount"
android:layout_alignEnd="@+id/etCount" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMed
ium"
android:id="@+id/tvHelloWorld"
android:layout_below="@+id/btnDisplay"
android:layout_alignStart="@+id/btnDisplay"
android:layout_alignEnd="@+id/btnDisplay" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLar
ge"
android:text="Practical 5 by Aditya Gurav"
android:id="@+id/tvPractical5"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:textColor="#ee01aaff"
android:textStyle="bold" />
</RelativeLayout>
21 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
Java Code for Practical-5
package com.example.tonystark.pract5aditya;
import
import
import
import
import
import
android.support.v7.app.AppCompatActivity;
android.os.Bundle;
android.view.View;
android.widget.Button;
android.widget.EditText;
android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText etCnt;
TextView tvHelloWorld;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etCnt=(EditText)findViewById(R.id.etCount);
tvHelloWorld=(TextView)findViewById(R.id.tvHelloWorld);
btn=(Button)findViewById(R.id.btnDisplay);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tvHelloWorld.setText("");
for(Integer
i=1;i<=Integer.parseInt(etCnt.getText().toString());i++
)
{
tvHelloWorld.setText(tvHelloWorld.getText() + "Hello
World!" + '\n' );
}
}
}
);
22 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
}
}
23 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
Outputs for Practicals-5
Start Date
24 | P a g e
End/Sign Date
Faculty Sign
Remarks/Grade
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
PRACTICAL-6
AIM : Create spinner with strings from the resource folder (res >> value
folder). On changing spinner value, change image.
XML for Practical-6
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/andro
id"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin
"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.tonystark.pract6aditya.MainA
ctivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/textView" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spnImages"
android:layout_below="@+id/textView"
android:layout_alignParentStart="true"
android:layout_marginTop="40dp"
android:spinnerMode="dropdown" />
<ImageView
android:layout_width="match_parent"
25 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_alignParentBottom="true"
android:layout_alignEnd="@+id/spnImages"
android:layout_below="@+id/spnImages"
android:layout_alignParentStart="true"
android:src="@drawable/me" />
</RelativeLayout>
26 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
Java Code for Practical-6
package com.example.tonystark.pract6aditya;
import
import
import
import
import
import
import
import
import
android.support.v7.app.AppCompatActivity;
android.os.Bundle;
android.util.Log;
android.view.View;
android.widget.AdapterView;
android.widget.ArrayAdapter;
android.widget.ImageView;
android.widget.Spinner;
android.widget.Toast;
public class MainActivity extends AppCompatActivity {
String names[]={"me","mom","sir"};
Spinner spn;
ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img=(ImageView)findViewById(R.id.imageView);
spn=(Spinner)findViewById(R.id.spnImages);
ArrayAdapter<String> ad=new
ArrayAdapter<String>(this,R.layout.support_simple_spinn
er_dropdown_item,names);
spn.setAdapter(ad);
spn.setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View
view, int position, long id) {
if (names[position] == "me")
img.setImageDrawable(getResources().getDrawable(R.drawa
ble.me));
else if (names[position] == "mom")
img.setImageDrawable(getResources().getDrawable(R.drawa
27 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
ble.mom));
else
img.setImageDrawable(getResources().getDrawable(R.drawa
ble.sir));
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
);
}
}
28 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
Outputs for Practicals-6
Start Date
29 | P a g e
End/Sign Date
Faculty Sign
Remarks/Grade
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
PRACTICAL-7
AIM : Create an application to change screen color as per the user choice
from a menu.
XML for Practical-7
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/andro
id"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin
"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.tonystark.pract7aditya.MainA
ctivity"
android:id="@+id/rlv1"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLar
ge"
android:text="Practical 7 by Aditya Gurav"
android:id="@+id/tvPract7"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:textColor="#005065"
30 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
android:textStyle="bold" />
</RelativeLayout>
31 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
Java Code for Practical-7
package com.example.tonystark.pract7aditya;
import
import
import
import
import
import
android.graphics.Color;
android.support.v7.app.AppCompatActivity;
android.os.Bundle;
android.view.Menu;
android.view.MenuItem;
android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
RelativeLayout rlv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rlv=(RelativeLayout)findViewById(R.id.rlv1);
}
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
MenuItem mnu1=menu.add(0,0,0,"Green");
MenuItem mnu2=menu.add(0,1,1,"Blue");
MenuItem mnu3=menu.add(0,2,2,"Gray");
return true;
}
public boolean onOptionsItemSelected(MenuItem item)
{
return MenuChoice(item);
}
private boolean MenuChoice(MenuItem item)
{
switch(item.getItemId())
{
case 0:
rlv.setBackgroundColor(Color.GREEN);
break;
32 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
case 1:
rlv.setBackgroundColor(Color.BLUE);
break;
case 2:
rlv.setBackgroundColor(Color.GRAY);
break;
}
return true;
}
}
33 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
Outputs for Practicals-7
Start Date
34 | P a g e
End/Sign Date
Faculty Sign
Remarks/Grade
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
PRACTICAL-8
AIM : Create an application that will display toast (Message) at some
regular interval of time.
XML for Practical-8
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/andro
id"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin
"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.tonystark.pract8aditya.MainA
ctivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/textView" />
<Chronometer
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/chronometer"
android:layout_below="@+id/textView"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
35 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
android:textAppearance="?android:attr/textAppearanceMed
ium"
android:id="@+id/tvMessage"
android:layout_below="@+id/chronometer"
android:layout_alignParentStart="true"
android:layout_marginTop="130dp"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLar
ge"
android:text="Practical 8 by Aditya Gurav"
android:id="@+id/tvPract8"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:textColor="#6a24e2" />
</RelativeLayout>
36 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
Java Code for Practical-8
package com.example.tonystark.pract8aditya;
import
import
import
import
import
android.support.v7.app.AppCompatActivity;
android.os.Bundle;
android.widget.Chronometer;
android.widget.TextView;
android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Chronometer timer;
int i=0;
int Duretion=10;
TextView txt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timer=(Chronometer)findViewById(R.id.chronometer);
txt=(TextView)findViewById(R.id.tvMessage);
timer.start();
timer.setOnChronometerTickListener(new
Chronometer.OnChronometerTickListener() {
@Override
public void onChronometerTick(Chronometer arg0) {
// TODO Auto-generated method stubicon.png
txt.setText("Next Message will display after [
"+(Duretion-(i+1))+" ] seconds ");
i++;
if(i>=Duretion)
{
Toast.makeText(MainActivity.this,
"Message "+(i/10), Toast.LENGTH_SHORT).show();
Duretion+=10;
}
}
37 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
});
}
}
38 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
Outputs for Practicals-8
Start Date
39 | P a g e
End/Sign Date
Faculty Sign
Remarks/Grade
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
PRACTICAL-9
AIM : Create a background application that will open activity on specific
time.
XML for Practical-9
second.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/andro
id"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin
"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.tonystark.pract9aditya.Secon
d">
<TextView android:id="@+id/textView1"
android:textAppearance="?android:attr/textAppearanceLar
ge"
android:layout_height="wrap_content"
android:text="Second Activity"
android:layout_width="match_parent"></TextView>
</RelativeLayout>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/andro
id"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
40 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin
"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.tonystark.pract9aditya.main"
>
<Button android:layout_height="wrap_content"
android:id="@+id/button1" android:text="Start Service"
android:layout_width="match_parent"></Button>
<Button android:layout_height="wrap_content"
android:id="@+id/button2" android:text="Stop Service"
android:layout_width="match_parent"
android:layout_below="@+id/button1"
android:layout_alignParentStart="true"
android:layout_marginTop="48dp"></Button>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLar
ge"
android:text="Practical 9 by Aditya Gurav"
android:id="@+id/tvPract9"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:textColor="#24b9de"
android:textStyle="bold" />
</RelativeLayout>
41 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
Java Code for Practical-9
main.java
package com.example.tonystark.pract9aditya;
import
import
import
import
import
android.content.Intent;
android.support.v7.app.AppCompatActivity;
android.os.Bundle;
android.view.View;
android.widget.Button;
public class main extends AppCompatActivity implements
View.OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button
start=(Button)findViewById(R.id.button1);
Button stop=(Button)findViewById(R.id.button2);
start.setOnClickListener(this);
stop.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v.getId()==R.id.button1)
{
startService(new Intent(this,
MyService.class));
}
if(v.getId()==R.id.button2)
{
stopService(new Intent(this,
MyService.class));
}
}
}
42 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
SERVICE.java
package com.example.tonystark.pract9aditya;
import
import
import
import
import
import
android.app.Service;
android.content.Intent;
android.media.MediaPlayer;
android.os.IBinder;
android.util.Log;
android.widget.Toast;
public class MyService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
public void onCreate()
{
Toast.makeText(this, "My Service Started",
Toast.LENGTH_LONG).show();
}
public void onDestroy()
{
Toast.makeText(this, "My Service Stopped",
Toast.LENGTH_LONG).show();
}
public void onStart(Intent intent, int startid)
{
Intent dialogIntent = new
Intent(getBaseContext(),
Second.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(dialogIntent);
}
}
43 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
Outputs for Practicals-9
Start Date
44 | P a g e
End/Sign Date
Faculty Sign
Remarks/Grade
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
PRACTICAL-10
AIM : Create an application that will have spinner with list of animation
names. On selecting animation name, that animation should affect on the
images displayed below.
XML for Practical-10
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/andro
id"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spinner"
android:layout_gravity="left|top" />
<ImageView
android:layout_width="300dp"
android:layout_height="300dp"
android:id="@+id/imageView"
android:layout_gravity="left"
android:layout_marginTop="100dp"
android:src="@drawable/h"
/>
</FrameLayout>
Java Code for Practical-10
package com.example.tonystark.pract10animating;
45 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
import
import
import
import
import
import
import
import
import
Government Polytechnic, Gandhinagar
android.support.v7.app.AppCompatActivity;
android.os.Bundle;
android.view.View;
android.widget.AdapterView;
android.widget.ArrayAdapter;
android.widget.ImageView;
android.widget.Spinner;
android.widget.TextView;
android.widget.Toast;
import org.w3c.dom.Text;
import java.lang.reflect.Array;
public class MainActivity extends AppCompatActivity
implements AdapterView.OnItemSelectedListener {
String[] effects={"-Selection Operation","Translate>>","Scale +","Rotate
+","<<Translate","Scale -","Rotate -"};
Spinner spin=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spin=(Spinner)findViewById(R.id.spinner);
ArrayAdapter<String> ad=new
ArrayAdapter<String>(this,android.R.layout.simple_spinn
er_dropdown_item,effects);
spin.setAdapter(ad);
spin.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> parent, View
view, int position, long id) {
//TextView t=(TextView)parent.getSelectedView();
46 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
try
{
//TextView t=(TextView)parent.getSelectedView();
ImageView iv=(ImageView)findViewById(R.id.imageView);
switch(position) {
case 1:
//Translate
iv.setX(iv.getX()+10);
iv.setY(iv.getY() + 10);
break;
case 2:
//Scale
iv.setScaleX(iv.getScaleX()+0.1f);
iv.setScaleY(iv.getScaleY()+0.1f);
break;
case 3:
//Rotate
iv.setRotation(iv.getRotation() + 90);
break;
case 4:
//Translate
iv.setX(iv.getX()-10);
iv.setY(iv.getY() - 10);
break;
case 5:
//Scale
iv.setScaleX(iv.getScaleX()-0.1f);
iv.setScaleY(iv.getScaleY()-0.1f);
break;
case 6:
//Rotate
iv.setRotation(iv.getRotation() - 90);
break;
}
spin.setSelection(0);
}
catch(Exception ex)
{
Toast.makeText(MainActivity.this,
ex.getMessage(), Toast.LENGTH_SHORT).show();
47 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
48 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
Outputs for Practicals-10
49 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Start Date
50 | P a g e
End/Sign Date
Government Polytechnic, Gandhinagar
Faculty Sign
Remarks/Grade
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
PRACTICAL-11
AIM : Create an UI listing the diploma engineering branches. If user
selects a branch name, display the number of semesters and subjects in
each semester.
XML for Practical-11
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/andro
id"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin
"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.tonystark.pract11aditya.Main
Activity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/textView" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spinner"
android:layout_below="@+id/textView"
android:layout_alignParentStart="true"
android:layout_marginTop="46dp" />
<TextView
51 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLar
ge"
android:id="@+id/tvNoOfBranches"
android:layout_centerVertical="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLar
ge"
android:text="Practical 11 by Aditya Gurav"
android:id="@+id/tvPract11"
android:textColor="#f56d05"
android:textStyle="bold"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
52 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
Java Code for Practical-11
package com.example.tonystark.pract11aditya;
import
import
import
import
import
import
import
import
import
android.support.v7.app.AppCompatActivity;
android.os.Bundle;
android.view.View;
android.widget.AdapterView;
android.widget.ArrayAdapter;
android.widget.ImageView;
android.widget.Spinner;
android.widget.TextView;
android.widget.Toast;
public class MainActivity extends AppCompatActivity
implements AdapterView.OnItemSelectedListener {
String[] effects = {"-Select Branch-", "I.T.",
"C.E.", "Fabrication", "Pharmacy"};
Integer countSem[] = {0, 6, 6, 8, 8 };
Integer countSubj[] = {0, 31, 31, 42, 40 };
Spinner spin = null;
TextView tv=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spin = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> ad = new
ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item,
effects);
spin.setAdapter(ad);
spin.setOnItemSelectedListener(this);
tv = (TextView)findViewById(R.id.tvNoOfBranches);
}
53 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
@Override
public void onItemSelected(AdapterView<?> parent, View
view, int position, long id) {
//TextView t=(TextView)parent.getSelectedView();
//try {
if(position==0) {
tv.setText("Select a branch to get its details...");
}
else
tv.setText("Selected Branch : " + effects[position] + "
\nTotal Semesters :" + countSem[position] + "\nTotal
Subjects : " + countSubj[position]);
/*} catch (Exception ex) {
Toast.makeText(MainActivity.this,
ex.getMessage(), Toast.LENGTH_SHORT).show();
}*/
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
54 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
Outputs for Practicals-11
Start Date
55 | P a g e
End/Sign Date
Faculty Sign
Remarks/Grade
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
PRACTICAL-12
AIM : Use content providers and permissions by implementing read
phonebook contacts with content providers and display in the list.
XML for Practical-12
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/andro
id"
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"
app:layout_behavior="@string/appbar_scrolling_view_beha
vior"
tools:context="com.example.tonystark.contactsreading.Sc
rollingActivity"
tools:showIn="@layout/activity_scrolling">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_margin="10dp"
android:text="TextView" />
</android.support.v4.widget.NestedScrollView>
56 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
Java Code for Practical-12
package com.example.tonystark.contactsreading;
import android.content.ContentResolver;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.MediaStore;
import
android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ScrollingActivity extends
AppCompatActivity {
TextView textDetail;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_scrolling);
textDetail = (TextView) findViewById(R.id.textView1);
try {
readContacts();
}
catch(Exception ex)
{
Toast.makeText(this, ex.getMessage(),
Toast.LENGTH_SHORT).show();
}
57 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
}
public void readContacts() {
StringBuffer sb = new StringBuffer();
sb.append("......Contact Details.....");
ContentResolver cr = getContentResolver();
Cursor cur =
cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
String phone = null;
String emailContact = null;
String emailType = null;
String image_uri = "";
Bitmap bitmap = null;
if (cur.getCount() >0) {
while (cur.moveToNext()) {
String id = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)
);
image_uri = cur
.getString(cur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.
PHOTO_URI));
if (Integer
.parseInt(cur.getString(cur
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUM
BER))) >0) {
System.out.println("name : " + name
+ ", ID : " + id);
sb.append("\n Contact Name:" +
name);
Cursor pCur = cr.query(
58 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[] { id }, null);
while (pCur.moveToNext()) {
phone = pCur
.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.
NUMBER));
sb.append("\n Phone number:" +
phone);
System.out.println("phone" +
phone);
}
pCur.close();
Cursor emailCur = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID
+ " = ?", new String[] { id }, null);
while (emailCur.moveToNext()) {
emailContact = emailCur
.getString(emailCur
.getColumnIndex(ContactsContract.CommonDataKinds.Email.
DATA));
emailType = emailCur
.getString(emailCur
.getColumnIndex(ContactsContract.CommonDataKinds.Email.
TYPE));
sb.append("\nEmail:" +
emailContact + "Email type:" + emailType);
//System.out.println("Email " + emailContact
59 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
//
+ " Email Type : " +
emailType);
}
emailCur.close();
}
if (image_uri != null) {
//System.out.println(Uri.parse(image_uri));
try {
bitmap =
MediaStore.Images.Media
.getBitmap(this.getContentResolver(),
Uri.parse(image_uri));
sb.append("\n Image in Bitmap:"
+ bitmap);
System.out.println(bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
}
}
sb.append("\n........................................")
;
}
textDetail.setText(sb);
}
}
60 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
}
61 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
Outputs for Practicals-12
Start Date
62 | P a g e
End/Sign Date
Faculty Sign
Remarks/Grade
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
PRACTICAL-13
AIM : Create an application to call a phone number entered by the user
the Edit Text.
XML for Practical-13
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/andro
id"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin
"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.tonystark.pract13aditya.Main
Activity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/textView" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="@+id/editText"
android:layout_below="@+id/textView"
android:layout_alignParentStart="true"
android:layout_marginTop="66dp"
android:layout_alignParentEnd="true" />
63 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DIAL"
android:id="@+id/btnDial"
android:layout_marginTop="42dp"
android:layout_below="@+id/editText"
android:layout_centerHorizontal="true" />
</RelativeLayout>
64 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
Java Code for Practical-13
package com.example.tonystark.pract13aditya;
import
import
import
import
import
import
import
android.content.Intent;
android.net.Uri;
android.support.v7.app.AppCompatActaivity;
android.os.Bundle;
android.view.View;
android.widget.Button;
android.widget.EditText;
public class MainActivity extends AppCompatActivity
implements View.OnClickListener {
Button btn=null;
EditText etNumber=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button)findViewById(R.id.btnDial);
btn.setOnClickListener(this);
etNumber=(EditText)findViewById(R.id.editText);
}
public void onClick(View vw)
{
Uri number = Uri.parse("tel:" +
etNumber.getText().toString());
Intent callIntent = new
Intent(Intent.ACTION_DIAL, number);
startActivity(callIntent);
}
}
65 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
Outputs for Practicals-13
Start Date
66 | P a g e
End/Sign Date
Faculty Sign
Remarks/Grade
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
PRACTICAL-14
AIM : Create an application that will create database to store username
and password.
XML for Practical-14
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/andro
id"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin
"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.tonystark.pract14aditya.Main
Activity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View All"
android:id="@+id/btnViewAll"
android:layout_alignTop="@+id/btnSave"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
android:id="@+id/btnSave"
android:layout_below="@+id/etPassword"
android:layout_alignParentStart="true" />
<EditText
67 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/etUsername"
android:layout_marginTop="66dp"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/etPassword"
android:layout_below="@+id/etUsername"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMed
ium"
android:id="@+id/tvViewAllResult"
android:layout_below="@+id/btnSave"
android:layout_alignParentStart="true"
android:layout_marginTop="44dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMed
ium"
android:text="Pract 14 by Aditya Gurav"
android:id="@+id/tvPract14"
android:textColor="@android:color/holo_blue_dark"
android:textSize="24sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
68 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
Java Code for Practical-14
package com.example.tonystark.pract14aditya;
import
import
import
import
import
import
import
import
import
import
android.content.Context;
android.database.Cursor;
android.database.sqlite.SQLiteDatabase;
android.support.v7.app.AppCompatActivity;
android.os.Bundle;
android.view.View;
android.widget.Button;
android.widget.EditText;
android.widget.TextView;
android.widget.Toast;
public class MainActivity extends AppCompatActivity
implements View.OnClickListener {
EditText etUsr=null;
EditText etPwd=null;
TextView tvViewAll=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etUsr=(EditText)findViewById(R.id.etUsername);
etPwd=(EditText)findViewById(R.id.etPassword);
tvViewAll=(TextView)findViewById(R.id.tvViewAllResult);
Button
btnSave=(Button)findViewById(R.id.btnSave);
Button
btnView=(Button)findViewById(R.id.btnViewAll);
btnSave.setOnClickListener(this);
btnView.setOnClickListener(this);
}
@Override
public void onClick(View v)
69 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
{
SQLiteDatabase
db=openOrCreateDatabase("StudentDB",
Context.MODE_PRIVATE, null);
if(v.getId() == R.id.btnSave)
{
db.execSQL("CREATE TABLE IF NOT EXISTS
student(username VARCHAR,password VARCHAR);");
//
db.execSQL("delete from student");
db.execSQL("INSERT INTO student VALUES('"+
etUsr.getText()+"','"+
etPwd.getText()+"');");
etUsr.setText("");
etPwd.setText("");
Toast.makeText(MainActivity.this, "Data
Saved Successfully!", Toast.LENGTH_SHORT).show();
}
else
{
try {
Cursor c = db.rawQuery("SELECT * FROM
student", null);// WHERE
rollno='"+editRollno.getText()+"'", null);
tvViewAll.setText("Data in student
table\n=======================");
while (c.moveToNext()) {
tvViewAll.setText(tvViewAll.getText().toString() + '\n'
+ c.getString(0) + '\t' + c.getString(1));
}
tvViewAll.setText(tvViewAll.getText().toString() +
"\n=======================");
}
catch(Exception ex)
{
Toast.makeText(MainActivity.this,
ex.getMessage().toString(), Toast.LENGTH_SHORT).show();
}
}
70 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
}
}
71 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Government Polytechnic, Gandhinagar
Outputs for Practicals-14
72 | P a g e
XXXXXXXXXXXX |Xxxxxx Xxxxx
3361602 – Android App Development
Start Date
73 | P a g e
End/Sign Date
Government Polytechnic, Gandhinagar
Faculty Sign
Remarks/Grade
XXXXXXXXXXXX |Xxxxxx Xxxxx
Download