MCWC Lab File
MCWC Lab File
LAB: 1
Introduction to Android and its version. Configuring android
development environment.
Android: - Android is a mobile operating system developed by Google. It is used by
several smartphones and tablets.
The Android operating system (OS) is based on the Linux kernel. Unlike Apple's iOS, Android
is open source, meaning developers can modify and customize the OS for each phone.
Therefore, different Android-based phones often have different graphical user interfaces GUIs
even though they use the same OS.
Android Versions: -
Code name Version number Initial release date API level Security patches
Ice Cream
4.0 – 4.0.4 October 18, 2011 14 – 15 Unsupported
Sandwich
1
150110107039
SUBJECT : MCWC SUBJECT CODE : 2170710
You will learn how to set the environment to develop Android application .This Comprises of
downloading, configuring and installing several software components required to make the IDE
complete and ready for developing purpose.
Java development kit or JDK since applications are being developed in java
Android Software Development Kit(SDK)
Eclipse IDE
The Android SDK is a complete set of API Libraries and developer tools. By configuring these
tools and libraries with Eclipse, an Android-specific development environment will be ready.
The link to download the latest Android SDK from the Android developer’s website is as
follows:
http://developers.android.com/sdk/index.html
The Android SDK is a kit, which helps in buildings, testing and debugging application for
Android. By accessing above link, you will be directed to the Android developer’s site, where
you will find [Download the SDK button]. This button will download Android SDK as an ADT
Bundle that includes everything you need to develop Android application. This bundle includes
following items:
You need to click on it and then select the check box to accept its terms and condition. Finally,
the Android SDK pack will be downloaded as a zip folder.
2
150110107039
SUBJECT : MCWC SUBJECT CODE : 2170710
You should not move any of the file or directories from the adt-bundle-<os_platform> directory;
otherwise ADT will not be able to locate the SDT and you will need to manually update the ADT
preferences.
ADT is the extension to increase the capabilities of Eclipse to create Android Projects, create an
Application UI, add packages of android API, and debug the Android application. The steps to
download the ADT plugin are as follows:
1. Start the Eclipse from the location you have installed earlier.
3
150110107039
SUBJECT : MCWC SUBJECT CODE : 2170710
Figure 3
4
150110107039
SUBJECT : MCWC SUBJECT CODE : 2170710
4. ADT plugins as named and enter the following location in the location box:
https://dl-ssl.google.com/android/eclipse/
If any problem persists in fetching ADT plugins, just try to use http instead of https in the
uniform Resource locator
6. Click the Select All button and then click the Next button, as shown in Figure 4:
Figure 4
7. Click the Next button. Read and accept the license agreement Terms and Conditions appearing
in the next page.
8. Click the Finish button. There might be home security warning, just ignore them.
9. Click the OK button on the box that is displaying warning. This will start the installation. On
completion of installing, you will be prompted to restart the Eclipse.
5
150110107039
SUBJECT : MCWC SUBJECT CODE : 2170710
Practical 2
Aim : Study of debugging tools: Logcat and DDMS.
Introduction
In this increasingly fragmented and complex world of Android application development,
thoroughly testing your Android app is becoming a real challenge. One of the biggest headaches
is how to test your Android app in all the conceivable real-world scenarios. To make matters
worse, most Android app development scenarios are mobile-specific, and will be unfamiliar to
developers who have previously dealt only with desktop and Web environments.
You may have an Android device with your targeted operating system on hand, but you'll still
struggle to recreate every possible situation, which is why the emulator has become the Android
developer's best friend. But even Android emulators have their limitations, and the testing phase
is crucial to the success of your Android app.
The good news for Android developers is that once you have the Android SDK and ADT plugin
installed, you'll have access to one of the most powerful debugging tools out there: the Dalvik
Debug Monitor Server (DDMS.)
What is DDMS?
The DDMS is an invaluable tool for checking the memory usage of your Android app, drilling
down into errors, simulating a range of real-world conditions, and much more. The DDMS can
analyze an Android project running in the emulator, or it can be connected to a real device (if both
are connected, the DDMS will default to the emulator.)
Where to find DDMS?
To access the DDMS, open the Window menu and select Open Perspective. Select Other, which
will present you with a list of additional options. Select DDMS.
This will bring up the DDMS perspective with the default views (note that you can always access
additional views by opening the Window menu and selecting Show View). In this article, I'll take
you through these different DDMS views and explain why they're so important for Android
development
150110107039
SUBJECT : MCWC SUBJECT CODE : 2170710
What is Logcat?
Logcat is a command-line tool that dumps a log of system messages, including stack traces when
the device throws an error and messages that you have written from your app with the Log class.
Syntax in Command-Line
To run logcat through the adb shell, the general usage is:
[adb] logcat [<option>] ... [<filter-spec>] ...
You can run logcat as an adb command or directly in a shell prompt of your emulator or
connected device. To view log output using adb, navigate to your SDK platform-tools/ directory
and execute:
adb logcat
150110107039
SUBJECT : MCWC SUBJECT CODE : 2170710
Practical 3
Aim : Event Handling in Android (Three Methods).
Introduction
Events are a useful way to collect data about a user's interaction with interactive components of
Applications.Like button presses or screen touch etc. The Android framework maintains an event
queue as first-in, first-out (FIFO) basis. You can capture these events in your program and take
appropriate action as per requirements.
What is Event Handler?
When an event happens and we have registered an event listener for the event, the event listener
calls the Event Handlers, which is the method that actually handles the event.
Event Handling Methods
o onClick()
This is called when the user either clicks or touches or focuses upon any widget like button, text, image
etc. We use onClick() event handler to handle such event.
Application.java
package com.javacodegeeks.android.example.androidonclicklistenerexample;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.view.View.OnClickListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
150110107039
SUBJECT : MCWC SUBJECT CODE : 2170710
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Button Clicked",
Toast.LENGTH_SHORT).show();
}
});
return true;
}
}
o onLongClick()
This is called when the user either clicks or touches or focuses upon any widget like button, text, image
etc. for one or more seconds. You will use onLongClick() event handler to handle such event.
Activity.xml
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Event Handling "
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="30dp"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorials point "
android:textColor="#ff87ff09"
android:textSize="30dp"
android:layout_above="@+id/imageButton"
android:layout_centerHorizontal="true"
150110107039
SUBJECT : MCWC SUBJECT CODE : 2170710
android:layout_marginBottom="40dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Small font"
android:id="@+id/button"
android:layout_below="@+id/imageButton"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Font"
android:id="@+id/button2"
android:layout_below="@+id/button"
android:layout_alignRight="@+id/button"
android:layout_alignEnd="@+id/button" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/textView"
android:layout_below="@+id/button2"
android:layout_centerHorizontal="true"
android:textSize="25dp" />
</RelativeLayout>
o onFocusChange()
This is called when the widget looses its focus ie. user goes away from the view item. You will use
onFocusChange() event handler to handle such event.
150110107039
SUBJECT : MCWC SUBJECT CODE : 2170710
Practical: 4
Aim: Passing Data to Another Activity (Explicit Intent)
MainActivity.java
package com.example.abc;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public
class MainActivity extends ActionBarActivity {
Button btn1, btn2;
Toast toast;
EditText et1, et2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1 =
(EditText) findViewById(R.id.editText1);
et2 = (EditText) findViewById(R.id.editText2);
}
public void click(View v) {
String str1 = et1.getText().toString();
String str2 = et2.getText().toString();
Intent intent = new Intent(getApplicationCont
ext(), Activity2.class);
intent.putExtra("name", str1);
intent.putExtra("email", str2);
startActivity(intent);
}
}
G.H.P ATEL COLLEGE OF ENGINEERING AND TECHNOLOGY
SUBJECT NAME:
150110107039
SUBJECT : MCWC SUBJECT CODE : 2170710
150110107039
SUBJECT : MCWC SUBJECT CODE : 2170710
150110107039
SUBJECT : MCWC SUBJECT CODE : 2170710
Practical: 5
Aim: Passing Data to Another Activity (Intent)
Examples of Implicit Intent.
CODE
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.p1.MainActivity"
tools:ignore="MergeRootFrame" >
<Button
android:id="@+id/button1"
android:onClick="clicked_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/b" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10" >
<requestFocus />
</EditText>
</LinearLayout>
activity_main_activity2.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.p1.MainActivity2"
tools:ignore="MergeRootFrame" >
<TextView
android:id="@+id/textView1"
android:layout_width="157dp"
android:layout_height="34dp"
android:text="TextView" />
</LinearLayout>
150110107039
SUBJECT : MCWC SUBJECT CODE : 2170710
MainActivity.java
package com.example.p1;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
EditText et1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1=(EditText)findViewById(R.id.editText1);
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
String str;
str=et1.getText().toString();
Intent intent=new Intent(getApplicationContext(),MainActivity2.class);
intent.putExtra("key", str);
startActivity(intent);
// TODO Auto-generated method stub
}
});
}}
MainActivity2.java
package com.example.p1;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.TextView;
public class MainActivity2 extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
Bundle bundle=getIntent().getExtras();
String str=bundle.getString("key");
TextView t=(TextView) findViewById(R.id.textView1);
t.setText("hello "+str);
}
}
150110107039
SUBJECT : MCWC SUBJECT CODE : 2170710
OUTPUT
150110107039
SUBJECT : MCWC SUBJECT CODE : 2170710
Practical 6
Method Description
onCreate called when activity is first created.
onStart called when activity is becoming visible to the user
onResume called when activity will start interacting with the user
onPause called when activity is not visible to the user
onStop called when activity is no longer visible to the user
onRestart called after your activity is stopped, prior to start
onDestroy called before the activity is destroyed
150110107039
SUBJECT : MCWC SUBJECT CODE : 2170710
File: activity_main.xml
150110107039
SUBJECT : MCWC SUBJECT CODE : 2170710
tools:context="example.javatpoint.com.activitylifecycle.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
It provides the details about the invocation of life cycle methods of activity. In this example,
the content of the logcat are displayed.
File: MainActivity.java
package example.javatpoint.com.activitylifecycle;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("lifecycle","onCreate invoked");
}
@Override
protected void onStart() {
super.onStart();
Log.d("lifecycle","onStart invoked");
}
@Override
protected void onResume() {
super.onResume();
Log.d("lifecycle","onResume invoked");
}
@Override
150110107039
SUBJECT : MCWC SUBJECT CODE : 2170710
150110107039
SUBJECT : MCWC SUBJECT CODE : 2170710
150110107039
SUBJECT : MCWC SUBJECT CODE : 2170710
Now emulator is on the home. Now click on the center button to launch the app again.
Now click on the lifecycleactivity icon.
150110107039
SUBJECT : MCWC SUBJECT CODE : 2170710
150110107039
SUBJECT : MCWC SUBJECT CODE : 2170710
Practical-7
Menu_main.xml
<menu 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"
tools:context="com.example.menubar.MainActivity" >
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never">
<menu>
<item
android:id="@+id/action_settings1"
android:orderInCategory="100"
android:title="Settings 1"
app:showAsAction="never"/>
<item
android:id="@+id/action_settings2"
android:orderInCategory="100"
android:title="Settings 2"
app:showAsAction="never"/>
</menu>
</item>
<item
android:id="@+id/action_logout"
android:orderInCategory="100"
android:title="Logout"
app:showAsAction="never"/>
<item
android:id="@+id/action_search"
android:orderInCategory="100"
android:title="Search"
app:showAsAction="always"/>
</menu>
MainActivity.java
package com.example.home.menubar;
import android.os.Bundle;
150110107039
SUBJECT : MCWC SUBJECT CODE : 2170710
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.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
Toast.makeText(getBaseContext(), "Settings selected",
Toast.LENGTH_LONG).show();
} else if (id == R.id.action_search) {
Toast.makeText(getBaseContext(), "Search selected",
Toast.LENGTH_LONG).show();
} else if (id == R.id.action_logout) {
Toast.makeText(getBaseContext(), "Logout selected",
Toast.LENGTH_LONG).show();
} else if (id == R.id.action_settings1) {
Toast.makeText(getBaseContext(), "Settings 1 selected",
Toast.LENGTH_LONG).show();
} else if (id == R.id.action_settings2) {
Toast.makeText(getBaseContext(), "Settings 2 selected",
Toast.LENGTH_LONG).show();
}
return super.onOptionsItemSelected(item);
}
}
150110107039
SUBJECT : MCWC SUBJECT CODE : 2170710
Output
150110107039
SUBJECT : MCWC SUBJECT CODE : 2170710
Practical-8
Aim: Develop simple android app to simulate the Linear Layout and
Relative Layout
Menu_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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.jonit.lab4.MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">
<ImageView
android:layout_width="match_parent"
android:layout_height="50dp"
app:srcCompat="@drawable/fb"
android:id="@+id/i" />
<TextView
android:text="Faebook"
android:layout_width="match_parent" android:paddingTop="5dp"
150110107039
SUBJECT : MCWC SUBJECT CODE : 2170710
android:layout_height="wrap_content" android:gravity="center"
android:id="@+id/textView2"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
</LinearLayout>
</RelativeLayout>
Output
150110107039
SUBJECT : MCWC SUBJECT CODE : 2170710
Practical-9
Aim: Develop simple android app to simulate the Scroll View Layout,
The Table Layout and The Frame Layout
Menu_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
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.lab5.MainActivity$PlaceholderFragment" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:src="@drawable/ic_launcher" />
<TableLayout
android:layout_width="match_parent"
android:layout_gravity="center"
android:layout_height="wrap_content" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="1dp"
150110107039
SUBJECT : MCWC SUBJECT CODE : 2170710
android:text="Apple"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="1dp"
android:text="Alphabet"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="1dp"
android:text="Amazon"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="1dp"
android:text="Microsoft"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
</TableLayout>
</FrameLayout>
</LinearLayout>
</ScrollView>
150110107039
SUBJECT : MCWC SUBJECT CODE : 2170710
</RelativeLayout>
Output
150110107039
SUBJECT : MCWC SUBJECT CODE : 2170710
Practical 10
Aim : Orientation in Android
Introduction
The screen orientation attribute is provided by the activity element in the Android Manifest.Xml
file. The orientations provided by the activity are Portrait, Landscape, Sensor, Unspecified and so
on. To perform a screen orientation activity you define the properties in the Android Manifest.Xml
file.
Types of Orientation
o Unspecified : It is the default value. In such case, system chooses the
orientation.
o Portrait : In a portrait mode the screen will be taller not wider.
o Landscape : In a landscape mode the screen will be wider not taller.
o Sensor : Orientation is determined by the device orientation sensor.
Example
In this example, we will create two activities of different screen orientation. The first
activity (MainActivity) will be as "portrait" orientation and second activity
(SecondActivity) as "landscape" orientation type.
activity_main.xml
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="112dp"
android:onClick="onClick"
android:text="Launch next activity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.612"
150110107064 AMIN
SUBJECT : MCWC SUBJECT CODE : 2170710
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText1"
app:layout_constraintVertical_bias="0.613" />
<TextView
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="124dp"
android:ems="10"
android:textSize="22dp"
android:text="This activity is portrait orientation"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
MainActivity.java
package example.javatpoint.com.screenorientation;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(Button)findViewById(R.id.button1);
}
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
}
}
150110107064 AMIN
SUBJECT : MCWC SUBJECT CODE : 2170710
activity_Second.xml
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="180dp"
android:text="this is landscape orientation"
android:textSize="22dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
SecondActivity.java
package example.javatpoint.com.screenorientation;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}}
150110107064 AMIN
SUBJECT : MCWC SUBJECT CODE : 2170710
AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name="example.javatpoint.com.screenorientation.MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
Output
150110107064 AMIN
SUBJECT : MCWC SUBJECT CODE : 2170710
Practical: 11
Aim: Create a Custom Button Toast & Create Custom Toast.
CODE
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.p10.MainActivity"
tools:ignore="MergeRootFrame" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/t" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/b" />
</LinearLayout>
MainActivity.java
package com.example.p10;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity implements OnClickListener {
Button btn1, btn2;
Toast toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.button1);
btn2 = (Button) findViewById(R.id.button2);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (R.id.button1 == v.getId()) {
150110107039
SUBJECT : MCWC SUBJECT CODE : 2170710
150110107039
SUBJECT : MCWC SUBJECT CODE : 2170710
Practical-12
Aim: Develop simple android app to simulate View, Edit Text View,
Button View, Radio Button, Check Box, Image Button, Rating bar.
Fragment_main.xml
package com.example.jonit.lab3;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RatingBar;
import android.widget.Toast;
rb1=(RatingBar)findViewById(R.id.ratingBar2);
c1=(CheckBox)findViewById(R.id.checkBox);
c2=(CheckBox)findViewById(R.id.checkBox2);
150110107039
SUBJECT : MCWC SUBJECT CODE : 2170710
ib=(ImageButton)findViewById(R.id.imageButton2);
ib.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
System.exit(0);
}
});
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
createString();
if(R.id.rbll== rg.getCheckedRadioButtonId()) {
Toast.makeText(getApplicationContext(), str,
Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(getApplicationContext(), str,
Toast.LENGTH_SHORT).show();
}
}
});
str= e1.getText().toString();
if(c1.isChecked()){
str=str+" "+c1.getText();
}
if(c2.isChecked()){
str=str+" "+c2.getText();
}
str=str+" Rating:"+rb1.getRating()+"/5";
}
}
150110107039
SUBJECT : MCWC SUBJECT CODE : 2170710
Output
150110107039
SUBJECT : MCWC SUBJECT CODE : 2170710
Practical-14
Graphical Layout
Fragment_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
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.menu.MainActivity$PlaceholderFragment" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
a<TextView
android:id="@+id/t1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="User Name" />
<EditText
android:id="@+id/edtUser"
android:layout_width="wrap_content"
150110107064 AMIN
SUBJECT : MCWC SUBJECT CODE : 2170710
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textEmailAddress" >
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/t2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password" />
<EditText
android:id="@+id/edtPass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textPassword" >
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
</LinearLayout>
fragment_second.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
150110107064 AMIN
SUBJECT : MCWC SUBJECT CODE : 2170710
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.mainactivity.SecondActivity$PlaceholderFragment" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome " />
</RelativeLayout>
MainActivity.java
package com.example.mainactivity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
Button sub;
EditText eu,ep;
String user,pass;
TextView t1,t2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
sub=(Button)findViewById(R.id.button1);
eu=(EditText)findViewById(R.id.edtUser);
ep=(EditText)findViewById(R.id.edtPass);
t1=(TextView)findViewById(R.id.t1);
t2=(TextView)findViewById(R.id.t2);
150110107064 AMIN
SUBJECT : MCWC SUBJECT CODE : 2170710
sub.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
user=eu.getText().toString();
pass=ep.getText().toString();
Intent i=new
Intent(getApplicationContext(),SecondActivity.class);
startActivity(i);
}
else
{
Toast.makeText(getApplicationContext(), "Invalid
User"+user+pass, Toast.LENGTH_LONG).show();
}
}
});
}
}
Output
150110107064 AMIN
SUBJECT : MCWC SUBJECT CODE : 2170710
Practical: -15
Aim: - Develop simple calculator.
.XML
<layout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
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.sample.foo.samplecalculator.MainActivity">
<TextView
android:id="@+id/infoTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:textSize="30sp" />
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/infoTextView"
android:enabled="false"
android:gravity="bottom"
android:lines="2"
android:maxLines="2"
android:textSize="20sp" />
<Button
android:id="@+id/buttonSeven"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editText" android:text="7"
android:textSize="20sp" />
<Button
android:id="@+id/buttonEight"
150110107039
SUBJECT : MCWC SUBJECT CODE : 2170710
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editText"
android:layout_toRightOf="@id/buttonSeven"
android:text="8"
android:textSize="20sp" />
<Button
android:id="@+id/buttonNine"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editText"
android:layout_toRightOf="@id/buttonEight"
android:text="9"
android:textSize="20sp" />
...
...
<Button
android:id="@+id/buttonDot"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonOne"
android:text="."
android:textSize="20sp" />
<Button
android:id="@+id/buttonZero"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@id/buttonEight"
android:layout_below="@id/buttonTwo"
android:text="0"
android:textSize="20sp" />
<Button
android:id="@+id/buttonEqual"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
150110107039
SUBJECT : MCWC SUBJECT CODE : 2170710
android:layout_height="wrap_content"
android:layout_alignRight="@id/buttonNine"
android:layout_below="@id/buttonThree"
android:text="="
android:textSize="20sp" />
<Button
android:id="@+id/buttonDivide"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/buttonNine"
android:layout_toRightOf="@id/buttonNine"
android:text="/"
android:textSize="20sp" />
<Button
android:id="@+id/buttonMultiply"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/buttonSix"
android:layout_toRightOf="@id/buttonSix"
android:text="*"
android:textSize="20sp" />
<Button
android:id="@+id/buttonSubtract"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/buttonThree"
android:layout_toRightOf="@id/buttonThree"
android:text="-"
android:textSize="20sp" />
<Button
android:id="@+id/buttonAdd"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/buttonEqual"
android:layout_toRightOf="@id/buttonEqual"
android:text="+"
android:textSize="20sp" />
150110107039
SUBJECT : MCWC SUBJECT CODE : 2170710
<Button
android:id="@+id/buttonClear"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@id/buttonAdd"
android:layout_below="@id/buttonAdd"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:text="C"
android:textSize="20sp" />
</RelativeLayout>
</layout>
.JAVA
private double valueOne = Double.NaN;
private double valueTwo;
private static final char ADDITION = '+';
private static final char SUBTRACTION = '-';
private static final char MULTIPLICATION = '*';
private static final char DIVISION = '/';
if(CURRENT_ACTION == ADDITION)
valueOne = this.valueOne + valueTwo;
else if(CURRENT_ACTION == SUBTRACTION)
valueOne = this.valueOne - valueTwo;
else if(CURRENT_ACTION == MULTIPLICATION)
valueOne = this.valueOne * valueTwo;
else if(CURRENT_ACTION == DIVISION)
valueOne = this.valueOne / valueTwo;
}
else {
150110107039
SUBJECT : MCWC SUBJECT CODE : 2170710
try {
valueOne = Double.parseDouble(binding.editText.getText().toString());
}
catch (Exception e){}
}
}
binding.buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
computeCalculation();
CURRENT_ACTION = ADDITION;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "+");
binding.editText.setText(null);
}
});
binding.buttonSubtract.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
computeCalculation();
CURRENT_ACTION = SUBTRACTION;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "-");
binding.editText.setText(null);
}
});
binding.buttonMultiply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
computeCalculation();
CURRENT_ACTION = MULTIPLICATION;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "*");
binding.editText.setText(null);
}
});
binding.buttonDivide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
computeCalculation();
CURRENT_ACTION = DIVISION;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "/");
binding.editText.setText(null);
}
});
150110107039
SUBJECT : MCWC SUBJECT CODE : 2170710
binding.buttonEqual.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
computeCalculation();
binding.infoTextView.setText(binding.infoTextView.getText().toString() +
decimalFormat.format(valueTwo) + " = " +
decimalFormat.format(valueOne));
valueOne = Double.NaN;
CURRENT_ACTION = '0';
}
});
Output: -
150110107039
SUBJECT : MCWC SUBJECT CODE : 2170710
Practical – 16
Aim: To Study about ad hoc network and show its working
For file and printer sharing, all users need to be in the same workgroup, or if one com-
puter is joined to a domain, the other users have to have accounts on that computer in
order to access shared items.
Other limitations of ad hoc wireless networking include the lack of security and a
slow data rate. Ad hoc mode offers minimal security. If an attacker comes
within range of your ad hoc network, he won't have any trouble connecting.
Step 2: Run the following command to verify that your network interface supports
virtualization:
netsh wlan show drivers
150110107039
SUBJECT : MCWC SUBJECT CODE : 2170710
If Hosted network supported says Yes, you’re all set. Otherwise, you need to upgrade
your hardware, if the software update doesn’t fix it.
Step 3: Now, set up the ad hoc wireless network using this command. Replace the
parts in markup tags with your own choices
netsh wlan set hostednetwork mode=allow ssid=<enter_network_name_here>
key=<enter_password_here>
Step 4: Until now, your hosted network has been created. Now, you need to start it.
Use the command below:
netsh wlan start hostednetwork
150110107039
SUBJECT : MCWC SUBJECT CODE : 2170710
Step 5: You’re all set, with just one thing remaining. If it’s not already enabled, you
need to allow Internet Connection Sharing (ICS) for your currently-active internet
connection. Simply head over to Network & Sharing Center, and in the properties for
the current internet connection, enable ICS. Make sure to select the ad hoc
connection under Home networking connection.
150110107039
SUBJECT : MCWC SUBJECT CODE : 2170710
Now you can connect any device to the internet using the ad hoc wireless network
with internet connection sharing that you just created on your Windows 8 PC
150110107039