diff --git a/app/src/main/java/io/pslab/InputMinMaxFilter.java b/app/src/main/java/io/pslab/InputMinMaxFilter.java new file mode 100644 index 000000000..ee2bfe74f --- /dev/null +++ b/app/src/main/java/io/pslab/InputMinMaxFilter.java @@ -0,0 +1,36 @@ +package io.pslab; + +import android.text.InputFilter; +import android.text.Spanned; + +public class InputMinMaxFilter implements InputFilter { + + private int min; + private int max; + + public InputMinMaxFilter(int min, int max) { + this.min = min; + this.max = max; + } + public InputMinMaxFilter(String min, String max) { + this.min = Integer.parseInt(min); + this.max = Integer.parseInt(max); + } + private boolean isInRange(int a, int b, int c) { + return b > a ? c >= a && c <= b : c >= b && c <= a; + } + @Override + public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { + try { + String newVal = dest.toString().substring(0, dstart) + dest.toString().substring(dend, dest.toString().length()); + newVal = newVal.substring(0, dstart) + source.toString() + newVal.substring(dstart, newVal.length()); + int input = Integer.parseInt(newVal); + if (newVal.length() >= 4) + return ""; + if (isInRange(min, max, input)) + return null; + } catch (NumberFormatException nfe) { + } + return ""; + } +} diff --git a/app/src/main/java/io/pslab/activity/RoboticArmActivity.java b/app/src/main/java/io/pslab/activity/RoboticArmActivity.java index 082added3..4251043bd 100644 --- a/app/src/main/java/io/pslab/activity/RoboticArmActivity.java +++ b/app/src/main/java/io/pslab/activity/RoboticArmActivity.java @@ -15,6 +15,7 @@ import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.Toolbar; +import android.text.InputFilter; import android.util.TypedValue; import android.view.Display; import android.view.DragEvent; @@ -41,6 +42,7 @@ import butterknife.BindView; import butterknife.ButterKnife; +import io.pslab.InputMinMaxFilter; import io.pslab.R; import io.pslab.communication.ScienceLab; import io.pslab.models.SensorDataBlock; @@ -155,6 +157,11 @@ public void onClick(View v) { degreeText3.setText(getResources().getString(R.string.zero)); degreeText4.setText(getResources().getString(R.string.zero)); + degreeText1.setFilters(new InputFilter[]{new InputMinMaxFilter(0,360)}); + degreeText2.setFilters(new InputFilter[]{new InputMinMaxFilter(0,360)}); + degreeText3.setFilters(new InputFilter[]{new InputMinMaxFilter(0,360)}); + degreeText4.setFilters(new InputFilter[]{new InputMinMaxFilter(0,360)}); + LinearLayout.LayoutParams servoControllerParams = new LinearLayout.LayoutParams(screen_width / 4 - 4, screen_height / 2 - 4); servoControllerParams.setMargins(2, 5, 2, 0); servo1Layout.setLayoutParams(servoControllerParams);