Skip to content

Commit 2c88924

Browse files
committed
TLIB.06-Solution-AddButterknife
1 parent ff4ef95 commit 2c88924

3 files changed

Lines changed: 22 additions & 33 deletions

File tree

app/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ dependencies {
2727
compile 'com.android.support:design:25.2.0'
2828
compile 'com.android.support:appcompat-v7:25.2.0'
2929
compile 'com.google.android.gms:play-services-vision:10.2.0'
30-
// TODO (1): Add Butterknife library and annotations dependency
30+
compile 'com.jakewharton:butterknife:8.4.0'
31+
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
3132
testCompile 'junit:junit:4.12'
3233
}

app/src/main/java/com/example/android/emojify/MainActivity.java

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,26 @@
3939
import java.io.File;
4040
import java.io.IOException;
4141

42+
import butterknife.BindView;
43+
import butterknife.ButterKnife;
44+
import butterknife.OnClick;
45+
4246
public class MainActivity extends AppCompatActivity {
4347

44-
// TODO (2): Replace all View declarations with Butterknife annotations
4548

4649
private static final int REQUEST_IMAGE_CAPTURE = 1;
4750
private static final int REQUEST_STORAGE_PERMISSION = 1;
4851

4952
private static final String FILE_PROVIDER_AUTHORITY = "com.example.android.fileprovider";
5053

51-
private ImageView mImageView;
54+
@BindView(R.id.image_view) ImageView mImageView;
5255

53-
private Button mEmojifyButton;
54-
private FloatingActionButton mShareFab;
55-
private FloatingActionButton mSaveFab;
56-
private FloatingActionButton mClearFab;
56+
@BindView(R.id.emojify_button) Button mEmojifyButton;
57+
@BindView(R.id.share_button) FloatingActionButton mShareFab;
58+
@BindView(R.id.save_button) FloatingActionButton mSaveFab;
59+
@BindView(R.id.clear_button) FloatingActionButton mClearFab;
5760

58-
private TextView mTitleTextView;
61+
@BindView(R.id.title_text_view) TextView mTitleTextView;
5962

6063
private String mTempPhotoPath;
6164

@@ -67,22 +70,15 @@ protected void onCreate(Bundle savedInstanceState) {
6770
super.onCreate(savedInstanceState);
6871
setContentView(R.layout.activity_main);
6972

70-
// TODO (3): Replace the findViewById calls with the Butterknife data binding
7173
// Bind the views
72-
mImageView = (ImageView) findViewById(R.id.image_view);
73-
mEmojifyButton = (Button) findViewById(R.id.emojify_button);
74-
mShareFab = (FloatingActionButton) findViewById(R.id.share_button);
75-
mSaveFab = (FloatingActionButton) findViewById(R.id.save_button);
76-
mClearFab = (FloatingActionButton) findViewById(R.id.clear_button);
77-
mTitleTextView = (TextView) findViewById(R.id.title_text_view);
74+
ButterKnife.bind(this);
7875
}
7976

8077
/**
8178
* OnClick method for "Emojify Me!" Button. Launches the camera app.
82-
*
83-
* @param view The emojify me button.
8479
*/
85-
public void emojifyMe(View view) {
80+
@OnClick(R.id.emojify_button)
81+
public void emojifyMe() {
8682
// Check for the external storage permission
8783
if (ContextCompat.checkSelfPermission(this,
8884
Manifest.permission.WRITE_EXTERNAL_STORAGE)
@@ -193,13 +189,12 @@ private void processAndSetImage() {
193189
}
194190

195191

196-
// TODO (4): Replace OnClick methods with Butterknife annotations for OnClicks
192+
197193
/**
198194
* OnClick method for the save button.
199-
*
200-
* @param view The save button.
201195
*/
202-
public void saveMe(View view) {
196+
@OnClick(R.id.save_button)
197+
public void saveMe() {
203198
// Delete the temporary image file
204199
BitmapUtils.deleteImageFile(this, mTempPhotoPath);
205200

@@ -209,10 +204,9 @@ public void saveMe(View view) {
209204

210205
/**
211206
* OnClick method for the share button, saves and shares the new bitmap.
212-
*
213-
* @param view The share button.
214207
*/
215-
public void shareMe(View view) {
208+
@OnClick(R.id.share_button)
209+
public void shareMe() {
216210
// Delete the temporary image file
217211
BitmapUtils.deleteImageFile(this, mTempPhotoPath);
218212

@@ -225,10 +219,9 @@ public void shareMe(View view) {
225219

226220
/**
227221
* OnClick for the clear button, resets the app to original state.
228-
*
229-
* @param view The clear button.
230222
*/
231-
public void clearImage(View view) {
223+
@OnClick(R.id.clear_button)
224+
public void clearImage() {
232225
// Clear the image and toggle the view visibility
233226
mImageView.setImageResource(0);
234227
mEmojifyButton.setVisibility(View.VISIBLE);

app/src/main/res/layout/activity_main.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
See the License for the specific language governing permissions and
1414
limitations under the License.-->
1515

16-
<!-- TODO (5): Remove all the onClick methods in the XML -->
1716

1817
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
1918
xmlns:app="http://schemas.android.com/apk/res-auto"
@@ -52,7 +51,6 @@
5251
android:layout_height="wrap_content"
5352
android:layout_centerHorizontal="true"
5453
android:layout_centerVertical="true"
55-
android:onClick="emojifyMe"
5654
android:text="@string/go"
5755
android:textAppearance="@style/TextAppearance.AppCompat.Display1"/>
5856

@@ -63,7 +61,6 @@
6361
android:layout_alignParentEnd="true"
6462
android:layout_alignParentRight="true"
6563
android:layout_alignParentTop="true"
66-
android:onClick="clearImage"
6764
android:src="@drawable/ic_clear"
6865
android:visibility="gone"
6966
app:backgroundTint="@android:color/white"
@@ -79,7 +76,6 @@
7976
android:layout_marginBottom="@dimen/fab_margins"
8077
android:layout_marginEnd="@dimen/fab_margins"
8178
android:layout_marginRight="@dimen/fab_margins"
82-
android:onClick="saveMe"
8379
android:src="@drawable/ic_save"
8480
android:visibility="gone"
8581
app:backgroundTint="@android:color/white" />
@@ -94,7 +90,6 @@
9490
android:layout_marginBottom="@dimen/fab_margins"
9591
android:layout_marginLeft="@dimen/fab_margins"
9692
android:layout_marginStart="@dimen/fab_margins"
97-
android:onClick="shareMe"
9893
android:src="@drawable/ic_share"
9994
android:visibility="gone"
10095
app:backgroundTint="@android:color/white" />

0 commit comments

Comments
 (0)