Practical No-1
Aim: Develop an application that uses GUI components, Fonts and Colours.
XML code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:gravity="center"
android:text="Hello World!"
android:textSize="25sp"
android:textStyle="bold" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:gravity="center"
android:text="Change font size"
android:textSize="25sp" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:gravity="center"
android:text="Change color"
android:textSize="25sp" />
</LinearLayout>
JAVA code:
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity
{
int ch=1;
float font=30;
@Override
protected void onCreate(Bundle savedInstanceState)
{
[Link](savedInstanceState);
setContentView([Link].activity_main);
final TextView t= (TextView) findViewById([Link]);
Button b1= (Button) findViewById([Link].button1);
[Link](new [Link]() {
@Override
public void onClick(View v) {
[Link](font);
font = font + 5;
if (font == 50)
font = 30;
}
});
Button b2= (Button) findViewById([Link].button2);
[Link](new [Link]() {
@Override
public void onClick(View v) {
switch (ch) {
case 1:
[Link]([Link]);
break;
case 2:
[Link]([Link]);
break;
case 3:
[Link]([Link]);
break;
case 4:
[Link]([Link]);
break;
case 5:
[Link]([Link]);
break;
case 6:
[Link]([Link]);
break;
}
ch++;
if (ch == 7)
ch = 1;
}
});
}
}
OUTPUT:
Practical No-2
Aim: Develop an application that uses layout Managers and event listeners.
XML code for Activity 1:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:text="Details Form"
android:textSize="25sp"
android:gravity="center"/>
</LinearLayout>
<GridLayout
android:id="@+id/gridLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="100dp"
android:layout_marginBottom="200dp"
android:columnCount="2"
android:rowCount="3">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_row="0"
android:layout_column="0"
android:text="Name"
android:textSize="20sp"
android:gravity="center"/>
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_row="0"
android:layout_column="1"
android:ems="10"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_row="1"
android:layout_column="0"
android:text="[Link]"
android:textSize="20sp"
android:gravity="center"/>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_row="1"
android:layout_column="1"
android:inputType="number"
android:ems="10"/>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_row="2"
android:layout_column="0"
android:text="Dept"
android:textSize="20sp"
android:gravity="center"/>
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_row="2"
android:layout_column="1"
android:spinnerMode="dropdown"/>
</GridLayout>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:layout_marginBottom="150dp"
android:text="Submit"/>
</RelativeLayout>
XML code for Activity 2:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="[Link]"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="New Text"
android:textSize="30sp"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="New Text"
android:textSize="30sp"/>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="New Text"
android:textSize="30sp"/>
</LinearLayout>
JAVA code for Main_Activity:
import [Link];
import [Link];
import [Link];
import [Link];
import [Link].*;
public class MainActivity extends AppCompatActivity {
EditText e1,e2;
Button bt;
Spinner s;
//Data for populating in Spinner
String [] dept_array={"CSE","ECE","IT","Mech","Civil"};
String name,reg,dept;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
e1= (EditText) findViewById([Link]);
e2= (EditText) findViewById([Link].editText2);
bt= (Button) findViewById([Link]);
s= (Spinner) findViewById([Link]);
//Creating Adapter for Spinner for adapting the data from array to Spinner
ArrayAdapter adapter = new
ArrayAdapter([Link],[Link].simple_spinner_item,dept_array);
[Link](adapter);
//Creating Listener for Button
[Link](new [Link]() {
@Override
public void onClick(View v) {
//Getting the Values from Views(Edittext & Spinner)
name=[Link]().toString();
reg=[Link]().toString();
dept=[Link]().toString();
//Intent For Navigating to Second Activity
Intent i = new Intent([Link],[Link]);
//For Passing the Values to Second Activity
[Link]("name_key", name);
[Link]("reg_key",reg);
[Link]("dept_key", dept);
startActivity(i);
}
});
}
}
JAVA code for Second Activity:
import [Link];
import [Link];
import [Link];
import [Link];
public class SecondActivity extends AppCompatActivity {
TextView t1,t2,t3;
String name,reg,dept;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_second);
t1= (TextView) findViewById([Link].textView1);
t2= (TextView) findViewById([Link].textView2);
t3= (TextView) findViewById([Link].textView3);
//Getting the Intent
Intent i = getIntent();
//Getting the Values from First Activity using the Intent received
name=[Link]("name_key");
reg=[Link]("reg_key");
dept=[Link]("dept_key");
//Setting the Values to Intent
[Link](name);
[Link](reg);
[Link](dept);
}
}
OUTPUT:
Practical No-3
Aim: Develop an application that draws basic graphical primitives on the
screen.
XML code :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView" />
</RelativeLayout>
JAVA code for Main_Activity:
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
[Link](savedInstanceState);
setContentView([Link].activity_main);
//Creating a Bitmap
Bitmap bg = [Link](720, 1280, [Link].ARGB_8888);
//Setting the Bitmap as background for the ImageView
ImageView i = (ImageView) findViewById([Link]);
[Link](new BitmapDrawable(bg));
//Creating the Canvas Object
Canvas canvas = new Canvas(bg);
//Creating the Paint Object and set its color & TextSize
Paint paint = new Paint();
[Link]([Link]);
[Link](50);
//To draw a Rectangle
[Link]("Rectangle", 420, 150, paint);
[Link](400, 200, 650, 700, paint);
//To draw a Circle
[Link]("Circle", 120, 150, paint);
[Link](200, 350, 150, paint);
//To draw a Square
[Link]("Square", 120, 800, paint);
[Link](50, 850, 350, 1150, paint);
//To draw a Line
[Link]("Line", 480, 800, paint);
[Link](520, 850, 520, 1150, paint);
}
}
OUTPUT:
Practical No-4
Aim: Implement an Application that implements Multi-threading.
XML code :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_margin="50dp"
android:layout_gravity="center" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_gravity="center"
android:text="Load Image 1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_gravity="center"
android:text="Load image 2" />
</LinearLayout>
JAVA code for Main_Activity:
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity
{
ImageView img;
Button bt1,bt2;
@Override
protected void onCreate(Bundle savedInstanceState)
{
[Link](savedInstanceState);
setContentView([Link].activity_main);
bt1 = (Button)findViewById([Link]);
bt2= (Button) findViewById([Link].button2);
img = (ImageView)findViewById([Link]);
[Link](new [Link]()
{
@Override
public void onClick(View v)
{
new Thread(new Runnable()
{
@Override
public void run()
{
[Link](new Runnable()
{
@Override
public void run()
{
[Link]([Link].india1);
}
});
}
}).start();
}
});
[Link](new [Link]()
{
@Override
public void onClick(View v)
{
new Thread(new Runnable()
{
@Override
public void run()
{
[Link](new Runnable()
{
@Override
public void run()
{
[Link]([Link].india2);
}
});
}
}).start();
}
});
}
}
OUTPUT:
Practical No-5
Aim: Implement an application that creates an alert upon receiving the message.
XML code :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Message"
android:textSize="30sp" />
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textSize="30sp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:layout_gravity="center"
android:text="Notify"
android:textSize="30sp"/>
</LinearLayout>
JAVA code for Main_Activity:
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity
{
Button notify;
EditText e;
@Override
protected void onCreate(Bundle savedInstanceState)
{
[Link](savedInstanceState);
setContentView([Link].activity_main);
notify= (Button) findViewById([Link]);
e= (EditText) findViewById([Link]);
[Link](new [Link]()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent([Link], [Link]);
PendingIntent pending = [Link]([Link], 0, intent, 0);
Notification noti = new
[Link]([Link]).setContentTitle("New
Message").setContentText([Link]().toString()).setSmallIcon([Link].ic_launcher).setCo
ntentIntent(pending).build();
NotificationManager manager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
[Link] |= Notification.FLAG_AUTO_CANCEL;
[Link](0, noti);
}
});
}
}
OUTPUT:
Practical No-6
Aim: Develop an application that creates an alarm clock.
XML code :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TimePicker
android:id="@+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dp"
android:checked="false"
android:onClick="OnToggleClicked" />
</LinearLayout>
JAVA code for Main_Activity:
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity
{
TimePicker alarmTimePicker;
PendingIntent pendingIntent;
AlarmManager alarmManager;
@Override
protected void onCreate(Bundle savedInstanceState)
{
[Link](savedInstanceState);
setContentView([Link].activity_main);
alarmTimePicker = (TimePicker) findViewById([Link]);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
}
public void OnToggleClicked(View view)
{
long time;
if (((ToggleButton) view).isChecked())
{
[Link]([Link], "ALARM ON",
Toast.LENGTH_SHORT).show();
Calendar calendar = [Link]();
[Link](Calendar.HOUR_OF_DAY, [Link]());
[Link]([Link], [Link]());
Intent intent = new Intent(this, [Link]);
pendingIntent = [Link](this, 0, intent, 0);
time=([Link]()-([Link]()%60000));
if([Link]()>time)
{
if (calendar.AM_PM == 0)
time = time + (1000*60*60*12);
else
time = time + (1000*60*60*24);
}
[Link](AlarmManager.RTC_WAKEUP, time, 10000,
pendingIntent);
}
else
{
[Link](pendingIntent);
[Link]([Link], "ALARM OFF",
Toast.LENGTH_SHORT).show();
}
}
}
JAVA code for Alarm Receiver :
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class AlarmReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
[Link](context, "Alarm! Wake up! Wake up!",
Toast.LENGTH_LONG).show();
Uri alarmUri = [Link](RingtoneManager.TYPE_ALARM);
if (alarmUri == null)
{
alarmUri =
[Link](RingtoneManager.TYPE_NOTIFICATION);
}
Ringtone ringtone = [Link](context, alarmUri);
[Link]();
}
}
OUTPUT:
Practical No -8
Aim: Develop an application that creates types of menus.
XML code :
<RelativeLayout xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_orange_light"
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=".MainActivity" >
<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="153dp" />
</RelativeLayout>
Defining Menus :
1. Context Menu
<menu xmlns:android="[Link] >
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/action_settings"/>
<item android:title="new group"
android:id="@+id/t1"/>
<item android:title="web"
android:id="@+id/t2"/>
<item android:title="status"
android:id="@+id/t3"/>
</menu>
2. Options Menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="[Link] >
<item android:title="cut" android:id="@+id/t4"/>
<item android:title="copy" android:id="@+id/t5"/>
<item android:title="paste" android:id="@+id/t6"/>
<item android:title="delete" android:id="@+id/t7"/>
</menu>
JAVA code for Main_Activity:
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends Activity
{
String [] p={"CS","IT","ECE","BT","ME","CIVIL","AUTOMOBILE"};
protected void onCreate(Bundle savedInstanceState)
{
[Link](savedInstanceState);
setContentView([Link].activity_main);
ArrayAdapter <String> a= new ArrayAdapter <String>(this,
[Link].simple_list_item_1, p);
Spinner s=(Spinner)findViewById([Link].spinner1);
[Link](a);
registerForContextMenu(s);
}
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate([Link], menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item)
{
switch([Link]())
{
case [Link].t1:
[Link](this,"Created new Group",5000).show();
break;
case [Link].t2:
[Link](this,"Connecting to web",5000).show();
break;
case [Link].t3:
[Link](this,"Viewing Profile",5000).show();
break;
}
return [Link](item);
public void onCreateContextMenu(ContextMenu menu,View v,ContextMenuInfo
menuinfo)
{
getMenuInflater().inflate([Link].menu1,menu);
[Link](menu, v, menuinfo);
}
public boolean onContextItemSelected(MenuItem item)
{
switch([Link]())
{
case [Link].t4:
[Link](this,"Cut",5000).show();
break;
case [Link].t5:
[Link](this,"Content Copied",5000).show();
break;
case [Link].t6:
[Link](this,"Added to Clipboard",5000).show();
break;
case [Link].t7:
[Link](this,"Deleting the item",5000).show();
break;
}
return [Link](item);
}
OUTPUT: