-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddEntry.java
More file actions
107 lines (98 loc) · 3.92 KB
/
Copy pathaddEntry.java
File metadata and controls
107 lines (98 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.example.davidtruong.list;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.support.design.widget.Snackbar;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
public class addEntry extends AppCompatActivity {
private static final String INSERTTAG = "Insertion_addEntry: ";
String categoryName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_entry);
Toolbar myToolbar = findViewById(R.id.addEntryToolbar);
setSupportActionBar(myToolbar);
ActionBar ab = getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(true);
ab.setTitle("Add entry");
ab.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.add)));
//Set the category name TextView
Intent callingIntent = getIntent();
TextView catTextView = findViewById(R.id.entryCat);
categoryName = callingIntent.getStringExtra("category");
ab.setSubtitle(categoryName);
catTextView.setText(categoryName);
}
public void onButtonPress(View view)
{
//Get the information from the entry screen
Log.d(INSERTTAG, "Get the info from the screen");
EditText detailView = findViewById(R.id.entryDetail);
String detail = detailView.getText().toString();
EditText inputAmt = findViewById(R.id.entryAmt);
String amtText = inputAmt.getText().toString();
if (amtText.isEmpty())
{
Log.d(INSERTTAG, "Go to error screen");
hideKeyboard();
updateErrorScreen();
return;
}
String formattedAmtText = MainActivity.amtParse(amtText);
if (formattedAmtText == null)
{
Snackbar amtSnack = Snackbar.make(
findViewById(R.id.addEntryTitle),
"Please enter a proper amount",
Snackbar.LENGTH_SHORT
);
amtSnack.show();
return;
}
Float amt = Float.parseFloat(formattedAmtText);
if (view == findViewById(R.id.entryMinus))
{
//If - was pressed, make the value negative
amt *= -1;
}
Log.d(INSERTTAG, "Amt is: " + amt);
//Add it to the table
Log.d(INSERTTAG, "Make the content and add it in");
ContentValues content = new ContentValues();
content.put(databaseHelper.tableCol1, detail);
content.put(databaseHelper.tableCol2, amt);
//Now do the insertion
Log.d(INSERTTAG, "Add the entry to the table");
MainActivity.maindb.insert(categoryName + "table", null, content);
Log.d(INSERTTAG, "Insertion successful");
//Now go back to Main
Intent mainIntent = new Intent(this, MainActivity.class);
startActivity(mainIntent);
}
public void updateErrorScreen()
{
//Use this to display a Snackbar error message
Snackbar errorSnack = Snackbar.make(
findViewById(R.id.editTitle),
"Please insert an amount",
Snackbar.LENGTH_SHORT);
errorSnack.show();
}
public void hideKeyboard()
{
//Hide the keyboard
TextView view = findViewById(R.id.addEntryTitle);
InputMethodManager keyHider = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
keyHider.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}