Code examples for week 6
Activity layout xml:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#eeeeee"
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="com.upt.cti.smartwallet.MainActivity6">
<TextView
android:id="@+id/tStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Found 5 payments for this month"/>
<ListView
android:id="@+id/listPayments"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/lBottom"
android:layout_below="@+id/tStatus"
android:divider="@android:color/transparent"
android:dividerHeight="4dp"/>
<LinearLayout
android:id="@+id/lBottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<Button
android:id="@+id/bPrevious"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="clicked"
android:text="← Previous"/>
<Button
android:id="@+id/bNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="clicked"
android:text="Next →"/>
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fabAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/lBottom"
android:layout_alignParentRight="true"
android:clickable="true"
android:onClick="clicked"
android:src="@drawable/ic_add"
app:backgroundTint="@color/blue"/>
</RelativeLayout>
Payment class:
@IgnoreExtraProperties
public class Payment {
public String timestamp;
private double cost;
private String name;
private String type;
public Payment() {
// Default constructor required for calls to DataSnapshot.getValue(User.class)
}
public String getName() {
return name;
}
public double getCost() {
return cost;
}
public String getType() {
return type;
}
}
Payment item xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
android:id="@+id/card_view"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardBackgroundColor="#fff"
card_view:cardCornerRadius="4dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dp">
<RelativeLayout
android:id="@+id/lHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/blue"
android:padding="4dp">
<TextView
android:id="@+id/tIndex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="1"
android:textColor="@color/white"/>
<TextView
android:id="@+id/tName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Fish and chips"
android:textColor="@color/white"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/lHeader"
android:padding="4dp">
<TextView
android:id="@+id/tDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Date: 2016-10-26"/>
<TextView
android:id="@+id/tTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tDate"
android:hint="Time: 11:15:05"/>
<TextView
android:id="@+id/tCost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:hint="12.5"
android:textColor="@color/black"
android:textStyle="bold"/>
<TextView
android:id="@+id/tType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/tCost"
android:hint="food"
android:textStyle="italic"/>
</RelativeLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
PaymentType class:
public class PaymentType {
public static int getColorFromPaymentType(String type) {
type = type.toLowerCase();
if (type.equals("entertainment"))
return Color.rgb(200, 50, 50);
else if (type.equals("food"))
return Color.rgb(50, 150, 50);
else if (type.equals("taxes"))
return Color.rgb(20, 20, 150);
else if (type.equals("travel"))
return Color.rgb(230, 140, 0);
else
return Color.rgb(100, 100, 100);
}
}