Skip to content

Commit 05cc7c2

Browse files
committed
Add lesson 09b
1 parent 639e342 commit 05cc7c2

File tree

656 files changed

+26469
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

656 files changed

+26469
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea
5+
.DS_Store
6+
/build
7+
/captures
8+
.externalNativeBuild
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 27
5+
buildToolsVersion '27.0.1'
6+
defaultConfig {
7+
applicationId "com.example.android.todolist"
8+
minSdkVersion 15
9+
targetSdkVersion 27
10+
versionCode 1
11+
versionName "1.0"
12+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13+
}
14+
buildTypes {
15+
release {
16+
minifyEnabled false
17+
}
18+
}
19+
}
20+
21+
dependencies {
22+
compile fileTree(dir: 'libs', include: ['*.jar'])
23+
compile 'com.android.support:appcompat-v7:27.0.2'
24+
25+
//add Recycler view dependencies; must match SDK version
26+
compile 'com.android.support:recyclerview-v7:27.0.2'
27+
28+
//FAB dependencies
29+
compile 'com.android.support:design:27.0.2'
30+
31+
// TODO (1) Add room dependencies
32+
33+
//Testing
34+
// Instrumentation dependencies use androidTestCompile
35+
// (as opposed to testCompile for local unit tests run in the JVM)
36+
androidTestCompile 'junit:junit:4.12'
37+
androidTestCompile 'com.android.support:support-annotations:27.0.2'
38+
androidTestCompile 'com.android.support.test:runner:1.0.1'
39+
androidTestCompile 'com.android.support.test:rules:1.0.1'
40+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
package="com.example.android.todolist">
5+
6+
<application
7+
android:allowBackup="false"
8+
android:icon="@mipmap/ic_launcher"
9+
android:label="@string/app_name"
10+
android:supportsRtl="true"
11+
android:theme="@style/AppTheme"
12+
tools:ignore="GoogleAppIndexingWarning">
13+
14+
<!-- The manifest entry for the MainActivity -->
15+
<activity android:name="com.example.android.todolist.MainActivity">
16+
<intent-filter>
17+
<action android:name="android.intent.action.MAIN" />
18+
19+
<category android:name="android.intent.category.LAUNCHER" />
20+
</intent-filter>
21+
</activity>
22+
23+
<!-- AddTaskActivity -->
24+
<activity
25+
android:name="com.example.android.todolist.AddTaskActivity"
26+
android:label="@string/add_task_activity_name" />
27+
28+
</application>
29+
30+
</manifest>
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/*
2+
* Copyright (C) 2016 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.android.todolist;
18+
19+
import android.content.Intent;
20+
import android.os.Bundle;
21+
import android.support.v7.app.AppCompatActivity;
22+
import android.view.View;
23+
import android.widget.Button;
24+
import android.widget.EditText;
25+
import android.widget.RadioGroup;
26+
27+
import com.example.android.todolist.database.TaskEntry;
28+
29+
30+
public class AddTaskActivity extends AppCompatActivity {
31+
32+
// Extra for the task ID to be received in the intent
33+
public static final String EXTRA_TASK_ID = "extraTaskId";
34+
// Extra for the task ID to be received after rotation
35+
public static final String INSTANCE_TASK_ID = "instanceTaskId";
36+
// Constants for priority
37+
public static final int PRIORITY_HIGH = 1;
38+
public static final int PRIORITY_MEDIUM = 2;
39+
public static final int PRIORITY_LOW = 3;
40+
// Constant for default task id to be used when not in update mode
41+
private static final int DEFAULT_TASK_ID = -1;
42+
// Constant for logging
43+
private static final String TAG = AddTaskActivity.class.getSimpleName();
44+
// Fields for views
45+
EditText mEditText;
46+
RadioGroup mRadioGroup;
47+
Button mButton;
48+
49+
private int mTaskId = DEFAULT_TASK_ID;
50+
51+
protected void onCreate(Bundle savedInstanceState) {
52+
super.onCreate(savedInstanceState);
53+
setContentView(R.layout.activity_add_task);
54+
55+
initViews();
56+
57+
if (savedInstanceState != null && savedInstanceState.containsKey(INSTANCE_TASK_ID)) {
58+
mTaskId = savedInstanceState.getInt(INSTANCE_TASK_ID, DEFAULT_TASK_ID);
59+
}
60+
61+
Intent intent = getIntent();
62+
if (intent != null && intent.hasExtra(EXTRA_TASK_ID)) {
63+
mButton.setText(R.string.update_button);
64+
if (mTaskId == DEFAULT_TASK_ID) {
65+
// populate the UI
66+
}
67+
}
68+
}
69+
70+
@Override
71+
protected void onSaveInstanceState(Bundle outState) {
72+
outState.putInt(INSTANCE_TASK_ID, mTaskId);
73+
super.onSaveInstanceState(outState);
74+
}
75+
76+
/**
77+
* initViews is called from onCreate to init the member variable views
78+
*/
79+
private void initViews() {
80+
mEditText = findViewById(R.id.editTextTaskDescription);
81+
mRadioGroup = findViewById(R.id.radioGroup);
82+
83+
mButton = findViewById(R.id.saveButton);
84+
mButton.setOnClickListener(new View.OnClickListener() {
85+
@Override
86+
public void onClick(View view) {
87+
onSaveButtonClicked();
88+
}
89+
});
90+
}
91+
92+
/**
93+
* populateUI would be called to populate the UI when in update mode
94+
*
95+
* @param task the taskEntry to populate the UI
96+
*/
97+
private void populateUI(TaskEntry task) {
98+
99+
}
100+
101+
/**
102+
* onSaveButtonClicked is called when the "save" button is clicked.
103+
* It retrieves user input and inserts that new task data into the underlying database.
104+
*/
105+
public void onSaveButtonClicked() {
106+
// Not yet implemented
107+
}
108+
109+
/**
110+
* getPriority is called whenever the selected priority needs to be retrieved
111+
*/
112+
public int getPriorityFromViews() {
113+
int priority = 1;
114+
int checkedId = ((RadioGroup) findViewById(R.id.radioGroup)).getCheckedRadioButtonId();
115+
switch (checkedId) {
116+
case R.id.radButton1:
117+
priority = PRIORITY_HIGH;
118+
break;
119+
case R.id.radButton2:
120+
priority = PRIORITY_MEDIUM;
121+
break;
122+
case R.id.radButton3:
123+
priority = PRIORITY_LOW;
124+
}
125+
return priority;
126+
}
127+
128+
/**
129+
* setPriority is called when we receive a task from MainActivity
130+
*
131+
* @param priority the priority value
132+
*/
133+
public void setPriorityInViews(int priority) {
134+
switch (priority) {
135+
case PRIORITY_HIGH:
136+
((RadioGroup) findViewById(R.id.radioGroup)).check(R.id.radButton1);
137+
break;
138+
case PRIORITY_MEDIUM:
139+
((RadioGroup) findViewById(R.id.radioGroup)).check(R.id.radButton2);
140+
break;
141+
case PRIORITY_LOW:
142+
((RadioGroup) findViewById(R.id.radioGroup)).check(R.id.radButton3);
143+
}
144+
}
145+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright (C) 2016 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.android.todolist;
18+
19+
import android.content.Intent;
20+
import android.os.Bundle;
21+
import android.support.design.widget.FloatingActionButton;
22+
import android.support.v7.app.AppCompatActivity;
23+
import android.support.v7.widget.DividerItemDecoration;
24+
import android.support.v7.widget.LinearLayoutManager;
25+
import android.support.v7.widget.RecyclerView;
26+
import android.support.v7.widget.helper.ItemTouchHelper;
27+
import android.view.View;
28+
29+
import static android.support.v7.widget.DividerItemDecoration.VERTICAL;
30+
31+
32+
public class MainActivity extends AppCompatActivity implements TaskAdapter.ItemClickListener {
33+
34+
// Constant for logging
35+
private static final String TAG = MainActivity.class.getSimpleName();
36+
// Member variables for the adapter and RecyclerView
37+
private RecyclerView mRecyclerView;
38+
private TaskAdapter mAdapter;
39+
40+
@Override
41+
protected void onCreate(Bundle savedInstanceState) {
42+
super.onCreate(savedInstanceState);
43+
setContentView(R.layout.activity_main);
44+
45+
// Set the RecyclerView to its corresponding view
46+
mRecyclerView = findViewById(R.id.recyclerViewTasks);
47+
48+
// Set the layout for the RecyclerView to be a linear layout, which measures and
49+
// positions items within a RecyclerView into a linear list
50+
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
51+
52+
// Initialize the adapter and attach it to the RecyclerView
53+
mAdapter = new TaskAdapter(this, this);
54+
mRecyclerView.setAdapter(mAdapter);
55+
56+
DividerItemDecoration decoration = new DividerItemDecoration(getApplicationContext(), VERTICAL);
57+
mRecyclerView.addItemDecoration(decoration);
58+
59+
/*
60+
Add a touch helper to the RecyclerView to recognize when a user swipes to delete an item.
61+
An ItemTouchHelper enables touch behavior (like swipe and move) on each ViewHolder,
62+
and uses callbacks to signal when a user is performing these actions.
63+
*/
64+
new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
65+
@Override
66+
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
67+
return false;
68+
}
69+
70+
// Called when a user swipes left or right on a ViewHolder
71+
@Override
72+
public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {
73+
// Here is where you'll implement swipe to delete
74+
}
75+
}).attachToRecyclerView(mRecyclerView);
76+
77+
/*
78+
Set the Floating Action Button (FAB) to its corresponding View.
79+
Attach an OnClickListener to it, so that when it's clicked, a new intent will be created
80+
to launch the AddTaskActivity.
81+
*/
82+
FloatingActionButton fabButton = findViewById(R.id.fab);
83+
84+
fabButton.setOnClickListener(new View.OnClickListener() {
85+
@Override
86+
public void onClick(View view) {
87+
// Create a new intent to start an AddTaskActivity
88+
Intent addTaskIntent = new Intent(MainActivity.this, AddTaskActivity.class);
89+
startActivity(addTaskIntent);
90+
}
91+
});
92+
}
93+
94+
@Override
95+
public void onItemClickListener(int itemId) {
96+
// Launch AddTaskActivity adding the itemId as an extra in the intent
97+
}
98+
}

0 commit comments

Comments
 (0)