mirror of
https://github.com/hmalik144/Udacity_Project_9---Habit_Tracker.git
synced 2026-01-31 02:51:45 +00:00
Initial commit
This commit is contained in:
1
app/.gitignore
vendored
Normal file
1
app/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/build
|
||||
29
app/build.gradle
Normal file
29
app/build.gradle
Normal file
@@ -0,0 +1,29 @@
|
||||
apply plugin: 'com.android.application'
|
||||
|
||||
android {
|
||||
compileSdkVersion 25
|
||||
buildToolsVersion "25.0.2"
|
||||
defaultConfig {
|
||||
applicationId "com.example.h_mal.habittrackerudacityh_mal"
|
||||
minSdkVersion 15
|
||||
targetSdkVersion 25
|
||||
versionCode 1
|
||||
versionName "1.0"
|
||||
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
|
||||
}
|
||||
buildTypes {
|
||||
release {
|
||||
minifyEnabled false
|
||||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compile fileTree(dir: 'libs', include: ['*.jar'])
|
||||
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
|
||||
exclude group: 'com.android.support', module: 'support-annotations'
|
||||
})
|
||||
compile 'com.android.support:appcompat-v7:25.3.0'
|
||||
testCompile 'junit:junit:4.12'
|
||||
}
|
||||
17
app/proguard-rules.pro
vendored
Normal file
17
app/proguard-rules.pro
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
# Add project specific ProGuard rules here.
|
||||
# By default, the flags in this file are appended to flags specified
|
||||
# in C:\Users\h_mal\AppData\Local\Android\Sdk/tools/proguard/proguard-android.txt
|
||||
# You can edit the include path and order by changing the proguardFiles
|
||||
# directive in build.gradle.
|
||||
#
|
||||
# For more details, see
|
||||
# http://developer.android.com/guide/developing/tools/proguard.html
|
||||
|
||||
# Add any project specific keep options here:
|
||||
|
||||
# If your project uses WebView with JS, uncomment the following
|
||||
# and specify the fully qualified class name to the JavaScript interface
|
||||
# class:
|
||||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
|
||||
# public *;
|
||||
#}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.example.h_mal.habittrackerudacityh_mal;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.test.InstrumentationRegistry;
|
||||
import android.support.test.runner.AndroidJUnit4;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Instrumentation test, which will execute on an Android device.
|
||||
*
|
||||
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
|
||||
*/
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
public class ExampleInstrumentedTest {
|
||||
@Test
|
||||
public void useAppContext() throws Exception {
|
||||
// Context of the app under test.
|
||||
Context appContext = InstrumentationRegistry.getTargetContext();
|
||||
|
||||
assertEquals("com.example.h_mal.habittrackerudacityh_mal", appContext.getPackageName());
|
||||
}
|
||||
}
|
||||
20
app/src/main/AndroidManifest.xml
Normal file
20
app/src/main/AndroidManifest.xml
Normal file
@@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.example.h_mal.habittrackerudacityh_mal">
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:label="@string/app_name"
|
||||
android:supportsRtl="true"
|
||||
android:theme="@style/AppTheme">
|
||||
<activity android:name=".MainActivity">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.example.h_mal.habittrackerudacityh_mal;
|
||||
|
||||
import android.provider.BaseColumns;
|
||||
|
||||
/**
|
||||
* Created by h_mal on 26/03/2017.
|
||||
*/
|
||||
|
||||
public class HabitContract {
|
||||
|
||||
private HabitContract() {}
|
||||
|
||||
public static final class HabitEntry implements BaseColumns {
|
||||
|
||||
public final static String TABLE_NAME = "Habits";
|
||||
|
||||
|
||||
public final static String _ID = BaseColumns._ID;
|
||||
|
||||
|
||||
public final static String COLUMN_HABIT_NAME ="name";
|
||||
|
||||
|
||||
public final static String COLUMN_HABIT_FREQUENCY = "frequency";
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package com.example.h_mal.habittrackerudacityh_mal;
|
||||
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.database.sqlite.SQLiteOpenHelper;
|
||||
|
||||
import com.example.h_mal.habittrackerudacityh_mal.HabitContract.HabitEntry;
|
||||
|
||||
/**
|
||||
* Created by h_mal on 26/03/2017.
|
||||
*/
|
||||
|
||||
public class HabitDbHelper extends SQLiteOpenHelper {
|
||||
|
||||
public static final String LOG_TAG = HabitDbHelper.class.getSimpleName();
|
||||
|
||||
private static final String DATABASE_NAME = "Habits.db";
|
||||
|
||||
private static final int DATABASE_VERSION = 1;
|
||||
|
||||
public HabitDbHelper(Context context) {
|
||||
super(context, DATABASE_NAME, null, DATABASE_VERSION);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(SQLiteDatabase db) {
|
||||
|
||||
String SQL_CREATE_HABITS_TABLE = "CREATE TABLE " + HabitEntry.TABLE_NAME + " ("
|
||||
+ HabitEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
|
||||
+ HabitEntry.COLUMN_HABIT_NAME + " TEXT NOT NULL, "
|
||||
+ HabitEntry.COLUMN_HABIT_FREQUENCY + " INTEGER NOT NULL DEFAULT 0);";
|
||||
|
||||
db.execSQL(SQL_CREATE_HABITS_TABLE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
||||
}
|
||||
|
||||
public Cursor readAllHabits() {
|
||||
SQLiteDatabase db = getReadableDatabase();
|
||||
|
||||
String[] projection = {
|
||||
HabitEntry._ID,
|
||||
HabitEntry.COLUMN_HABIT_NAME,
|
||||
HabitEntry.COLUMN_HABIT_FREQUENCY };
|
||||
|
||||
Cursor cursor = db.query(
|
||||
HabitEntry.TABLE_NAME,
|
||||
projection,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null);
|
||||
return cursor;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
package com.example.h_mal.habittrackerudacityh_mal;
|
||||
|
||||
import android.content.ContentValues;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.os.Bundle;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.text.TextUtils;
|
||||
import android.view.View;
|
||||
import android.widget.EditText;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.example.h_mal.habittrackerudacityh_mal.HabitContract.HabitEntry;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
|
||||
private EditText mNameEditText;
|
||||
|
||||
private EditText mFrequencyEditText;
|
||||
|
||||
private HabitDbHelper mDbHelper;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
mNameEditText = (EditText) findViewById(R.id.autoCompleteTextView);
|
||||
mFrequencyEditText = (EditText) findViewById(R.id.editText3);
|
||||
|
||||
mDbHelper = new HabitDbHelper(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStart() {
|
||||
super.onStart();
|
||||
displayDatabaseInfo();
|
||||
}
|
||||
|
||||
private void insertHabit() {
|
||||
String nameString = mNameEditText.getText().toString().trim();
|
||||
String frequencyString = mFrequencyEditText.getText().toString().trim();
|
||||
|
||||
if (
|
||||
TextUtils.isEmpty(nameString) || TextUtils.isEmpty(frequencyString)) {
|
||||
Toast.makeText(MainActivity.this, "please insert all data", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
|
||||
int frequency = Integer.parseInt(frequencyString);
|
||||
|
||||
HabitDbHelper mDbHelper = new HabitDbHelper(this);
|
||||
|
||||
SQLiteDatabase db = mDbHelper.getWritableDatabase();
|
||||
|
||||
ContentValues values = new ContentValues();
|
||||
|
||||
values.put(HabitEntry.COLUMN_HABIT_NAME, nameString);
|
||||
|
||||
values.put(HabitEntry.COLUMN_HABIT_FREQUENCY, frequency);
|
||||
|
||||
long newRowId = db.insert(HabitEntry.TABLE_NAME, null, values);
|
||||
|
||||
if (newRowId == -1) {
|
||||
Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
|
||||
} else {
|
||||
Toast.makeText(this, "New row created", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void displayDatabaseInfo() {
|
||||
|
||||
HabitDbHelper habitDbHelper = new HabitDbHelper(this);
|
||||
Cursor cursor = habitDbHelper.readAllHabits();
|
||||
|
||||
TextView displayView = (TextView) findViewById(R.id.textbox);
|
||||
|
||||
try {
|
||||
displayView.setText(cursor.getCount() + " Habits in habits table.\n\n");
|
||||
displayView.append(HabitEntry._ID + " - " +
|
||||
HabitEntry.COLUMN_HABIT_NAME + " - " +
|
||||
HabitEntry.COLUMN_HABIT_FREQUENCY + "\n");
|
||||
|
||||
int idColumnIndex = cursor.getColumnIndex(HabitEntry._ID);
|
||||
int nameColumnIndex = cursor.getColumnIndex(HabitEntry.COLUMN_HABIT_NAME);
|
||||
int frequencyColumnIndex = cursor.getColumnIndex(HabitEntry.COLUMN_HABIT_FREQUENCY);
|
||||
|
||||
while (cursor.moveToNext()) {
|
||||
int currentID = cursor.getInt(idColumnIndex);
|
||||
String currentName = cursor.getString(nameColumnIndex);
|
||||
int currentFrequency = cursor.getInt(frequencyColumnIndex);
|
||||
displayView.append(("\n" + currentID + " - " +
|
||||
currentName + " - " +
|
||||
currentFrequency));
|
||||
}
|
||||
} finally {
|
||||
cursor.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void onClick(View view){
|
||||
insertHabit();
|
||||
displayDatabaseInfo();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
59
app/src/main/res/layout/activity_main.xml
Normal file
59
app/src/main/res/layout/activity_main.xml
Normal file
@@ -0,0 +1,59 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:id="@+id/activity_main"
|
||||
android:orientation="vertical"
|
||||
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"
|
||||
tools:context="com.example.h_mal.habittrackerudacityh_mal.MainActivity">
|
||||
|
||||
<TextView
|
||||
android:text="Habit"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/textView3"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Body2" />
|
||||
|
||||
<AutoCompleteTextView
|
||||
android:hint="Insert habit name"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/autoCompleteTextView" />
|
||||
|
||||
<TextView
|
||||
android:text="Frequency"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/textView4"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Body2" />
|
||||
|
||||
<EditText
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:inputType="number"
|
||||
android:ems="10"
|
||||
android:id="@+id/editText3"
|
||||
android:hint="Insert Frequency of habit (weekly)" />
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="horizontal"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
<Button
|
||||
android:text="Add"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:onClick="onClick"
|
||||
android:layout_weight="1" />
|
||||
</LinearLayout>
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="something something something..."
|
||||
android:id="@+id/textbox"/>
|
||||
</LinearLayout>
|
||||
BIN
app/src/main/res/mipmap-hdpi/ic_launcher.png
Normal file
BIN
app/src/main/res/mipmap-hdpi/ic_launcher.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 3.3 KiB |
BIN
app/src/main/res/mipmap-mdpi/ic_launcher.png
Normal file
BIN
app/src/main/res/mipmap-mdpi/ic_launcher.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.2 KiB |
BIN
app/src/main/res/mipmap-xhdpi/ic_launcher.png
Normal file
BIN
app/src/main/res/mipmap-xhdpi/ic_launcher.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 4.7 KiB |
BIN
app/src/main/res/mipmap-xxhdpi/ic_launcher.png
Normal file
BIN
app/src/main/res/mipmap-xxhdpi/ic_launcher.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 7.5 KiB |
BIN
app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
Normal file
BIN
app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 10 KiB |
6
app/src/main/res/values-w820dp/dimens.xml
Normal file
6
app/src/main/res/values-w820dp/dimens.xml
Normal file
@@ -0,0 +1,6 @@
|
||||
<resources>
|
||||
<!-- Example customization of dimensions originally defined in res/values/dimens.xml
|
||||
(such as screen margins) for screens with more than 820dp of available width. This
|
||||
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
|
||||
<dimen name="activity_horizontal_margin">64dp</dimen>
|
||||
</resources>
|
||||
6
app/src/main/res/values/colors.xml
Normal file
6
app/src/main/res/values/colors.xml
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<color name="colorPrimary">#3F51B5</color>
|
||||
<color name="colorPrimaryDark">#303F9F</color>
|
||||
<color name="colorAccent">#FF4081</color>
|
||||
</resources>
|
||||
5
app/src/main/res/values/dimens.xml
Normal file
5
app/src/main/res/values/dimens.xml
Normal file
@@ -0,0 +1,5 @@
|
||||
<resources>
|
||||
<!-- Default screen margins, per the Android Design guidelines. -->
|
||||
<dimen name="activity_horizontal_margin">16dp</dimen>
|
||||
<dimen name="activity_vertical_margin">16dp</dimen>
|
||||
</resources>
|
||||
3
app/src/main/res/values/strings.xml
Normal file
3
app/src/main/res/values/strings.xml
Normal file
@@ -0,0 +1,3 @@
|
||||
<resources>
|
||||
<string name="app_name">Habit tracker Udacity H_mal</string>
|
||||
</resources>
|
||||
11
app/src/main/res/values/styles.xml
Normal file
11
app/src/main/res/values/styles.xml
Normal file
@@ -0,0 +1,11 @@
|
||||
<resources>
|
||||
|
||||
<!-- Base application theme. -->
|
||||
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
|
||||
<!-- Customize your theme here. -->
|
||||
<item name="colorPrimary">@color/colorPrimary</item>
|
||||
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
|
||||
<item name="colorAccent">@color/colorAccent</item>
|
||||
</style>
|
||||
|
||||
</resources>
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.example.h_mal.habittrackerudacityh_mal;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Example local unit test, which will execute on the development machine (host).
|
||||
*
|
||||
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
|
||||
*/
|
||||
public class ExampleUnitTest {
|
||||
@Test
|
||||
public void addition_isCorrect() throws Exception {
|
||||
assertEquals(4, 2 + 2);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user