0% found this document useful (0 votes)
6 views

Android all programs

android

Uploaded by

rexeena chinju
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Android all programs

android

Uploaded by

rexeena chinju
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 17

BCS6B16- Part A

Android Programming
1. Hello World Program ( Write a program to Toast Hello World)

.xml file
<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=".ToastMainActivity" >

<Button
android:id="@+id/buttonToast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="44dp"
android:layout_marginTop="74dp"
android:text="Show Toast" />

</RelativeLayout>

.java file
package com.example.onetoast;

import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;

public class ToastMainActivity extends Activity {

private Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_toast_main);

button = (Button) findViewById(R.id.buttonToast);

button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {

Toast.makeText(getApplicationContext(),
"Hello World", Toast.LENGTH_LONG).show();

}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.toast_main, menu);
return true;
}

2. Addition of two Numbers (Write a program to add two numbers)


.xml file
<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=".MainActivity" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="97dp"
android:text="Addition"
android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginLeft="20dp"
android:layout_marginTop="43dp"
android:text="Number One"
android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
android:id="@+id/txtNumber1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView2"
android:layout_alignRight="@+id/textView1"
android:ems="2"
android:inputType="number" >

<requestFocus />
</EditText>

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginTop="47dp"
android:text="Number Two"
android:textAppearance="?android:attr/textAppearanceMedium" />

<Button
android:id="@+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_alignRight="@+id/textView3"
android:layout_below="@+id/textView3"
android:layout_marginTop="46dp"
android:text="Add" />

<EditText
android:id="@+id/txtNumber2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/btnAdd"
android:layout_alignLeft="@+id/txtNumber1"
android:ems="2"
android:inputType="number" />

<TextView
android:id="@+id/txtResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/txtNumber2"
android:layout_alignTop="@+id/btnAdd"
android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

.java file
package com.example.twosum;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

EditText firstNumber;
EditText secondNumber;
TextView addResult;
Button btnAdd;

double num1,num2,sum;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

firstNumber = (EditText)findViewById(R.id.txtNumber1);
secondNumber = (EditText)findViewById(R.id.txtNumber2);
addResult = (TextView)findViewById(R.id.txtResult);
btnAdd = (Button)findViewById(R.id.btnAdd);

btnAdd.setOnClickListener(new OnClickListener() {

public void onClick(View v) {


num1 = Double.parseDouble(firstNumber.getText().toString());
num2 = Double.parseDouble(secondNumber.getText().toString());
sum = num1 + num2;
addResult.setText(Double.toString(sum));
}
});
}

@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);
return true;

}
3. Date and Time Dialog box( Write a program to display date and time using dialog box)
.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/widget28"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/lblDateAndTime"
android:layout_width="fill_parent"
android:layout_height="67dp"
android:background="#FFFFFF"
android:textStyle="bold">
</TextView>
<Button
android:id="@+id/btnDate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Set the Date">
</Button>

<Button
android:id="@+id/btnTime"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Set the Time" />

</LinearLayout>
.java file
package com.example.threedateandtime;

import android.app.Activity;
import android.os.Bundle;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.TextView;
import java.text.DateFormat;
import java.util.Calendar;

public class MainActivity extends Activity {


DateFormat fmtDateAndTime = DateFormat.getDateTimeInstance();
TextView lblDateAndTime;
Calendar myCalendar = Calendar.getInstance();

DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener() {


public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel();
}
};

TimePickerDialog.OnTimeSetListener t = new TimePickerDialog.OnTimeSetListener() {


public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
myCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
myCalendar.set(Calendar.MINUTE, minute);
updateLabel();
}
};

private void updateLabel() {


lblDateAndTime.setText(fmtDateAndTime.format(myCalendar.getTime()));
}

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
lblDateAndTime = (TextView) findViewById(R.id.lblDateAndTime);
Button btnDate = (Button) findViewById(R.id.btnDate);
btnDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new DatePickerDialog(MainActivity.this, d, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
});

Button btnTime = (Button) findViewById(R.id.btnTime);


btnTime.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new TimePickerDialog(MainActivity.this, t, myCalendar
.get(Calendar.HOUR_OF_DAY), myCalendar
.get(Calendar.MINUTE), true).show();
}
});

updateLabel();
}// onCreate
} // class
4. Alert Box (Write a program to Display an alert box with OK and Cancel)
.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Button
android:id="@+id/buttonAlert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Alert Box" />

</LinearLayout>

.java file
package com.example.fouralertbox;

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

public class MainActivity extends Activity {

final Context context = this;


private Button button;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

button = (Button) findViewById(R.id.buttonAlert);

// add button listener


button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(


context);

// set title
alertDialogBuilder.setTitle("Your Title");

// set dialog message


alertDialogBuilder
.setMessage("Click yes to exit!")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
MainActivity.this.finish();
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});

// create alert dialog


AlertDialog alertDialog = alertDialogBuilder.create();

// show it
alertDialog.show();
}
});
}
}
5. Menu Program(Write a Program to create menu with three menu items)
.xml file

.java file

6. Radio Button(Write a Program to Select gender using radio button)


.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<RadioGroup
android:id="@+id/radioSex"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<RadioButton
android:id="@+id/radioMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_male"
android:checked="true" />

<RadioButton
android:id="@+id/radioFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_female" />

</RadioGroup>

<Button
android:id="@+id/btnDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_display" />

</LinearLayout>

.java file
package com.example.sixradiobutton;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends Activity {

private RadioGroup radioSexGroup;


private RadioButton radioSexButton;
private Button btnDisplay;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

addListenerOnButton();

public void addListenerOnButton() {

radioSexGroup = (RadioGroup) findViewById(R.id.radioSex);


btnDisplay = (Button) findViewById(R.id.btnDisplay);

btnDisplay.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

// get selected radio button from radioGroup


int selectedId = radioSexGroup.getCheckedRadioButtonId();

// find the radiobutton by returned id


radioSexButton = (RadioButton) findViewById(selectedId);

Toast.makeText(MainActivity.this,
radioSexButton.getText(), Toast.LENGTH_SHORT).show();

});

}
}
7. Spinner(Write a Program To Spin the four items)
.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/myLinearLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/selection"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ff0033cc"
android:textColor="#ff0000"
android:textSize="25dp"
android:textStyle="bold">
</TextView>
<Spinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Spinner>
</LinearLayout>

.java file
package com.example.sevenspinnercontrol;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends Activity implements


AdapterView.OnItemSelectedListener {
TextView selection;
String[] items = { "an double os", "COMPUTER", "MOUSE", "KEYBORD", "MONITOR",
"HARD DISK", "LAPTOP", "PRINTER", "SCANNER", "SPEAKER" };

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
selection = (TextView) findViewById(R.id.selection);

Spinner spin = (Spinner) findViewById(R.id.spinner);


spin.setOnItemSelectedListener(this);

ArrayAdapter aa = new
ArrayAdapter(this,android.R.layout.simple_spinner_item,items);

aa.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(aa);
}

public void onItemSelected(AdapterView<?> parent, View v, int position,


long id) {
selection.setText(items[position]);
}

public void onNothingSelected(AdapterView<?> parent) {


selection.setText("");
}
}//class

8. Timer Program (Write a Program to display Stop watch)


.xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="#000000"
android:layout_height="match_parent" >

<TextView
android:id="@+id/timerValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/pauseButton"
android:layout_centerHorizontal="true"
android:layout_marginBottom="37dp"
android:textSize="40sp"
android:textColor="#ffffff"
android:text="@string/timerVal" />

<Button
android:id="@+id/startButton"
android:layout_width="90dp"
android:layout_height="45dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="38dp"
android:text="@string/startButtonLabel" />

<Button
android:id="@+id/pauseButton"
android:layout_width="90dp"
android:layout_height="45dp"
android:layout_alignBaseline="@+id/startButton"
android:layout_alignBottom="@+id/startButton"
android:layout_alignParentRight="true"
android:layout_marginRight="38dp"
android:text="@string/pauseButtonLabel" />

<Button
android:id="@+id/resetButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/startButton"
android:layout_marginTop="28dp"
android:layout_toLeftOf="@+id/pauseButton"
android:text="@string/resetButtonLabel" />

</RelativeLayout>

.java file
package com.example.eightstopwatch;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

private Button startButton;


private Button pauseButton;
private Button resetButton;

private TextView timerValue;

private long startTime = 0L;

private Handler customHandler = new Handler();

long timeInMilliseconds = 0L;


long timeSwapBuff = 0L;
long updatedTime = 0L;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

timerValue = (TextView) findViewById(R.id.timerValue);

startButton = (Button) findViewById(R.id.startButton);

startButton.setOnClickListener(new View.OnClickListener() {

public void onClick(View view) {


startTime = SystemClock.uptimeMillis();
customHandler.postDelayed(updateTimerThread, 0);

}
});
resetButton = (Button) findViewById(R.id.resetButton);
resetButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

/* MillisecondTime = 0L ;
StartTime = 0L ;
TimeBuff = 0L ;
UpdateTime = 0L ;
Seconds = 0 ;
Minutes = 0 ;
MilliSeconds = 0 ; */

timerValue.setText("00:00:00");

// adapter.notifyDataSetChanged();
}
});
pauseButton = (Button) findViewById(R.id.pauseButton);

pauseButton.setOnClickListener(new View.OnClickListener() {

public void onClick(View view) {

timeSwapBuff += timeInMilliseconds;
customHandler.removeCallbacks(updateTimerThread);

}
});

private Runnable updateTimerThread = new Runnable() {

public void run() {

timeInMilliseconds = SystemClock.uptimeMillis() - startTime;

updatedTime = timeSwapBuff + timeInMilliseconds;

int secs = (int) (updatedTime / 1000);


int mins = secs / 60;
secs = secs % 60;
int milliseconds = (int) (updatedTime % 1000);
timerValue.setText("" + mins + ":"
+ String.format("%02d", secs) + ":"
+ String.format("%03d", milliseconds));
customHandler.postDelayed(this, 0);
}
};

}
9. Check box(Write a Program to check the items listed)
.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<CheckBox
android:id="@+id/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/chk_first" />

<CheckBox
android:id="@+id/java"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/chk_second"
android:checked="true" />

<CheckBox
android:id="@+id/opencv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/chk_third" />

<CheckBox
android:id="@+id/symbian"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/chk_fourth" />

<Button
android:id="@+id/Clickhere"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_click" />

</LinearLayout>

.java file
package com.example.ninecheckbox;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

public class MainActivity extends Activity {

private CheckBox android, java, opencv, symbian;


private Button Clickhere;
@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

android = (CheckBox) findViewById(R.id.android);


java = (CheckBox) findViewById(R.id.java);
opencv = (CheckBox) findViewById(R.id.opencv);
symbian = (CheckBox) findViewById(R.id.symbian);
Clickhere = (Button) findViewById(R.id.Clickhere);

Clickhere.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

// Create string buffer to

StringBuffer OUTPUT = new StringBuffer();


OUTPUT.append("Android : ")
.append(android.isChecked());

OUTPUT.append("\nJava : ").append(
java.isChecked());

OUTPUT.append("\nOpenCV :").append(
opencv.isChecked());

OUTPUT.append("\nSymbian :").append(
symbian.isChecked());

Toast.makeText(MainActivity.this, OUTPUT.toString(),
Toast.LENGTH_LONG).show();

}
});

}
}

10. Date Time Picker(Write a Program to Select current system time using date time picker)
.xml file
<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="200dp"
android:layout_height="wrap_content"
android:id="@+id/in_date"
android:layout_marginTop="82dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sel_date"
android:id="@+id/btn_date"
android:layout_alignBottom="@+id/in_date"
android:layout_toRightOf="@+id/in_date"
android:layout_toEndOf="@+id/in_date" />

<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/in_time"
android:layout_below="@+id/in_date"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sel_time"
android:id="@+id/btn_time"
android:layout_below="@+id/btn_date"
android:layout_alignLeft="@+id/btn_date"
android:layout_alignStart="@+id/btn_date" />

</RelativeLayout>

.java file
package com.example.tencurrentsystemtime;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;

import java.util.Calendar;

public class MainActivity extends Activity implements


View.OnClickListener {

Button btnDatePicker, btnTimePicker;


EditText txtDate, txtTime;
private int mYear, mMonth, mDay, mHour, mMinute;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btnDatePicker=(Button)findViewById(R.id.btn_date);
btnTimePicker=(Button)findViewById(R.id.btn_time);
txtDate=(EditText)findViewById(R.id.in_date);
txtTime=(EditText)findViewById(R.id.in_time);

btnDatePicker.setOnClickListener(this);
btnTimePicker.setOnClickListener(this);

@Override
public void onClick(View v) {

if (v == btnDatePicker) {

// Get Current Date


final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);

DatePickerDialog datePickerDialog = new DatePickerDialog(this,


new DatePickerDialog.OnDateSetListener() {

@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {

txtDate.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-"


+ year);

}
}, mYear, mMonth, mDay);
datePickerDialog.show();
}
if (v == btnTimePicker) {

// Get Current Time


final Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);

// Launch Time Picker Dialog


TimePickerDialog timePickerDialog = new TimePickerDialog(this,
new TimePickerDialog.OnTimeSetListener() {

@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {

txtTime.setText(hourOfDay + ":" + minute);


}
}, mHour, mMinute, false);
timePickerDialog.show();
}
}
}

11. Grid View (Write a Program to display contacts using Grid View Control)
.xml file

.java file
12. Image View (Write a Program to Display images from local drive of the computer)
.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/android" />

<Button
android:id="@+id/btnChangeImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Image" />

</LinearLayout>

.java file
package com.example.twelvedisplayimage;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {

Button button;
ImageView image;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

addListenerOnButton();

public void addListenerOnButton() {

image = (ImageView) findViewById(R.id.imageView1);

button = (Button) findViewById(R.id.btnChangeImage);


button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
image.setImageResource(R.drawable.android3d);

});

}
}

13. List View(Write a Program to Display the items in a list)


.xml file

.java file

14. Log In(optional- Write a program for Log in Using username and password)

You might also like