You are on page 1of 24

M.

TECH(CSE)-II SEMESTER MOBILE APPLICATION DEVELOPMENT LAB

1.Design and develop an Mobile App for smart phones The Easy Unit Converter using Android.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/statistics_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:dividerHeight="1.0sp"
android:scrollbars="none"
android:background="@android:color/transparent"
android:cacheColorHint="@android:color/transparent"
android:layout_marginBottom="8dp"
android:layout_centerHorizontal="true"/>

</RelativeLayout>

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="12dp"
android:paddingBottom="12dp">
<com.github.ivbaranov.mli.MaterialLetterIcon
android:id="@+id/imageView"
android:layout_width="@dimen/letter_icon_size"
android:layout_height="@dimen/letter_icon_size"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:contentDescription="@string/hello_world"
android:layout_alignParentStart="true"
android:layout_marginLeft="16dp"
app:mli_shape_color="@color/primary_text"
app:mli_shape_type="circle"
app:mli_letter_color="@color/icons"
app:mli_letter_size="26"
android:layout_marginTop="8dp"/>

<TextView
android:id="@+id/navigation_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:layout_marginLeft="16dp"
android:layout_marginTop="12dp"
android:textColor="@color/primary_text"
android:layout_alignTop="@+id/imageView"
android:layout_toRightOf="@+id/imageView"

Page 1 of 24
M.TECH(CSE)-II SEMESTER MOBILE APPLICATION DEVELOPMENT LAB

android:text="@string/hello_world"/>
</RelativeLayout>
import android.content.Context;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.github.ivbaranov.mli.MaterialLetterIcon;
import java.util.List;
import java.util.Random;
import elearning.chidi.com.elearning.R;
public class CalculationAdapter extends BaseAdapter {
private LayoutInflater lInflater;
private List<String> listStorage;
private final TypedValue mTypedValue = new TypedValue();
private int mBackground;
private List<String> mValues;
private int[] mMaterialColors;
private int mType;
private Context context;
private static final Random RANDOM = new Random();
public CalculationAdapter(Context context, List<String> customizedListView) {
lInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.context = context;
listStorage = customizedListView;
}
@Override
public int getCount() {
return listStorage.size();
}
@Override
public Object getItem(int position) {
return listStorage.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder listViewHolder;
if(convertView == null){
listViewHolder = new ViewHolder();
convertView = lInflater.inflate(R.layout.calculation_list, parent, false);
listViewHolder.calculationText = (TextView)convertView.findViewById(R.id.navigation_name);
listViewHolder.letterIcon = (MaterialLetterIcon)convertView.findViewById(R.id.imageView);
convertView.setTag(listViewHolder);
}else{
listViewHolder = (ViewHolder) convertView.getTag();
}
mMaterialColors = context.getResources().getIntArray(R.array.colors);

Page 2 of 24
M.TECH(CSE)-II SEMESTER MOBILE APPLICATION DEVELOPMENT LAB

mBackground = mTypedValue.resourceId;
mValues = this.listStorage;
listViewHolder.calculationText.setText(listStorage.get(position));
listViewHolder.letterIcon.setShapeColor(mMaterialColors[RANDOM.nextInt(mMaterialColors.length)]);
listViewHolder.letterIcon.setLetter(mValues.get(position));
return convertView;
}
static class ViewHolder{
TextView calculationText;
MaterialLetterIcon letterIcon;
}
}

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
import elearning.chidi.com.elearning.adapter.CalculationAdapter;
public class UnitConverterFragment extends Fragment {
public UnitConverterFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_unit_converter, container, false);
List<String> calculationSolver = calculationList();
ListView calculation = (ListView) view.findViewById(R.id.statistics_list);
CalculationAdapter tutorialAdapter = new CalculationAdapter(getActivity().getApplicationContext(),
calculationSolver);
calculation.setAdapter(tutorialAdapter);
calculation.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position){
case 0:
Intent unitIntent = new Intent(getActivity(), UnitConverterActivity.class);
startActivity(unitIntent);
break;
case 1:
Intent lengthIntent = new Intent(getActivity(), LengthConverterActivity.class);
startActivity(lengthIntent);
break;
case 2:
Intent massIntent = new Intent(getActivity(), MassConverterActivity.class);
startActivity(massIntent);
break;
case 3:
Intent pressureIntent = new Intent(getActivity(), PressureConverterActivity.class);

Page 3 of 24
M.TECH(CSE)-II SEMESTER MOBILE APPLICATION DEVELOPMENT LAB

startActivity(pressureIntent);
break;
case 4:
Intent volumeIntent = new Intent(getActivity(), VolumeConverterActivity.class);
startActivity(volumeIntent);
break;
case 5:
Intent speedIntent = new Intent(getActivity(), SpeedConverterActivity.class);
startActivity(speedIntent);
break;
case 6:
Intent temperatureIntent = new Intent(getActivity(), TemperatureConverterActivity.class);
startActivity(temperatureIntent);
break;
case 7:
Intent timeIntent = new Intent(getActivity(), TimeConverterActivity.class);
startActivity(timeIntent);
break;
case 8:
Intent energyIntent = new Intent(getActivity(), EnergyConverterActivity.class);
startActivity(energyIntent);
break;
case 9:
Intent powerIntent = new Intent(getActivity(), PowerConverterActivity.class);
startActivity(powerIntent);
break;
case 10:
Intent velocityIntent = new Intent(getActivity(), VelocityConverterActivity.class);
startActivity(velocityIntent);
break;
}
}
});
return view;
}
private List<String> calculationList() {
List<String> calculations = new ArrayList<String>();
calculations.add("Area");
calculations.add("Length");
calculations.add("Mass");
calculations.add("Pressure");
calculations.add("Volume");
calculations.add("Speed");
calculations.add("Temperature");
calculations.add("Time");
calculations.add("Energy");
calculations.add("Power");
calculations.add("Velocity");
return calculations;
}
}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

Page 4 of 24
M.TECH(CSE)-II SEMESTER MOBILE APPLICATION DEVELOPMENT LAB

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="64dp"
android:background="@color/color_primary"
android:minHeight="?attr/actionBarSize"
app:titleTextAppearance="@style/AppTheme.Toolbar.Title" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/icons">
<ScrollView
android:id="@+id/scroll_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_above="@+id/relativeAd"
android:layout_marginBottom="8dp
android:background="#ffffff"
android:padding="16dp"
android:scrollbars="none">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/binary_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:text="@string/title_activity_length_converter"
android:textColor="@color/primary_text"
android:textSize="18dp"
android:textStyle="bold" />
<TextView
android:id="@+id/first_binary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/binary_title"
android:layout_below="@+id/binary_title"
android:layout_marginTop="24dp"
android:text="@string/from_unit"
android:textStyle="bold"
android:textColor="@color/primary_text" />
<Spinner

Page 5 of 24
M.TECH(CSE)-II SEMESTER MOBILE APPLICATION DEVELOPMENT LAB

android:id="@+id/from_spinner"
android:layout_width="match_parent"
android:layout_alignLeft="@+id/first_binary"
android:layout_below="@+id/first_binary"
android:background="@drawable/bottom_border"
android:layout_marginTop="4dp"
android:layout_height="wrap_content">
</Spinner>
<TextView
android:id="@+id/enter_a_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/unit_value"
android:layout_below="@+id/from_spinner"
android:layout_marginTop="24dp"
android:text="@string/enter_value"
android:textStyle="bold"
android:textColor="@color/primary_text" />
<EditText
android:id="@+id/unit_value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/enter_a_number"
android:layout_marginLeft="16dp"
android:layout_marginTop="4dp"
android:background="@drawable/bottom_border"
android:inputType="numberDecimal"
android:padding="8dp" />
<TextView
android:id="@+id/to_unit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/unit_value"
android:layout_below="@+id/unit_value"
android:layout_marginTop="24dp"
android:text="@string/to_unit"
android:textStyle="bold"
android:textColor="@color/primary_text" />
<Spinner
android:id="@+id/to_spinner"
android:layout_width="match_parent"
android:layout_alignLeft="@+id/first_binary"
android:layout_below="@+id/to_unit"
android:background="@drawable/bottom_border"
android:layout_marginTop="4dp"
android:layout_height="wrap_content">
</Spinner>
<Button
android:id="@+id/calculation_button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/button_all"

Page 6 of 24
M.TECH(CSE)-II SEMESTER MOBILE APPLICATION DEVELOPMENT LAB

android:text="@string/calculation_text"
android:textAllCaps="false"
android:layout_marginTop="32dp"
android:padding="8dp"
android:layout_below="@+id/to_spinner"
android:layout_centerHorizontal="true"
android:textColor="@color/primary_text"/>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/calculation_button"
android:layout_marginTop="40dp"
android:layout_centerHorizontal="true"
android:stretchColumns="1">
<TableRow>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/unit_result"
android:textSize="16sp"
android:padding="16dp"
android:textColor="@color/primary_text"
android:textStyle="bold"/>
<TextView
android:id="@+id/convert_result"
android:layout_height="match_parent"
android:layout_width="0dp"
android:textColor="@color/primary_text"
android:text="@string/no_text"
android:padding="16dp"
android:layout_weight="1"
android:background="@drawable/bottom_border"/>
</TableRow>
</TableLayout>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
</LinearLayout>
String.xml
<string-array name="length_units">
<item id="l_inches">Inches</item>
<item>Feet</item>
<item>Yards</item>
<item>Miles</item>
<item>Millimeters</item>
<item>Centimeters</item>
<item>Meters</item>
<item>Kilometers</item>
</string-array>

Page 7 of 24
M.TECH(CSE)-II SEMESTER MOBILE APPLICATION DEVELOPMENT LAB
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<!-- Demonstrates using a relative layout to create a form -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<Button android:id="@+id/one"
android:layout_width="100px"
android:layout_height="100px"
android:layout_alignParentRight="true"
android:layout_marginLeft="5px"
android:text=" "
android:textSize="70px" />

<Button android:id="@+id/two"
android:layout_width="100px"
android:layout_height="100px"
android:layout_toLeftOf="@id/one"
android:layout_alignTop="@id/one"
android:layout_marginLeft="5px"
android:text=" "
android:textSize="70px" />

<Button android:id="@+id/three"
android:layout_width="100px"
android:layout_height="100px"
android:layout_toLeftOf="@id/two"
android:layout_alignTop="@id/two"
android:layout_marginLeft="5px"
android:text=" "
android:textSize="70px"
android:padding="0px"/>

<Button android:id="@+id/four"
android:layout_width="100px"
android:layout_height="100px"
android:layout_alignParentRight="true"
android:layout_marginLeft="5px"
android:layout_below="@id/one"
android:text=" "
android:textSize="70px" />

<Button android:id="@+id/five"
android:layout_width="100px"
android:layout_height="100px"
android:layout_toLeftOf="@id/four"
android:layout_alignTop="@id/four"
android:layout_marginLeft="5px"
android:text=" "
android:textSize="70px" />

<Button android:id="@+id/six"

Page 8 of 24
M.TECH(CSE)-II SEMESTER MOBILE APPLICATION DEVELOPMENT LAB

android:layout_width="100px"
android:layout_height="100px"
android:layout_toLeftOf="@id/five"
android:layout_alignTop="@id/five"
android:layout_marginLeft="5px"
android:text=" "
android:textSize="70px" />

<Button android:id="@+id/seven"
android:layout_width="100px"
android:layout_height="100px"
android:layout_alignParentRight="true"
android:layout_marginLeft="5px"
android:layout_below="@id/four"
android:text=" "
android:textSize="70px" />

<Button android:id="@+id/eight"
android:layout_width="100px"
android:layout_height="100px"
android:layout_toLeftOf="@id/seven"
android:layout_alignTop="@id/seven"
android:layout_marginLeft="5px"
android:text=" "
android:textSize="70px" />

<Button android:id="@+id/nine"
android:layout_width="100px"
android:layout_height="100px"
android:layout_toLeftOf="@id/eight"
android:layout_alignTop="@id/eight"
android:layout_marginLeft="5px"
android:text=" "
android:textSize="70px" />

<TextView android:id="@+id/dialogue"
android:layout_width="fill_parent"
android:layout_below="@id/nine"
android:layout_height="wrap_content"
android:text="Click a button to start"
android:gravity="center_horizontal"
android:layout_marginTop="20px"
android:textSize="20px"/>

</RelativeLayout>

Page 9 of 24
M.TECH(CSE)-II SEMESTER MOBILE APPLICATION DEVELOPMENT LAB

Output:-

Page 10 of 24
M.TECH(CSE)-II SEMESTER MOBILE APPLICATION DEVELOPMENT LAB

2. Design and develop an mobile app for smart phones currency converter.

activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
android:background="#ff8899"
tools:context=".MainActivity" >
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/from_currency"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="18sp"
android:text="@string/from_currency" />
<Spinner
android:id="@+id/fromcurrency_spin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/to_currency"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="18sp"
android:text="@string/to_currency" />
<Spinner
android:id="@+id/tocurrency_spin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

Page 11 of 24
M.TECH(CSE)-II SEMESTER MOBILE APPLICATION DEVELOPMENT LAB

/>
</LinearLayout>

<EditText
android:id="@+id/txt_rate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/txt_rate"
android:inputType="numberDecimal" />

<EditText
android:id="@+id/txt_fromamount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/txt_fromamount"
android:inputType="numberDecimal" />

<Button
android:id="@+id/bt_show"
android:background="@drawable/bt_style"
android:text="@string/bt_label"
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center"
android:onClick="showResult"
/>

</LinearLayout>

spinner_style.xml

<?xml version="1.0" encoding="utf-8"?>

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"

/>

bt_style.xml

<?xml version="1.0" encoding="utf-8"?>


<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<solid
android:color="#ff00ff" />

Page 12 of 24
M.TECH(CSE)-II SEMESTER MOBILE APPLICATION DEVELOPMENT LAB

<stroke
android:width="1dp"
android:color="#171717" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#ffffff"
android:endColor="#992211"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#171717" />
<corners
android:radius="4dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>

MainActivity.java

package com.example.currencyconverter;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

Page 13 of 24
M.TECH(CSE)-II SEMESTER MOBILE APPLICATION DEVELOPMENT LAB

public class MainActivity extends Activity {

private String fromCurrency;


private String toCurrency;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

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

protected void onResume(){


super.onResume();
setUpSpinnerData();

}
//This method will be invoked to setup data of the spinner views
//to show lists of currency types for selection
public void setUpSpinnerData(){
Spinner spFrom=(Spinner)findViewById(R.id.fromcurrency_spin);
Spinner spTo=(Spinner)findViewById(R.id.tocurrency_spin);
String[]
currencyList={"AUD","CAD","CHF","EUR","GBP","JPY","NZD","KHR","USD","CNY","THB","IN
R"};
ArrayAdapter<String> afrom=new ArrayAdapter<String>(this,R.layout.spinner_style,currencyList);
spFrom.setAdapter(afrom);
spFrom.setOnItemSelectedListener(new ItemSelectedFrom());
ArrayAdapter<String> ato=new ArrayAdapter<String>(this,R.layout.spinner_style,currencyList);
spTo.setAdapter(ato);
spTo.setOnItemSelectedListener(new ItemSelectedTo());

private class ItemSelectedFrom implements OnItemSelectedListener{


public void onNothingSelected(AdapterView<?> av){

}
public void onItemSelected(AdapterView<?> av, View view, int position, long id){
TextView sel=(TextView)view;
String from=sel.getText().toString();
fromCurrency=from; //capture the currency of the From side
EditText txtfrom=(EditText)findViewById(R.id.txt_fromamount);
txtfrom.setHint("Enter "+fromCurrency+" amount");

}
}

Page 14 of 24
M.TECH(CSE)-II SEMESTER MOBILE APPLICATION DEVELOPMENT LAB

private class ItemSelectedTo implements OnItemSelectedListener{


public void onNothingSelected(AdapterView<?> av){

}
public void onItemSelected(AdapterView<?> av, View view, int position, long id){
TextView sel=(TextView)view;
String to=sel.getText().toString();
toCurrency=to; //capture the currency of the To side

}
}

public void showResult(View view){


EditText txtRate=(EditText)findViewById(R.id.txt_rate);
EditText txtAmount=(EditText)findViewById(R.id.txt_fromamount);
if(txtRate.getText().toString().length()<=0 || txtAmount.getText().toString().length()<=0){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Please input value in text box.");
builder.setCancelable(true);
builder.setPositiveButton("OK", new OnClickListener(){
public void onClick(DialogInterface di,int which){
di.cancel();
}
});
AlertDialog dialog = builder.create();
dialog.show();

}
else{
//create intent, place data in it and start the ConversionTable activity
Intent intent=new Intent(this, ConversionTable.class);
intent.putExtra("fromCurrency", fromCurrency);
intent.putExtra("toCurrency", toCurrency);
intent.putExtra("Rate", Double.valueOf(txtRate.getText().toString()));
intent.putExtra("fromAmount", Double.valueOf(txtAmount.getText().toString()));
startActivity(intent);
}
}

activity_conversion_tabl.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"

Page 15 of 24
M.TECH(CSE)-II SEMESTER MOBILE APPLICATION DEVELOPMENT LAB

android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".ConversionTable" >

<ListView
android:id="@+id/conversion_list"
android:layout_width="match_parent"
android:layout_height="match_parent"

/>

</LinearLayout>

listlayout.xml

<?xml version="1.0" encoding="utf-8"?>


<!-- Single List Item Design -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#000000"
android:padding="5dip" >

<ImageView
android:id="@+id/icon"
android:layout_width="30dp"
android:layout_height="30dp"
android:padding="5sp"
android:contentDescription="countryicon"
/>

<TextView
android:id="@+id/currency"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10sp"
android:textSize="20sp"
android:textColor="#ffffff"
android:textStyle="bold" >
</TextView>
<TextView
android:id="@+id/label_amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10sp"
android:textSize="20sp"
android:textColor="#ffffff"
android:textStyle="bold" >

Page 16 of 24
M.TECH(CSE)-II SEMESTER MOBILE APPLICATION DEVELOPMENT LAB

</TextView>
</LinearLayout>

ListAdapterModel.java

package com.example.currencyconverter;
import java.text.DecimalFormat;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ListAdapterModel extends ArrayAdapter<String>{


int groupid;
String[] clist;
Context context;
String to;
Double[] alist;

public ListAdapterModel(Context context, int vg,String[] clist, Double[] alist, String to){
super(context,vg, clist);
this.context=context;
groupid=vg;
this.clist=clist;
this.to=to;
this.alist=alist;
}
public View getView(int position, View convertView, ViewGroup parent) {

LayoutInflater inflater = (LayoutInflater)


context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View itemView = inflater.inflate(groupid, parent, false);


ImageView imageView = (ImageView) itemView.findViewById(R.id.icon);
imageView.setImageDrawable(context.getResources().getDrawable(getImage(position)));
TextView textCurrency = (TextView) itemView.findViewById(R.id.currency);
TextView textAmount= (TextView) itemView.findViewById(R.id.label_amount);
textCurrency.setText(clist[position]);
DecimalFormat df=new DecimalFormat("#,###.0000");
String value=df.format(Double.valueOf(alist[position]));
textAmount.setText(value);
if(clist[position].equals(to)) //highlight the target conversion row
itemView.setBackgroundColor(Color.MAGENTA);
return itemView;

Page 17 of 24
M.TECH(CSE)-II SEMESTER MOBILE APPLICATION DEVELOPMENT LAB

public Integer getImage(int pos){


Integer[] imageIds=new Integer[12];
imageIds[0]=R.drawable.aud;
imageIds[1]=R.drawable.cad;
imageIds[2]=R.drawable.chf;
imageIds[3]=R.drawable.eur;
imageIds[4]=R.drawable.gbp;
imageIds[5]=R.drawable.jpy;
imageIds[6]=R.drawable.nzd;
imageIds[7]=R.drawable.khr;
imageIds[8]=R.drawable.usd;
imageIds[9]=R.drawable.cny;
imageIds[10]=R.drawable.thb;
imageIds[11]=R.drawable.inr;

return(imageIds[pos]);

Page 18 of 24
M.TECH(CSE)-II SEMESTER MOBILE APPLICATION DEVELOPMENT LAB

Output:-

Page 19 of 24
M.TECH(CSE)-II SEMESTER MOBILE APPLICATION DEVELOPMENT LAB
3.Design and develop an Mobile App game for smart phones The Tic Tac Toe using Android

TicTacToe.java:

package com.games.tictactoe;

import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class TicTacToe extends Activity {

int c[][];
int i, j, k = 0;
Button b[][];
TextView textView;
AI ai;

/** Called when the activity is first created. */


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setBoard();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuItem item = menu.add("New Game");
return true;
}

public boolean onOptionsItemSelected(MenuItem item) {


setBoard();
return true;
}

// Set up the game board.


private void setBoard() {
ai = new AI();
b = new Button[4][4];
c = new int[4][4];

textView = (TextView) findViewById(R.id.dialogue);

b[1][3] = (Button) findViewById(R.id.one);


b[1][2] = (Button) findViewById(R.id.two);

Page 20 of 24
M.TECH(CSE)-II SEMESTER MOBILE APPLICATION DEVELOPMENT LAB

b[1][1] = (Button) findViewById(R.id.three);


b[2][3] = (Button) findViewById(R.id.four);
b[2][2] = (Button) findViewById(R.id.five);
b[2][1] = (Button) findViewById(R.id.six);
b[3][3] = (Button) findViewById(R.id.seven);
b[3][2] = (Button) findViewById(R.id.eight);
b[3][1] = (Button) findViewById(R.id.nine);

for (i = 1; i <= 3; i++) {


for (j = 1; j <= 3; j++)
c[i][j] = 2;
}

textView.setText("Click a button to start.");

// add the click listeners for each button


for (i = 1; i <= 3; i++) {
for (j = 1; j <= 3; j++) {
b[i][j].setOnClickListener(new MyClickListener(i, j));
if(!b[i][j].isEnabled()) {
b[i][j].setText(" ");
b[i][j].setEnabled(true);
}
}
}
}

class MyClickListener implements View.OnClickListener {


int x;
int y;
public MyClickListener(int x, int y) {
this.x = x;
this.y = y;
}
public void onClick(View view) {
if (b[x][y].isEnabled()) {
b[x][y].setEnabled(false);
b[x][y].setText("O");
c[x][y] = 0;
textView.setText("");
if (!checkBoard()) {
ai.takeTurn();
}
}
}
}

private class AI {
public void takeTurn() {

Page 21 of 24
M.TECH(CSE)-II SEMESTER MOBILE APPLICATION DEVELOPMENT LAB
if(c[1][1]==2 &&
((c[1][2]==0 && c[1][3]==0) ||
(c[2][2]==0 && c[3][3]==0) ||
(c[2][1]==0 && c[3][1]==0))) {
markSquare(1,1);
} else if (c[1][2]==2 &&
((c[2][2]==0 && c[3][2]==0) ||
(c[1][1]==0 && c[1][3]==0))) {
markSquare(1,2);
} else if(c[1][3]==2 &&
((c[1][1]==0 && c[1][2]==0) ||
(c[3][1]==0 && c[2][2]==0) ||
(c[2][3]==0 && c[3][3]==0))) {
markSquare(1,3);
} else if(c[2][1]==2 &&
((c[2][2]==0 && c[2][3]==0) ||
(c[1][1]==0 && c[3][1]==0))){
markSquare(2,1);
} else if(c[2][2]==2 &&
((c[1][1]==0 && c[3][3]==0) ||
(c[1][2]==0 && c[3][2]==0) ||
(c[3][1]==0 && c[1][3]==0) ||
(c[2][1]==0 && c[2][3]==0))) {
markSquare(2,2);
} else if(c[2][3]==2 &&
((c[2][1]==0 && c[2][2]==0) ||
(c[1][3]==0 && c[3][3]==0))) {
markSquare(2,3);
} else if(c[3][1]==2 &&
((c[1][1]==0 && c[2][1]==0) ||
(c[3][2]==0 && c[3][3]==0) ||
(c[2][2]==0 && c[1][3]==0))){
markSquare(3,1);
} else if(c[3][2]==2 &&
((c[1][2]==0 && c[2][2]==0) ||
(c[3][1]==0 && c[3][3]==0))) {
markSquare(3,2);
}else if( c[3][3]==2 &&
((c[1][1]==0 && c[2][2]==0) ||
(c[1][3]==0 && c[2][3]==0) ||
(c[3][1]==0 && c[3][2]==0))) {
markSquare(3,3);
} else {
Random rand = new Random();
int a = rand.nextInt(4);
int b = rand.nextInt(4);
while(a==0 || b==0 || c[a][b]!=2) {
a = rand.nextInt(4);
b = rand.nextInt(4);
}
markSquare(a,b);
}
}

Page 22 of 24
M.TECH(CSE)-II SEMESTER MOBILE APPLICATION DEVELOPMENT LAB

private void markSquare(int x, int y) {


b[x][y].setEnabled(false);
b[x][y].setText("X");
c[x][y] = 1;
checkBoard();
}
}

// check the board to see if someone has won


private boolean checkBoard() {
boolean gameOver = false;
if ((c[1][1] == 0 && c[2][2] == 0 && c[3][3] == 0)
|| (c[1][3] == 0 && c[2][2] == 0 && c[3][1] == 0)
|| (c[1][2] == 0 && c[2][2] == 0 && c[3][2] == 0)
|| (c[1][3] == 0 && c[2][3] == 0 && c[3][3] == 0)
|| (c[1][1] == 0 && c[1][2] == 0 && c[1][3] == 0)
|| (c[2][1] == 0 && c[2][2] == 0 && c[2][3] == 0)
|| (c[3][1] == 0 && c[3][2] == 0 && c[3][3] == 0)
|| (c[1][1] == 0 && c[2][1] == 0 && c[3][1] == 0)) {
textView.setText("Game over. You win!");
gameOver = true;
} else if ((c[1][1] == 1 && c[2][2] == 1 && c[3][3] == 1)
|| (c[1][3] == 1 && c[2][2] == 1 && c[3][1] == 1)
|| (c[1][2] == 1 && c[2][2] == 1 && c[3][2] == 1)
|| (c[1][3] == 1 && c[2][3] == 1 && c[3][3] == 1)
|| (c[1][1] == 1 && c[1][2] == 1 && c[1][3] == 1)
|| (c[2][1] == 1 && c[2][2] == 1 && c[2][3] == 1)
|| (c[3][1] == 1 && c[3][2] == 1 && c[3][3] == 1)
|| (c[1][1] == 1 && c[2][1] == 1 && c[3][1] == 1)) {
textView.setText("Game over. You lost!");
gameOver = true;
} else {
boolean empty = false;
for(i=1; i<=3; i++) {
for(j=1; j<=3; j++) {
if(c[i][j]==2) {
empty = true;
break;
}
}
}
if(!empty) {
gameOver = true;
textView.setText("Game over. It's a draw!");
}
}

return gameOver;
}
}

Page 23 of 24
M.TECH(CSE)-II SEMESTER MOBILE APPLICATION DEVELOPMENT LAB

Output:-

Page 24 of 24

You might also like