MASTER
OF COMPUTER APPLICATION (MCA)
Semester : IV
Subject
Name : Software Lab
Q-1 Create “Hello World” application. That will display “Hello World” in the middle of the screen in the
red color with white background.
Question -1
Step 1: Create New Project in eclipse and go to res/layout folder.
Step 2: Edit MainActivity 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:background="@color/White" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="@dimen/padding_medium"
android:text="@string/hello_world"
android:textColor="@color/Red"
tools:context=".MainActivity"
/>
</RelativeLayout>
Step 2: Edit MainActivity 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:background="@color/White" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="@dimen/padding_medium"
android:text="@string/hello_world"
android:textColor="@color/Red"
tools:context=".MainActivity"
/>
</RelativeLayout>
Q-2 To understand Activity, Intent
a. Create sample application with login module.(Check username and password)
b. On successful login, go to next screen. And on failing login, alert user using Toast.
c. Also pass username to next screen.
a. Create sample application with login module.(Check username and password)
b. On successful login, go to next screen. And on failing login, alert user using Toast.
c. Also pass username to next screen.
Question -2
a. Create sample application with login module.(Check username and password)
b. On successful login, go to next screen. And on failing login, alert user using Toast.
c. Also pass username to next screen
Step 1: Create New Project in eclipse
Step 2: Edit main activity layout xml file.
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1"
>
<TableRow
android:id="@+id/tableRow0"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView android:text="Login Form" android:textSize="30dp"/>
</TableRow>
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView android:text="UserName : " android:textSize="20dp"/>
<EditText android:id="@+id/etUname"/>
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView android:text="PassWord : " android:textSize="20dp"/>
<EditText android:id="@+id/etPwd"/>
</TableRow>
<Button
android:id="@+id/btnLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login" />
</TableLayout>
Step 3: Add new layout xml file sucess.xml in res/layout folder.
Step 2: Edit main activity layout xml file.
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1"
>
<TableRow
android:id="@+id/tableRow0"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView android:text="Login Form" android:textSize="30dp"/>
</TableRow>
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView android:text="UserName : " android:textSize="20dp"/>
<EditText android:id="@+id/etUname"/>
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView android:text="PassWord : " android:textSize="20dp"/>
<EditText android:id="@+id/etPwd"/>
</TableRow>
<Button
android:id="@+id/btnLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login" />
</TableLayout>
Step 3: Add new layout xml file sucess.xml in res/layout folder.
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView android:text="Login Sucessfull" android:textSize="30dp"/>
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView android:text="WellCome :" android:textSize="25dp"/>
<TextView android:text="" android:id="@+id/tvUser" android:textSize="25dp"/>
</TableRow>
</TableLayout>
Step 4: Edit Main Activity Java File from src/package/MainActivity.java
package myLab.Pack;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
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 implements OnClickListener{
/** Called when the activity is first created. */
EditText uname;
EditText pwd;
Button login;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
uname = (EditText)findViewById(R.id.etUname);
pwd = (EditText)findViewById(R.id.etPwd);
login = (Button)findViewById(R.id.btnLogin);
login.setOnClickListener(this);
}
public void onClick(View v)
{
String un = uname.getText().toString();
String pd = pwd.getText().toString();
try
{
if(un.equalsIgnoreCase("sahin") && pd.equalsIgnoreCase("raj"))
{
Intent i = new Intent(this,Sucess.class);
i.putExtra("user", un);
startActivity(i);
}
else
{
Toast.makeText(this,"Usernae & Passward Mismatch",Toast.LENGTH_LONG).show();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Step 5: Add new Java file in src/package/sucess.java
package myLab.Pack;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class Sucess extends Activity
{
TextView tv;
@Override
protected void onCreate(Bundle s)
{
// TODO Auto-generated method stub
super.onCreate(s);
setContentView(R.layout.sucess);
Intent i = getIntent();
String uname = i.getStringExtra("user");
tv = (TextView)findViewById(R.id.tvUser);
tv.setText(""+uname);
}
}
Q-3 Create login application where you will have to validate EmailID(UserName). Till the username and
password is not validated , login button should remain disabled.
c. Also pass username to next screen.
c. Also pass username to next screen.
Question -3
Step 1: Create New Project in Eclipse.
Step 2: Edit Main Layout XML file from res/layout folder
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="1" >
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="30dp"
android:text="@string/username" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/uid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/username" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30dp"
android:text="@string/password" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/password" />
</TableRow>
<TableRow>
<Button android:text="@string/login" android:id="@+id/btnlogin" android:enabled="false"/>
</TableRow>
</TableLayout>
Step 3: Edit MainActivity.java file from scr/package/ folder
package com.example.login;
import android.os.Bundle;
import android.app.Activity;
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;
import android.widget.Toast;
import android.support.v4.app.NavUtils;
import android.text.Editable;
import android.text.TextWatcher;
public class MainActivity extends Activity implements OnClickListener,TextWatcher
{
Button login;
EditText uname;
EditText pwd;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uname = (EditText)findViewById(R.id.uid);
pwd = (EditText)findViewById(R.id.pwd);
login = (Button)findViewById(R.id.btnlogin);
login.setOnClickListener(this);
uname.addTextChangedListener(this);
pwd.addTextChangedListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public void onClick(View v)
{
Toast.makeText(this,"Sucess",Toast.LENGTH_LONG).show();
}
@Override
public void afterTextChanged(Editable e)
{
if(uname.getText().toString().equalsIgnoreCase("sahin") && pwd.getText().toString().equalsIgnoreCase("raj"))
{
login.setEnabled(true);
}
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
{
}
}
Step 2: Edit Main Layout XML file from res/layout folder
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="1" >
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="30dp"
android:text="@string/username" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/uid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/username" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30dp"
android:text="@string/password" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/password" />
</TableRow>
<TableRow>
<Button android:text="@string/login" android:id="@+id/btnlogin" android:enabled="false"/>
</TableRow>
</TableLayout>
Step 3: Edit MainActivity.java file from scr/package/ folder
package com.example.login;
import android.os.Bundle;
import android.app.Activity;
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;
import android.widget.Toast;
import android.support.v4.app.NavUtils;
import android.text.Editable;
import android.text.TextWatcher;
public class MainActivity extends Activity implements OnClickListener,TextWatcher
{
Button login;
EditText uname;
EditText pwd;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uname = (EditText)findViewById(R.id.uid);
pwd = (EditText)findViewById(R.id.pwd);
login = (Button)findViewById(R.id.btnlogin);
login.setOnClickListener(this);
uname.addTextChangedListener(this);
pwd.addTextChangedListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public void onClick(View v)
{
Toast.makeText(this,"Sucess",Toast.LENGTH_LONG).show();
}
@Override
public void afterTextChanged(Editable e)
{
if(uname.getText().toString().equalsIgnoreCase("sahin") && pwd.getText().toString().equalsIgnoreCase("raj"))
{
login.setEnabled(true);
}
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
{
}
}
Q-4 Create and Login application as above . On successful login , open browser with any URL.
c. Also pass username to next screen.
c. Also pass username to next screen.
Question -4
Create an Login application as above . On successful login , open browser with any URL.Step 1: Create New Project in Eclipse.
Step 2: Edit Main_Activity layout xml file from res/layout folder in your project
<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" >
<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="15dp"
android:text="@string/loginform"
android:textSize="40dp" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="41dp"
android:text="@string/username" />
<EditText
android:id="@+id/uname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView3"
android:layout_alignBottom="@+id/textView3"
android:layout_alignParentRight="true"
android:layout_marginRight="14dp"
android:ems="10"
android:hint="@string/enter_username" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/uname"
android:layout_alignRight="@+id/uname"
android:layout_below="@+id/uname"
android:layout_marginTop="17dp"
android:ems="10"
android:hint="@string/enter_password"
android:inputType="textPassword" >
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/password"
android:layout_alignBottom="@+id/password"
android:layout_alignParentLeft="true"
android:text="@string/password" />
<Button
android:id="@+id/btnlogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:enabled="false"
android:text="@string/login" />
</RelativeLayout>
Step 3: Edit MainActivity.java file from src/package/ folder in your project
package com.example.loginuri;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
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;
import android.support.v4.app.NavUtils;
import android.text.Editable;
import android.text.TextWatcher;
a public class MainActivity extends Activity implements OnClickListener,TextWatcher
{
Button login;
EditText uname;
EditText pwd;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
login = (Button)findViewById(R.id.btnlogin);
uname = (EditText)findViewById(R.id.uname);
pwd = (EditText)findViewById(R.id.password);
login.setOnClickListener(this);
uname.addTextChangedListener(this);
pwd.addTextChangedListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public void afterTextChanged(Editable arg0)
{
if(uname.getText().toString().equalsIgnoreCase("sahin") && pwd.getText().toString().equalsIgnoreCase("raj"))
{
login.setEnabled(true);
}
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void onClick(View arg0)
{
Intent i = new Intent("android.intent.action.VIEW",Uri.parse("http://licamca.blogspot.com"));
startActivity(i);
}
}
Q-5 Create an application that will pass some number to the next screen , and on the next screen that
number of items should be display in the list.
Question -5
Create an application that will pass some number to the next screen and on the next screen that number of items should be display in the list.Step 1: Create New Project in Eclipse.
Step 2: Edit Main ListActivity xml file from res/layout folder of your project.
<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" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="71dp"
android:text="Enter Any Number"
android:textSize="30dp" />
<EditText
android:id="@+id/txtnum"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Integer Only" />
<Button
android:id="@+id/btnenter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="Submit" />
</RelativeLayout>
Step 3: Add New ListDisplay xml file to your res/layout folder of your project.
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="No of Items Entered Are As Follows"
android:textSize="20dp" />
<ListView android:layout_height="wrap_content"
android:layout_width="match_parent" android:id="@+id/@android:id/list"
android:longClickable="true" android:fastScrollEnabled="false"></ListView>
</TableLayout>
Step 4: Edit Main List Activity Java file from src/package/ folder of your project.
package com.example.List;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
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;
import android.support.v4.app.NavUtils;
public class ListActivity extends Activity implements OnClickListener
{
Button submit;
EditText num;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
submit = (Button)findViewById(R.id.btnenter);
num = (EditText)findViewById(R.id.txtnum);
submit.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_list, menu);
return true;
}
@Override
public void onClick(View arg0)
{
String n = num.getText().toString();
Intent i = new Intent(this,ListDisplay.class);
i.putExtra("number",n);
startActivity(i);
}
}
Step 5: Add New ListDisplay Java class file to your src/package/ folder of your project.
package com.example.List;
import java.util.ArrayList;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class ListDisplay extends ListActivity
{
ArrayList<String> arr=new ArrayList<String>();
ArrayAdapter<String> a;
int i=0;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_disp);
a = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 ,arr);
setListAdapter(a);
for(i=1; i<= Integer.parseInt(getIntent().getStringExtra("number")); i++)
{
arr.add("Item : "+i);
}
//Toast.makeText(this, "Welcome", Toast.LENGTH_LONG).show();
}
}
Q-6 Understand resource folders :
a. Create spinner with strings taken from resource folder(res >> value folder).
b. On changing spinner value, change image.
Question - 6
a. Create spinner with strings taken from resource folder(res >> value folder).
b. On changing spinner value, change image.
Step 1: Create New Project in eclipse and go to res/layout folder.
Step 2: Add 5 Images of your choice into res/drawable-hdpi/ folder of your project.
Step 3: Edit Activity_Spinner XML file in res/layout/ folder of your project.
<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" >
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/Spinner"
/>
<ImageView
android:id="@+id/imgview1"
android:contentDescription="@string/imgviewer"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/spinner1"
android:src="@drawable/image1" />
</RelativeLayout>
Step 4: Edit Activity Spinner Java file in src/package/ folder of your project.
package com.example.Spinner;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;
import android.support.v4.app.NavUtils;
public class SpinnerActivity extends Activity implements OnItemSelectedListener
{
Spinner spn;
ImageView iv;
Integer[] ids = { R.drawable.image1,R.drawable.image2,R.drawable.image3,R.drawable.image4,R.drawable.image5};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner);
spn = (Spinner)findViewById(R.id.spinner1);
iv = (ImageView)findViewById(R.id.imgview1);
spn.setOnItemSelectedListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_spinner, menu);
return true;
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3)
{
iv.setImageResource(ids[arg2]);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
Step 2: Add 5 Images of your choice into res/drawable-hdpi/ folder of your project.
Step 3: Edit Activity_Spinner XML file in res/layout/ folder of your project.
<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" >
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/Spinner"
/>
<ImageView
android:id="@+id/imgview1"
android:contentDescription="@string/imgviewer"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/spinner1"
android:src="@drawable/image1" />
</RelativeLayout>
Step 4: Edit Activity Spinner Java file in src/package/ folder of your project.
package com.example.Spinner;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;
import android.support.v4.app.NavUtils;
public class SpinnerActivity extends Activity implements OnItemSelectedListener
{
Spinner spn;
ImageView iv;
Integer[] ids = { R.drawable.image1,R.drawable.image2,R.drawable.image3,R.drawable.image4,R.drawable.image5};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner);
spn = (Spinner)findViewById(R.id.spinner1);
iv = (ImageView)findViewById(R.id.imgview1);
spn.setOnItemSelectedListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_spinner, menu);
return true;
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3)
{
iv.setImageResource(ids[arg2]);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
Q-7 Understand Menu option.
a. Create an application that will change color of the screen, based on selected options from the menu.
a. Create an application that will change color of the screen, based on selected options from the menu.
Question -7
Understand Menu option.
a. Create an application that will change color of the screen, based on selected options from the
menu.
Step 1: Create New Project in eclipse.
Step 2: Edit MainActivity XML file in folder res/layout folder of your project
<TableLayout 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" >
<RelativeLayout
android:id="@+id/mainlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/tv1"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Menu Color Change"
android:textSize="40dp" />
</RelativeLayout>
</TableLayout>
Step 3: Edit MenuActivity XML file in folder res/menu folder of your project
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/itemBlue"
android:title="Blue Background."
/>
<item
android:id="@+id/itemGreen"
android:title="Green Background."
/>
</menu>
Step 4: Edit MainActivity Java file in folder src/package/ folder of your project
package com.example.MenuOption;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;
import android.support.v4.app.NavUtils;
public class MenuOption extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu_option);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.menuoption, menu);
super.onCreateOptionsMenu(menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// TODO Auto-generated method stub
super.onOptionsItemSelected(item);
RelativeLayout rl = (RelativeLayout)findViewById(R.id.mainlayout);
switch(item.getItemId())
{
case R.id.itemBlue:
rl.setBackgroundColor(Color.BLUE);
break;
case R.id.itemGreen:
rl.setBackgroundColor(Color.GREEN);
break;
}
return false;
}
}
Step 2: Edit MainActivity XML file in folder res/layout folder of your project
<TableLayout 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" >
<RelativeLayout
android:id="@+id/mainlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/tv1"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Menu Color Change"
android:textSize="40dp" />
</RelativeLayout>
</TableLayout>
Step 3: Edit MenuActivity XML file in folder res/menu folder of your project
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/itemBlue"
android:title="Blue Background."
/>
<item
android:id="@+id/itemGreen"
android:title="Green Background."
/>
</menu>
Step 4: Edit MainActivity Java file in folder src/package/ folder of your project
package com.example.MenuOption;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;
import android.support.v4.app.NavUtils;
public class MenuOption extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu_option);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.menuoption, menu);
super.onCreateOptionsMenu(menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// TODO Auto-generated method stub
super.onOptionsItemSelected(item);
RelativeLayout rl = (RelativeLayout)findViewById(R.id.mainlayout);
switch(item.getItemId())
{
case R.id.itemBlue:
rl.setBackgroundColor(Color.BLUE);
break;
case R.id.itemGreen:
rl.setBackgroundColor(Color.GREEN);
break;
}
return false;
}
}
Q-8 Create an application that will display toast(Message) on specific interval of time.
Q-9 Create an background application that will open activity on specific time.
Q-10 Create an application that will have spinner with list of animation names. On selecting animation
name , that animation should affect on the images displayed below.
name , that animation should affect on the images displayed below.
It's great to have all the codes of practical sheet here but it will be no longer useful to GTU mCA students as practical sheet is changed now..so , can u please provide solution of new sheet??? thanx & regards..
ReplyDelete