Skip to content

Commit d3597b8

Browse files
ujjwalagrawal17Vivek Maskara
authored and
Vivek Maskara
committed
Added hideKeyboard in ViewUtil (commons-app#1488)
1 parent 35f05be commit d3597b8

File tree

6 files changed

+25
-62
lines changed

6 files changed

+25
-62
lines changed

app/src/main/java/fr/free/nrw/commons/auth/LoginActivity.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import fr.free.nrw.commons.mwapi.MediaWikiApi;
4848
import fr.free.nrw.commons.theme.NavigationBaseActivity;
4949
import fr.free.nrw.commons.ui.widget.HtmlTextView;
50+
import fr.free.nrw.commons.utils.ViewUtil;
5051
import io.reactivex.Observable;
5152
import io.reactivex.android.schedulers.AndroidSchedulers;
5253
import io.reactivex.schedulers.Schedulers;
@@ -109,14 +110,14 @@ public void onCreate(Bundle savedInstanceState) {
109110
usernameEdit.addTextChangedListener(textWatcher);
110111
usernameEdit.setOnFocusChangeListener((v, hasFocus) -> {
111112
if (!hasFocus) {
112-
hideKeyboard(v);
113+
ViewUtil.hideKeyboard(v);
113114
}
114115
});
115116

116117
passwordEdit.addTextChangedListener(textWatcher);
117118
passwordEdit.setOnFocusChangeListener((v, hasFocus) -> {
118119
if (!hasFocus) {
119-
hideKeyboard(v);
120+
ViewUtil.hideKeyboard(v);
120121
}
121122
});
122123

@@ -144,16 +145,6 @@ void onPrivacyPolicyClicked() {
144145
Utils.handleWebUrl(this,Uri.parse("https://github.com/commons-app/apps-android-commons/wiki/Privacy-policy\\"));
145146
}
146147

147-
public void hideKeyboard(View view) {
148-
if (view != null) {
149-
InputMethodManager inputMethodManager = (InputMethodManager) this.getSystemService(Activity.INPUT_METHOD_SERVICE);
150-
if (inputMethodManager != null) {
151-
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
152-
}
153-
}
154-
}
155-
156-
157148
@Override
158149
protected void onPostCreate(Bundle savedInstanceState) {
159150
super.onPostCreate(savedInstanceState);

app/src/main/java/fr/free/nrw/commons/category/CategorizationFragment.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package fr.free.nrw.commons.category;
22

33

4-
import android.app.Activity;
54
import android.content.SharedPreferences;
65
import android.os.Bundle;
76
import android.support.v7.app.AlertDialog;
@@ -16,7 +15,6 @@
1615
import android.view.MenuItem;
1716
import android.view.View;
1817
import android.view.ViewGroup;
19-
import android.view.inputmethod.InputMethodManager;
2018
import android.widget.EditText;
2119
import android.widget.ProgressBar;
2220
import android.widget.TextView;
@@ -43,6 +41,7 @@
4341
import fr.free.nrw.commons.mwapi.MediaWikiApi;
4442
import fr.free.nrw.commons.upload.MwVolleyApi;
4543
import fr.free.nrw.commons.utils.StringSortingUtils;
44+
import fr.free.nrw.commons.utils.ViewUtil;
4645
import io.reactivex.Observable;
4746
import io.reactivex.android.schedulers.AndroidSchedulers;
4847
import io.reactivex.schedulers.Schedulers;
@@ -116,7 +115,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
116115

117116
categoriesFilter.setOnFocusChangeListener((v, hasFocus) -> {
118117
if (!hasFocus) {
119-
hideKeyboard(v);
118+
ViewUtil.hideKeyboard(v);
120119
}
121120
});
122121

@@ -128,16 +127,6 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
128127
return rootView;
129128
}
130129

131-
public void hideKeyboard(View view) {
132-
133-
if (view != null) {
134-
InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
135-
if (inputMethodManager != null) {
136-
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
137-
}
138-
}
139-
}
140-
141130
@Override
142131
public void onDestroyView() {
143132
categoriesFilter.removeTextChangedListener(textWatcher);

app/src/main/java/fr/free/nrw/commons/upload/MultipleUploadListFragment.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import fr.free.nrw.commons.R;
3434
import fr.free.nrw.commons.contributions.Contribution;
3535
import fr.free.nrw.commons.media.MediaDetailPagerFragment;
36+
import fr.free.nrw.commons.utils.ViewUtil;
3637

3738
public class MultipleUploadListFragment extends Fragment {
3839

@@ -129,7 +130,7 @@ public void onStop() {
129130

130131
// FIXME: Stops the keyboard from being shown 'stale' while moving out of this fragment into the next
131132
View target = getActivity().getCurrentFocus();
132-
hideKeyboard(target);
133+
ViewUtil.hideKeyboard(target);
133134
}
134135

135136
// FIXME: Wrong result type
@@ -178,22 +179,13 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
178179

179180
baseTitle.setOnFocusChangeListener((v, hasFocus) -> {
180181
if (!hasFocus) {
181-
hideKeyboard(v);
182+
ViewUtil.hideKeyboard(v);
182183
}
183184
});
184185

185186
return view;
186187
}
187188

188-
public void hideKeyboard(View view) {
189-
if (view != null) {
190-
InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
191-
if (inputMethodManager != null) {
192-
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
193-
}
194-
}
195-
}
196-
197189
@Override
198190
public void onDestroyView() {
199191
baseTitle.removeTextChangedListener(textWatcher);

app/src/main/java/fr/free/nrw/commons/upload/ShareActivity.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878

7979
import fr.free.nrw.commons.utils.ImageUtils;
8080
import fr.free.nrw.commons.mwapi.MediaWikiApi;
81+
import fr.free.nrw.commons.utils.ViewUtil;
8182
import timber.log.Timber;
8283

8384

@@ -735,7 +736,7 @@ private void zoomImageFromThumb(final View thumbView, Uri imageuri ) {
735736
if (CurrentAnimator != null) {
736737
CurrentAnimator.cancel();
737738
}
738-
hideKeyboard(ShareActivity.this);
739+
ViewUtil.hideKeyboard(ShareActivity.this.findViewById(R.id.titleEdit | R.id.descEdit));
739740
InputStream input = null;
740741
Bitmap scaled = null;
741742
try {
@@ -905,12 +906,5 @@ public void onAnimationCancel(Animator animation) {
905906

906907
});
907908
}
908-
public static void hideKeyboard(Activity activity) {
909-
View view = activity.findViewById(R.id.titleEdit | R.id.descEdit);
910-
if (view != null) {
911-
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
912-
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
913-
}
914-
}
915909

916910
}

app/src/main/java/fr/free/nrw/commons/upload/SingleUploadFragment.java

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package fr.free.nrw.commons.upload;
22

33
import android.annotation.SuppressLint;
4-
import android.app.Activity;
5-
64
import android.content.Intent;
75
import android.content.SharedPreferences;
86
import android.graphics.Color;
@@ -11,20 +9,17 @@
119
import android.support.annotation.NonNull;
1210
import android.support.v4.view.ViewCompat;
1311
import android.support.v7.app.AlertDialog;
14-
1512
import android.text.Editable;
1613
import android.text.Html;
1714
import android.text.TextWatcher;
1815
import android.text.method.LinkMovementMethod;
19-
2016
import android.view.LayoutInflater;
2117
import android.view.Menu;
2218
import android.view.MenuInflater;
2319
import android.view.MenuItem;
2420
import android.view.MotionEvent;
2521
import android.view.View;
2622
import android.view.ViewGroup;
27-
import android.view.inputmethod.InputMethodManager;
2823
import android.widget.AdapterView;
2924
import android.widget.ArrayAdapter;
3025
import android.widget.Button;
@@ -47,9 +42,9 @@
4742
import fr.free.nrw.commons.Utils;
4843
import fr.free.nrw.commons.di.CommonsDaggerSupportFragment;
4944
import fr.free.nrw.commons.settings.Prefs;
45+
import fr.free.nrw.commons.utils.ViewUtil;
5046
import timber.log.Timber;
5147

52-
import static android.view.MotionEvent.ACTION_DOWN;
5348
import static android.view.MotionEvent.ACTION_UP;
5449

5550
public class SingleUploadFragment extends CommonsDaggerSupportFragment {
@@ -168,13 +163,13 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
168163

169164
titleEdit.setOnFocusChangeListener((v, hasFocus) -> {
170165
if (!hasFocus) {
171-
hideKeyboard(v);
166+
ViewUtil.hideKeyboard(v);
172167
}
173168
});
174169

175170
descEdit.setOnFocusChangeListener((v, hasFocus) -> {
176171
if(!hasFocus){
177-
hideKeyboard(v);
172+
ViewUtil.hideKeyboard(v);
178173
}
179174
});
180175

@@ -183,15 +178,6 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
183178
return rootView;
184179
}
185180

186-
public void hideKeyboard(View view) {
187-
if (view != null) {
188-
InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
189-
if (inputMethodManager != null) {
190-
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
191-
}
192-
}
193-
}
194-
195181
@Override
196182
public void onDestroyView() {
197183
titleEdit.removeTextChangedListener(textWatcher);
@@ -305,7 +291,7 @@ public void onStop() {
305291

306292
// FIXME: Stops the keyboard from being shown 'stale' while moving out of this fragment into the next
307293
View target = getActivity().getCurrentFocus();
308-
hideKeyboard(target);
294+
ViewUtil.hideKeyboard(target);
309295
}
310296

311297
@NonNull

app/src/main/java/fr/free/nrw/commons/utils/ViewUtil.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.support.design.widget.Snackbar;
66
import android.view.Display;
77
import android.view.View;
8+
import android.view.inputmethod.InputMethodManager;
89
import android.widget.Toast;
910

1011
public class ViewUtil {
@@ -27,4 +28,14 @@ public static boolean isPortrait(Context context) {
2728
}
2829
}
2930

31+
public static void hideKeyboard(View view){
32+
if (view != null) {
33+
InputMethodManager manager = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
34+
view.clearFocus();
35+
if (manager != null) {
36+
manager.hideSoftInputFromWindow(view.getWindowToken(), 0);
37+
}
38+
}
39+
}
40+
3041
}

0 commit comments

Comments
 (0)