Skip to content
This repository was archived by the owner on Jun 27, 2020. It is now read-only.

Commit 93c8ae5

Browse files
author
evgenii
committed
Moved the add/cancel buttons to actionbar.
1 parent 1a51519 commit 93c8ae5

File tree

7 files changed

+55
-78
lines changed

7 files changed

+55
-78
lines changed

app/src/main/java/com/blogspot/e_kanivets/moneytracker/fragment/AddExpenseFragment.java

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
import android.support.v7.app.ActionBar;
77
import android.support.v7.app.ActionBarActivity;
88
import android.view.LayoutInflater;
9+
import android.view.Menu;
10+
import android.view.MenuInflater;
11+
import android.view.MenuItem;
912
import android.view.View;
1013
import android.view.ViewGroup;
11-
import android.widget.Button;
1214
import android.widget.EditText;
1315
import android.widget.Toast;
1416

@@ -24,7 +26,7 @@
2426
* Use the {@link AddExpenseFragment#newInstance} factory method to
2527
* create an instance of this fragment.
2628
*/
27-
public class AddExpenseFragment extends Fragment implements View.OnClickListener {
29+
public class AddExpenseFragment extends Fragment {
2830
public static final String TAG = "AddExpenseFragment";
2931

3032
private static final String KEY_RECORD = "key_record";
@@ -72,6 +74,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
7274
View rootView = inflater.inflate(R.layout.fragment_add_expense, container, false);
7375
initViews(rootView);
7476
initActionBar();
77+
setHasOptionsMenu(true);
7578
return rootView;
7679
}
7780

@@ -83,9 +86,15 @@ public void onAttach(Activity activity) {
8386
}
8487

8588
@Override
86-
public void onClick(View v) {
87-
switch (v.getId()) {
88-
case R.id.btn_add:
89+
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
90+
inflater.inflate(R.menu.menu_add_record, menu);
91+
super.onCreateOptionsMenu(menu, inflater);
92+
}
93+
94+
@Override
95+
public boolean onOptionsItemSelected(MenuItem item) {
96+
switch (item.getItemId()) {
97+
case R.id.action_done:
8998
String title = etTitle.getText().toString();
9099
String category = etCategory.getText().toString();
91100

@@ -110,14 +119,14 @@ public void onClick(View v) {
110119
}
111120

112121
finish();
113-
break;
122+
return true;
114123

115-
case R.id.btn_cancel:
124+
case R.id.action_close:
116125
finish();
117-
break;
126+
return true;
118127

119128
default:
120-
break;
129+
return super.onOptionsItemSelected(item);
121130
}
122131
}
123132

@@ -127,19 +136,12 @@ private void initViews(final View rootView) {
127136
etCategory = (EditText) rootView.findViewById(R.id.et_category);
128137
etPrice = (EditText) rootView.findViewById(R.id.et_price);
129138

130-
Button buttonAdd = (Button) rootView.findViewById(R.id.btn_add);
131-
132139
//Add texts to dialog if it's edit dialog
133140
if (mode == Mode.MODE_EDIT) {
134141
etTitle.setText(record.getTitle());
135142
etCategory.setText(record.getCategory());
136143
etPrice.setText(Integer.toString(record.getPrice()));
137-
138-
buttonAdd.setText(getResources().getString(R.string.save));
139144
}
140-
141-
buttonAdd.setOnClickListener(this);
142-
rootView.findViewById(R.id.btn_cancel).setOnClickListener(this);
143145
}
144146
}
145147

app/src/main/java/com/blogspot/e_kanivets/moneytracker/fragment/AddIncomeFragment.java

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import android.support.v7.app.ActionBar;
77
import android.support.v7.app.ActionBarActivity;
88
import android.view.LayoutInflater;
9+
import android.view.Menu;
10+
import android.view.MenuInflater;
11+
import android.view.MenuItem;
912
import android.view.View;
1013
import android.view.ViewGroup;
1114
import android.widget.Button;
@@ -24,7 +27,7 @@
2427
* Use the {@link AddIncomeFragment#newInstance} factory method to
2528
* create an instance of this fragment.
2629
*/
27-
public class AddIncomeFragment extends Fragment implements View.OnClickListener {
30+
public class AddIncomeFragment extends Fragment {
2831
public static final String TAG = "AddIncomeFragment";
2932

3033
private static final String KEY_RECORD = "key_record";
@@ -72,6 +75,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
7275
View rootView = inflater.inflate(R.layout.fragment_add_income, container, false);
7376
initViews(rootView);
7477
initActionBar();
78+
setHasOptionsMenu(true);
7579
return rootView;
7680
}
7781

@@ -83,9 +87,15 @@ public void onAttach(Activity activity) {
8387
}
8488

8589
@Override
86-
public void onClick(View v) {
87-
switch (v.getId()) {
88-
case R.id.btn_add:
90+
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
91+
inflater.inflate(R.menu.menu_add_record, menu);
92+
super.onCreateOptionsMenu(menu, inflater);
93+
}
94+
95+
@Override
96+
public boolean onOptionsItemSelected(MenuItem item) {
97+
switch (item.getItemId()) {
98+
case R.id.action_done:
8999
String title = etTitle.getText().toString();
90100
String category = etCategory.getText().toString();
91101

@@ -100,22 +110,23 @@ public void onClick(View v) {
100110
if (mode == Mode.MODE_EDIT) {
101111
MTHelper.getInstance().updateRecordById(record.getId(), title, category, price);
102112
}
103-
finish();
104113
} else {
105114
throw new NumberFormatException();
106115
}
107116
} catch (NumberFormatException e) {
108117
Toast.makeText(getActivity(), getResources().getString(R.string.wrong_number_text),
109118
Toast.LENGTH_SHORT).show();
110119
}
111-
break;
120+
finish();
112121

113-
case R.id.btn_cancel:
122+
return true;
123+
124+
case R.id.action_close:
114125
finish();
115-
break;
126+
return true;
116127

117128
default:
118-
break;
129+
return super.onOptionsItemSelected(item);
119130
}
120131
}
121132

@@ -125,19 +136,12 @@ private void initViews(final View rootView) {
125136
etCategory = (EditText) rootView.findViewById(R.id.et_category);
126137
etPrice = (EditText) rootView.findViewById(R.id.et_price);
127138

128-
Button buttonAdd = (Button) rootView.findViewById(R.id.btn_add);
129-
130139
//Add texts to dialog if it's edit dialog
131140
if (mode == Mode.MODE_EDIT) {
132141
etTitle.setText(record.getTitle());
133142
etCategory.setText(record.getCategory());
134143
etPrice.setText(Integer.toString(record.getPrice()));
135-
136-
buttonAdd.setText(getResources().getString(R.string.save));
137144
}
138-
139-
buttonAdd.setOnClickListener(this);
140-
rootView.findViewById(R.id.btn_cancel).setOnClickListener(this);
141145
}
142146
}
143147

436 Bytes
Loading
308 Bytes
Loading

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

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -84,27 +84,4 @@
8484

8585
</LinearLayout>
8686
</LinearLayout>
87-
88-
<LinearLayout
89-
android:layout_width="match_parent"
90-
android:layout_height="wrap_content"
91-
android:layout_gravity="center_horizontal"
92-
android:layout_margin="10dp"
93-
android:orientation="horizontal">
94-
95-
<Button
96-
android:id="@+id/btn_add"
97-
android:layout_width="match_parent"
98-
android:layout_height="match_parent"
99-
android:layout_weight="1"
100-
android:text="@string/add" />
101-
102-
<Button
103-
android:id="@+id/btn_cancel"
104-
android:layout_width="match_parent"
105-
android:layout_height="wrap_content"
106-
android:layout_weight="1"
107-
android:text="@string/cancel" />
108-
</LinearLayout>
109-
11087
</LinearLayout>

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

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -84,27 +84,4 @@
8484

8585
</LinearLayout>
8686
</LinearLayout>
87-
88-
<LinearLayout
89-
android:layout_width="match_parent"
90-
android:layout_height="wrap_content"
91-
android:layout_gravity="center_horizontal"
92-
android:layout_margin="10dp"
93-
android:orientation="horizontal">
94-
95-
<Button
96-
android:id="@+id/btn_add"
97-
android:layout_width="match_parent"
98-
android:layout_height="match_parent"
99-
android:layout_weight="1"
100-
android:text="@string/add" />
101-
102-
<Button
103-
android:id="@+id/btn_cancel"
104-
android:layout_width="match_parent"
105-
android:layout_height="wrap_content"
106-
android:layout_weight="1"
107-
android:text="@string/cancel" />
108-
</LinearLayout>
109-
11087
</LinearLayout>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<menu xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/apk/res-auto">
4+
5+
<item
6+
android:id="@+id/action_done"
7+
android:icon="@drawable/ic_done_white_24dp"
8+
android:title="@string/add"
9+
tools:showAsAction="ifRoom" />
10+
11+
<item
12+
android:id="@+id/action_close"
13+
android:icon="@drawable/ic_close_white_24dp"
14+
android:title="@string/add"
15+
tools:showAsAction="ifRoom" />
16+
17+
</menu>

0 commit comments

Comments
 (0)