You are on page 1of 42

Pratical List Android

 Pratical : 1 – Installation and setup of java development kit(JDK),setup android SDK,setup eclipse
IDE,setup android development tools (ADT) plugins,create android virtual device
 Let's see the list of software required to setup android for eclipse IDE manually.
 Install the JDK
 For creating android application, JDK must be installed if you are developing the android
application with Java language. download the JDK
 Download and install the Eclipse for developing android application
 For developing the android application using eclipse IDE, you need to install the Eclipse. you can
download it from this location download the Eclipse. Eclipse classic version is recommended but
we are using the Eclipse IDE for JavaEE Developers.
 Download and Install the android SDK
 First of all, download the android SDK. In this example we have installed the android SDK for
windows (.exe version).
 Now double click on the exe file, it will be installed. I am using the android 2.2 version here.
 Intall the ADT plugin for eclipse
 ADT (Android Development Tools) is required for developing the android application in the
eclipse IDE. It is the plugin for Eclipse IDE that is designed to provide the integrated
environment.
 For downloading the ADT, you need to follow these steps:
 Start the eclipse IDE, then select Help > Install new software...
 In the work with combo box, write https://dl-ssl.google.com/android/eclipse/
 select the checkbox next to Developer Tools and click next
 You will see, a list of tools to be downloaded here, click next
 click finish
 After completing the installation, restart the eclipse IDE
 Configure the ADT plugin
 After the installing ADT plugin, now tell the eclipse IDE for your android SDK location. To do so:
 Select the Window menu > preferences
 Now select the android from the left panel. Here you may see a dialog box asking if you want
to send the statistics to the google. Click proceed.
 Click on the browse button and locate your SDK directory e.g. my SDK location is C:\Program
Files\Android\android-sdk.
 Click the apply button then OK.
 Create the AVD
 For running the android application in the Android Emulator, you need to create and AVD. For
creating the AVD:
 Select the Window menu > AVD Manager
 Click on the new button, to create the AVD
 Now a dialog appears, write the AVD name e.g. myavd. Now choose the target android
version e.g. android2.2.
 click the create AVD
 Create the hello android application
 Pratical : 2- Create “Hello World” application. That will display “Hello World” in the middle of the screen
using TextView Widget in the red color
 Example 1
 Step 1: (activity_main.xml)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rl1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world"
tools:context=".MainActivity" />
</RelativeLayout>
 Step 2: (MainActivity.java)
package com.example.helloexample;

import android.os.Bundle;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.app.Activity;
import android.graphics.Color;

public class MainActivity extends Activity {


RelativeLayout rl1;
TextView tv1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rl1=(RelativeLayout)findViewById(R.id.rl1);
tv1=(TextView)findViewById(R.id.tv1);
rl1.setBackgroundColor(Color.WHITE);
tv1.setBackgroundColor(Color.RED);
}
}
 Pratical:3- Create application for demonstration of android activity life cycle
 STEP 1(MainActivity.java)
package com.example.lifecycle;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
public class MainActivity extends Activity
{
String abc=null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(abc, "oncreate");
}
protected void onStart()
{
super.onStart();
Log.d(abc, "OnStart");
}
protected void onResume()
{
super.onStart();
Log.d(abc, "OnResume");
}
protected void onPause()
{
super.onStart();
Log.d(abc, "OnPause");
}
protected void onStop()
{
super.onStop();
Log.d(abc, "OnStop");
}
protected void onDestroy()
{
super.onDestroy();
Log.d(abc, "OnDestroy");
}
}
 
Pratical:3- Create application for demonstration of android activity life cycle
 STEP 1(MainActivity.java)
package com.example.lifecycle;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.widget.Toast;
public class MainActivity extends Activity
{
String abc=null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(getBaseContext(), "Run APPS", Toast.LENGTH_SHORT).show();
}
protected void onStart()
{
super.onStart();
Toast.makeText(getBaseContext(), "START", Toast.LENGTH_SHORT).show();
}
protected void onResume()
{
super.onResume();
Toast.makeText(getBaseContext(), "RESUME", Toast.LENGTH_SHORT).show();
}
protected void onPause()
{
super.onPause();
Toast.makeText(getBaseContext(), "PAUSE", Toast.LENGTH_SHORT).show();
}
protected void onStop()
{
super.onStop();
Toast.makeText(getBaseContext(), "STOP", Toast.LENGTH_SHORT).show();
}
protected void onDestroy()
{
super.onDestroy();
Toast.makeText(getBaseContext(), "DESTROY", Toast.LENGTH_SHORT).show();
}
}
 Pratical: 4- Create Registration page to demonstration of Basic widgets available in android.
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/TableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Registration" />
</TableRow>
<TableRow>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name:-" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="name" >
<requestFocus />
</EditText>
</TableRow>
<TableRow>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mobile No:-" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="mobile" />
</TableRow>
<TableRow>
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="53dp"
android:text="Gender" />
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Female" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male" />
</RadioGroup>
</TableRow>
<TableRow>
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hobbies" />
</TableRow>
<TableRow>
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Singing" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yoga" />
</TableRow>
<TableRow>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="submit"
android:text="Submit" />
</TableRow>
</TableLayout>
 Pratical: 5 -Create sample application with login module.(Check username and password) On successful
login, Chnage TextView “Login Sucessful”. And on failing login, alert user using Toast “Login fail”
 Step 1: (activity_main.xml)
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/TableLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1" >
<TableRow>
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="User Name: " >
</TextView>
<EditText
android:id="@+id/txtUname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="" >
</EditText>
</TableRow>
<TableRow>
<TextView
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password: " >
</TextView>
<EditText
android:id="@+id/txtPwd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:password="true"
android:text="" >
</EditText>
</TableRow>
<TableRow>
<Button
android:id="@+id/btnCancel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Cancel" >
</Button>
<Button
android:id="@+id/btnLogin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Login" >
</Button>
</TableRow>
</TableLayout>
 Step 2: (MainActivity.java)
package com.example.loginrequestexample;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText txtUserName;
EditText txtPassword;
Button btnLogin;
Button btnCancel;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtUserName=(EditText)this.findViewById(R.id.txtUname);
txtPassword=(EditText)this.findViewById(R.id.txtPwd);
btnLogin=(Button)this.findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
if((txtUserName.getText().toString()).equals(txtPassword.getText().toString()))
{
Toast.makeText(MainActivity.this, "Login
Successful",Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(MainActivity.this, "Invalid
Login",Toast.LENGTH_LONG).show();
}
}
});
}}
 Pratical : 6-Create login application where you will have to validate EmailID (UserName) . Till the user
name and password is not validated, login button should remain disabled.
 Step 1: (activity_main.xml)
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/TableLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1" >
<TableRow>

<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="User Name: " >
</TextView>
<EditText
android:id="@+id/txtUname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="" >
</EditText>
</TableRow>
<TableRow>
<TextView
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password: " >
</TextView>
<EditText
android:id="@+id/txtPwd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:password="true"
android:text="" >
</EditText>
</TableRow>
<TableRow>
<Button
android:id="@+id/btnClear"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Clear" >
</Button>
<Button
android:id="@+id/btnLogin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Login" >
</Button>
</TableRow>
</TableLayout>

 Step 2: (MainActivity.java)
package com.example.disablebuttonlogin;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity
{
String uname="abc@gmail.com";
String pwd="rahul";
EditText txtUsr,txtPwd;
Button btnLogin,btnClear;
boolean usrStatus=false;
boolean pwdStatus=false;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnLogin= (Button) findViewById(R.id.btnLogin);
txtUsr = (EditText)findViewById(R.id.txtUname);
txtPwd = (EditText)findViewById(R.id.txtPwd);
btnLogin.setEnabled(false);
btnLogin.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
Toast.makeText(getBaseContext(), "Valid UserName And Password",
Toast.LENGTH_LONG).show();
}
});
final Button btnClear = (Button)findViewById(R.id.btnClear);
btnClear.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
txtUsr.setText("");
txtPwd.setText("");
}
});

txtUsr.addTextChangedListener(new TextWatcher()
{

public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
{
if(uname.equals(txtUsr.getText().toString()))
{
usrStatus=true;
}
else
{
usrStatus=false;
}
}
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,int arg3)
{
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable arg0)
{

if(usrStatus && pwdStatus)


{
btnLogin.setEnabled(true);
}
else
{
btnLogin.setEnabled(false);
}

}
});
txtPwd.addTextChangedListener(new TextWatcher()
{
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
{
// TODO Auto-generated method stub
if(pwd.equals(txtPwd.getText().toString()))
{
pwdStatus=true;
}
else
{
pwdStatus=false;
}
}
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,int arg3)
{

}
public void afterTextChanged(Editable arg0)
{
if(usrStatus && pwdStatus)
{
btnLogin.setEnabled(true);
}
else
{
btnLogin.setEnabled(false);
}
}
});
}
}
 Pratical : 7 - Create and Login application as above. Validate login data and display Error to user using
setError() method.
 Step 1: (activity_main.xml)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="REGISTER USER"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#176CEC"
android:textStyle="bold" />
<EditText
android:id="@+id/editText_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:ems="10"
android:hint="YOUR EMAIL"
android:inputType="textEmailAddress"
android:padding="12dp" />

<EditText
android:id="@+id/editText_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="1dp"
android:background="#fff"
android:ems="10"
android:hint="ENTER PASSWORD"
android:inputType="textPassword"
android:padding="12dp" />
<Button
android:id="@+id/btn_signup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="#176CEC"
android:text="SIGN UP"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#fff"
android:textStyle="bold" />
</LinearLayout>
 Step 2: (MainActivity.java)
package com.example.edittextvalidation;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
public class MainActivity extends Activity
{
private EditText emailEditText;
private EditText passEditText;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
emailEditText = (EditText) findViewById(R.id.editText_email);
passEditText = (EditText) findViewById(R.id.editText_password);
findViewById(R.id.btn_signup).setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
final String email = emailEditText.getText().toString();
if (!isValidEmail(email))
{
emailEditText.setError("Invalid Email");
}

final String pass = passEditText.getText().toString();


if (!isValidPassword(pass))
{
passEditText.setError("Invalid Password");
}

}
});
}
private boolean isValidEmail(String email)
{
String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
Pattern p1 = Pattern.compile(EMAIL_PATTERN);
Matcher m1 = p1.matcher(email);
return m1.matches();
}

// validating password with retype password


private boolean isValidPassword(String pass)
{
if (pass != null && pass.length() > 6)
{
return true;
}
return false;
}
}
 Pratical: 8 -Create an application for demonstration of Relative and Table Layout in android
 Relative Example(activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<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_marginTop="15dp"
android:text="User :"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_marginLeft="14dp"
android:layout_toRightOf="@+id/textView1"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="17dp"
android:text="Submit" />
</RelativeLayout>

 Table Layout Example(activity_main.xml)


<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<!-- 2 columns -->
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip" >
<TextView
android:id="@+id/textView1"
android:text="Column 1"
android:textAppearance="?android:attr/textAppearanceLarge" />

<Button
android:id="@+id/button1"
android:text="Column 2" />
</TableRow>
<View
android:layout_height="2dip"
android:background="#FF0000" />
<!-- edittext span 2 column -->
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip" >

<EditText
android:id="@+id/editText1"
android:layout_span="2"
android:text="Column 1 & 2" />
</TableRow>
<View
android:layout_height="2dip"
android:background="#FF0000" />
<!-- display this button in 3rd column via layout_column(zero based) -->
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip" >
<Button
android:id="@+id/button4"
android:layout_column="2"
android:text="Column 3" />
</TableRow>
</TableLayout>
 Pratical: 9 -Create an application for demonstration of Relative and Table Layout in android
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:text="Button 1"
/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:text="Button 2"
/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:text="Button 3"
/>
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:text="Button 4"
/>
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:text="Button 5"
/>
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:text="Button 6"
/>
<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:text="Button 7"
/>
<Button
android:id="@+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:text="Button 8"
/>
<Button
android:id="@+id/button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:text="Button 9"
/>
<Button
android:id="@+id/button10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:text="Button 10"
/>
</LinearLayout>
</ScrollView>
 Pratical: 10 -Create an application for demonstration of Explicitly Starting New Activity using Intent.
 Step:-1(Androidmanifest.xml)
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.eexample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:label="@string/title_activity_main"
></activity>
</application>
</manifest>

 Step:-2(activity_main.xml)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="perform"
android:text="Click" />
</LinearLayout>

 Step:-3(second.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Helloword" />
</LinearLayout>

 Step:-4(MainActivity.java)
package com.example.eexample;
import android.os.Bundle;
import android.view.View;
import android.app.Activity;
import android.content.Intent;
public class MainActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void perform(View v)
{
Intent i1=new Intent(MainActivity.this,SecondActivity.class);
startActivity(i1);
}
}

 Step:-5(SecondActivity.java)
package com.example.eexample;
import android.app.Activity;
import android.os.Bundle;
public class SecondActivity extends Activity
{
public void onCreate(Bundle b1)
{
super.onCreate(b1);
setContentView(R.layout.second);
}
}
 Pratical :11 - Create an application that will pass two number using TextView to the next screen , and on
the next screen display sum of that number
 Step:-1(Androidmanifest.xml)
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.impexample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SActivity"></activity>
</application>
</manifest>
 Step:-2(activity_main.xml)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/et1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter the Number 1" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/et2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter the Number 2" />
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="Perform"
android:text="Sum" />
</LinearLayout>
 Step:-3(second.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Number1" />

<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Number2" />

<TextView
android:id="@+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer" />

</LinearLayout>
 Step:-4(MainActivity.java)
package com.example.impexample;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.app.Activity;
import android.content.Intent;

public class MainActivity extends Activity {


EditText et1,et2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void Perform(View v)
{
et1=(EditText)findViewById(R.id.et1);
et2=(EditText)findViewById(R.id.et2);
String s1,s2;
s1=et1.getText().toString();
s2=et2.getText().toString();
Intent i1=new Intent(MainActivity.this,SActivity.class);
i1.putExtra("no1", s1);
i1.putExtra("no2", s2);
startActivity(i1);

}
}
 Step:-5(SActivity.java)
package com.example.impexample;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class SActivity extends Activity


{
TextView tv1,tv2,tv3;
public void onCreate(Bundle b1)
{
super.onCreate(b1);
setContentView(R.layout.second);
tv1=(TextView)findViewById(R.id.tv1);
tv2=(TextView)findViewById(R.id.tv2);
tv3=(TextView)findViewById(R.id.tv3);
String s2= getIntent().getStringExtra("no1").toString();
String s3= getIntent().getStringExtra("no2").toString();
tv1.setText(s2);
tv2.setText(s3);
int a,b,c;
a=Integer.parseInt(s2);
b=Integer.parseInt(s3);
c=a+b;
tv3.setText(String.valueOf(c));
}
}
 Pratical :12 - Create spinner with strings taken from resource folder(res >> value folder). On changing
spinner value, change background of screen.
 Step:-1(String.xml)
<resources>
<string name="app_name">ChangeColor</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
<string name="color_prompt">Choose a Color</string>
<string-array name="color_arrays">
<item>Red</item>
<item>Blue</item>
<item>Green</item>
</string-array>
</resources>

 Step:-2(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:id="@+id/main_layout">
<Spinner
android:id="@+id/spinner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:entries="@array/color_arrays"
android:prompt="@string/color_prompt"
/>
</RelativeLayout>
 Step:-3(MainActivity.java)
package com.example.changecolor;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.app.Activity;
import android.graphics.Color;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Spinner;
public class MainActivity extends Activity implements OnItemSelectedListener
{
Spinner spin;
RelativeLayout rl;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spin=(Spinner) findViewById(R.id.spinner1);
rl = (RelativeLayout) findViewById(R.id.main_layout);
spin.setOnItemSelectedListener(this);
}
public void onItemSelected(AdapterView arg0, View arg1, int arg2,long arg3)
{
// TODO Auto-generated method stub
String name=spin.getSelectedItem().toString();
if(name.equals("Red"))
rl.setBackgroundColor(Color.RED);
else if(name.equals("Blue"))
rl.setBackgroundColor(Color.BLUE);
else if(name.equals("Green"))
rl.setBackgroundColor(Color.GREEN);
}
public void onNothingSelected(AdapterView arg0)
{
// TODO Auto-generated method stub
}
}
 Pratical: 13 -Create an application that will get the Text Entered in Edit Text and display that Text using
toast (Message).
 Step:-1(activity_main.xml)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EditText Event Example" />
<EditText
android:id="@+id/et1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter Your Name" >
<requestFocus />
</EditText>
</LinearLayout>
 Step:-2(MainActivity.java)
package com.example.pthirteenexample;

import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {


EditText et1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1=(EditText)findViewById(R.id.et1);
et1.addTextChangedListener(new TextWatcher() {

public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
String s=et1.getText().toString();
Toast.makeText(getBaseContext(), s, Toast.LENGTH_SHORT).show();
}

public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,


int arg3) {
// TODO Auto-generated method stub

public void afterTextChanged(Editable arg0) {


// TODO Auto-generated method stub

}
});
}
}
 Pratical: 14 -Create an application that will Demonstrate Button onClick() Event and change the TextView
Color based on button Clicked
 step:-1(activity_main.xml)
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/AbsoluteLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="37dp"
android:layout_y="52dp"
android:text="Red" />
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="37dp"
android:layout_y="21dp"
android:text="Text View Change the BackGround Color" />
<Button
android:id="@+id/bt3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="212dp"
android:layout_y="51dp"
android:text="Green" />

<Button
android:id="@+id/bt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="120dp"
android:layout_y="54dp"
android:text="Blue" />
</AbsoluteLayout>
 Step:-2(Mainactivity.java)
package com.example.pfourteenexample;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;
import android.graphics.Color;

public class MainActivity extends Activity implements OnClickListener


{
TextView tv1;
Button bt1,bt2,bt3;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1=(TextView)findViewById(R.id.tv1);
bt1=(Button)findViewById(R.id.bt1);
bt2=(Button)findViewById(R.id.bt2);
bt3=(Button)findViewById(R.id.bt3);
bt1.setOnClickListener(this);
bt2.setOnClickListener(this);
bt3.setOnClickListener(this);

}
public void onClick(View v)
{
switch(v.getId())
{
case R.id.bt1:
tv1.setBackgroundColor(Color.RED);
tv1.setTextColor(Color.CYAN);
break;
case R.id.bt2:
tv1.setBackgroundColor(Color.BLUE);
tv1.setTextColor(Color.WHITE);
break;
case R.id.bt3:
tv1.setBackgroundColor(Color.GREEN);
tv1.setTextColor(Color.MAGENTA);
break;
default:
tv1.setBackgroundColor(Color.BLACK);
break;
}
}
}
 Pratical: 15 -Create an UI such that, one screen have list of all the types of cars. On selecting of any car
name, next screen should show Car details like: name, launched date, company name
 step:-1(string.xml)
<resources>
<string name="app_name">carproject</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">Car</string>
<string-array name="car">
<item>alto</item>
<item>hundi</item>
<item>oodi</item>
</string-array>
</resources>

 step:-2(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" >
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:entries="@array/car"/>

</RelativeLayout>

 Step:-3(Mainactivity.java)

package com.example.carproject;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
public class MainActivity extends Activity implements OnItemClickListener {
ListView lv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(ListView) findViewById(R.id.listView1);
lv.setOnItemClickListener(this);
}
public void onItemClick(AdapterView arg0, View arg1, int pos, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(this, pos + " click", Toast.LENGTH_SHORT).show();

Intent myintent=new Intent(this,Details.class);


myintent.putExtra("id", pos);
startActivity(myintent);
}
}

 step:-4(details.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textViewname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/textViewdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/textViewcompanyname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>

 step:-5(DetailsActivity.java)
package com.example.carproject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class DetailsActivity extends Activity
{
TextView tvname,tvdate,tvcompanyname;
int id=0;
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.details);
tvname=(TextView) findViewById(R.id.textViewname);
tvdate=(TextView) findViewById(R.id.textViewdate);
tvcompanyname=(TextView) findViewById(R.id.textViewcompanyname);
Intent myintent=getIntent();
id=myintent.getIntExtra("id", 0);
if(id==0)
{
tvname.setText("maruti suzuki");
tvdate.setText("1-12-1987");
tvcompanyname.setText("Maruti");
}
if(id==1)
{
tvname.setText("hundai i10");
tvdate.setText("2-12-1987");
tvcompanyname.setText("Hundai");
}
if(id==2)
{
tvname.setText("oodi");
tvdate.setText("1-10-1993");
tvcompanyname.setText("Oodi");
}
}
}

 step:-6(Androidmanifest.xml)
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.carproject"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Details">
</activity>
</application>
</manifest>
 Pratical: 16 -Create an application that will Demonstrate Dialog Box Control In Android
 Step 1: (activity_main.xml)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
android:id="@+id/btnone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Show Alert With Three Button" />

</LinearLayout>
 Step 2: (MainActivity.java)
package com.example.AlertDialogexample;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity


{
private Button b1;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
private void addListenerOnButton()
{
b1 = (Button)findViewById(R.id.btnone);
b1.setOnClickListener(new OnClickListener()
{
public void onClick(View view)
{
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
alertDialog.setTitle("Save File...");
alertDialog.setMessage("Do you want to save this file?");
alertDialog.setIcon(R.drawable.save);
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
Toast.makeText(getApplicationContext(), "You clicked on
YES",Toast.LENGTH_SHORT).show();
}
});
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
Toast.makeText(getApplicationContext(), "You clicked on NO",
Toast.LENGTH_SHORT).show();
}
});
alertDialog.setNeutralButton("Cancel", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
Toast.makeText(getApplicationContext(), "You clicked on
Cancel",Toast.LENGTH_SHORT).show();
}
});
alertDialog.show();
}
});
}
}
 Pratical 17:- To Create an android application that will perform insert, update, delete andselectoperation
on Student table using SQLite Database
 Step:1 (String.xml)
<resources>
<string name="app_name">sqlex</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
<string name="title">Student Details</string>
<string name="roll_no">Enter Rollno: </string>
<string name="name">Enter Name: </string>
<string name="marks">Enter Marks: </string>
<string name="add">Add</string>
<string name="delete">Delete</string>
<string name="modify">Modify</string>
<string name="view">View</string>
<string name="view_all">View All</string>
<string name="show_info">Show Information</string>
</resources>

 Step:2 (activity_main.xml)
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myLayout"
android:stretchColumns="0"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:text="@string/title"
android:layout_x="110dp"
android:layout_y="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:text="@string/roll_no"
android:layout_x="30dp"
android:layout_y="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText android:id="@+id/editRollno"
android:inputType="number"
android:layout_x="150dp"
android:layout_y="50dp"
android:layout_width="150dp"
android:layout_height="40dp"/>
<TextView android:text="@string/name"
android:layout_x="30dp"
android:layout_y="100dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText android:id="@+id/editName"
android:inputType="text"
android:layout_x="150dp"
android:layout_y="100dp"
android:layout_width="150dp"
android:layout_height="40dp"/>
<TextView android:text="@string/marks"
android:layout_x="30dp"
android:layout_y="150dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText android:id="@+id/editMarks"
android:inputType="number"
android:layout_x="150dp"
android:layout_y="150dp"
android:layout_width="150dp"
android:layout_height="40dp"/>
<Button android:id="@+id/btnAdd"
android:text="@string/add"
android:layout_x="30dp"
android:layout_y="200dp"
android:layout_width="100dp"
android:layout_height="40dp"/>
<Button android:id="@+id/btnDelete"
android:text="@string/delete"
android:layout_x="150dp"
android:layout_y="200dp"
android:layout_width="100dp"
android:layout_height="40dp"/>n
<Button android:id="@+id/btnModify"
android:text="@string/modify"
android:layout_x="30dp"
android:layout_y="250dp"
android:layout_width="100dp"
android:layout_height="40dp"/>
<Button android:id="@+id/btnView"
android:text="@string/view"
android:layout_x="150dp"
android:layout_y="250dp"
android:layout_width="100dp"
android:layout_height="40dp"/>
<Button android:id="@+id/btnViewAll"
android:text="@string/view_all"
android:layout_x="30dp"
android:layout_y="300dp"
android:layout_width="100dp"
android:layout_height="40dp"/>
<Button
android:id="@+id/btnShowInfo"
android:layout_width="186dp"
android:layout_height="wrap_content"
android:layout_x="80dp"
android:layout_y="352dp"
android:text="@string/show_info" />

</AbsoluteLayout>

 Step:3 (MainActivity.java)
package com.example.sqlex;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity implements OnClickListener


{
EditText editRollno,editName,editMarks;
Button btnAdd,btnDelete,btnModify,btnView,btnViewAll,btnShowInfo;
SQLiteDatabase db;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editRollno=(EditText)findViewById(R.id.editRollno);
editName=(EditText)findViewById(R.id.editName);
editMarks=(EditText)findViewById(R.id.editMarks);
btnAdd=(Button)findViewById(R.id.btnAdd);
btnDelete=(Button)findViewById(R.id.btnDelete);
btnModify=(Button)findViewById(R.id.btnModify);
btnView=(Button)findViewById(R.id.btnView);
btnViewAll=(Button)findViewById(R.id.btnViewAll);
btnShowInfo=(Button)findViewById(R.id.btnShowInfo);
btnAdd.setOnClickListener(this);
btnDelete.setOnClickListener(this);
btnModify.setOnClickListener(this);
btnView.setOnClickListener(this);
btnViewAll.setOnClickListener(this);
btnShowInfo.setOnClickListener(this);

db=openOrCreateDatabase("StudentDB", Context.MODE_PRIVATE, null);


db.execSQL("CREATE TABLE IF NOT EXISTS student(rollno VARCHAR,name VARCHAR,marks
VARCHAR);");
}
public void onClick(View view)
{
if(view==btnAdd)
{
if(editRollno.getText().toString().trim().length()==0||
editName.getText().toString().trim().length()==0||
editMarks.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter all values");
return;
}
db.execSQL("INSERT INTO student
VALUES('"+editRollno.getText()+"','"+editName.getText()+
"','"+editMarks.getText()+"');");
showMessage("Success", "Record added");
clearText();
}
if(view==btnDelete)
{
if(editRollno.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter Rollno");
return;
}
Cursor c=db.rawQuery("SELECT * FROM student WHERE
rollno='"+editRollno.getText()+"'", null);
if(c.moveToFirst())
{
db.execSQL("DELETE FROM student WHERE rollno='"+editRollno.getText()+"'");
showMessage("Success", "Record Deleted");
}
else
{
showMessage("Error", "Invalid Rollno");
}
clearText();
}
if(view==btnModify)
{
if(editRollno.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter Rollno");
return;
}
Cursor c=db.rawQuery("SELECT * FROM student WHERE
rollno='"+editRollno.getText()+"'", null);
if(c.moveToFirst())
{
db.execSQL("UPDATE student SET
name='"+editName.getText()+"',marks='"+editMarks.getText()+
"' WHERE rollno='"+editRollno.getText()+"'");
showMessage("Success", "Record Modified");
}
else
{
showMessage("Error", "Invalid Rollno");
}
clearText();
}
if(view==btnView)
{
if(editRollno.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter Rollno");
return;
}
Cursor c=db.rawQuery("SELECT * FROM student WHERE
rollno='"+editRollno.getText()+"'", null);
if(c.moveToFirst())
{
editName.setText(c.getString(1));
editMarks.setText(c.getString(2));
}
else
{
showMessage("Error", "Invalid Rollno");
clearText();
}
}
if(view==btnViewAll)
{
Cursor c=db.rawQuery("SELECT * FROM student", null);
if(c.getCount()==0)
{
showMessage("Error", "No records found");
return;
}
StringBuffer buffer=new StringBuffer();
while(c.moveToNext())
{
buffer.append("Rollno: "+c.getString(0)+"\n");
buffer.append("Name: "+c.getString(1)+"\n");
buffer.append("Marks: "+c.getString(2)+"\n\n");
}
showMessage("Student Details", buffer.toString());
}
if(view==btnShowInfo)
{
showMessage("Student Management Application", "Developed By Rahul Patel");
}
}
public void showMessage(String title,String message)
{
Builder builder=new Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
public void clearText()
{
editRollno.setText("");
editName.setText("");
editMarks.setText("");
editRollno.requestFocus();
}

You might also like