Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
S.no.
Experiment Name
Date
10
Application having
ImageButton.
11
Images
and
Signature
Program 1
Getting started with Android Development
Get Started
Everything you need to start developing apps for Android is available here on
developer.android.com. You'll find everything from the API documentation and design
guidelines, to information about the current device landscape and how you can
distribute and monetize your app.
No two apps are built in the same way, but we've structured the information you need
to build an app into the following three sections that represent the general order for app
development.
Design
Before you write a single line of code, you need to design the user interface and make
it fit the Android user experience. Although you may know what a user will do with
your app, you should pause to focus on how a user will interact with it. Your design
should be sleek, simple, powerful, and tailored to the Android experience.
So whether you're creating an app alone or you're part of a team, study the Design
guidelines first.
Develop
Once your design is finalized, all you need are the tools to turn your app ideas into
reality. Android's framework provides you the APIs to build apps that take full
advantage of device hardware, connected accessory devices, the Internet, software
features, and more. With the power of Android, there's no limit to the power of your
apps.
Everything you need to learn about the app framework and developer tools is in the
Develop documentation.
Distribute
Now your app is complete. You've built it to support a variety of screen sizes and
densities, and tested it on the Android emulator and on real devices. You're ready to
ship your app.
How you proceed depends on a variety of factors, such as your monetization strategy
and which types of devices your app supports. Everything you need to get started with
this process is available in the Distribute section.
Go!
Get started by installing Android Studiothe official IDE for Android development,
which includes the Android SDK tools. Then when you're ready to begin coding,
follow the training for Building Your First App.
Android Studio is a fairly new IDE (Integrated Development Environment) made
available for free by Google to Android developers. Android Studio is based on IntelliJ
IDEA, an IDE that also offers a good Android development environment. In this
tutorial, I'll show you how to create a new Android project and take advantage of the
features that Android Studio has to offer.
1. Project Setup
Before start exploring Android Studio, you'll first need to download and install it. Note
that you need to have JDK 6 or higher installed. If you're on Windows, launch the .exe
file and follow the steps of the setup wizard. If you're running OS X, mount the disk
image by double-clicking it and drag Android Studio to your Applications folder.
If you've successfully completed the above steps, then your development environment
should be set up correctly. You're now ready to create your first Android application
using Android Studio. When you launch Android Studio for the first time, you should
be presented with a welcome screen, offering you a number of choices to get you
started.
In this tutorial, we're going to choose the New Project option. However, you can
choose Import Project if you'd like to import a project from, for example, Eclipse,
into Android Studio. Android Studio will convert the Eclipse project to an Android
Studio project, adding the necessary configuration files for you.
If you select Open Project from the list of options, you can open projects created with
either Android Studio or IntelliJ IDEA. By choosing Check out from Version
Control, you can check out a copy of a project that's under version control. This is a
great way to quickly get up to speed with an existing project.
To get us started, choose New Project from the list of options. This will show you a
list of options to configure your new project. In this tutorial, we're going to create a
simple application to show you some of Android Studio's most important features. I'm
sure you agree that there's no better name for our project than HelloWorld.
As you can see in the above screenshot, I've named my application MyApplication. If
you're unfamiliar with IntelliJ IDEA, you may be wondering what a module is. A
module is a discrete unit of functionality that can be compiled, run, tested, and
debugged independently. Modules contain source code, build scripts, and everything
else required for their specific task.
When creating a new project, you can also set the package name of the project. By
default, Android Studio sets the last element of the project's package name to the name
of the module, but you can change it to whatever you want.
The other settings are the project's location on your machine, the minimum and target
SDK, the SDK your project will be compiled with, and the project's theme. You can
also tell Android Studio to create an Activity class and a custom launch icon for you,
and whether the project supports GridLayout, Fragments, a Navigation Drawer, or an
Action Bar.
Since we'll be starting with a blank Activity class, you can click Next to proceed to the
next step in the setup process in which you're asked to name the Activity class, the main
layout, and the fragment layout. You can also set the navigation type, which we'll leave
at None for this project. Take a look at the next screenshot to see what your settings
should look like.
After clicking Finish, you'll be presented with Android Studio's user interface with the
project explorer on the left and the workspace on the right. With your project set up in
Android Studio, it's time to explore some of the key features of Android Studio.
To create a New AVD, click on Create Virtual Device button in the center.
To use your newly created AVD, select it from the list in the AVD manager, and
click Start... on the right. If your AVD is set up correctly, the Android Emulator should
launch as shown in the screen shot below.
With the Android Emulator up and running, it's time to launch your application
by selecting Run 'helloworld' from the Run menu. That's how easy it is to run an
application in the Android Emulator.
Program 2
Activities and Views: Android Manifest.xml and
Activity Class
Activity Life cycle
Activities in the system are managed as an activity stack. When a new activity is
started, it is placed on the top of the stack and becomes the running activity -- the
previous activity always remains below it in the stack, and will not come to the
foreground again until the new activity exits.
An activity has essentially four states:
If an activity in the foreground of the screen (at the top of the stack), it is active
or running.
If an activity has lost focus but is still visible (that is, a new non-full-sized or
transparent activity has focus on top of your activity), it is paused. A paused
activity is completely alive (it maintains all state and member information and
remains attached to the window manager), but can be killed by the system in
extreme low memory situations.
If an activity is completely obscured by another activity, it is stopped. It still
retains all state and member information, however, it is no longer visible to the
user so its window is hidden and it will often be killed by the system when
memory is needed elsewhere.
If an activity is paused or stopped, the system can drop the activity from
memory by either asking it to finish, or simply killing its process. When it is
displayed again to the user, it must be completely restarted and restored to its
previous state.
The following diagram shows the important state paths of an Activity. The square
rectangles represent callback methods you can implement to perform operations when
the Activity moves between states. The colored ovals are major states the Activity can
be in.
There are three key loops you may be interested in monitoring within your activity:
onStart() to monitor for changes that impact your UI, and unregister it in
onStop() when the user no longer sees what you are displaying. The onStart()
and onStop() methods can be called multiple times, as the activity becomes
visible and hidden to the user.
The foreground lifetime of an activity happens between a call to onResume()
until a corresponding call to onPause(). During this time the activity is in front of
all other activities and interacting with the user. An activity can frequently go
between the resumed and paused states -- for example when the device goes to
sleep, when an activity result is delivered, when a new intent is delivered -- so
the code in these methods should be fairly lightweight.
The entire lifecycle of an activity is defined by the following Activity methods. All of
these are hooks that you can override to do appropriate work when the activity changes
state. All activities will implement onCreate(Bundle) to do their initial setup; many will
also implement onPause() to commit changes to data and otherwise prepare to stop
interacting with the user. You should always call up to your superclass when
implementing these methods.
AndroidManifest.xml
Every application must have an AndroidManifest.xml file (with precisely that name) in
its root directory. The manifest file presents essential information about your app to the
Android system, information the system must have before it can run any of the app's
code. Among other things, the manifest does the following:
It names the Java package for the application. The package name serves as a
unique identifier for the application.
It describes the components of the application the activities, services,
broadcast receivers, and content providers that the application is composed of.
It names the classes that implement each of the components and publishes their
capabilities (for example, which Intent messages they can handle). These
declarations let the Android system know what the components are and under
what conditions they can be launched.
It determines which processes will host application components.
It declares which permissions the application must have in order to access
protected parts of the API and interact with other applications.
It also declares the permissions that others are required to have in order to
interact with the application's components.
It lists the Instrumentation classes that provide profiling and other information as
the application is running. These declarations are present in the manifest only
while the application is being developed and tested; they're removed before the
application is published.
It declares the minimum level of the Android API that the application requires.
It lists the libraries that the application must be linked against.
Program 3
Application to print Hello World
MainActivity.java
package com.divij.helloworld;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activity_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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Hello World"
android:textSize="50dp"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="137dp" />
</RelativeLayout>
Output
Program 4
Application for adding two numbers
MainActivity.java
package com.divij.myapplication;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
EditText edtxt1,edtxt2;
Button btn;
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtxt1 = (EditText) findViewById(R.id.edt);
edtxt2 = (EditText) findViewById(R.id.edt2);
btn = (Button) findViewById(R.id.button);
txt = (TextView) findViewById(R.id.textView);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int a = Integer.parseInt(edtxt1.getText().toString());
int b = Integer.parseInt(edtxt2.getText().toString());
int sum = a + b;
edtxt1.setText("");
edtxt2.setText("");
txt.setText("The sum is " + sum);
}
});
}
}
activity_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">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edt"
android:hint="enter first number"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="79dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edt2"
android:hint="enter second number"
android:layout_below="@+id/editText"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sum"
android:id="@+id/button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text=""
android:id="@+id/textView"
android:layout_below="@+id/button"
android:layout_centerHorizontal="true"
android:layout_marginTop="60dp" />
</RelativeLayout>
Output
Program 5
});
activity_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">
<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_marginLeft="60dp"
android:layout_marginTop="75dp"
android:text="how are you?"
android:textAppearance="?android:attr/textAppearanceLarge" />
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:text="good" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/radioButton1"
android:layout_below="@+id/radioButton1"
android:text="bad" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/radioButton1"
android:layout_below="@+id/textView2"
android:text="CSE" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/checkBox1"
android:layout_below="@+id/checkBox1"
android:text="EEE" />
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/checkBox1"
android:layout_alignBottom="@+id/checkBox1"
android:layout_toRightOf="@+id/checkBox1"
android:text="ME" />
<CheckBox
android:id="@+id/checkBox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/checkBox3"
android:layout_below="@+id/checkBox3"
android:text="ECE" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/radioButton2"
android:layout_centerVertical="true"
android:text="enter your stream"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Output
Program 6
Application having a toast and a custom toast
MainActivity.java
package com.divij.myapplication;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
Button btn1, btn2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.button);
btn2 = (Button) findViewById(R.id.button2);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "I am a Simple Toast.",
Toast.LENGTH_SHORT).show();
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LayoutInflater myInflater = getLayoutInflater();
View view = myInflater.inflate(R.layout.toast_layout, null);
ImageView img = (ImageView)
view.findViewById(R.id.imageView);
img.setImageResource(R.drawable.ditm);
TextView txt = (TextView) view.findViewById(R.id.textView);
});
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Toast"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="79dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Custom Toast"
android:id="@+id/button2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
toast_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="@drawable/toast_border"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ditm"
android:id="@+id/imageView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/textView"
android:textColor="#fff"
android:layout_below="@+id/imageView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
toast_border.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="1dp" android:color="#3d3d3d" />
<padding android:left="7dp" android:top="7dp"
android:right="7dp" android:bottom="7dp" />
<corners android:radius="0dp" />
<gradient android:startColor="#626262"
android:endColor="#242424"
android:angle="-90"/>
</shape>
Output
Program 7
Application that sends message from one activity
to another activity
MainActivity.java
package com.divij.sendmsgtootheractivity;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
EditText edtxt;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtxt = (EditText) findViewById(R.id.editText);
btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String string = edtxt.getText().toString();
Intent intent = new Intent(MainActivity.this, NextActivity.class);
intent.putExtra("msg", string);
startActivity(intent);
}
});
}
activity_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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:hint="Type your message"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="96dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
android:id="@+id/button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
NextActivity.java
package com.divij.sendmsgtootheractivity;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class NextActivity extends Activity {
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_next);
txt = (TextView) findViewById(R.id.textView);
String s1 = getIntent().getExtras().getString("msg");
txt.setText(s1 + "");
}
activity_next.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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="92dp" />
</RelativeLayout>
Output
Program 8
Application having list
MainActivity.java
package com.divij.list;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
ListView list;
String subjects[] = {"Java", "Html", "JavaScript", "Php", "C++", "Python",
"MySql", "JQuery", "Ruby", "Xml"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.listView);
ArrayAdapter<String> adapter = new
ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1,
subjects);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int
position, long id) {
Toast.makeText(MainActivity.this, "You Choose " +
subjects[position], Toast.LENGTH_SHORT).show();
}
});
activity_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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
Output
Program 9
Application having AlertDialog Box
MainActivity.java
package com.example.divij.alertdialogbox;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.preference.DialogPreference;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new
AlertDialog.Builder(MainActivity.this);
builder.setMessage("Do you want to close this Application?")
.setCancelable(false).setPositiveButton("Yes", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int
which) {
dialog.cancel();
}
});
});
activity_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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show Alert Dialog Box"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="120dp" />
</RelativeLayout>
Output
Program 11
Application having Image and Image Button
MainActivity.java
package com.divij.imagebutton;
import android.app.Activity;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
ImageView img;
ImageButton imgBtn;
Boolean flag = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageView) findViewById(R.id.imageView);
imgBtn = (ImageButton) findViewById(R.id.imageButton);
imgBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// img.setImageResource(R.drawable.ditm);
if(flag==false)
{
Toast.makeText(MainActivity.this, "ImageButton is
Working", Toast.LENGTH_SHORT).show();
imgBtn.setImageResource(R.drawable.logo);
img.setImageResource(R.drawable.ditm);
flag=true;
}
else
{
Toast.makeText(MainActivity.this, "ImageButton is
Working", Toast.LENGTH_SHORT).show();
});
imgBtn.setImageResource(R.drawable.ditm);
img.setImageResource(R.drawable.logo);
flag=false;
activity_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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/logo"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="87dp" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton"
android:src="@drawable/ditm"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="94dp" />
</RelativeLayout>
Output
Program 11
Application having database
MainActivity.java
package com.divij.databasedemo;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
Button btn1,btn2;
EditText ed1,ed2;
@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);
ed1 = (EditText) findViewById(R.id.editText1);
ed2 = (EditText) findViewById(R.id.editText2);
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String name = ed1.getText().toString();
String subject = ed2.getText().toString();
ContentValues value = new ContentValues();
value.put("Name", name);
value.put("Subject", subject);
DatabaseHelper db = new DatabaseHelper(MainActivity.this);
db.open();
db.InsertData("student", value);
db.close();
ed1.setText("");
ed2.setText("");
saved.",Toast.LENGTH_LONG).show();
});
btn2.setOnClickListener(new OnClickListener() {
});
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent in = new Intent(MainActivity.this,DataActivity.class);
startActivity(in);
}
DataActivity.java
package com.divijl.database;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
public class DataActivity extends Activity {
ListView li;
Cursor c;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_list);
li = (ListView) findViewById(R.id.list);
DatabaseHelper db = new DatabaseHelper(DataActivity.this);
db.open();
c = db.getData("student");
li.setAdapter(new MyAdapter(DataActivity.this,c));
DatabaseHelper.java
package com.divij.database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper {
MainHelperInner helper;
String dbName = "Mydata.db";
SQLiteDatabase db;
public DatabaseHelper(Context context){
helper = new MainHelperInner(context,dbName,null,1);
}
public DatabaseHelper open(){
db = helper.getWritableDatabase();
return this;
}
public void close(){
db.close();
}
public long InsertData(String table,ContentValues value){
return db.insert(table,null,value);
}
public Cursor getData(String table){
return db.query(table,null,null,null,null,null,null);
//return db.rawQuery("Select * from" + table, null);
}
public void DeleteData(String table, String value){
//db.delete();
db.execSQL("delete from" + table + "where Name = " + value);
}
class MainHelperInner extends SQLiteOpenHelper{
public MainHelperInner(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
null);");
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("create table 'student'(Name text not null,Subject text not
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
onCreate(db);
}
}
}
MyAdapter.java
package com.divij.database;
import android.app.Activity;
import android.database.Cursor;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter{
Activity myactivity;
Cursor mycursor;
public MyAdapter(Activity activity, Cursor cursor){
myactivity = activity;
mycursor = cursor;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mycursor.getCount();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = myactivity.getLayoutInflater().inflate(R.layout.activity_data, null);
mycursor.moveToPosition(position);
TextView txt1 = (TextView) v.findViewById(R.id.textView1);
txt1.setText(mycursor.getString(mycursor.getColumnIndex("Name")));
TextView txt2 = (TextView) v.findViewById(R.id.textView2);
txt2.setText(mycursor.getString(mycursor.getColumnIndex("Subject")));
return v;
}
activity_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" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="85dp"
android:ems="10"
android:hint="Enter the name" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="34dp"
android:ems="10"
android:hint="Enter the subject" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_below="@+id/editText2"
android:layout_marginTop="60dp"
android:text="Save" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button1"
android:layout_alignRight="@+id/editText2"
android:text="View" />
</RelativeLayout>
activity_data.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" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="19dp"
android:layout_marginTop="14dp"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imageView1"
android:layout_centerHorizontal="true"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
fragment_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/list">
</ListView>
</RelativeLayout>
Output