Thursday, 19 June 2014

Android Bus Ticket App Source code

Screen Layout for Android XML


activity_seat_selection_screen.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" >

    <include
        android:id="@+id/header"
        layout="@layout/common_header_with_sub_title" />

    <RelativeLayout
        android:id="@+id/busDeckSelectionLayout"
        android:layout_width="fill_parent"
        android:layout_height="45.0dip"
        android:layout_below="@+id/header" >

        <RelativeLayout
            android:id="@+id/busDeckSelectionButtons"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:background="#ffeeeeee" >

            <Button
                android:id="@+id/lowerDeck"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:background="@drawable/left_button"
                android:text="@string/lower"
                android:textColor="@color/black"
                android:textSize="12.0sp" />

            <Button
                android:id="@+id/upperDeck"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@+id/lowerDeck"
                android:background="@drawable/right_button"
                android:text="@string/upper"
                android:textColor="@color/black"
                android:textSize="12.0sp" />
        </RelativeLayout>
    </RelativeLayout>

    <View
        android:id="@+id/busDeckSelectionMargin"
        android:layout_width="fill_parent"
        android:layout_height="1.0dip"
        android:layout_below="@+id/busDeckSelectionLayout"
        android:background="#ffdddddd" />

    <RelativeLayout
        android:id="@+id/seatSelectionHeader"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/busDeckSelectionMargin" >

        <RelativeLayout
            android:id="@+id/seatsLayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="5.0dip"
            android:layout_marginTop="5.0dip" >

            <TextView
                android:id="@+id/seatsSelectedText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:text="@string/seats_selected"
                android:textColor="@color/black"
                android:textSize="12.0sp" />

            <TextView
                android:id="@+id/seatsSelected"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_below="@+id/seatsSelectedText"
                android:layout_marginTop="10.0dip"
                android:textColor="@color/black"
                android:textSize="14.0sp" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/fareLayout"
            android:layout_width="wrap_content"
            android:layout_height="40.0dip"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/seatsLayout"
            android:background="@layout/fare_seat_selection_background" >

            <TextView
                android:id="@+id/rsText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:layout_marginBottom="10.0dip"
                android:layout_marginLeft="5.0dip"
                android:layout_marginTop="10.0dip"
                android:text="@string/rupees"
                android:textColor="@color/black"
                android:textSize="18.0sp" />

            <TextView
                android:id="@+id/fare"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginBottom="10.0dip"
                android:layout_marginRight="5.0dip"
                android:layout_marginTop="10.0dip"
                android:textColor="@color/black"
                android:textSize="18.0sp" />
        </RelativeLayout>
    </RelativeLayout>

    <View
        android:id="@+id/seatSelectionHeaderMargin"
        android:layout_width="fill_parent"
        android:layout_height="1.0dip"
        android:layout_below="@+id/seatSelectionHeader"
        android:background="#ffdddddd" />

    <RelativeLayout
        android:id="@+id/seatLayoutScroll"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/bottomBarSeatSelection"
        android:layout_below="@+id/seatSelectionHeaderMargin"
        android:background="@color/white" >

        <RelativeLayout
            android:id="@+id/scrollableLayout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <RelativeLayout
                android:id="@+id/seatLegendLayout"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5.0dip"
                android:layout_marginTop="5.0dip" >

                <RelativeLayout
                    android:id="@+id/availableImageLayout"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true" >

                    <ImageView
                        android:id="@+id/availableImage"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentLeft="true"
                        android:layout_marginLeft="10.0dip"
                        android:rotation="90.0"
                        android:src="@drawable/seat_layout_tab_nor_avl" />

                    <TextView
                        android:id="@+id/availableText"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerVertical="true"
                        android:layout_marginLeft="5.0dip"
                        android:layout_toRightOf="@+id/availableImage"
                        android:text="@string/available"
                        android:textSize="12.0sp" />
                </RelativeLayout>

                <RelativeLayout
                    android:id="@+id/bookedImageLayout"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_toRightOf="@+id/availableImageLayout" >

                    <ImageView
                        android:id="@+id/bookedImage"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentLeft="true"
                        android:layout_centerVertical="true"
                        android:layout_marginLeft="10.0dip"
                        android:rotation="90.0"
                        android:src="@drawable/seat_layout_tab_nor_bkd" />

                    <TextView
                        android:id="@+id/bookedText"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerVertical="true"
                        android:layout_marginLeft="5.0dip"
                        android:layout_toRightOf="@+id/bookedImage"
                        android:text="@string/booked"
                        android:textSize="12.0sp" />
                </RelativeLayout>

                <RelativeLayout
                    android:id="@+id/selectedImageLayout"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_toRightOf="@+id/bookedImageLayout" >

                    <ImageView
                        android:id="@+id/selectedImage"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10.0dip"
                        android:rotation="90.0"
                        android:src="@drawable/seat_layout_tab_nor_std" />

                    <TextView
                        android:id="@+id/selectedText"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerVertical="true"
                        android:layout_marginLeft="5.0dip"
                        android:layout_toRightOf="@+id/selectedImage"
                        android:text="@string/selected"
                        android:textSize="12.0sp" />
                </RelativeLayout>

                <RelativeLayout
                    android:id="@+id/ladiesImageLayout"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/availableImageLayout"
                    android:layout_marginTop="5.0dip" >

                    <ImageView
                        android:id="@+id/ladiesImage"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10.0dip"
                        android:rotation="90.0"
                        android:src="@drawable/seat_layout_tab_nor_lad_avl" />

                    <TextView
                        android:id="@+id/ladiesText"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerVertical="true"
                        android:layout_marginLeft="5.0dip"
                        android:layout_toRightOf="@+id/ladiesImage"
                        android:text="@string/ladies"
                        android:textSize="12.0sp" />
                </RelativeLayout>
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/seatLayoutLowerMain"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_below="@+id/seatLegendLayout"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="10.0dip"
                android:background="@layout/seat_layout_border"
                android:paddingBottom="5.0dp"
                android:paddingRight="5.0dp" >

                <GridView
                    android:id="@+id/gridView1"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_margin="4dp"
                    android:columnWidth="100dp"
                    android:gravity="center"
                    android:numColumns="4"
                    android:stretchMode="columnWidth"
                    android:layout_below="@+id/driver" >
                </GridView>
               
                <RelativeLayout
                    android:id="@+id/driver"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_alignRight="@+id/gridView1"
                    android:layout_marginRight="20.0dp"
                    android:layout_marginTop="5.0dp"
                    android:background="@drawable/steering_icon"
                    android:orientation="horizontal" >
                </RelativeLayout>
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/seatLayoutUpper"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/seatLegendLayout"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="10.0dip"
                android:background="@layout/seat_layout_border"
                android:visibility="invisible" />
        </RelativeLayout>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/bottomBarSeatSelection"
        android:layout_width="fill_parent"
        android:layout_height="49.0dip"
        android:layout_alignParentBottom="true"
        android:background="@color/white" >

        <Button
            android:id="@+id/doneButton"
            android:layout_width="160.0dip"
            android:layout_height="48.0dip"
            android:layout_alignParentLeft="true"
            android:layout_marginTop="1.0dip"
            android:background="@drawable/selector_red_btn"
            android:text="@string/done"
            android:textColor="@color/white"
            android:textSize="18.0sp" />

        <Button
            android:id="@+id/cancelButton"
            android:layout_width="160.0dip"
            android:layout_height="48.0dip"
            android:layout_alignParentRight="true"
            android:layout_marginTop="1.0dip"
            android:background="@drawable/selector_black_btn"
            android:text="@string/goBack"
            android:textColor="@color/white"
            android:textSize="18.0sp" />
    </RelativeLayout>

</RelativeLayout>

Android Java Code For Seat Selection


SeatSelectionActivity.java

package com.ipay.mobilemoney;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

import com.ipay.arrayadapter.CustomGridViewAdapter;
import com.ipay.arrayadapter.CustomGridViewAdapter.RecordHolder;
import com.ipay.arrayadapter.Item;

/**
 *
 * @author Saurabh tomar
 *
 */

public class SeatSelectionActivityNew extends Activity implements OnItemClickListener
{
    GridView gridView;
    ArrayList<Item> gridArray = new ArrayList<Item>();
    CustomGridViewAdapter customGridAdapter;
    public Bitmap seatIcon;
    public Bitmap seatSelect;

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

        // set grid view item
        seatIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.seat_layout_screen_nor_avl);
        seatSelect = BitmapFactory.decodeResource(this.getResources(), R.drawable.seat_layout_screen_nor_std);
        totalSeat(50);

        gridView = (GridView) findViewById(R.id.gridView1);
        customGridAdapter = new CustomGridViewAdapter(this, R.layout.seatrow_grid, gridArray);
        gridView.setAdapter(customGridAdapter);
        gridView.setOnItemClickListener(this);
    }

    public void totalSeat(int n)
    {
        for (int i = 1; i <= n; ++i)
        {
            gridArray.add(new Item(seatIcon, "seat " + i));

        }
    }

    public void seatSelected(int pos)
    {
        gridArray.remove(pos);
        gridArray.add(pos, new Item(seatSelect, "select"));
        customGridAdapter.notifyDataSetChanged();
    }

    public void seatDeselcted(int pos)
    {

        gridArray.remove(pos);
        int i = pos + 1;
        gridArray.add(pos, new Item(seatIcon, "seat" + i));
        customGridAdapter.notifyDataSetChanged();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {

        Item item = gridArray.get(position);
        Bitmap seatcompare = item.getImage();
        if (seatcompare == seatIcon)
        {
            seatSelected(position);
        }
        else
        {
            seatDeselcted(position);

        }

    }

}

Custom Array Adapter For GridView

package com.ipay.arrayadapter;

import java.util.ArrayList;

import android.app.Activity;
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;

import com.ipay.mobilemoney.R;

/**
 *
 * @author Saurabh Tomar
 *
 */
public class CustomGridViewAdapter extends ArrayAdapter<Item>
{

    Context context;
    int layoutResourceId;
    ArrayList<Item> data = new ArrayList<Item>();

    public CustomGridViewAdapter(Context context, int layoutResourceId, ArrayList<Item> data)
    {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        View row = convertView;
        RecordHolder holder = null;

        if (row == null)
        {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new RecordHolder();
            holder.txtTitle = (TextView) row.findViewById(R.id.item_text);
            holder.imageItem = (ImageView) row.findViewById(R.id.item_image);
            row.setTag(holder);
        }
        else
        {
            holder = (RecordHolder) row.getTag();
        }

        Item item = data.get(position);
        holder.txtTitle.setText(item.getTitle());
        holder.imageItem.setImageBitmap(item.getImage());

        return row;
    }

    public static class RecordHolder
    {
        public TextView txtTitle;
        public ImageView imageItem;

    }
}

OUTPUT FROM THE CODE: