Skip to content

Commit 0439d32

Browse files
authored
Merge pull request #85 from NordicSemiconductor/improvements/sample-app
Sample app Improvements
2 parents 36fed6a + 9456272 commit 0439d32

File tree

7 files changed

+70
-52
lines changed

7 files changed

+70
-52
lines changed

sample/src/main/java/no/nordicsemi/android/log/example/fragment/KeyNameDialogFragment.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
package no.nordicsemi.android.log.example.fragment;
3333

3434
import no.nordicsemi.android.log.example.R;
35+
3536
import android.app.Dialog;
3637
import android.content.DialogInterface;
3738
import android.os.Bundle;
@@ -48,8 +49,10 @@
4849
import androidx.appcompat.app.AlertDialog;
4950
import androidx.fragment.app.DialogFragment;
5051

52+
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
53+
5154
public class KeyNameDialogFragment extends DialogFragment implements
52-
OnEditorActionListener, DialogInterface.OnClickListener {
55+
OnEditorActionListener, View.OnClickListener {
5356
public static final String RESULT = "session";
5457
public static final String RESULT_KEY = "session_key";
5558
public static final String RESULT_NAME = "session_name";
@@ -60,24 +63,30 @@ public class KeyNameDialogFragment extends DialogFragment implements
6063
@NonNull
6164
@Override
6265
public Dialog onCreateDialog(@Nullable final Bundle savedInstanceState) {
63-
final AlertDialog.Builder builder = new AlertDialog.Builder(requireContext())
66+
final MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(requireContext())
6467
.setTitle(R.string.dialog_title)
6568
.setNegativeButton(android.R.string.no, null)
66-
.setPositiveButton(android.R.string.ok, this);
69+
.setPositiveButton(android.R.string.ok, null);
6770

6871
final View view = getLayoutInflater()
6972
.inflate(R.layout.fragment_dialog_key_name, null);
7073
mKeyView = view.findViewById(R.id.key);
7174
mNameView = view.findViewById(R.id.name);
7275
builder.setView(view);
73-
return builder.create();
76+
77+
// Setting onClickListener to a button this way allows to
78+
// validate the input.
79+
final AlertDialog dialog = builder.create();
80+
dialog.show();
81+
dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(this);
82+
return dialog;
7483
}
7584

7685
/**
7786
* Called when OK button has been pressed.
7887
*/
7988
@Override
80-
public void onClick(final DialogInterface dialog, final int which) {
89+
public void onClick(final View v) {
8190
// Validate key and name
8291
String key = mKeyView.getText().toString().trim();
8392
String name = mNameView.getText().toString().trim();
@@ -88,24 +97,20 @@ public void onClick(final DialogInterface dialog, final int which) {
8897
mKeyView.setError(getString(R.string.dialog_error));
8998
}
9099

91-
if (TextUtils.isEmpty(name)) {
92-
valid = false;
93-
mNameView.setError(getString(R.string.dialog_error));
94-
}
95-
96100
if (valid) {
97101
final Bundle result = new Bundle();
98102
result.putString(KeyNameDialogFragment.RESULT_KEY, key);
99103
result.putString(KeyNameDialogFragment.RESULT_NAME, name);
100104
getParentFragmentManager().setFragmentResult(KeyNameDialogFragment.RESULT, result);
105+
dismiss();
101106
}
102107
}
103108

104109
@Override
105110
public boolean onEditorAction(final TextView v, final int actionId, final KeyEvent event) {
106111
if (EditorInfo.IME_ACTION_DONE == actionId) {
107112
// Return input text to activity
108-
onClick(null, DialogInterface.BUTTON_POSITIVE);
113+
onClick(null);
109114
return true;
110115
}
111116
return false;

sample/src/main/java/no/nordicsemi/android/log/example/fragment/MainFragment.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ public void onDestroy() {
218218
* If the nRF Logger application is not installed it will change the "Open in nRF Logger"
219219
* button to "Download nRF Logger".
220220
*/
221-
private void createLogSession(String key, String name) {
221+
private void createLogSession(@NonNull String key, @Nullable String name) {
222222
mLogSession = Logger.newSession(requireContext(), key, name);
223223

224224
// Enable buttons
@@ -230,7 +230,7 @@ private void createLogSession(String key, String name) {
230230

231231
// The session is null if nRF Logger is not installed
232232
if (mLogSession == null) {
233-
Toast.makeText(getActivity(), R.string.error_no_nrf_logger, Toast.LENGTH_SHORT).show();
233+
Toast.makeText(requireContext(), R.string.error_no_nrf_logger, Toast.LENGTH_SHORT).show();
234234
mLogSession = LocalLogSession.newSession(requireContext(), MyLogContentProvider.AUTHORITY_URI, key, name);
235235

236236
// The button will be used to download the nRF Logger

sample/src/main/res/layout-land/fragment_main.xml

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@
3434
android:layout_width="match_parent"
3535
android:layout_height="match_parent"
3636
android:orientation="vertical"
37-
android:paddingBottom="@dimen/activity_vertical_margin"
3837
android:paddingLeft="@dimen/activity_horizontal_margin"
38+
android:paddingTop="@dimen/activity_vertical_margin"
3939
android:paddingRight="@dimen/activity_horizontal_margin"
40-
android:paddingTop="@dimen/activity_vertical_margin" >
40+
android:paddingBottom="@dimen/activity_vertical_margin">
4141

42-
<com.google.android.material.button.MaterialButton
42+
<Button
4343
android:id="@+id/action_create"
4444
android:layout_width="match_parent"
4545
android:layout_height="wrap_content"
@@ -48,60 +48,62 @@
4848

4949
<com.google.android.material.textfield.TextInputLayout
5050
android:id="@+id/field_layout"
51+
style="@style/Widget.Material3.TextInputEditText.OutlinedBox"
5152
android:layout_width="match_parent"
5253
android:layout_height="wrap_content"
53-
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
5454
android:hint="@string/action_append_hint">
5555

5656
<com.google.android.material.textfield.TextInputEditText
5757
android:id="@+id/field"
5858
android:layout_width="match_parent"
59-
android:layout_height="wrap_content"/>
59+
android:layout_height="wrap_content" />
6060
</com.google.android.material.textfield.TextInputLayout>
6161

6262
<LinearLayout
6363
android:layout_width="match_parent"
6464
android:layout_height="wrap_content"
65-
android:orientation="horizontal" >
65+
android:orientation="horizontal">
6666

6767
<com.google.android.material.textfield.TextInputLayout
6868
android:id="@+id/log_level_layout"
69+
style="@style/Widget.Material3.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
6970
android:layout_width="0dp"
7071
android:layout_height="wrap_content"
71-
android:layout_weight="1"
72-
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu">
72+
android:layout_weight="1">
7373

74-
<com.google.android.material.textfield.MaterialAutoCompleteTextView
74+
<AutoCompleteTextView
7575
android:id="@+id/log_level"
76+
style="@style/Widget.Material3.AutoCompleteTextView.OutlinedBox"
7677
android:layout_width="match_parent"
7778
android:layout_height="wrap_content"
78-
android:saveEnabled="false"
79-
android:inputType="none"/>
79+
android:hint="Level"
80+
android:inputType="none"
81+
android:saveEnabled="false" />
8082
</com.google.android.material.textfield.TextInputLayout>
8183

82-
<com.google.android.material.button.MaterialButton
84+
<Button
8385
android:id="@+id/action_append"
8486
android:layout_width="100dp"
8587
android:layout_height="wrap_content"
86-
android:text="@string/action_append"
87-
android:layout_marginLeft="16dp" />
88+
android:layout_marginStart="16dp"
89+
android:text="@string/action_append" />
8890
</LinearLayout>
8991

9092
<ListView
9193
android:id="@android:id/list"
9294
android:layout_width="match_parent"
9395
android:layout_height="0dp"
94-
android:layout_weight="1"
9596
android:layout_marginTop="16dp"
97+
android:layout_weight="1"
9698
android:divider="@null"
97-
tools:listitem="@layout/log_item"/>
99+
tools:listitem="@layout/log_item" />
98100

99101
<LinearLayout
100102
style="?android:attr/buttonBarStyle"
101103
android:layout_width="match_parent"
102104
android:layout_height="wrap_content"
103105
android:gravity="center_horizontal"
104-
android:orientation="horizontal" >
106+
android:orientation="horizontal">
105107

106108
<Button
107109
android:id="@+id/action_open"

sample/src/main/res/layout/fragment_dialog_key_name.xml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,23 @@
3434
android:layout_width="wrap_content"
3535
android:layout_height="wrap_content"
3636
android:orientation="vertical"
37-
android:padding="8dp" >
37+
android:paddingTop="16dp"
38+
android:paddingHorizontal="24dp" >
3839

3940
<TextView
4041
android:layout_width="wrap_content"
4142
android:layout_height="wrap_content"
4243
android:text="@string/dialog_message" />
4344

45+
<Space
46+
android:layout_width="wrap_content"
47+
android:layout_height="16dp" />
48+
4449
<com.google.android.material.textfield.TextInputLayout
4550
android:layout_width="match_parent"
4651
android:layout_height="wrap_content"
4752
android:hint="@string/dialog_key"
48-
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
53+
style="@style/Widget.Material3.TextInputLayout.OutlinedBox">
4954

5055
<com.google.android.material.textfield.TextInputEditText
5156
android:id="@+id/key"
@@ -54,11 +59,15 @@
5459
android:imeOptions="actionNext"/>
5560
</com.google.android.material.textfield.TextInputLayout>
5661

62+
<Space
63+
android:layout_width="wrap_content"
64+
android:layout_height="16dp" />
65+
5766
<com.google.android.material.textfield.TextInputLayout
5867
android:layout_width="match_parent"
5968
android:layout_height="wrap_content"
6069
android:hint="@string/dialog_name"
61-
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
70+
style="@style/Widget.Material3.TextInputLayout.OutlinedBox">
6271

6372
<com.google.android.material.textfield.TextInputEditText
6473
android:id="@+id/name"

sample/src/main/res/layout/fragment_main.xml

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@
3434
android:layout_width="match_parent"
3535
android:layout_height="match_parent"
3636
android:orientation="vertical"
37-
android:paddingBottom="@dimen/activity_vertical_margin"
3837
android:paddingLeft="@dimen/activity_horizontal_margin"
38+
android:paddingTop="@dimen/activity_vertical_margin"
3939
android:paddingRight="@dimen/activity_horizontal_margin"
40-
android:paddingTop="@dimen/activity_vertical_margin" >
40+
android:paddingBottom="@dimen/activity_vertical_margin">
4141

42-
<com.google.android.material.button.MaterialButton
42+
<Button
4343
android:id="@+id/action_create"
4444
android:layout_width="match_parent"
4545
android:layout_height="wrap_content"
@@ -48,60 +48,62 @@
4848

4949
<com.google.android.material.textfield.TextInputLayout
5050
android:id="@+id/field_layout"
51+
style="@style/Widget.Material3.TextInputEditText.OutlinedBox"
5152
android:layout_width="match_parent"
5253
android:layout_height="wrap_content"
53-
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
5454
android:hint="@string/action_append_hint">
5555

5656
<com.google.android.material.textfield.TextInputEditText
5757
android:id="@+id/field"
5858
android:layout_width="match_parent"
59-
android:layout_height="wrap_content"/>
59+
android:layout_height="wrap_content" />
6060
</com.google.android.material.textfield.TextInputLayout>
6161

6262
<LinearLayout
6363
android:layout_width="match_parent"
6464
android:layout_height="wrap_content"
65-
android:orientation="horizontal" >
65+
android:orientation="horizontal">
6666

6767
<com.google.android.material.textfield.TextInputLayout
6868
android:id="@+id/log_level_layout"
69+
style="@style/Widget.Material3.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
6970
android:layout_width="0dp"
7071
android:layout_height="wrap_content"
71-
android:layout_weight="1"
72-
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu">
72+
android:layout_weight="1">
7373

74-
<com.google.android.material.textfield.MaterialAutoCompleteTextView
74+
<AutoCompleteTextView
7575
android:id="@+id/log_level"
76+
style="@style/Widget.Material3.AutoCompleteTextView.OutlinedBox"
7677
android:layout_width="match_parent"
7778
android:layout_height="wrap_content"
78-
android:saveEnabled="false"
79-
android:inputType="none"/>
79+
android:hint="Level"
80+
android:inputType="none"
81+
android:saveEnabled="false" />
8082
</com.google.android.material.textfield.TextInputLayout>
8183

82-
<com.google.android.material.button.MaterialButton
84+
<Button
8385
android:id="@+id/action_append"
8486
android:layout_width="100dp"
8587
android:layout_height="wrap_content"
86-
android:text="@string/action_append"
87-
android:layout_marginLeft="16dp" />
88+
android:layout_marginStart="16dp"
89+
android:text="@string/action_append" />
8890
</LinearLayout>
8991

9092
<ListView
9193
android:id="@android:id/list"
9294
android:layout_width="match_parent"
9395
android:layout_height="0dp"
94-
android:layout_weight="1"
9596
android:layout_marginTop="16dp"
97+
android:layout_weight="1"
9698
android:divider="@null"
97-
tools:listitem="@layout/log_item"/>
99+
tools:listitem="@layout/log_item" />
98100

99101
<LinearLayout
100102
style="?android:attr/buttonBarStyle"
101103
android:layout_width="match_parent"
102104
android:layout_height="wrap_content"
103105
android:gravity="center_horizontal"
104-
android:orientation="vertical" >
106+
android:orientation="vertical">
105107

106108
<Button
107109
android:id="@+id/action_open"

sample/src/main/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
<string name="dialog_title">Create session</string>
5858
<string name="dialog_message">Please, provide a key and a name for the new log session:</string>
5959
<string name="dialog_key">Key</string>
60-
<string name="dialog_name">Name</string>
60+
<string name="dialog_name">Name (optional)</string>
6161
<string name="dialog_error">The field may not be empty.</string>
6262

6363
</resources>

sample/src/main/res/values/styles.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
Base application theme, dependent on API level. This theme is replaced
3636
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
3737
-->
38-
<style name="AppBaseTheme" parent="@style/Theme.MaterialComponents.Light.DarkActionBar">
38+
<style name="AppBaseTheme" parent="@style/Theme.Material3.Light">
3939
<item name="colorPrimary">@color/nordicBlue</item>
4040
</style>
4141

0 commit comments

Comments
 (0)